Browse Source

Add testdata big&small circuits, update proof parsers, add compile-circuits.sh

ed255-patch-1
arnaucube 4 years ago
parent
commit
7aa69a5b2c
11 changed files with 119 additions and 234 deletions
  1. +7
    -0
      .gitignore
  2. +8
    -2
      README.md
  3. +19
    -0
      compile-circuits.sh
  4. +11
    -1
      parsers.go
  5. +49
    -4
      prover_test.go
  6. +14
    -0
      testdata/big/circuit.circom
  7. +1
    -0
      testdata/big/input.json
  8. +0
    -221
      testdata/provingkey.json
  9. +9
    -0
      testdata/small/circuit.circom
  10. +1
    -0
      testdata/small/input.json
  11. +0
    -6
      testdata/witness.json

+ 7
- 0
.gitignore

@ -0,0 +1,7 @@
testdata/*/*.json
testdata/*/*.wasm
testdata/*/*.cpp
testdata/*/*.sym
testdata/*/*.r1cs
!testdata/*/input.json

+ 8
- 2
README.md

@ -21,6 +21,12 @@ w, _ := circomprover.ParseWitness(witnessJson)
// generate the proof
proof, pubSignals, err := circomprover.GenerateProof(pk, w)
assert.Nil(t, err)
fmt.Println(pubSignals)
fmt.Println(proof)
proofStr, err := circomprover.ProofToString(proof)
assert.Nil(t, err)
publicStr, err := json.Marshal(circomprover.ArrayBigIntToString(pubSignals)
assert.Nil(t, err)
fmt.Println(proofStr)
fmt.Println(publicStr)
```

+ 19
- 0
compile-circuits.sh

@ -0,0 +1,19 @@
#!/bin/sh
echo "testdata/small/circuit.circom"
cd testdata/small
echo "compiling circuit"
circom circuit.circom -r1cs --wasm --sym
echo "generating setup"
snarkjs setup
echo "calculating witness"
snarkjs calculatewitness --wasm circuit.wasm --input input.json --witness witness.json
echo "\ntestdata/big/circuit.circom"
cd ../big
echo "compiling circuit"
circom circuit.circom -r1cs --wasm --sym
echo "generating setup"
snarkjs setup
echo "calculating witness"
snarkjs calculatewitness --wasm circuit.wasm --input input.json --witness witness.json

+ 11
- 1
parsers.go

@ -166,6 +166,15 @@ func polsStringToBigInt(s []map[string]string) ([]map[int]*big.Int, error) {
return o, nil
}
// ArrayBigIntToString converts an []*big.Int into []string, used to output the Public Signals
func ArrayBigIntToString(bi []*big.Int) []string {
var s []string
for i := 0; i < len(bi); i++ {
s = append(s, bi[i].String())
}
return s
}
func arrayStringToBigInt(s []string) ([]*big.Int, error) {
var o []*big.Int
for i := 0; i < len(s); i++ {
@ -350,7 +359,8 @@ func stringToG2(h [][]string) (*bn256.G2, error) {
return p, err
}
func proofToString(p *Proof) ([]byte, error) {
// ProofToJson outputs the Proof i Json format
func ProofToJson(p *Proof) ([]byte, error) {
var ps ProofString
a := p.A.Marshal()

+ 49
- 4
prover_test.go

@ -1,6 +1,7 @@
package gocircomprover
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/big"
@ -10,8 +11,8 @@ import (
"github.com/stretchr/testify/require"
)
func TestGenerateProf(t *testing.T) {
provingKeyJson, err := ioutil.ReadFile("testdata/provingkey.json")
func TestSmallCircuitGenerateProf(t *testing.T) {
provingKeyJson, err := ioutil.ReadFile("testdata/small/proving_key.json")
require.Nil(t, err)
pk, err := ParseProvingKey(provingKeyJson)
require.Nil(t, err)
@ -20,16 +21,60 @@ func TestGenerateProf(t *testing.T) {
fmt.Println("polsB", pk.PolsB)
fmt.Println("polsC", pk.PolsC)
witnessJson, err := ioutil.ReadFile("testdata/witness.json")
witnessJson, err := ioutil.ReadFile("testdata/small/witness.json")
require.Nil(t, err)
w, err := ParseWitness(witnessJson)
require.Nil(t, err)
fmt.Println("w", w)
assert.Equal(t, Witness{big.NewInt(1), big.NewInt(33), big.NewInt(3), big.NewInt(11)}, w)
proof, pubSignals, err := GenerateProof(pk, w)
assert.Nil(t, err)
fmt.Println("proof", proof)
fmt.Println("pubSignals", pubSignals)
proofStr, err := ProofToJson(proof)
assert.Nil(t, err)
fmt.Println("prover\n", string(proofStr))
err = ioutil.WriteFile("testdata/small/proof.json", proofStr, 0644)
assert.Nil(t, err)
publicStr, err := json.Marshal(ArrayBigIntToString(pubSignals))
assert.Nil(t, err)
err = ioutil.WriteFile("testdata/small/public.json", publicStr, 0644)
assert.Nil(t, err)
// to verify the proof:
// snarkjs verify --vk testdata/small/verification_key.json -p testdata/small/proof.json --pub testdata/small/public.json
}
func TestBigCircuitGenerateProf(t *testing.T) {
provingKeyJson, err := ioutil.ReadFile("testdata/big/proving_key.json")
require.Nil(t, err)
pk, err := ParseProvingKey(provingKeyJson)
require.Nil(t, err)
witnessJson, err := ioutil.ReadFile("testdata/big/witness.json")
require.Nil(t, err)
w, err := ParseWitness(witnessJson)
require.Nil(t, err)
proof, pubSignals, err := GenerateProof(pk, w)
assert.Nil(t, err)
fmt.Println("proof", proof)
fmt.Println("pubSignals", pubSignals)
proofStr, err := ProofToJson(proof)
assert.Nil(t, err)
fmt.Println("prover\n", string(proofStr))
err = ioutil.WriteFile("testdata/big/proof.json", proofStr, 0644)
assert.Nil(t, err)
publicStr, err := json.Marshal(ArrayBigIntToString(pubSignals))
assert.Nil(t, err)
err = ioutil.WriteFile("testdata/big/public.json", publicStr, 0644)
assert.Nil(t, err)
// to verify the proof:
// snarkjs verify --vk testdata/big/verification_key.json -p testdata/big/proof.json --pub testdata/big/public.json
}

+ 14
- 0
testdata/big/circuit.circom

@ -0,0 +1,14 @@
template A(n) {
signal input in;
signal output out;
signal intermediate[n];
intermediate[0] <== in;
for (var i=1; i<n; i++) {
intermediate[i] <== intermediate[i-1] * intermediate[i-1] + i;
}
out <== intermediate[n-1];
}
component main = A(1000); // bigger takes too much time on generating trusted setup

+ 1
- 0
testdata/big/input.json

@ -0,0 +1 @@
{ "in": 1}

+ 0
- 221
testdata/provingkey.json

@ -1,221 +0,0 @@
{
"protocol": "groth",
"nVars": 4,
"nPublic": 1,
"domainBits": 2,
"domainSize": 4,
"polsA": [
{
"1": "1"
},
{
"2": "1"
},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616"
},
{}
],
"polsB": [
{},
{},
{},
{
"0": "1"
}
],
"polsC": [
{},
{
"0": "21888242871839275222246405745257275088548364400416034343698204186575808495616"
},
{},
{}
],
"A": [
[
"16145916318196730299582072104388453231952213805668281741813587224450782397538",
"4434505318477484327659527264104806919103674231447634885054368605283938696207",
"1"
],
[
"10618406967550056457559358662746625591602641004174976323307214433994084907915",
"1843236360452735081347085412539192450068665510574800388201121698908391533923",
"1"
],
[
"1208972877970123411566574123860641832032384890981476033353526096830198333194",
"777503551507025252294438107100944741641946695980350712141258191590862204805",
"1"
],
[
"0",
"1",
"0"
]
],
"B1": [
[
"0",
"1",
"0"
],
[
"0",
"1",
"0"
],
[
"0",
"1",
"0"
],
[
"1208972877970123411566574123860641832032384890981476033353526096830198333194",
"21110739320332249969951967638156330347054364461317472950547779703054364003778",
"1"
]
],
"B2": [
[
[
"0",
"0"
],
[
"1",
"0"
],
[
"0",
"0"
]
],
[
[
"0",
"0"
],
[
"1",
"0"
],
[
"0",
"0"
]
],
[
[
"0",
"0"
],
[
"1",
"0"
],
[
"0",
"0"
]
],
[
[
"9283666785342556550467669770956850930982548182701254051508520248901282197973",
"11369378229277445316894458966429873744779877313900506577160370623273013178252"
],
[
"10625777544326349817513295021482494426101347915428005055375725845993157551870",
"21401790227434807639472120486932615400751346915707967674912972446672152512583"
],
[
"1",
"0"
]
]
],
"C": [
["0", "0", "0"],
["0", "0", "0"],
[
"18545397130363256321926549041639729743141431075318462370025152832852939073307",
"2616861286119881509516846668252666108741377487742351180864484963773522850295",
"1"
],
[
"2525636894222523143142808451978966577881491159416335121257094078801746645235",
"16535861715720884910945782094658684127457731006848459401043529763859412916301",
"1"
]
],
"vk_alfa_1": [
"16068200068882411629410035093795608526771554471937479213726134794660780102550",
"20501676791339890155108177259376873296876233680064261776170683533526889207340",
"1"
],
"vk_beta_1": [
"5365573823291502335794132193185274277974617763863996013954364593194136465016",
"11644349101388223784378896360832586557929271772024496470708905460439243894584",
"1"
],
"vk_delta_1": [
"15680151079584844532489259722917096938769907841931133291928746685613811358733",
"16784279394546603697881462850128771845781623009095957214568117820968443242506",
"1"
],
"vk_beta_2": [
[
"13973091636763944887728510851169742544309374663995476311690518173988838518856",
"12903946180439304546475897520537621821375470264150438270817301786763517825250"
],
[
"370374369234123593044872519351942112043402224488849374153134091815693350697",
"17423079115073430837335625309232513526393852743032331213038909731579295753224"
],
[
"1",
"0"
]
],
"vk_delta_2": [
[
"1192908377564945353343974763532707545526009748811618581810344379529229172159",
"10373868200341234689659697947697825014338252335089936445608341428888724327154"
],
[
"6258644116885726740914814071115026921733331135830050167672544002787860516536",
"2784479362505735918824286514153638713518597314121639212447411360814573899319"
],
[
"1",
"0"
]
],
"hExps": [
[
"1137454402546542017796495169973321459788661791339116580816039119135416491226",
"10581992627412174102808274058339351114019966039682644500297077873241797449624",
"1"
],
[
"3253811140290017934039655168718326971539049766532829948316663892796117200680",
"3496418280903365070403555364992889823060908616232765866481366503085657668823",
"1"
],
[
"7426424892372059053157891943364774187577620238460342150964457392480230669997",
"14261604113665464620229095737623968407326243628348811684313201927885047569756",
"1"
],
[
"14706800931196014592083141709960980909656368788497354451613143286705158867076",
"8321358240716309588423491516494287064322707776658072083979021495463106099808",
"1"
],
[
"21560594640856118286219580794351895174554979903538723611152363886530011848778",
"15512645592267656573910252616175869133748229079507420222439452334013754939136",
"1"
]
]
}

+ 9
- 0
testdata/small/circuit.circom

@ -0,0 +1,9 @@
template Multiplier() {
signal private input a;
signal private input b;
signal output c;
c <== a*b;
}
component main = Multiplier();

+ 1
- 0
testdata/small/input.json

@ -0,0 +1 @@
{ "a":3, "b": 11}

+ 0
- 6
testdata/witness.json

@ -1,6 +0,0 @@
[
"1",
"33",
"3",
"11"
]

Loading…
Cancel
Save