|
| 1 | +package barycentric |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + "math/big" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/consensys/gnark-crypto/ecc" |
| 9 | + "github.com/consensys/gnark/backend" |
| 10 | + "github.com/consensys/gnark/frontend" |
| 11 | + "github.com/consensys/gnark/std/math/emulated" |
| 12 | + "github.com/consensys/gnark/test" |
| 13 | +) |
| 14 | + |
| 15 | +type BarycentricCircuit[T emulated.FieldParams] struct { |
| 16 | + Omega big.Int // ω |
| 17 | + PolynomialDegree int |
| 18 | + |
| 19 | + // Inputs (private) |
| 20 | + YNodes []emulated.Element[T] // len(YNodes) == PolynomialDegree |
| 21 | + TargetPoint emulated.Element[T] |
| 22 | + |
| 23 | + // Output |
| 24 | + InterpolatedPoint emulated.Element[T] `gnark:",public"` |
| 25 | +} |
| 26 | + |
| 27 | +func (circuit *BarycentricCircuit[T]) Define(api frontend.API) error { |
| 28 | + field, err := emulated.NewField[T](api) |
| 29 | + if err != nil { |
| 30 | + return err |
| 31 | + } |
| 32 | + |
| 33 | + api.AssertIsEqual(len(circuit.YNodes), circuit.PolynomialDegree) |
| 34 | + |
| 35 | + omegasToI := make([]emulated.Element[T], circuit.PolynomialDegree) |
| 36 | + omegaToI := big.NewInt(1) |
| 37 | + for i := range circuit.PolynomialDegree { |
| 38 | + omegasToI[i] = emulated.ValueOf[T](omegaToI) |
| 39 | + omegaToI.Mul(omegaToI, &circuit.Omega) |
| 40 | + } |
| 41 | + |
| 42 | + // Method under test |
| 43 | + interpolatedPointCalculated := CalculateBarycentricFormula[T](field, omegasToI, circuit.YNodes, circuit.TargetPoint) |
| 44 | + |
| 45 | + field.AssertIsEqual(&circuit.InterpolatedPoint, &interpolatedPointCalculated) |
| 46 | + |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +func setupTestEnvironment(polynomialDegree int) (*big.Int, *big.Int) { |
| 51 | + // The test assumes BLS12381Fr field and a certain polynomial degree |
| 52 | + modulus, _ := new(big.Int).SetString( |
| 53 | + "52435875175126190479447740508185965837690552500527637822603658699938581184513", 10, |
| 54 | + ) |
| 55 | + |
| 56 | + // For polynomial degree d = 4096 = 2^12: |
| 57 | + // ω^(2^32) = ω^(2^20 * 2^12) |
| 58 | + // Calculate ω^20 starting with root of unity of 2^32 degree |
| 59 | + omega, _ := new(big.Int).SetString( |
| 60 | + "10238227357739495823651030575849232062558860180284477541189508159991286009131", 10, |
| 61 | + ) |
| 62 | + polynomialDegreeExp := int(math.Log2(float64(polynomialDegree))) |
| 63 | + omegaExpExp := 32 // ω^(2^32) |
| 64 | + for range omegaExpExp - polynomialDegreeExp { |
| 65 | + omega.Mul(omega, omega) |
| 66 | + omega.Mod(omega, modulus) |
| 67 | + } |
| 68 | + |
| 69 | + return omega, modulus |
| 70 | +} |
| 71 | + |
| 72 | +func TestCalculateBarycentricFormula(t *testing.T) { |
| 73 | + type Fr = emulated.BLS12381Fr |
| 74 | + const polynomialDegree = 4096 |
| 75 | + omega, modulus := setupTestEnvironment(polynomialDegree) |
| 76 | + |
| 77 | + // Test cases |
| 78 | + type PolynomialTestCase[T emulated.FieldParams] struct { |
| 79 | + Name string |
| 80 | + CalculateYNodes func(omega *big.Int, modulus *big.Int, polynomialDegree int) []emulated.Element[T] |
| 81 | + TargetPoint int64 |
| 82 | + InterpolatedPoint int64 |
| 83 | + } |
| 84 | + tests := []PolynomialTestCase[Fr]{ |
| 85 | + { |
| 86 | + Name: "f(x) = x^3", |
| 87 | + CalculateYNodes: func(omega *big.Int, modulus *big.Int, polynomialDegree int) []emulated.Element[Fr] { |
| 88 | + y := make([]emulated.Element[Fr], polynomialDegree) |
| 89 | + for i := range y { |
| 90 | + res := new(big.Int).Exp(omega, big.NewInt(int64(i*3)), modulus) |
| 91 | + y[i] = emulated.ValueOf[Fr](res) |
| 92 | + } |
| 93 | + return y |
| 94 | + }, |
| 95 | + TargetPoint: 3, |
| 96 | + InterpolatedPoint: 27, |
| 97 | + }, |
| 98 | + { |
| 99 | + Name: "f(x) = 3x^7 + 2x^4 + 4x + 20", |
| 100 | + CalculateYNodes: func(omega *big.Int, modulus *big.Int, polynomialDegree int) []emulated.Element[Fr] { |
| 101 | + y := make([]emulated.Element[Fr], polynomialDegree) |
| 102 | + for i := range y { |
| 103 | + a := new(big.Int).Exp(omega, big.NewInt(int64(i*7)), modulus) |
| 104 | + a.Mul(a, big.NewInt(3)) |
| 105 | + |
| 106 | + b := new(big.Int).Exp(omega, big.NewInt(int64(i*4)), modulus) |
| 107 | + b.Mul(b, big.NewInt(2)) |
| 108 | + |
| 109 | + c := new(big.Int).Exp(omega, big.NewInt(int64(i)), modulus) |
| 110 | + c.Mul(c, big.NewInt(4)) |
| 111 | + |
| 112 | + res := new(big.Int).Add(a, b) |
| 113 | + res.Add(res, c) |
| 114 | + res.Add(res, big.NewInt(20)) |
| 115 | + res.Mod(res, modulus) |
| 116 | + |
| 117 | + y[i] = emulated.ValueOf[Fr](res) |
| 118 | + } |
| 119 | + return y |
| 120 | + }, |
| 121 | + TargetPoint: 3, |
| 122 | + InterpolatedPoint: 6755, |
| 123 | + }, |
| 124 | + } |
| 125 | + |
| 126 | + for _, tc := range tests { |
| 127 | + assert := test.NewAssert(t) |
| 128 | + assert.Run( |
| 129 | + func(a *test.Assert) { |
| 130 | + circuit := BarycentricCircuit[Fr]{ |
| 131 | + Omega: *omega, |
| 132 | + PolynomialDegree: polynomialDegree, |
| 133 | + YNodes: make([]emulated.Element[Fr], polynomialDegree), |
| 134 | + } |
| 135 | + |
| 136 | + assignment := BarycentricCircuit[Fr]{ |
| 137 | + YNodes: tc.CalculateYNodes(omega, modulus, polynomialDegree), |
| 138 | + TargetPoint: emulated.ValueOf[Fr](tc.TargetPoint), |
| 139 | + InterpolatedPoint: emulated.ValueOf[Fr](tc.InterpolatedPoint), |
| 140 | + } |
| 141 | + |
| 142 | + assert.CheckCircuit( |
| 143 | + &circuit, test.WithBackends(backend.GROTH16), test.WithCurves(ecc.BN254), |
| 144 | + test.WithValidAssignment(&assignment), |
| 145 | + ) |
| 146 | + }, tc.Name, |
| 147 | + ) |
| 148 | + } |
| 149 | +} |
0 commit comments