Skip to content

Commit

Permalink
fix: deprecate arbo.BytesToBigInt in favor of explicit LE and BE funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Oct 14, 2024
1 parent 279e0eb commit edb37ba
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
12 changes: 6 additions & 6 deletions tree/arbo/circomproofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ type CircomVerifierProof struct {
func (cvp CircomVerifierProof) MarshalJSON() ([]byte, error) {
m := make(map[string]any)

m["root"] = BytesToBigInt(cvp.Root).String()
m["root"] = BytesBEToBigInt(cvp.Root).String()
m["siblings"] = siblingsToStringArray(cvp.Siblings)
m["oldKey"] = BytesToBigInt(cvp.OldKey).String()
m["oldValue"] = BytesToBigInt(cvp.OldValue).String()
m["oldKey"] = BytesBEToBigInt(cvp.OldKey).String()
m["oldValue"] = BytesBEToBigInt(cvp.OldValue).String()
if cvp.IsOld0 {
m["isOld0"] = "1"
} else {
m["isOld0"] = "0"
}
m["key"] = BytesToBigInt(cvp.Key).String()
m["value"] = BytesToBigInt(cvp.Value).String()
m["key"] = BytesBEToBigInt(cvp.Key).String()
m["value"] = BytesBEToBigInt(cvp.Value).String()
m["fnc"] = cvp.Fnc

return json.Marshal(m)
Expand All @@ -41,7 +41,7 @@ func (cvp CircomVerifierProof) MarshalJSON() ([]byte, error) {
func siblingsToStringArray(s [][]byte) []string {
var r []string
for i := 0; i < len(s); i++ {
r = append(r, BytesToBigInt(s[i]).String())
r = append(r, BytesBEToBigInt(s[i]).String())
}
return r
}
Expand Down
1 change: 1 addition & 0 deletions tree/arbo/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package arbo

import (
"crypto/sha256"
"fmt"

Check failure on line 5 in tree/arbo/hash.go

View workflow job for this annotation

GitHub Actions / job_go_test

"fmt" imported and not used

Check failure on line 5 in tree/arbo/hash.go

View workflow job for this annotation

GitHub Actions / job_go_checks

"fmt" imported and not used
"math/big"

"github.com/iden3/go-iden3-crypto/poseidon"
Expand Down
29 changes: 29 additions & 0 deletions tree/arbo/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,48 @@ func SwapEndianness(b []byte) []byte {
}

// BigIntToBytes converts a *big.Int into a byte array in Little-Endian
//
// Deprecated: use BigIntToBytesLE or BigIntToBytesBE
func BigIntToBytes(blen int, bi *big.Int) []byte {
return BigIntToBytesLE(blen, bi)
}

// BigIntToBytesLE converts a *big.Int into a byte array in Little-Endian
func BigIntToBytesLE(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()))
return b
}

// BigIntToBytesBE converts a *big.Int into a byte array in Big-Endian
func BigIntToBytesBE(blen int, bi *big.Int) []byte {
// TODO make the length depending on the tree.hashFunction.Len()
b := make([]byte, blen)
copy(b, bi.Bytes())
return b
}

// BytesToBigInt converts a byte array in Little-Endian representation into
// *big.Int
//
// Deprecated: use BytesLEToBigInt or BytesBEToBigInt
func BytesToBigInt(b []byte) *big.Int {
return BytesLEToBigInt(b)
}

// BytesLEToBigInt converts a byte array in Little-Endian representation into
// *big.Int
func BytesLEToBigInt(b []byte) *big.Int {
return new(big.Int).SetBytes(SwapEndianness(b))
}

// BytesBEToBigInt converts a byte array in Big-Endian representation into
// *big.Int
func BytesBEToBigInt(b []byte) *big.Int {
return new(big.Int).SetBytes(b)
}

// newLeafValue takes a key & value from a leaf, and computes the leaf hash,
// which is used as the leaf key. And the value is the concatenation of the
// inputted key & value. The output of this function is used as key-value to
Expand Down

0 comments on commit edb37ba

Please sign in to comment.