diff --git a/gnark.go b/gnark.go index 45f5829..004eeeb 100644 --- a/gnark.go +++ b/gnark.go @@ -38,7 +38,7 @@ func (t *Tree) GenerateGnarkVerifierProof(k []byte) (*GnarkVerifierProof, error) if err != nil { return nil, err } - // convert the siblings to big.Int swaping the endianess + // convert the siblings to big.Int swapping the endianess bigSiblings := make([]*big.Int, len(unpackedSiblings)) for i := range bigSiblings { bigSiblings[i] = BytesToBigInt(unpackedSiblings[i]) diff --git a/tree_big.go b/tree_big.go index a5e8723..a12a4a9 100644 --- a/tree_big.go +++ b/tree_big.go @@ -176,7 +176,7 @@ func (t *Tree) leafToBigInts(key, value, serializedBigInts []byte) (*big.Int, [] // reverse the process of bigints encoding bigints := deserializeBigInts(serializedBigInts) // reencode the leaf value of the tree to check if it matches the value - bigintsHash, err := hashBigInts(t.HashFunction(), bigints...) + bigintsHash, err := HashBigInts(t.HashFunction(), bigints...) if err != nil { return nil, nil, err } @@ -253,18 +253,18 @@ func bigIntsToLeaf(hFn HashFunction, keyLen int, key *big.Int, bigints []*big.In return nil, nil, nil, err } // calculate the value used to build the tree - bValue, err = hashBigInts(hFn, bigints...) + bValue, err = HashBigInts(hFn, bigints...) if err != nil { return nil, nil, nil, err } return bKey, bValue, serializedBigInts, nil } -// hashBigInts hashes the bytes of the big.Int values +// HashBigInts hashes the bytes of the big.Int values // using the hash function of the tree. The resulting hash can be used as the leaf value -func hashBigInts(hFn HashFunction, bigints ...*big.Int) ([]byte, error) { - chunks := make([][]byte, len(bigints)) - for _, v := range bigints { +func HashBigInts(hFn HashFunction, values ...*big.Int) ([]byte, error) { + chunks := make([][]byte, len(values)) + for _, v := range values { value := hFn.SafeBigInt(v) if value == nil { return nil, fmt.Errorf("value cannot be nil") diff --git a/utils.go b/utils.go index e88c917..2931a85 100644 --- a/utils.go +++ b/utils.go @@ -11,21 +11,21 @@ func SwapEndianness(b []byte) []byte { for i := range b { o[len(b)-1-i] = b[i] } - return ExplicitZero(o) + return o } // BigIntToBytes converts a *big.Int into a byte array in Little-Endian func BigIntToBytes(blen int, bi *big.Int) []byte { // TODO make the length depending on the tree.hashFunction.Len() b := make([]byte, blen) - copy(b[:], SwapEndianness(bi.Bytes())) + copy(b[:], ExplicitZero(SwapEndianness(bi.Bytes()))) return b[:] } // BytesToBigInt converts a byte array in Little-Endian representation into // *big.Int func BytesToBigInt(b []byte) *big.Int { - return new(big.Int).SetBytes(SwapEndianness(b)) + return new(big.Int).SetBytes(ExplicitZero(SwapEndianness(b))) } // ExplicitZero returns a byte slice with a single zero byte if the input slice