@ -0,0 +1,69 @@ |
|||||
|
package zk |
||||
|
|
||||
|
import ( |
||||
|
"crypto/rand" |
||||
|
"fmt" |
||||
|
"math/big" |
||||
|
|
||||
|
"github.com/arnaucube/go-snark/bn128" |
||||
|
) |
||||
|
|
||||
|
const bits = 512 |
||||
|
|
||||
|
func GenerateTrustedSetup(bn bn128.Bn128, pollength int) ([][3]*big.Int, [][3][2]*big.Int, error) { |
||||
|
// generate random t value
|
||||
|
t, err := rand.Prime(rand.Reader, bits) |
||||
|
if err != nil { |
||||
|
return [][3]*big.Int{}, [][3][2]*big.Int{}, err |
||||
|
} |
||||
|
fmt.Print("trusted t: ") |
||||
|
fmt.Println(t) |
||||
|
|
||||
|
// encrypt t values with curve generators
|
||||
|
var gt1 [][3]*big.Int |
||||
|
var gt2 [][3][2]*big.Int |
||||
|
for i := 0; i < pollength; i++ { |
||||
|
tPow := bn.Fq1.Exp(t, big.NewInt(int64(i))) |
||||
|
tEncr1 := bn.G1.MulScalar(bn.G1.G, tPow) |
||||
|
gt1 = append(gt1, tEncr1) |
||||
|
tEncr2 := bn.G2.MulScalar(bn.G2.G, tPow) |
||||
|
gt2 = append(gt2, tEncr2) |
||||
|
} |
||||
|
// gt1: g1, g1*t, g1*t^2, g1*t^3, ...
|
||||
|
// gt2: g2, g2*t, g2*t^2, ...
|
||||
|
return gt1, gt2, nil |
||||
|
} |
||||
|
func GenerateProofs(bn bn128.Bn128, gt1 [][3]*big.Int, gt2 [][3][2]*big.Int, ax, bx, cx, hx, zx []*big.Int) ([3]*big.Int, [3][2]*big.Int, [3]*big.Int, [3]*big.Int, [3][2]*big.Int) { |
||||
|
|
||||
|
// multiply g1*A(x), g2*B(x), g1*C(x), g1*H(x)
|
||||
|
|
||||
|
// g1*A(x)
|
||||
|
g1At := [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()} |
||||
|
for i := 0; i < len(ax); i++ { |
||||
|
m := bn.G1.MulScalar(gt1[i], ax[i]) |
||||
|
g1At = bn.G1.Add(g1At, m) |
||||
|
} |
||||
|
g2Bt := bn.Fq6.Zero() |
||||
|
for i := 0; i < len(bx); i++ { |
||||
|
m := bn.G2.MulScalar(gt2[i], bx[i]) |
||||
|
g2Bt = bn.G2.Add(g2Bt, m) |
||||
|
} |
||||
|
|
||||
|
g1Ct := [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()} |
||||
|
for i := 0; i < len(cx); i++ { |
||||
|
m := bn.G1.MulScalar(gt1[i], cx[i]) |
||||
|
g1Ct = bn.G1.Add(g1Ct, m) |
||||
|
} |
||||
|
g1Ht := [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()} |
||||
|
for i := 0; i < len(hx); i++ { |
||||
|
m := bn.G1.MulScalar(gt1[i], hx[i]) |
||||
|
g1Ht = bn.G1.Add(g1Ht, m) |
||||
|
} |
||||
|
g2Zt := bn.Fq6.Zero() |
||||
|
for i := 0; i < len(bx); i++ { |
||||
|
m := bn.G2.MulScalar(gt2[i], zx[i]) |
||||
|
g2Zt = bn.G2.Add(g2Zt, m) |
||||
|
} |
||||
|
|
||||
|
return g1At, g2Bt, g1Ct, g1Ht, g2Zt |
||||
|
} |
@ -0,0 +1,96 @@ |
|||||
|
package zk |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"math/big" |
||||
|
"testing" |
||||
|
|
||||
|
"github.com/arnaucube/go-snark/bn128" |
||||
|
"github.com/arnaucube/go-snark/fields" |
||||
|
"github.com/arnaucube/go-snark/r1csqap" |
||||
|
"github.com/stretchr/testify/assert" |
||||
|
) |
||||
|
|
||||
|
func TestZk(t *testing.T) { |
||||
|
bn, err := bn128.NewBn128() |
||||
|
assert.Nil(t, err) |
||||
|
|
||||
|
// new Finite Field
|
||||
|
f := fields.NewFq(bn.R) |
||||
|
|
||||
|
// new Polynomial Field
|
||||
|
pf := r1csqap.NewPolynomialField(f) |
||||
|
|
||||
|
b0 := big.NewInt(int64(0)) |
||||
|
b1 := big.NewInt(int64(1)) |
||||
|
b3 := big.NewInt(int64(3)) |
||||
|
b5 := big.NewInt(int64(5)) |
||||
|
b9 := big.NewInt(int64(9)) |
||||
|
b27 := big.NewInt(int64(27)) |
||||
|
b30 := big.NewInt(int64(30)) |
||||
|
b35 := big.NewInt(int64(35)) |
||||
|
a := [][]*big.Int{ |
||||
|
[]*big.Int{b0, b1, b0, b0, b0, b0}, |
||||
|
[]*big.Int{b0, b0, b0, b1, b0, b0}, |
||||
|
[]*big.Int{b0, b1, b0, b0, b1, b0}, |
||||
|
[]*big.Int{b5, b0, b0, b0, b0, b1}, |
||||
|
} |
||||
|
b := [][]*big.Int{ |
||||
|
[]*big.Int{b0, b1, b0, b0, b0, b0}, |
||||
|
[]*big.Int{b0, b1, b0, b0, b0, b0}, |
||||
|
[]*big.Int{b1, b0, b0, b0, b0, b0}, |
||||
|
[]*big.Int{b1, b0, b0, b0, b0, b0}, |
||||
|
} |
||||
|
c := [][]*big.Int{ |
||||
|
[]*big.Int{b0, b0, b0, b1, b0, b0}, |
||||
|
[]*big.Int{b0, b0, b0, b0, b1, b0}, |
||||
|
[]*big.Int{b0, b0, b0, b0, b0, b1}, |
||||
|
[]*big.Int{b0, b0, b1, b0, b0, b0}, |
||||
|
} |
||||
|
alphas, betas, gammas, zx := pf.R1CSToQAP(a, b, c) |
||||
|
|
||||
|
w := []*big.Int{b1, b3, b35, b9, b27, b30} |
||||
|
ax, bx, cx, px := pf.CombinePolynomials(w, alphas, betas, gammas) |
||||
|
|
||||
|
hx := pf.DivisorPolinomial(px, zx) |
||||
|
|
||||
|
// hx==px/zx so px==hx*zx
|
||||
|
assert.Equal(t, px, pf.Mul(hx, zx)) |
||||
|
|
||||
|
// p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
|
||||
|
abc := pf.Sub(pf.Mul(ax, bx), cx) |
||||
|
assert.Equal(t, abc, px) |
||||
|
hz := pf.Mul(hx, zx) |
||||
|
assert.Equal(t, abc, hz) |
||||
|
|
||||
|
// calculate trusted setup
|
||||
|
gt1, gt2, err := GenerateTrustedSetup(bn, len(ax)) |
||||
|
assert.Nil(t, err) |
||||
|
fmt.Println("trusted setup:") |
||||
|
fmt.Println(gt1) |
||||
|
fmt.Println(gt2) |
||||
|
|
||||
|
// piA = g1 * A(t), piB = g2 * B(t), piC = g1 * C(t), piH = g1 * H(t)
|
||||
|
piA, piB, piC, piH, piZ := GenerateProofs(bn, gt1, gt2, ax, bx, cx, hx, zx) |
||||
|
fmt.Println("proofs:") |
||||
|
fmt.Println(piA) |
||||
|
fmt.Println(piB) |
||||
|
fmt.Println(piC) |
||||
|
fmt.Println(piH) |
||||
|
fmt.Println(piZ) |
||||
|
|
||||
|
// pairing
|
||||
|
fmt.Println("pairing") |
||||
|
pairingAB, err := bn.Pairing(piA, piB) |
||||
|
assert.Nil(t, err) |
||||
|
pairingCg2, err := bn.Pairing(piC, bn.G2.G) |
||||
|
assert.Nil(t, err) |
||||
|
pairingLeft := bn.Fq12.Div(pairingAB, pairingCg2) |
||||
|
pairingHg2Z, err := bn.Pairing(piH, piZ) |
||||
|
assert.Nil(t, err) |
||||
|
|
||||
|
fmt.Println(bn.Fq12.Affine(pairingLeft)) |
||||
|
fmt.Println(bn.Fq12.Affine(pairingHg2Z)) |
||||
|
|
||||
|
assert.True(t, bn.Fq12.Equal(pairingLeft, pairingHg2Z)) |
||||
|
} |