|
@ -1,14 +1,168 @@ |
|
|
package gocircomprover |
|
|
package gocircomprover |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
|
|
|
"bytes" |
|
|
"encoding/hex" |
|
|
"encoding/hex" |
|
|
|
|
|
"encoding/json" |
|
|
"fmt" |
|
|
"fmt" |
|
|
"math/big" |
|
|
"math/big" |
|
|
|
|
|
"strconv" |
|
|
"strings" |
|
|
"strings" |
|
|
|
|
|
|
|
|
bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare" |
|
|
bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
type ProvingKeyString struct { |
|
|
|
|
|
A [][]string `json:"A"` |
|
|
|
|
|
B2 [][][]string `json:"B2"` |
|
|
|
|
|
B1 [][]string `json:"B1"` |
|
|
|
|
|
C [][]string `json:"C"` |
|
|
|
|
|
NVars int `json:"nVars"` |
|
|
|
|
|
NPublic int `json:"nPublic"` |
|
|
|
|
|
VkAlpha1 []string `json:"vk_alfa_1"` |
|
|
|
|
|
VkDelta1 []string `json:"vk_delta_1"` |
|
|
|
|
|
VkBeta1 []string `json:"vk_beta_1"` |
|
|
|
|
|
VkBeta2 [][]string `json:"vk_beta_2"` |
|
|
|
|
|
VkDelta2 [][]string `json:"vk_delta_2"` |
|
|
|
|
|
HExps [][]string `json:"hExps"` |
|
|
|
|
|
DomainSize int `json:"domainSize"` |
|
|
|
|
|
PolsA []map[string]string `json:"polsA"` |
|
|
|
|
|
PolsB []map[string]string `json:"polsB"` |
|
|
|
|
|
PolsC []map[string]string `json:"polsC"` |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type WitnessString []string |
|
|
|
|
|
|
|
|
|
|
|
func ParseProvingKey(pkJson []byte) (*ProvingKey, error) { |
|
|
|
|
|
var pkStr ProvingKeyString |
|
|
|
|
|
err := json.Unmarshal(pkJson, &pkStr) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
pk, err := provingKeyStringToProvingKey(pkStr) |
|
|
|
|
|
return pk, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func provingKeyStringToProvingKey(ps ProvingKeyString) (*ProvingKey, error) { |
|
|
|
|
|
var p ProvingKey |
|
|
|
|
|
var err error |
|
|
|
|
|
|
|
|
|
|
|
p.A, err = arrayStringToG1(ps.A) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
p.B2, err = arrayStringToG2(ps.B2) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
p.B1, err = arrayStringToG1(ps.B1) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
p.C, err = arrayStringToG1(ps.C) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
p.NVars = ps.NVars |
|
|
|
|
|
p.NPublic = ps.NPublic |
|
|
|
|
|
|
|
|
|
|
|
p.VkAlpha1, err = stringToG1(ps.VkAlpha1) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
p.VkDelta1, err = stringToG1(ps.VkDelta1) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
p.VkBeta1, err = stringToG1(ps.VkBeta1) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
p.VkBeta2, err = stringToG2(ps.VkBeta2) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
p.VkDelta2, err = stringToG2(ps.VkDelta2) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
p.HExps, err = arrayStringToG1(ps.HExps) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
p.DomainSize = ps.DomainSize |
|
|
|
|
|
|
|
|
|
|
|
p.PolsA, err = polsStringToBigInt(ps.PolsA) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
p.PolsB, err = polsStringToBigInt(ps.PolsB) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
p.PolsC, err = polsStringToBigInt(ps.PolsC) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return &p, nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// polsStringToBigInt is for taking string polynomials and converting it to *big.Int polynomials
|
|
|
|
|
|
func polsStringToBigInt(s []map[string]string) ([]map[int]*big.Int, error) { |
|
|
|
|
|
var o []map[int]*big.Int |
|
|
|
|
|
for i := 0; i < len(s); i++ { |
|
|
|
|
|
// var oi map[int]*big.Int
|
|
|
|
|
|
oi := make(map[int]*big.Int) |
|
|
|
|
|
for j, v := range s[i] { |
|
|
|
|
|
si, err := stringToBigInt(v) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return o, err |
|
|
|
|
|
} |
|
|
|
|
|
// oi = append(oi, si)
|
|
|
|
|
|
jInt, err := strconv.Atoi(j) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
fmt.Println(j) |
|
|
|
|
|
return o, err |
|
|
|
|
|
} |
|
|
|
|
|
fmt.Println(jInt, si) |
|
|
|
|
|
oi[jInt] = si |
|
|
|
|
|
} |
|
|
|
|
|
o = append(o, oi) |
|
|
|
|
|
} |
|
|
|
|
|
return o, nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func arrayStringToBigInt(s []string) ([]*big.Int, error) { |
|
|
|
|
|
var o []*big.Int |
|
|
|
|
|
for i := 0; i < len(s); i++ { |
|
|
|
|
|
si, err := stringToBigInt(s[i]) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return o, nil |
|
|
|
|
|
} |
|
|
|
|
|
o = append(o, si) |
|
|
|
|
|
} |
|
|
|
|
|
return o, nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func stringToBigInt(s string) (*big.Int, error) { |
|
|
|
|
|
base := 10 |
|
|
|
|
|
if bytes.HasPrefix([]byte(s), []byte("0x")) { |
|
|
|
|
|
base = 16 |
|
|
|
|
|
s = strings.TrimPrefix(s, "0x") |
|
|
|
|
|
} |
|
|
|
|
|
n, ok := new(big.Int).SetString(s, base) |
|
|
|
|
|
if !ok { |
|
|
|
|
|
return nil, fmt.Errorf("Can not parse string to *big.Int: %s", s) |
|
|
|
|
|
} |
|
|
|
|
|
return n, nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func addZPadding(b []byte) []byte { |
|
|
func addZPadding(b []byte) []byte { |
|
|
var z [32]byte |
|
|
var z [32]byte |
|
|
var r []byte |
|
|
var r []byte |
|
|