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
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)
38
39var GOLDILOCKS_MULTIPLICATIVE_GROUP_GENERATOR = goldilocks.NewElement(7)
40var GOLDILOCKS_TWO_ADICITY = uint64(32)
41var GOLDILOCKS_POWER_OF_TWO_GENERATOR = goldilocks.NewElement(1753635133440165772)
42
43func GoldilocksPrimitiveRootOfUnity(nLog uint64) goldilocks.Element {
44	if nLog > GOLDILOCKS_TWO_ADICITY {
45		panic("nLog is greater than GOLDILOCKS_TWO_ADICITY")
46	}
47
48	res := goldilocks.NewElement(GOLDILOCKS_POWER_OF_TWO_GENERATOR.Uint64())
49	for i := 0; i < int(GOLDILOCKS_TWO_ADICITY-nLog); i++ {
50		res.Square(&res)
51	}
52
53	return res
54}