kzg-commitments-study/README.md
README.mdRaw
1# kzg-commitments-study [](https://godoc.org/github.com/arnaucube/kzg-commitments-study) [](https://goreportcard.com/report/github.com/arnaucube/kzg-commitments-study) [](https://github.com/arnaucube/kzg-commitments-study/actions?query=workflow%3ATest)
2
3Doing this to study and learn [KZG commitments](http://cacr.uwaterloo.ca/techreports/2010/cacr2010-10.pdf), do not use in production. More details at https://arnaucube.com/blog/kzg-commitments.html .
4
5Thanks to [Dankrad Feist](https://dankradfeist.de/ethereum/2020/06/16/kate-polynomial-commitments.html), [Alin Tomescu](https://alinush.github.io/2020/05/06/kzg-polynomial-commitments.html), [Tom Walton-Pocock](https://hackmd.io/@tompocock/Hk2A7BD6U) for their articles, which helped me understand a bit the KZG Commitments.
6
7It uses the [ethereum bn256](https://github.com/ethereum/go-ethereum/tree/master/crypto/bn256/cloudflare).
8
9### Usage
10
11```go
12// p(x) = x^3 + x + 5
13p := []*big.Int{
14 big.NewInt(5),
15 big.NewInt(1), // x^1
16 big.NewInt(0), // x^2
17 big.NewInt(1), // x^3
18}
19assert.Equal(t, "1x³ + 1x¹ + 5", PolynomialToString(p))
20
21// TrustedSetup
22ts, err := NewTrustedSetup(p)
23assert.Nil(t, err)
24
25// Commit
26c := Commit(ts, p)
27
28// p(z)=y --> p(3)=35
29z := big.NewInt(3)
30y := big.NewInt(35)
31
32// z & y: to prove an evaluation p(z)=y
33proof, err := EvaluationProof(ts, p, z, y)
34assert.Nil(t, err)
35
36// verification
37v := Verify(ts, c, proof, z, y)
38assert.True(t, v)
39```
40
41Batch Proofs:
42```go
43// zs & ys contain the f(z_i)=y_i values that will be proved inside a batch proof
44zs := []*big.Int{z0, z1, z2}
45ys := []*big.Int{y0, y1, y2}
46
47// prove an evaluation of the multiple z_i & y_i
48proof, err := EvaluationBatchProof(ts, p, zs, ys)
49assert.Nil(t, err)
50
51// batch proof verification
52v := VerifyBatchProof(ts, c, proof, zs, ys)
53assert.True(t, v)
54```