Browse Source

Add Proof & VerifierKey data structs with parsers from json

master
arnaucube 4 years ago
parent
commit
94166178bc
5 changed files with 228 additions and 1 deletions
  1. +1
    -1
      go.mod
  2. +29
    -0
      testdata/proof0.json
  3. +58
    -0
      testdata/vk0.json
  4. +120
    -0
      verifier.go
  5. +20
    -0
      verifier_test.go

+ 1
- 1
go.mod

@ -1,6 +1,6 @@
module bellmanverifier module bellmanverifier
go 1.12
go 1.13
require ( require (
github.com/ethereum/go-ethereum v1.9.11 github.com/ethereum/go-ethereum v1.9.11

+ 29
- 0
testdata/proof0.json

@ -0,0 +1,29 @@
{
"a": [
"0x2cdfab288afda1ba399d60951423e76445754b7d2e7827634732988373c8e0ff",
"0x1bb2e3543dfdd373610db3ea82703bc70e4d6f01b5c0e10709e27545670824f4"
],
"b": [
[
"0x0f9cd75cae408d3b6d2731035dbb6aa6f66cf900c1d64b46510cbbcd7cf94f64",
"0x165997362d7c2bf5672d16206fbee620ad8eb54f609b98c49c7c6287c9979077"
],
[
"0x0392982b7cd7bdbbee79d5f808c67ead8ef3e2347810b546b419da5f738aab92",
"0x0f94d6781b9de113b86abd1930accd2260d0c979d520bc1a0e79dec3c8ce76a3"
]
],
"c": [
"0x0b710c2f32925ad4576f925e8c5954c7dace91437fd6bc3ded1d15b70990a885",
"0x2cd6ea79e38ad566aceb09113e67142d278d12a76b49031ce1bde1dedca696b4"
],
"public": [
"6"
]
}

+ 58
- 0
testdata/vk0.json

@ -0,0 +1,58 @@
{
"alpha_g1":[
"0x0c0e14d07f2281c592952c72f86a7f5df7189ab6d00b84609ad777fbf062f38d",
"0x2f60c0e4913ec4691bdf2dd9f2b5fed9b80a3267eec107b9f1d69418a19a30a8"
],
"beta_g1":[
"0x22b91a2e3f772de5fab517027ffc911b267cd2bb5ecb0e4af04d2fff16492245",
"0x0fba22e490ec7870abd5181d2ec52f7030f0a905f0fa0a8bf1881f054250aac2"
],
"beta_g2":[
[
"0x0b5f21c2d981916cd5e1037b446b170b6c60dd184fdbb3381b7d0880fb48300d",
"0x2275d97dce5445433ec7bc6d01c35f0afad9afcf6f3350cd15eeef1023242c01"
],
[
"0x1690100372c53776b60c0ee56926debb4d0acee90f7952ecc63861e0269a098a",
"0x2c2a08a60032f536afbcb21c079b563cdce68b7ef906e973c52f574121a95df0"
]
],
"delta_g1":[
"0x2bf9136e1957878c29d7feb39318eab00a45f7239f2682abafcb4cf84edadfee",
"0x01348c9322f0cf582b47d8fff512945ca377b9c8c1696743a0b1f83454ada9eb"
],
"delta_g2":[
[
"0x106fb9af91458db7637ad8e4fcc292fb91f7d41c5f8b6f248ac10aa21c8c95fa",
"0x1007e6644507b7b194673f88b49a1b22d7ca9d85a82d8a9ae48f0bc88314c08b"
],
[
"0x1c56275a69d22732c867b9b537d173a4ae8e8994c3e0591566f874847f8223d7",
"0x24264a0b9040259eb7e7d7456d9197b1938294f1eb749efa3adc4829360acb3d"
]
],
"gamma_g2":[
[
"0x2d0c4fa1239184802aeda1f206e49104940aa3eccc1b3e0141c25b2dba8e7caf",
"0x15c9b1123841897787badbe858eb00943fc8a99454666f21acf4e79e13547471"
],
[
"0x256ad09ecb0abc15fd48f20c37d28ffcf0f8eb3b23cb10cdeee7365b598963ac",
"0x2bc9bc381cf68badd992338c637b36b54936b69cb8560eaf5a8cbe2c20ff8522"
]
],
"ic":[
[
"0x0fd572add09b4d9b1f3bcf382792dd3cc173af539fd234b362030aef49c15e48",
"0x20ee06100c05c94e58edd1f01a051bec231574fe0cf025ee984df2d775b266cd"
],
[
"0x2577ec7416ef3379567e196243e3c3bd7a456e3cf26f1cef8e92a83c302108ac",
"0x1766d057aab33f1af0acac750f941e23d23027f28055be67bc13427b9b27d617"
]
],
"input_names":[
"main.r"
]
}

+ 120
- 0
verifier.go

@ -3,6 +3,7 @@ package bellmanverifier
import ( import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"encoding/json"
"fmt" "fmt"
"math/big" "math/big"
"strings" "strings"
@ -10,6 +11,35 @@ import (
bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare" bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
) )
type Vk struct {
Alpha *bn256.G1
Beta *bn256.G2
Gamma *bn256.G2
Delta *bn256.G2
GammaABC []*bn256.G1
}
type VkRaw struct {
Alpha []string `json:"alpha_g1"`
Beta [][]string `json:"beta_g2"`
Gamma [][]string `json:"gamma_g2"`
Delta [][]string `json:"delta_g2"`
GammaABC [][]string `json:"ic"`
}
type Proof struct {
A *bn256.G1
B *bn256.G2
C *bn256.G1
}
type ProofRaw struct {
A []string `json:"a"`
B [][]string `json:"b"`
C []string `json:"c"`
Inputs []string `json:"inputs"`
}
func hexToG1(h []string) (*bn256.G1, error) { func hexToG1(h []string) (*bn256.G1, error) {
in := "" in := ""
for i := range h { for i := range h {
@ -71,3 +101,93 @@ func stringToBigInt(s string) *big.Int {
} }
return n return n
} }
func parsePublicRaw(pj []byte) ([]*big.Int, error) {
var pr []string
err := json.Unmarshal(pj, &pr)
if err != nil {
return nil, err
}
var public []*big.Int
for _, s := range pr {
public = append(public, stringToBigInt(s))
}
return public, nil
}
// ParseVkRaw takes a json []byte and outputs the *Vk struct
func ParseVkRaw(vj []byte) (*Vk, error) {
var vr VkRaw
err := json.Unmarshal(vj, &vr)
if err != nil {
return nil, err
}
v, err := vkRawToVk(vr)
return v, err
}
// ParseProofRaw takes a json []byte and outputs the *Proof struct
func ParseProofRaw(pj []byte) (*Proof, error) {
var pr ProofRaw
err := json.Unmarshal(pj, &pr)
if err != nil {
return nil, err
}
p, err := proofRawToProof(pr)
return p, err
}
func vkRawToVk(vr VkRaw) (*Vk, error) {
var v Vk
var err error
v.Alpha, err = hexToG1(vr.Alpha)
if err != nil {
return nil, err
}
v.Beta, err = hexToG2(vr.Beta)
if err != nil {
return nil, err
}
v.Gamma, err = hexToG2(vr.Gamma)
if err != nil {
return nil, err
}
v.Delta, err = hexToG2(vr.Delta)
if err != nil {
return nil, err
}
for i := 0; i < len(vr.GammaABC); i++ {
p, err := hexToG1(vr.GammaABC[i])
if err != nil {
return nil, err
}
v.GammaABC = append(v.GammaABC, p)
}
return &v, nil
}
func proofRawToProof(pr ProofRaw) (*Proof, error) {
var p Proof
var err error
p.A, err = hexToG1(pr.A)
if err != nil {
return nil, err
}
p.B, err = hexToG2(pr.B)
if err != nil {
return nil, err
}
p.C, err = hexToG1(pr.C)
if err != nil {
return nil, err
}
return &p, nil
}

+ 20
- 0
verifier_test.go

@ -1,9 +1,11 @@
package bellmanverifier package bellmanverifier
import ( import (
"io/ioutil"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestParsePoints(t *testing.T) { func TestParsePoints(t *testing.T) {
@ -40,3 +42,21 @@ func TestParsePoints(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "bn256.G2((15c9b1123841897787badbe858eb00943fc8a99454666f21acf4e79e13547471, 2d0c4fa1239184802aeda1f206e49104940aa3eccc1b3e0141c25b2dba8e7caf), (2bc9bc381cf68badd992338c637b36b54936b69cb8560eaf5a8cbe2c20ff8522, 256ad09ecb0abc15fd48f20c37d28ffcf0f8eb3b23cb10cdeee7365b598963ac))", g.String()) assert.Equal(t, "bn256.G2((15c9b1123841897787badbe858eb00943fc8a99454666f21acf4e79e13547471, 2d0c4fa1239184802aeda1f206e49104940aa3eccc1b3e0141c25b2dba8e7caf), (2bc9bc381cf68badd992338c637b36b54936b69cb8560eaf5a8cbe2c20ff8522, 256ad09ecb0abc15fd48f20c37d28ffcf0f8eb3b23cb10cdeee7365b598963ac))", g.String())
} }
func TestParseProof(t *testing.T) {
proofJson, err := ioutil.ReadFile("testdata/proof0.json")
require.Nil(t, err)
vkJson, err := ioutil.ReadFile("testdata/vk0.json")
require.Nil(t, err)
proof, err := ParseProofRaw(proofJson)
require.Nil(t, err)
vk, err := ParseVkRaw(vkJson)
require.Nil(t, err)
assert.Equal(t, "bn256.G1(2cdfab288afda1ba399d60951423e76445754b7d2e7827634732988373c8e0ff, 1bb2e3543dfdd373610db3ea82703bc70e4d6f01b5c0e10709e27545670824f4)", proof.A.String())
assert.Equal(t, "bn256.G1(0b710c2f32925ad4576f925e8c5954c7dace91437fd6bc3ded1d15b70990a885, 2cd6ea79e38ad566aceb09113e67142d278d12a76b49031ce1bde1dedca696b4)", proof.C.String())
assert.Equal(t, "bn256.G1(0c0e14d07f2281c592952c72f86a7f5df7189ab6d00b84609ad777fbf062f38d, 2f60c0e4913ec4691bdf2dd9f2b5fed9b80a3267eec107b9f1d69418a19a30a8)", vk.Alpha.String())
assert.Equal(t, "bn256.G2((2275d97dce5445433ec7bc6d01c35f0afad9afcf6f3350cd15eeef1023242c01, 0b5f21c2d981916cd5e1037b446b170b6c60dd184fdbb3381b7d0880fb48300d), (2c2a08a60032f536afbcb21c079b563cdce68b7ef906e973c52f574121a95df0, 1690100372c53776b60c0ee56926debb4d0acee90f7952ecc63861e0269a098a))", vk.Beta.String())
assert.Equal(t, "bn256.G2((15c9b1123841897787badbe858eb00943fc8a99454666f21acf4e79e13547471, 2d0c4fa1239184802aeda1f206e49104940aa3eccc1b3e0141c25b2dba8e7caf), (2bc9bc381cf68badd992338c637b36b54936b69cb8560eaf5a8cbe2c20ff8522, 256ad09ecb0abc15fd48f20c37d28ffcf0f8eb3b23cb10cdeee7365b598963ac))", vk.Gamma.String())
}

Loading…
Cancel
Save