mirror of
https://github.com/arnaucube/go-circom-prover-verifier.git
synced 2026-02-06 19:06:43 +01:00
Add witness parser from binary file
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
package parsers
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/big"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -496,3 +499,31 @@ func ProofToJson(p *types.Proof) ([]byte, error) {
|
||||
|
||||
return json.Marshal(ps)
|
||||
}
|
||||
|
||||
// ParseWitness parses binary file representation of the Witness into the Witness struct
|
||||
func ParseWitnessBin(f *os.File) (types.Witness, error) {
|
||||
var w types.Witness
|
||||
r := bufio.NewReader(f)
|
||||
for {
|
||||
b := make([]byte, 32)
|
||||
n, err := r.Read(b)
|
||||
if err == io.EOF {
|
||||
return w, nil
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if n != 32 {
|
||||
return nil, fmt.Errorf("error on value format, expected 32 bytes, got %v", n)
|
||||
}
|
||||
w = append(w, new(big.Int).SetBytes(swapEndianness(b[0:32])))
|
||||
}
|
||||
}
|
||||
|
||||
// swapEndianness swaps the order of the bytes in the slice.
|
||||
func swapEndianness(b []byte) []byte {
|
||||
o := make([]byte, len(b))
|
||||
for i := range b {
|
||||
o[len(b)-1-i] = b[i]
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package parsers
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestParseArrayG1(t *testing.T) {
|
||||
@@ -143,3 +146,29 @@ func TestParseArrayG2(t *testing.T) {
|
||||
assert.Equal(t, "bn256.G2((1922d70c934543aa655ec3277f7fa10a25ec973a4f001a7c54ce4954b4916f8c, 14865e836947c42cf35b47d30e06535fff9dab319c4296e28afde368960671d5), (2f50fbe77925b0a9d718c9ab38638bafa7c65f43f0d09035e518df97ad294847, 177dfa1a3b8627faf0425d9511bcb4c6ca986ea05e3803b5c643c35b94a7e6fe))", a[3].String())
|
||||
|
||||
}
|
||||
|
||||
func testCircuitParseWitnessBin(t *testing.T, circuit string) {
|
||||
witnessBinFile, err := os.Open("../testdata/" + circuit + "/witness.bin")
|
||||
require.Nil(t, err)
|
||||
defer witnessBinFile.Close()
|
||||
witness, err := ParseWitnessBin(witnessBinFile)
|
||||
require.Nil(t, err)
|
||||
|
||||
witnessJson, err := ioutil.ReadFile("../testdata/" + circuit + "/witness.json")
|
||||
require.Nil(t, err)
|
||||
w, err := ParseWitness(witnessJson)
|
||||
require.Nil(t, err)
|
||||
|
||||
assert.Equal(t, len(w), len(witness))
|
||||
assert.Equal(t, w[0], witness[0])
|
||||
assert.Equal(t, w[1], witness[1])
|
||||
assert.Equal(t, w[10], witness[10])
|
||||
assert.Equal(t, w[len(w)-3], witness[len(w)-3])
|
||||
assert.Equal(t, w[len(w)-2], witness[len(w)-2])
|
||||
assert.Equal(t, w[len(w)-1], witness[len(w)-1])
|
||||
}
|
||||
|
||||
func TestParseWitnessBin(t *testing.T) {
|
||||
testCircuitParseWitnessBin(t, "circuit1k")
|
||||
testCircuitParseWitnessBin(t, "circuit5k")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user