mirror of
https://github.com/arnaucube/go-circom-prover-verifier.git
synced 2026-02-07 03:16:46 +01:00
Add bn256.G1 parser
This commit is contained in:
68
parsers.go
Normal file
68
parsers.go
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package gocircomprover
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"math/big"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
|
||||||
|
)
|
||||||
|
|
||||||
|
func stringToG1(h []string) (*bn256.G1, error) {
|
||||||
|
if len(h) <= 2 {
|
||||||
|
return nil, fmt.Errorf("not enought data for stringToG1")
|
||||||
|
}
|
||||||
|
h = h[:2]
|
||||||
|
hexa := false
|
||||||
|
if len(h[0]) > 1 {
|
||||||
|
if "0x" == h[0][:2] {
|
||||||
|
hexa = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
in := ""
|
||||||
|
|
||||||
|
var b []byte
|
||||||
|
var err error
|
||||||
|
if hexa {
|
||||||
|
for i := range h {
|
||||||
|
in += strings.TrimPrefix(h[i], "0x")
|
||||||
|
}
|
||||||
|
b, err = hex.DecodeString(in)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// TODO TMP
|
||||||
|
// TODO use stringToBytes()
|
||||||
|
if h[0] == "1" {
|
||||||
|
h[0] = "0"
|
||||||
|
}
|
||||||
|
if h[1] == "1" {
|
||||||
|
h[1] = "0"
|
||||||
|
}
|
||||||
|
bi0, ok := new(big.Int).SetString(h[0], 10)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("error parsing stringToG1")
|
||||||
|
}
|
||||||
|
bi1, ok := new(big.Int).SetString(h[1], 10)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("error parsing stringToG1")
|
||||||
|
}
|
||||||
|
b0 := bi0.Bytes()
|
||||||
|
b1 := bi1.Bytes()
|
||||||
|
if len(b0) != 32 {
|
||||||
|
b0 = addZPadding(b0)
|
||||||
|
}
|
||||||
|
if len(b1) != 32 {
|
||||||
|
b1 = addZPadding(b1)
|
||||||
|
}
|
||||||
|
|
||||||
|
b = append(b, b0...)
|
||||||
|
b = append(b, b1...)
|
||||||
|
}
|
||||||
|
p := new(bn256.G1)
|
||||||
|
_, err = p.Unmarshal(b)
|
||||||
|
|
||||||
|
return p, err
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user