@ -1,6 +1,7 @@
package snark
import (
"encoding/json"
"fmt"
"math/big"
"strings"
@ -12,6 +13,76 @@ import (
"github.com/stretchr/testify/assert"
)
/ *
func TestZkMultiplication ( t * testing . T ) {
// compile circuit and get the R1CS
flatCode := `
func test ( a , b ) :
out = a * b
`
// parse the code
parser := circuitcompiler . NewParser ( strings . NewReader ( flatCode ) )
circuit , err := parser . Parse ( )
assert . Nil ( t , err )
b3 := big . NewInt ( int64 ( 3 ) )
b4 := big . NewInt ( int64 ( 4 ) )
inputs := [ ] * big . Int { b3 , b4 }
// wittness
w , err := circuit . CalculateWitness ( inputs )
assert . Nil ( t , err )
fmt . Println ( "circuit" )
fmt . Println ( circuit . NPublic )
// flat code to R1CS
a , b , c := circuit . GenerateR1CS ( )
fmt . Println ( "\nR1CS:" )
fmt . Println ( "a:" , a )
fmt . Println ( "b:" , b )
fmt . Println ( "c:" , c )
// R1CS to QAP
alphas , betas , gammas , zx := Utils . PF . R1CSToQAP ( a , b , c )
fmt . Println ( "qap" )
fmt . Println ( "alphas" , alphas )
fmt . Println ( "betas" , betas )
fmt . Println ( "gammas" , gammas )
ax , bx , cx , px := Utils . PF . CombinePolynomials ( w , alphas , betas , gammas )
hx := Utils . PF . DivisorPolynomial ( px , zx )
// hx==px/zx so px==hx*zx
assert . Equal ( t , px , Utils . PF . Mul ( hx , zx ) )
// p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
abc := Utils . PF . Sub ( Utils . PF . Mul ( ax , bx ) , cx )
assert . Equal ( t , abc , px )
hz := Utils . PF . Mul ( hx , zx )
assert . Equal ( t , abc , hz )
div , rem := Utils . PF . Div ( px , zx )
assert . Equal ( t , hx , div )
assert . Equal ( t , rem , r1csqap . ArrayOfBigZeros ( 1 ) )
// calculate trusted setup
setup , err := GenerateTrustedSetup ( len ( w ) , * circuit , alphas , betas , gammas , zx )
assert . Nil ( t , err )
// piA = g1 * A(t), piB = g2 * B(t), piC = g1 * C(t), piH = g1 * H(t)
proof , err := GenerateProofs ( * circuit , setup , hx , w )
assert . Nil ( t , err )
// assert.True(t, VerifyProof(*circuit, setup, proof, false))
b35 := big . NewInt ( int64 ( 35 ) )
publicSignals := [ ] * big . Int { b35 }
assert . True ( t , VerifyProof ( * circuit , setup , proof , publicSignals , true ) )
}
* /
func TestZkFromFlatCircuitCode ( t * testing . T ) {
// compile circuit and get the R1CS
@ -30,11 +101,13 @@ func TestZkFromFlatCircuitCode(t *testing.T) {
circuit , err := parser . Parse ( )
assert . Nil ( t , err )
fmt . Println ( "\ncircuit data:" , circuit )
circuitJson , _ := json . Marshal ( circuit )
fmt . Println ( "circuit:" , string ( circuitJson ) )
b3 := big . NewInt ( int64 ( 3 ) )
inputs := [ ] * big . Int { b3 }
pr ivateI nputs := [ ] * big . Int { b3 }
// wittness
w , err := circuit . CalculateWitness ( inputs )
w , err := circuit . CalculateWitness ( pr ivateI nputs)
assert . Nil ( t , err )
fmt . Println ( "\nwitness" , w )
@ -49,11 +122,16 @@ func TestZkFromFlatCircuitCode(t *testing.T) {
// R1CS to QAP
alphas , betas , gammas , zx := Utils . PF . R1CSToQAP ( a , b , c )
fmt . Println ( "qap" )
fmt . Println ( alphas )
fmt . Println ( betas )
fmt . Println ( gammas )
fmt . Println ( "alphas" , alphas )
fmt . Println ( "betas" , betas )
fmt . Println ( "gammas" , gammas )
fmt . Println ( "zx" , zx )
ax , bx , cx , px := Utils . PF . CombinePolynomials ( w , alphas , betas , gammas )
fmt . Println ( "ax" , ax )
// fmt.Println("bx", bx)
// fmt.Println("cx", cx)
// fmt.Println("px", px)
hx := Utils . PF . DivisorPolynomial ( px , zx )
@ -72,21 +150,29 @@ func TestZkFromFlatCircuitCode(t *testing.T) {
// calculate trusted setup
setup , err := GenerateTrustedSetup ( len ( w ) , * circuit , alphas , betas , gammas , zx )
// setup, err := GenerateTrustedSetup(len(w), *circuit, ax, bx, cx, zx)
assert . Nil ( t , err )
fmt . Println ( "\nt:" , setup . Toxic . T )
// piA = g1 * A(t), piB = g2 * B(t), piC = g1 * C(t), piH = g1 * H(t)
proof , err := GenerateProofs ( * circuit , setup , hx , w )
assert . Nil ( t , err )
fmt . Println ( "IC" , setup . Vk . IC )
fmt . Println ( "\n proofs:" )
fmt . Println ( proof )
fmt . Println ( "public signals:" , proof . PublicSignals )
// fmt.Println("\n proofs:")
// fmt.Println(proof)
// fmt.Println("public signals:", proof.PublicSignals)
fmt . Println ( "\nwitness" , w )
b35 := big . NewInt ( int64 ( 35 ) )
publicSignals := [ ] * big . Int { b35 }
fmt . Println ( "public signals:" , publicSignals )
before := time . Now ( )
assert . True ( t , VerifyProof ( * circuit , setup , proof , true ) )
assert . True ( t , VerifyProof ( * circuit , setup , proof , publicSignals , true ) )
fmt . Println ( "verify proof time elapsed:" , time . Since ( before ) )
}
/ *
func TestZkFromHardcodedR1CS ( t * testing . T ) {
b0 := big . NewInt ( int64 ( 0 ) )
b1 := big . NewInt ( int64 ( 1 ) )
@ -148,7 +234,9 @@ func TestZkFromHardcodedR1CS(t *testing.T) {
proof , err := GenerateProofs ( circuit , setup , hx , w )
assert . Nil ( t , err )
assert . True ( t , VerifyProof ( circuit , setup , proof , true ) )
// assert.True(t, VerifyProof(circuit, setup, proof, true))
publicSignals := [ ] * big . Int { b35 }
assert . True ( t , VerifyProof ( circuit , setup , proof , publicSignals , true ) )
}
func TestZkMultiplication ( t * testing . T ) {
@ -202,5 +290,9 @@ func TestZkMultiplication(t *testing.T) {
proof , err := GenerateProofs ( * circuit , setup , hx , w )
assert . Nil ( t , err )
assert . True ( t , VerifyProof ( * circuit , setup , proof , false ) )
// assert.True(t, VerifyProof(*circuit, setup, proof, false))
b35 := big . NewInt ( int64 ( 35 ) )
publicSignals := [ ] * big . Int { b35 }
assert . True ( t , VerifyProof ( * circuit , setup , proof , publicSignals , true ) )
}
* /