Add witness parser from binary file

This commit is contained in:
arnaucube
2020-04-30 10:29:28 +02:00
parent 700f2a4503
commit a314ecc02f
8 changed files with 1027 additions and 9 deletions

View File

@@ -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
}