kzg-commitments-study/kzg_test.go
kzg_test.goRaw
1package kzg
2
3import (
4 "math/big"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8)
9
10func TestSimpleFlow(t *testing.T) {
11 // p(x) = x^3 + x + 5
12 p := []*big.Int{
13 big.NewInt(5),
14 big.NewInt(1), // x^1
15 big.NewInt(0), // x^2
16 big.NewInt(1), // x^3
17 }
18 assert.Equal(t, "x³ + x¹ + 5", PolynomialToString(p))
19
20 // TrustedSetup
21 ts, err := NewTrustedSetup(len(p))
22 assert.Nil(t, err)
23
24 // Commit
25 c := Commit(ts, p)
26
27 // p(z)=y --> p(3)=35
28 z := big.NewInt(3)
29 y := big.NewInt(35)
30
31 // z & y: to prove an evaluation p(z)=y
32 proof, err := EvaluationProof(ts, p, z, y)
33 assert.Nil(t, err)
34
35 v := Verify(ts, c, proof, z, y)
36 assert.True(t, v)
37
38 v = Verify(ts, c, proof, big.NewInt(4), y)
39 assert.False(t, v)
40}
41
42func TestBatchProof(t *testing.T) {
43 // p(x) = x^3 + x + 5
44 p := []*big.Int{
45 big.NewInt(5),
46 big.NewInt(1), // x^1
47 big.NewInt(0), // x^2
48 big.NewInt(1), // x^3
49 big.NewInt(10), // x^4
50 }
51 assert.Equal(t, "10x⁴ + x³ + x¹ + 5", PolynomialToString(p))
52
53 // TrustedSetup
54 ts, err := NewTrustedSetup(len(p))
55 assert.Nil(t, err)
56
57 // Commit
58 c := Commit(ts, p)
59
60 // 1st point: p(z)=y --> p(3)=35
61 z0 := big.NewInt(3)
62 y0 := polynomialEval(p, z0)
63
64 // 2nd point: p(10)=1015
65 z1 := big.NewInt(10)
66 y1 := polynomialEval(p, z1)
67
68 // 3nd point: p(256)=16777477
69 z2 := big.NewInt(256)
70 y2 := polynomialEval(p, z2)
71
72 zs := []*big.Int{z0, z1, z2}
73 ys := []*big.Int{y0, y1, y2}
74
75 // prove an evaluation of the multiple z_i & y_i
76 proof, err := EvaluationBatchProof(ts, p, zs, ys)
77 assert.Nil(t, err)
78
79 // batch proof verification
80 v := VerifyBatchProof(ts, c, proof, zs, ys)
81 assert.True(t, v)
82
83 // changing order of the points to be verified
84 zs[0], zs[1], zs[2] = zs[1], zs[2], zs[0]
85 ys[0], ys[1], ys[2] = ys[1], ys[2], ys[0]
86 v = VerifyBatchProof(ts, c, proof, zs, ys)
87 assert.True(t, v)
88
89 // change a value of zs and check that verification fails
90 zs[0] = big.NewInt(2)
91 v = VerifyBatchProof(ts, c, proof, zs, ys)
92 assert.False(t, v)
93
94 // using a value that is not in the evaluation proof should generate a
95 // proof that will not correctly be verified
96 zs = []*big.Int{z0, z1, z2}
97 ys = []*big.Int{y0, y1, y2}
98 proof, err = EvaluationBatchProof(ts, p, zs, ys)
99 assert.Nil(t, err)
100 zs[2] = big.NewInt(2500)
101 ys[2] = polynomialEval(p, zs[2])
102 v = VerifyBatchProof(ts, c, proof, zs, ys)
103 assert.False(t, v)
104}