Skip to content

Commit b534049

Browse files
authored
feat: make constraint.Element generic interface (#1463)
1 parent ad986c6 commit b534049

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+4663
-1215
lines changed

backend/witness/vector.go

+52-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313
fr_bn254 "github.com/consensys/gnark-crypto/ecc/bn254/fr"
1414
fr_bw6633 "github.com/consensys/gnark-crypto/ecc/bw6-633/fr"
1515
fr_bw6761 "github.com/consensys/gnark-crypto/ecc/bw6-761/fr"
16-
"github.com/consensys/gnark/internal/tinyfield"
16+
"github.com/consensys/gnark-crypto/field/babybear"
17+
"github.com/consensys/gnark-crypto/field/koalabear"
18+
"github.com/consensys/gnark/internal/smallfields/tinyfield"
1719
"github.com/consensys/gnark/internal/utils"
1820
)
1921

@@ -37,9 +39,14 @@ func newVector(field *big.Int, size int) (any, error) {
3739
default:
3840
if field.Cmp(tinyfield.Modulus()) == 0 {
3941
return make(tinyfield.Vector, size), nil
40-
} else {
41-
return nil, errors.New("unsupported modulus")
4242
}
43+
if field.Cmp(babybear.Modulus()) == 0 {
44+
return make(babybear.Vector, size), nil
45+
}
46+
if field.Cmp(koalabear.Modulus()) == 0 {
47+
return make(koalabear.Vector, size), nil
48+
}
49+
return nil, errors.New("unsupported modulus")
4350
}
4451
}
4552

@@ -77,6 +84,14 @@ func newFrom(from any, n int) (any, error) {
7784
a := make(tinyfield.Vector, n)
7885
copy(a, wt)
7986
return a, nil
87+
case babybear.Vector:
88+
a := make(babybear.Vector, n)
89+
copy(a, wt)
90+
return a, nil
91+
case koalabear.Vector:
92+
a := make(koalabear.Vector, n)
93+
copy(a, wt)
94+
return a, nil
8095
default:
8196
return nil, errors.New("unsupported modulus")
8297
}
@@ -100,6 +115,10 @@ func leafType(v any) reflect.Type {
100115
return reflect.TypeOf(fr_bw6633.Element{})
101116
case tinyfield.Vector:
102117
return reflect.TypeOf(tinyfield.Element{})
118+
case babybear.Vector:
119+
return reflect.TypeOf(babybear.Element{})
120+
case koalabear.Vector:
121+
return reflect.TypeOf(koalabear.Element{})
103122
default:
104123
panic("invalid input")
105124
}
@@ -155,6 +174,18 @@ func set(v any, index int, value any) error {
155174
}
156175
_, err := pv[index].SetInterface(value)
157176
return err
177+
case babybear.Vector:
178+
if index >= len(pv) {
179+
return errors.New("out of bounds")
180+
}
181+
_, err := pv[index].SetInterface(value)
182+
return err
183+
case koalabear.Vector:
184+
if index >= len(pv) {
185+
return errors.New("out of bounds")
186+
}
187+
_, err := pv[index].SetInterface(value)
188+
return err
158189
default:
159190
panic("invalid input")
160191
}
@@ -219,6 +250,20 @@ func iterate(v any) chan any {
219250
}
220251
close(chValues)
221252
}()
253+
case babybear.Vector:
254+
go func() {
255+
for i := 0; i < len(pv); i++ {
256+
chValues <- &(pv)[i]
257+
}
258+
close(chValues)
259+
}()
260+
case koalabear.Vector:
261+
go func() {
262+
for i := 0; i < len(pv); i++ {
263+
chValues <- &(pv)[i]
264+
}
265+
close(chValues)
266+
}()
222267
default:
223268
panic("invalid input")
224269
}
@@ -243,6 +288,10 @@ func resize(v any, n int) any {
243288
return make(fr_bw6633.Vector, n)
244289
case tinyfield.Vector:
245290
return make(tinyfield.Vector, n)
291+
case babybear.Vector:
292+
return make(babybear.Vector, n)
293+
case koalabear.Vector:
294+
return make(koalabear.Vector, n)
246295
default:
247296
panic("invalid input")
248297
}

backend/witness/witness.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ import (
5454
fr_bn254 "github.com/consensys/gnark-crypto/ecc/bn254/fr"
5555
fr_bw6633 "github.com/consensys/gnark-crypto/ecc/bw6-633/fr"
5656
fr_bw6761 "github.com/consensys/gnark-crypto/ecc/bw6-761/fr"
57+
"github.com/consensys/gnark-crypto/field/babybear"
58+
"github.com/consensys/gnark-crypto/field/koalabear"
5759
"github.com/consensys/gnark/debug"
5860
"github.com/consensys/gnark/frontend/schema"
59-
"github.com/consensys/gnark/internal/tinyfield"
61+
"github.com/consensys/gnark/internal/smallfields/tinyfield"
6062
)
6163

6264
var ErrInvalidWitness = errors.New("invalid witness")
@@ -189,6 +191,10 @@ func (w *witness) WriteTo(wr io.Writer) (n int64, err error) {
189191
m, err = t.WriteTo(wr)
190192
case tinyfield.Vector:
191193
m, err = t.WriteTo(wr)
194+
case babybear.Vector:
195+
m, err = t.WriteTo(wr)
196+
case koalabear.Vector:
197+
m, err = t.WriteTo(wr)
192198
default:
193199
panic("invalid input")
194200
}
@@ -235,6 +241,12 @@ func (w *witness) ReadFrom(r io.Reader) (n int64, err error) {
235241
case tinyfield.Vector:
236242
m, err = t.ReadFrom(r)
237243
w.vector = t
244+
case babybear.Vector:
245+
m, err = t.ReadFrom(r)
246+
w.vector = t
247+
case koalabear.Vector:
248+
m, err = t.ReadFrom(r)
249+
w.vector = t
238250
default:
239251
panic("invalid input")
240252
}

constraint/babybear/coeff.go

+219
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)