arnaucube
arnaucube/gnark-plonky2-verifierPublic Self-hosted repository
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 Hash = [4]F
14
15var TEST_CURVE = ecc.BN254
16
17func NewFieldElement(x uint64) F {
18	return emulated.NewElement[EmulatedField](x)
19}
20
21func NewFieldElementFromString(x string) F {
22	return emulated.NewElement[EmulatedField](x)
23}
24
25func NewFieldAPI(api frontend.API) frontend.API {
26	field, err := emulated.NewField[EmulatedField](api)
27	if err != nil {
28		panic(err)
29	}
30	return field
31}
32
33var ONE_F = NewFieldElement(1)
34var ZERO_F = NewFieldElement(0)
35
36var GOLDILOCKS_MULTIPLICATIVE_GROUP_GENERATOR = goldilocks.NewElement(7)
37var GOLDILOCKS_TWO_ADICITY = uint64(32)
38var GOLDILOCKS_POWER_OF_TWO_GENERATOR = goldilocks.NewElement(1753635133440165772)
39
40func GoldilocksPrimitiveRootOfUnity(nLog uint64) goldilocks.Element {
41	if nLog > GOLDILOCKS_TWO_ADICITY {
42		panic("nLog is greater than GOLDILOCKS_TWO_ADICITY")
43	}
44
45	res := goldilocks.NewElement(GOLDILOCKS_POWER_OF_TWO_GENERATOR.Uint64())
46	for i := 0; i < int(GOLDILOCKS_TWO_ADICITY-nLog); i++ {
47		res.Square(&res)
48	}
49
50	return res
51}