gnark-plonky2-verifier/field/field.go
field/field.goRaw
1package field
2
3import (
4 "github.com/consensys/gnark-crypto/ecc"
5 "github.com/consensys/gnark-crypto/field/goldilocks"
6 "github.com/consensys/gnark/frontend"
7 "github.com/consensys/gnark/std/math/emulated"
8)
9
10type EmulatedField = emulated.Goldilocks
11type F = emulated.Element[EmulatedField]
12type QuadraticExtension = [2]F
13type QEAlgebra = [2]QuadraticExtension
14type Hash = [4]F
15
16var TEST_CURVE = ecc.BN254
17
18func NewFieldElement(x uint64) F {
19 return emulated.NewElement[EmulatedField](x)
20}
21
22func NewFieldElementFromString(x string) F {
23 return emulated.NewElement[EmulatedField](x)
24}
25
26func NewFieldAPI(api frontend.API) frontend.API {
27 field, err := emulated.NewField[EmulatedField](api)
28 if err != nil {
29 panic(err)
30 }
31 return field
32}
33
34var ONE_F = NewFieldElement(1)
35var ZERO_F = NewFieldElement(0)
36
37var GOLDILOCKS_MULTIPLICATIVE_GROUP_GENERATOR = goldilocks.NewElement(7)
38var GOLDILOCKS_TWO_ADICITY = uint64(32)
39var GOLDILOCKS_POWER_OF_TWO_GENERATOR = goldilocks.NewElement(1753635133440165772)
40
41func GoldilocksPrimitiveRootOfUnity(nLog uint64) goldilocks.Element {
42 if nLog > GOLDILOCKS_TWO_ADICITY {
43 panic("nLog is greater than GOLDILOCKS_TWO_ADICITY")
44 }
45
46 res := goldilocks.NewElement(GOLDILOCKS_POWER_OF_TWO_GENERATOR.Uint64())
47 for i := 0; i < int(GOLDILOCKS_TWO_ADICITY-nLog); i++ {
48 res.Square(&res)
49 }
50
51 return res
52}