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
10const D = 2
11
12type EmulatedField = emulated.Goldilocks
13type F = emulated.Element[EmulatedField]
14type QuadraticExtension = [2]F
15type QEAlgebra = [D]QuadraticExtension
16type Hash = [4]F
17
18var TEST_CURVE = ecc.BN254
19
20func NewFieldElement(x uint64) F {
21 return emulated.NewElement[EmulatedField](x)
22}
23
24func NewFieldElementFromString(x string) F {
25 return emulated.NewElement[EmulatedField](x)
26}
27
28func NewFieldAPI(api frontend.API) frontend.API {
29 field, err := emulated.NewField[EmulatedField](api)
30 if err != nil {
31 panic(err)
32 }
33 return field
34}
35
36var ONE_F = NewFieldElement(1)
37var ZERO_F = NewFieldElement(0)
38var NEG_ONE_F = NewFieldElement(EmulatedField{}.Modulus().Uint64() - 1)
39
40var GOLDILOCKS_MULTIPLICATIVE_GROUP_GENERATOR = goldilocks.NewElement(7)
41var GOLDILOCKS_TWO_ADICITY = uint64(32)
42var GOLDILOCKS_POWER_OF_TWO_GENERATOR = goldilocks.NewElement(1753635133440165772)
43
44func GoldilocksPrimitiveRootOfUnity(nLog uint64) goldilocks.Element {
45 if nLog > GOLDILOCKS_TWO_ADICITY {
46 panic("nLog is greater than GOLDILOCKS_TWO_ADICITY")
47 }
48
49 res := goldilocks.NewElement(GOLDILOCKS_POWER_OF_TWO_GENERATOR.Uint64())
50 for i := 0; i < int(GOLDILOCKS_TWO_ADICITY-nLog); i++ {
51 res.Square(&res)
52 }
53
54 return res
55}
56
57func TwoAdicSubgroup(nLog uint64) []goldilocks.Element {
58 if nLog > GOLDILOCKS_TWO_ADICITY {
59 panic("nLog is greater than GOLDILOCKS_TWO_ADICITY")
60 }
61
62 var res []goldilocks.Element
63 rootOfUnity := GoldilocksPrimitiveRootOfUnity(nLog)
64 res = append(res, goldilocks.NewElement(1))
65
66 for i := 0; i < (1 << nLog); i++ {
67 lastElement := res[len(res)-1]
68 res = append(res, *lastElement.Mul(&lastElement, &rootOfUnity))
69 }
70
71 return res
72}