diff --git a/censustree/censustree.go b/censustree/censustree.go index 515648b73..7a5ce433f 100644 --- a/censustree/censustree.go +++ b/censustree/censustree.go @@ -125,12 +125,12 @@ func (t *Tree) Hash(data []byte) ([]byte, error) { // BytesToBigInt unmarshals a slice of bytes into a bigInt following the censusTree encoding rules func (*Tree) BytesToBigInt(data []byte) *big.Int { - return arbo.BytesToBigInt(data) + return arbo.BytesLEToBigInt(data) } // BigIntToBytes marshals a bigInt following the censusTree encoding rules func (t *Tree) BigIntToBytes(b *big.Int) []byte { - return arbo.BigIntToBytes(t.hashLen, b) + return arbo.BigIntToBytesLE(t.hashLen, b) } // FromRoot returns a new read-only Tree for the given root, that uses the same diff --git a/cmd/tools/censusdump/main.go b/cmd/tools/censusdump/main.go index a7f9b09c0..7ae193aed 100644 --- a/cmd/tools/censusdump/main.go +++ b/cmd/tools/censusdump/main.go @@ -122,7 +122,7 @@ func main() { } if err := censusRef.Tree().IterateLeaves(func(key, value []byte) bool { - balance := arbo.BytesToBigInt(value) + balance := arbo.BytesLEToBigInt(value) census.Data = append(census.Data, struct { Address common.Address `json:"address"` Balance string `json:"balance"` diff --git a/crypto/ethereum/vocdoni_sik.go b/crypto/ethereum/vocdoni_sik.go index daa6144b4..ea2359f01 100644 --- a/crypto/ethereum/vocdoni_sik.go +++ b/crypto/ethereum/vocdoni_sik.go @@ -37,7 +37,7 @@ func (k *SignKeys) AccountSIK(secret []byte) ([]byte, error) { return nil, fmt.Errorf("error signing sik payload: %w", err) } seed := []*big.Int{ - arbo.BytesToBigInt(k.Address().Bytes()), + arbo.BytesLEToBigInt(k.Address().Bytes()), util.BigToFF(new(big.Int).SetBytes(secret)), util.BigToFF(new(big.Int).SetBytes(sign)), } @@ -45,7 +45,7 @@ func (k *SignKeys) AccountSIK(secret []byte) ([]byte, error) { if err != nil { return nil, err } - return arbo.BigIntToBytes(arbo.HashFunctionPoseidon.Len(), hash), nil + return arbo.BigIntToBytesLE(arbo.HashFunctionPoseidon.Len(), hash), nil } // AccountSIKnullifier method composes the nullifier of the current SignKeys diff --git a/crypto/zk/circuit/inputs.go b/crypto/zk/circuit/inputs.go index c019d9749..ac13cecb3 100644 --- a/crypto/zk/circuit/inputs.go +++ b/crypto/zk/circuit/inputs.go @@ -84,10 +84,10 @@ func GenerateCircuitInput(p CircuitInputsParameters) (*CircuitInputs, error) { Nullifier: new(big.Int).SetBytes(nullifier).String(), AvailableWeight: p.AvailableWeight.String(), VoteHash: util.BytesToArboSplitStr(p.VotePackage), - SIKRoot: arbo.BytesToBigInt(p.SIKRoot).String(), - CensusRoot: arbo.BytesToBigInt(p.CensusRoot).String(), + SIKRoot: arbo.BytesLEToBigInt(p.SIKRoot).String(), + CensusRoot: arbo.BytesLEToBigInt(p.CensusRoot).String(), - Address: arbo.BytesToBigInt(p.Account.Address().Bytes()).String(), + Address: arbo.BytesLEToBigInt(p.Account.Address().Bytes()).String(), Password: ffPassword.String(), Signature: util.BigToFF(new(big.Int).SetBytes(signature)).String(), diff --git a/crypto/zk/circuit/inputs_test.go b/crypto/zk/circuit/inputs_test.go index a063f0ba1..37e37a4bd 100644 --- a/crypto/zk/circuit/inputs_test.go +++ b/crypto/zk/circuit/inputs_test.go @@ -50,7 +50,7 @@ func TestGenerateCircuitInput(t *testing.T) { SIKRoot: "7714269703880573582519379213888374390024853519732158909852028066903886590497", CensusRoot: "7714269703880573582519379213888374390024853519732158909852028066903886590497", - Address: arbo.BytesToBigInt(acc.Address().Bytes()).String(), + Address: arbo.BytesLEToBigInt(acc.Address().Bytes()).String(), Password: "0", Signature: util.BigToFF(new(big.Int).SetBytes(signature)).String(), diff --git a/crypto/zk/prover/pubsignals.go b/crypto/zk/prover/pubsignals.go index 1aa769397..ca3e83692 100644 --- a/crypto/zk/prover/pubsignals.go +++ b/crypto/zk/prover/pubsignals.go @@ -68,7 +68,7 @@ func (p *Proof) CensusRoot() ([]byte, error) { if err != nil { return nil, err } - return arbo.BigIntToBytes(arbo.HashFunctionPoseidon.Len(), bi), nil + return arbo.BigIntToBytesLE(arbo.HashFunctionPoseidon.Len(), bi), nil } // VoteWeight returns the VoteWeight included into the current proof. @@ -108,5 +108,5 @@ func (p *Proof) SIKRoot() ([]byte, error) { if err != nil { return nil, err } - return arbo.BigIntToBytes(arbo.HashFunctionPoseidon.Len(), bi), nil + return arbo.BigIntToBytesLE(arbo.HashFunctionPoseidon.Len(), bi), nil } diff --git a/crypto/zk/utils.go b/crypto/zk/utils.go index ad52869ce..60a58f5f8 100644 --- a/crypto/zk/utils.go +++ b/crypto/zk/utils.go @@ -93,20 +93,20 @@ func ProverProofToProtobufZKProof(p *prover.Proof, electionId, sikRoot, func zkProofPublicInputs(electionId, sikRoot, censusRoot, nullifier types.HexBytes, voteWeight *big.Int) []string { pubInputs := []string{} // 0. electionId[0] - pubInputs = append(pubInputs, arbo.BytesToBigInt(electionId[:16]).String()) + pubInputs = append(pubInputs, arbo.BytesLEToBigInt(electionId[:16]).String()) // 1. electionId[1] - pubInputs = append(pubInputs, arbo.BytesToBigInt(electionId[16:]).String()) + pubInputs = append(pubInputs, arbo.BytesLEToBigInt(electionId[16:]).String()) // 2. nullifier - pubInputs = append(pubInputs, arbo.BytesToBigInt(nullifier).String()) + pubInputs = append(pubInputs, arbo.BytesLEToBigInt(nullifier).String()) voteHash := sha256.Sum256(voteWeight.Bytes()) // 3. voteHash[0] - pubInputs = append(pubInputs, arbo.BytesToBigInt(voteHash[:16]).String()) + pubInputs = append(pubInputs, arbo.BytesLEToBigInt(voteHash[:16]).String()) // 4. voteHash[1] - pubInputs = append(pubInputs, arbo.BytesToBigInt(voteHash[16:]).String()) + pubInputs = append(pubInputs, arbo.BytesLEToBigInt(voteHash[16:]).String()) // 5. sikRoot - pubInputs = append(pubInputs, arbo.BytesToBigInt(sikRoot).String()) + pubInputs = append(pubInputs, arbo.BytesLEToBigInt(sikRoot).String()) // 6. censusRoot - pubInputs = append(pubInputs, arbo.BytesToBigInt(censusRoot).String()) + pubInputs = append(pubInputs, arbo.BytesLEToBigInt(censusRoot).String()) // 7. availableWeight pubInputs = append(pubInputs, voteWeight.String()) @@ -133,7 +133,7 @@ func ProofToCircomSiblings(proof []byte) ([]string, error) { siblings := make([]string, censustree.DefaultMaxLevels+1) for i := 0; i < len(siblings); i++ { if i < len(rawSiblings) { - siblings[i] = arbo.BytesToBigInt(rawSiblings[i]).String() + siblings[i] = arbo.BytesLEToBigInt(rawSiblings[i]).String() } else { siblings[i] = "0" } diff --git a/test/testcommon/testutil/util.go b/test/testcommon/testutil/util.go index 629d74e11..83e2d4b68 100644 --- a/test/testcommon/testutil/util.go +++ b/test/testcommon/testutil/util.go @@ -82,7 +82,7 @@ func (r *Random) RandomInZKField() []byte { panic(err) } b[31] &= 0b00111111 - if iden3cryptoutils.CheckBigIntInField(arbo.BytesToBigInt(b)) { + if iden3cryptoutils.CheckBigIntInField(arbo.BytesLEToBigInt(b)) { return b } } diff --git a/tree/arbo/addbatch_test.go b/tree/arbo/addbatch_test.go index 0c4cab15f..33ba97dae 100644 --- a/tree/arbo/addbatch_test.go +++ b/tree/arbo/addbatch_test.go @@ -56,8 +56,8 @@ func testInit(c *qt.C, n int) (*Tree, *Tree) { // add the initial leafs to fill a bit the trees before calling the // AddBatch method for i := 0; i < n; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) if err := tree1.Add(k, v); err != nil { c.Fatal(err) } @@ -80,8 +80,8 @@ func TestAddBatchTreeEmpty(t *testing.T) { bLen := 32 var keys, values [][]byte for i := 0; i < nLeafs; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) keys = append(keys, k) values = append(values, v) } @@ -128,8 +128,8 @@ func TestAddBatchTreeEmptyNotPowerOf2(t *testing.T) { bLen := 32 for i := 0; i < nLeafs; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) if err := tree.Add(k, v); err != nil { t.Fatal(err) } @@ -144,8 +144,8 @@ func TestAddBatchTreeEmptyNotPowerOf2(t *testing.T) { var keys, values [][]byte for i := 0; i < nLeafs; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) keys = append(keys, k) values = append(values, v) } @@ -277,17 +277,17 @@ func TestAddBatchTestVector2(t *testing.T) { bLen := tree1.HashFunction().Len() var keys, values [][]byte // 1 - keys = append(keys, BigIntToBytes(bLen, big.NewInt(int64(1)))) - values = append(values, BigIntToBytes(bLen, big.NewInt(int64(1)))) + keys = append(keys, BigIntToBytesLE(bLen, big.NewInt(int64(1)))) + values = append(values, BigIntToBytesLE(bLen, big.NewInt(int64(1)))) // 2 - keys = append(keys, BigIntToBytes(bLen, big.NewInt(int64(2)))) - values = append(values, BigIntToBytes(bLen, big.NewInt(int64(2)))) + keys = append(keys, BigIntToBytesLE(bLen, big.NewInt(int64(2)))) + values = append(values, BigIntToBytesLE(bLen, big.NewInt(int64(2)))) // 3 - keys = append(keys, BigIntToBytes(bLen, big.NewInt(int64(3)))) - values = append(values, BigIntToBytes(bLen, big.NewInt(int64(3)))) + keys = append(keys, BigIntToBytesLE(bLen, big.NewInt(int64(3)))) + values = append(values, BigIntToBytesLE(bLen, big.NewInt(int64(3)))) // 5 - keys = append(keys, BigIntToBytes(bLen, big.NewInt(int64(5)))) - values = append(values, BigIntToBytes(bLen, big.NewInt(int64(5)))) + keys = append(keys, BigIntToBytesLE(bLen, big.NewInt(int64(5)))) + values = append(values, BigIntToBytesLE(bLen, big.NewInt(int64(5)))) for i := 0; i < len(keys); i++ { if err := tree1.Add(keys[i], values[i]); err != nil { @@ -324,17 +324,17 @@ func TestAddBatchTestVector3(t *testing.T) { bLen := tree1.HashFunction().Len() var keys, values [][]byte // 0 - keys = append(keys, BigIntToBytes(bLen, big.NewInt(int64(0)))) - values = append(values, BigIntToBytes(bLen, big.NewInt(int64(0)))) + keys = append(keys, BigIntToBytesLE(bLen, big.NewInt(int64(0)))) + values = append(values, BigIntToBytesLE(bLen, big.NewInt(int64(0)))) // 3 - keys = append(keys, BigIntToBytes(bLen, big.NewInt(int64(3)))) - values = append(values, BigIntToBytes(bLen, big.NewInt(int64(3)))) + keys = append(keys, BigIntToBytesLE(bLen, big.NewInt(int64(3)))) + values = append(values, BigIntToBytesLE(bLen, big.NewInt(int64(3)))) // 7 - keys = append(keys, BigIntToBytes(bLen, big.NewInt(int64(7)))) - values = append(values, BigIntToBytes(bLen, big.NewInt(int64(7)))) + keys = append(keys, BigIntToBytesLE(bLen, big.NewInt(int64(7)))) + values = append(values, BigIntToBytesLE(bLen, big.NewInt(int64(7)))) // 135 - keys = append(keys, BigIntToBytes(bLen, big.NewInt(int64(135)))) - values = append(values, BigIntToBytes(bLen, big.NewInt(int64(135)))) + keys = append(keys, BigIntToBytesLE(bLen, big.NewInt(int64(135)))) + values = append(values, BigIntToBytesLE(bLen, big.NewInt(int64(135)))) for i := 0; i < len(keys); i++ { if err := tree1.Add(keys[i], values[i]); err != nil { @@ -403,8 +403,8 @@ func TestAddBatchTreeNotEmptyFewLeafs(t *testing.T) { bLen := tree1.HashFunction().Len() start := time.Now() for i := initialNLeafs; i < nLeafs; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) if err := tree1.Add(k, v); err != nil { t.Fatal(err) } @@ -414,8 +414,8 @@ func TestAddBatchTreeNotEmptyFewLeafs(t *testing.T) { // prepare the key-values to be added var keys, values [][]byte for i := initialNLeafs; i < nLeafs; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) keys = append(keys, k) values = append(values, v) } @@ -446,8 +446,8 @@ func TestAddBatchTreeNotEmptyEnoughLeafs(t *testing.T) { bLen := tree1.HashFunction().Len() start := time.Now() for i := initialNLeafs; i < nLeafs; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) if err := tree1.Add(k, v); err != nil { t.Fatal(err) } @@ -457,8 +457,8 @@ func TestAddBatchTreeNotEmptyEnoughLeafs(t *testing.T) { // prepare the key-values to be added var keys, values [][]byte for i := initialNLeafs; i < nLeafs; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) keys = append(keys, k) values = append(values, v) } @@ -488,15 +488,15 @@ func TestAddBatchTreeEmptyRepeatedLeafs(t *testing.T) { // prepare the key-values to be added var keys, values [][]byte for i := 0; i < nLeafs; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) keys = append(keys, k) values = append(values, v) } // add repeated key-values for i := 0; i < nRepeatedKeys; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) keys = append(keys, k) values = append(values, v) } @@ -527,8 +527,8 @@ func TestAddBatchTreeNotEmptyFewLeafsRepeatedLeafs(t *testing.T) { // prepare the key-values to be added var keys, values [][]byte for i := 0; i < nLeafs; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) keys = append(keys, k) values = append(values, v) } @@ -554,8 +554,8 @@ func TestSplitInBuckets(t *testing.T) { nLeafs := 16 kvs := make([]kv, nLeafs) for i := 0; i < nLeafs; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) keyPath := make([]byte, 32) copy(keyPath, k) kvs[i].pos = i @@ -660,8 +660,8 @@ func TestAddBatchTreeNotEmpty(t *testing.T) { bLen := tree1.HashFunction().Len() start := time.Now() for i := initialNLeafs; i < nLeafs; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) if err := tree1.Add(k, v); err != nil { t.Fatal(err) } @@ -671,8 +671,8 @@ func TestAddBatchTreeNotEmpty(t *testing.T) { // prepare the key-values to be added var keys, values [][]byte for i := initialNLeafs; i < nLeafs; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) keys = append(keys, k) values = append(values, v) } @@ -702,8 +702,8 @@ func TestAddBatchNotEmptyUnbalanced(t *testing.T) { start := time.Now() for i := initialNLeafs; i < nLeafs; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) if err := tree1.Add(k, v); err != nil { t.Fatal(err) } @@ -722,8 +722,8 @@ func TestAddBatchNotEmptyUnbalanced(t *testing.T) { // add the initial leafs to fill a bit the tree before calling the // AddBatch method for i := 0; i < initialNLeafs; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) // use only the keys of one bucket, store the not used ones for // later if i%4 != 0 { @@ -737,8 +737,8 @@ func TestAddBatchNotEmptyUnbalanced(t *testing.T) { } for i := initialNLeafs; i < nLeafs; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) keys = append(keys, k) values = append(values, v) } @@ -950,7 +950,7 @@ func TestAddKeysWithEmptyValues(t *testing.T) { bLen := 32 var keys, values [][]byte for i := 0; i < nLeafs; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) v := []byte{} keys = append(keys, k) values = append(values, v) diff --git a/tree/arbo/circomproofs.go b/tree/arbo/circomproofs.go index e16419078..94a4aacbb 100644 --- a/tree/arbo/circomproofs.go +++ b/tree/arbo/circomproofs.go @@ -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"] = BytesLEToBigInt(cvp.Root).String() m["siblings"] = siblingsToStringArray(cvp.Siblings) - m["oldKey"] = BytesToBigInt(cvp.OldKey).String() - m["oldValue"] = BytesToBigInt(cvp.OldValue).String() + m["oldKey"] = BytesLEToBigInt(cvp.OldKey).String() + m["oldValue"] = BytesLEToBigInt(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"] = BytesLEToBigInt(cvp.Key).String() + m["value"] = BytesLEToBigInt(cvp.Value).String() m["fnc"] = cvp.Fnc return json.Marshal(m) @@ -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, BytesLEToBigInt(s[i]).String()) } return r } diff --git a/tree/arbo/circomproofs_test.go b/tree/arbo/circomproofs_test.go index a75eba83f..47e69583e 100644 --- a/tree/arbo/circomproofs_test.go +++ b/tree/arbo/circomproofs_test.go @@ -26,15 +26,15 @@ func TestCircomVerifierProof(t *testing.T) { } bLen := 1 for i := 0; i < len(testVector); i++ { - k := BigIntToBytes(bLen, big.NewInt(testVector[i][0])) - v := BigIntToBytes(bLen, big.NewInt(testVector[i][1])) + k := BigIntToBytesLE(bLen, big.NewInt(testVector[i][0])) + v := BigIntToBytesLE(bLen, big.NewInt(testVector[i][1])) if err := tree.Add(k, v); err != nil { t.Fatal(err) } } // proof of existence - k := BigIntToBytes(bLen, big.NewInt(int64(2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(2))) cvp, err := tree.GenerateCircomVerifierProof(k) c.Assert(err, qt.IsNil) jCvp, err := json.Marshal(cvp) @@ -48,7 +48,7 @@ func TestCircomVerifierProof(t *testing.T) { `,"0"],"value":"22"}`) // proof of non-existence - k = BigIntToBytes(bLen, big.NewInt(int64(5))) + k = BigIntToBytesLE(bLen, big.NewInt(int64(5))) cvp, err = tree.GenerateCircomVerifierProof(k) c.Assert(err, qt.IsNil) jCvp, err = json.Marshal(cvp) diff --git a/tree/arbo/hash.go b/tree/arbo/hash.go index 0949c916d..650f77f03 100644 --- a/tree/arbo/hash.go +++ b/tree/arbo/hash.go @@ -89,14 +89,14 @@ func (HashPoseidon) Len() int { func (f HashPoseidon) Hash(b ...[]byte) ([]byte, error) { var toHash []*big.Int for i := 0; i < len(b); i++ { - bi := BytesToBigInt(b[i]) + bi := BytesLEToBigInt(b[i]) toHash = append(toHash, bi) } h, err := poseidon.Hash(toHash) if err != nil { return nil, err } - hB := BigIntToBytes(f.Len(), h) + hB := BigIntToBytesLE(f.Len(), h) return hB, nil } diff --git a/tree/arbo/hash_test.go b/tree/arbo/hash_test.go index da0d6297b..7ddc6bdcf 100644 --- a/tree/arbo/hash_test.go +++ b/tree/arbo/hash_test.go @@ -27,12 +27,12 @@ func TestHashPoseidon(t *testing.T) { hashFunc := &HashPoseidon{} bLen := hashFunc.Len() h, err := hashFunc.Hash( - BigIntToBytes(bLen, big.NewInt(1)), - BigIntToBytes(bLen, big.NewInt(2))) + BigIntToBytesLE(bLen, big.NewInt(1)), + BigIntToBytesLE(bLen, big.NewInt(2))) if err != nil { t.Fatal(err) } - hBI := BytesToBigInt(h) + hBI := BytesLEToBigInt(h) // value checked with circomlib c := qt.New(t) c.Assert(hBI.String(), diff --git a/tree/arbo/testvectors/circom/go-data-generator/generator_test.go b/tree/arbo/testvectors/circom/go-data-generator/generator_test.go index 6b681d9e1..13562ed20 100644 --- a/tree/arbo/testvectors/circom/go-data-generator/generator_test.go +++ b/tree/arbo/testvectors/circom/go-data-generator/generator_test.go @@ -28,15 +28,15 @@ func TestGenerator(t *testing.T) { } bLen := 1 for i := 0; i < len(testVector); i++ { - k := arbo.BigIntToBytes(bLen, big.NewInt(testVector[i][0])) - v := arbo.BigIntToBytes(bLen, big.NewInt(testVector[i][1])) + k := arbo.BigIntToBytesLE(bLen, big.NewInt(testVector[i][0])) + v := arbo.BigIntToBytesLE(bLen, big.NewInt(testVector[i][1])) if err := tree.Add(k, v); err != nil { t.Fatal(err) } } // proof of existence - k := arbo.BigIntToBytes(bLen, big.NewInt(int64(2))) + k := arbo.BigIntToBytesLE(bLen, big.NewInt(int64(2))) cvp, err := tree.GenerateCircomVerifierProof(k) c.Assert(err, qt.IsNil) jCvp, err := json.Marshal(cvp) @@ -46,7 +46,7 @@ func TestGenerator(t *testing.T) { c.Assert(err, qt.IsNil) // proof of non-existence - k = arbo.BigIntToBytes(bLen, big.NewInt(int64(5))) + k = arbo.BigIntToBytesLE(bLen, big.NewInt(int64(5))) cvp, err = tree.GenerateCircomVerifierProof(k) c.Assert(err, qt.IsNil) jCvp, err = json.Marshal(cvp) diff --git a/tree/arbo/tree_test.go b/tree/arbo/tree_test.go index 9ff0e9679..7980e1a93 100644 --- a/tree/arbo/tree_test.go +++ b/tree/arbo/tree_test.go @@ -21,7 +21,7 @@ import ( func checkRootBIString(c *qt.C, tree *Tree, expected string) { root, err := tree.Root() c.Assert(err, qt.IsNil) - rootBI := BytesToBigInt(root) + rootBI := BytesLEToBigInt(root) c.Check(rootBI.String(), qt.Equals, expected) } @@ -86,20 +86,20 @@ func testAdd(c *qt.C, hashFunc HashFunction, testVectors []string) { bLen := 32 err = tree.Add( - BigIntToBytes(bLen, big.NewInt(1)), - BigIntToBytes(bLen, big.NewInt(2))) + BigIntToBytesLE(bLen, big.NewInt(1)), + BigIntToBytesLE(bLen, big.NewInt(2))) c.Assert(err, qt.IsNil) checkRootBIString(c, tree, testVectors[1]) err = tree.Add( - BigIntToBytes(bLen, big.NewInt(33)), - BigIntToBytes(bLen, big.NewInt(44))) + BigIntToBytesLE(bLen, big.NewInt(33)), + BigIntToBytesLE(bLen, big.NewInt(44))) c.Assert(err, qt.IsNil) checkRootBIString(c, tree, testVectors[2]) err = tree.Add( - BigIntToBytes(bLen, big.NewInt(1234)), - BigIntToBytes(bLen, big.NewInt(9876))) + BigIntToBytesLE(bLen, big.NewInt(1234)), + BigIntToBytesLE(bLen, big.NewInt(9876))) c.Assert(err, qt.IsNil) checkRootBIString(c, tree, testVectors[3]) } @@ -112,8 +112,8 @@ func TestAddBatch(t *testing.T) { bLen := 32 for i := 0; i < 1000; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(0)) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(0)) if err := tree.Add(k, v); err != nil { t.Fatal(err) } @@ -128,8 +128,8 @@ func TestAddBatch(t *testing.T) { var keys, values [][]byte for i := 0; i < 1000; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(0)) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(0)) keys = append(keys, k) values = append(values, v) } @@ -149,8 +149,8 @@ func TestAddDifferentOrder(t *testing.T) { bLen := 32 for i := 0; i < 16; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(0)) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(0)) if err := tree1.Add(k, v); err != nil { t.Fatal(err) } @@ -161,8 +161,8 @@ func TestAddDifferentOrder(t *testing.T) { c.Assert(err, qt.IsNil) for i := 16 - 1; i >= 0; i-- { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(0)) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(0)) if err := tree2.Add(k, v); err != nil { t.Fatal(err) } @@ -184,8 +184,8 @@ func TestAddRepeatedIndex(t *testing.T) { c.Assert(err, qt.IsNil) bLen := 32 - k := BigIntToBytes(bLen, big.NewInt(int64(3))) - v := BigIntToBytes(bLen, big.NewInt(int64(12))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(3))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(12))) err = tree.Add(k, v) c.Assert(err, qt.IsNil) @@ -200,13 +200,13 @@ func TestUpdate(t *testing.T) { c.Assert(err, qt.IsNil) bLen := 32 - k := BigIntToBytes(bLen, big.NewInt(int64(20))) - v := BigIntToBytes(bLen, big.NewInt(int64(12))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(20))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(12))) if err := tree.Add(k, v); err != nil { t.Fatal(err) } - v = BigIntToBytes(bLen, big.NewInt(int64(11))) + v = BigIntToBytesLE(bLen, big.NewInt(int64(11))) err = tree.Update(k, v) c.Assert(err, qt.IsNil) @@ -217,21 +217,21 @@ func TestUpdate(t *testing.T) { // add more leafs to the tree to do another test for i := 0; i < 16; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) if err := tree.Add(k, v); err != nil { t.Fatal(err) } } - k = BigIntToBytes(bLen, big.NewInt(int64(3))) - v = BigIntToBytes(bLen, big.NewInt(int64(11))) + k = BigIntToBytesLE(bLen, big.NewInt(int64(3))) + v = BigIntToBytesLE(bLen, big.NewInt(int64(11))) // check that before the Update, value for 3 is !=11 gettedKey, gettedValue, err = tree.Get(k) c.Assert(err, qt.IsNil) c.Check(gettedKey, qt.DeepEquals, k) c.Check(gettedValue, qt.Not(qt.DeepEquals), v) - c.Check(gettedValue, qt.DeepEquals, BigIntToBytes(bLen, big.NewInt(6))) + c.Check(gettedValue, qt.DeepEquals, BigIntToBytesLE(bLen, big.NewInt(6))) err = tree.Update(k, v) c.Assert(err, qt.IsNil) @@ -241,7 +241,7 @@ func TestUpdate(t *testing.T) { c.Assert(err, qt.IsNil) c.Check(gettedKey, qt.DeepEquals, k) c.Check(gettedValue, qt.DeepEquals, v) - c.Check(gettedValue, qt.DeepEquals, BigIntToBytes(bLen, big.NewInt(11))) + c.Check(gettedValue, qt.DeepEquals, BigIntToBytesLE(bLen, big.NewInt(11))) } func TestRootOnTx(t *testing.T) { @@ -251,8 +251,8 @@ func TestRootOnTx(t *testing.T) { c.Assert(err, qt.IsNil) bLen := 32 - k := BigIntToBytes(bLen, big.NewInt(int64(20))) - v := BigIntToBytes(bLen, big.NewInt(int64(12))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(20))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(12))) if err := tree.Add(k, v); err != nil { t.Fatal(err) } @@ -263,7 +263,7 @@ func TestRootOnTx(t *testing.T) { tx := database.WriteTx() // Update the value of the key - v = BigIntToBytes(bLen, big.NewInt(int64(11))) + v = BigIntToBytesLE(bLen, big.NewInt(int64(11))) err = tree.UpdateWithTx(tx, k, v) c.Assert(err, qt.IsNil) @@ -286,8 +286,8 @@ func TestRootOnTx(t *testing.T) { c.Assert(rootAfter, qt.DeepEquals, newRoot) // Add a new key-value pair - k2 := BigIntToBytes(bLen, big.NewInt(int64(30))) - v2 := BigIntToBytes(bLen, big.NewInt(int64(40))) + k2 := BigIntToBytesLE(bLen, big.NewInt(int64(30))) + v2 := BigIntToBytesLE(bLen, big.NewInt(int64(40))) if err := tree.AddWithTx(tx, k2, v2); err != nil { t.Fatal(err) } @@ -340,29 +340,29 @@ func TestAux(t *testing.T) { // TODO split in proper tests c.Assert(err, qt.IsNil) bLen := 32 - k := BigIntToBytes(bLen, big.NewInt(int64(1))) - v := BigIntToBytes(bLen, big.NewInt(int64(0))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(1))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(0))) err = tree.Add(k, v) c.Assert(err, qt.IsNil) - k = BigIntToBytes(bLen, big.NewInt(int64(256))) + k = BigIntToBytesLE(bLen, big.NewInt(int64(256))) err = tree.Add(k, v) c.Assert(err, qt.IsNil) - k = BigIntToBytes(bLen, big.NewInt(int64(257))) + k = BigIntToBytesLE(bLen, big.NewInt(int64(257))) err = tree.Add(k, v) c.Assert(err, qt.IsNil) - k = BigIntToBytes(bLen, big.NewInt(int64(515))) + k = BigIntToBytesLE(bLen, big.NewInt(int64(515))) err = tree.Add(k, v) c.Assert(err, qt.IsNil) - k = BigIntToBytes(bLen, big.NewInt(int64(770))) + k = BigIntToBytesLE(bLen, big.NewInt(int64(770))) err = tree.Add(k, v) c.Assert(err, qt.IsNil) - k = BigIntToBytes(bLen, big.NewInt(int64(388))) + k = BigIntToBytesLE(bLen, big.NewInt(int64(388))) err = tree.Add(k, v) c.Assert(err, qt.IsNil) - k = BigIntToBytes(bLen, big.NewInt(int64(900))) + k = BigIntToBytesLE(bLen, big.NewInt(int64(900))) err = tree.Add(k, v) c.Assert(err, qt.IsNil) // @@ -381,18 +381,18 @@ func TestGet(t *testing.T) { bLen := 32 for i := 0; i < 10; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) if err := tree.Add(k, v); err != nil { t.Fatal(err) } } - k := BigIntToBytes(bLen, big.NewInt(int64(7))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(7))) gettedKey, gettedValue, err := tree.Get(k) c.Assert(err, qt.IsNil) c.Check(gettedKey, qt.DeepEquals, k) - c.Check(gettedValue, qt.DeepEquals, BigIntToBytes(bLen, big.NewInt(int64(7*2)))) + c.Check(gettedValue, qt.DeepEquals, BigIntToBytesLE(bLen, big.NewInt(int64(7*2)))) } func TestBitmapBytes(t *testing.T) { @@ -522,15 +522,15 @@ func TestGenProofAndVerify(t *testing.T) { bLen := 32 for i := 0; i < 10; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) if err := tree.Add(k, v); err != nil { t.Fatal(err) } } - k := BigIntToBytes(bLen, big.NewInt(int64(7))) - v := BigIntToBytes(bLen, big.NewInt(int64(14))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(7))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(14))) kAux, proofV, siblings, existence, err := tree.GenProof(k) c.Assert(err, qt.IsNil) c.Assert(proofV, qt.DeepEquals, v) @@ -563,8 +563,8 @@ func testDumpAndImportDump(t *testing.T, inFile bool) { bLen := 32 for i := 0; i < 16; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i*2))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i*2))) if err := tree1.Add(k, v); err != nil { t.Fatal(err) } @@ -621,8 +621,8 @@ func TestRWMutex(t *testing.T) { bLen := 32 var keys, values [][]byte for i := 0; i < 1000; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(0)) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(0)) keys = append(keys, k) values = append(values, v) } @@ -634,8 +634,8 @@ func TestRWMutex(t *testing.T) { }() time.Sleep(500 * time.Millisecond) - k := BigIntToBytes(bLen, big.NewInt(int64(99999))) - v := BigIntToBytes(bLen, big.NewInt(int64(99999))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(99999))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(99999))) if err := tree.Add(k, v); err != nil { t.Fatal(err) } @@ -660,7 +660,7 @@ func TestAddBatchFullyUsed(t *testing.T) { var keys, values [][]byte for i := 0; i < 16; i++ { - k := BigIntToBytes(1, big.NewInt(int64(i))) + k := BigIntToBytesLE(1, big.NewInt(int64(i))) v := k keys = append(keys, k) @@ -683,10 +683,10 @@ func TestAddBatchFullyUsed(t *testing.T) { // get all key-values and check that are equal between both trees for i := 0; i < 16; i++ { - auxK1, auxV1, err := tree1.Get(BigIntToBytes(1, big.NewInt(int64(i)))) + auxK1, auxV1, err := tree1.Get(BigIntToBytesLE(1, big.NewInt(int64(i)))) c.Assert(err, qt.IsNil) - auxK2, auxV2, err := tree2.Get(BigIntToBytes(1, big.NewInt(int64(i)))) + auxK2, auxV2, err := tree2.Get(BigIntToBytesLE(1, big.NewInt(int64(i)))) c.Assert(err, qt.IsNil) c.Assert(auxK1, qt.DeepEquals, auxK2) @@ -695,7 +695,7 @@ func TestAddBatchFullyUsed(t *testing.T) { // try adding one more key to both trees (through Add & AddBatch) and // expect not being added due the tree is already full - k := BigIntToBytes(1, big.NewInt(int64(16))) + k := BigIntToBytesLE(1, big.NewInt(int64(16))) v := k err = tree1.Add(k, v) c.Assert(err, qt.Equals, ErrMaxVirtualLevel) @@ -720,8 +720,8 @@ func TestSetRoot(t *testing.T) { bLen := 32 var keys, values [][]byte for i := 0; i < 1000; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i))) keys = append(keys, k) values = append(values, v) } @@ -733,8 +733,8 @@ func TestSetRoot(t *testing.T) { expectedRoot) // add one more k-v - k := BigIntToBytes(bLen, big.NewInt(1000)) - v := BigIntToBytes(bLen, big.NewInt(1000)) + k := BigIntToBytesLE(bLen, big.NewInt(1000)) + v := BigIntToBytesLE(bLen, big.NewInt(1000)) err = tree.Add(k, v) c.Assert(err, qt.IsNil) checkRootBIString(c, tree, @@ -751,9 +751,9 @@ func TestSetRoot(t *testing.T) { c.Assert(string(root), qt.Equals, string(roots[0])) // check that the tree can be updated - err = tree.Add(BigIntToBytes(bLen, big.NewInt(int64(1024))), []byte("test")) + err = tree.Add(BigIntToBytesLE(bLen, big.NewInt(int64(1024))), []byte("test")) c.Assert(err, qt.IsNil) - err = tree.Update(BigIntToBytes(bLen, big.NewInt(int64(1024))), []byte("test2")) + err = tree.Update(BigIntToBytesLE(bLen, big.NewInt(int64(1024))), []byte("test2")) c.Assert(err, qt.IsNil) // check that the k-v '1000' does not exist in the new tree @@ -778,8 +778,8 @@ func TestSnapshot(t *testing.T) { bLen := 32 var keys, values [][]byte for i := 0; i < 1000; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i))) keys = append(keys, k) values = append(values, v) } @@ -827,7 +827,7 @@ func TestGetFromSnapshotExpectArboErrKeyNotFound(t *testing.T) { c.Assert(err, qt.IsNil) bLen := 32 - k := BigIntToBytes(bLen, big.NewInt(int64(3))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(3))) root, err := tree.Root() c.Assert(err, qt.IsNil) @@ -852,8 +852,8 @@ func TestKeyLen(t *testing.T) { // expect no errors when adding a key of only 4 bytes (when the // required length of keyPath for 100 levels would be 13 bytes) bLen := 4 - k := BigIntToBytes(bLen, big.NewInt(1)) - v := BigIntToBytes(bLen, big.NewInt(1)) + k := BigIntToBytesLE(bLen, big.NewInt(1)) + v := BigIntToBytesLE(bLen, big.NewInt(1)) err = tree.Add(k, v) c.Assert(err, qt.IsNil) @@ -867,8 +867,8 @@ func TestKeyLen(t *testing.T) { _, _, err = tree.Get(k) c.Assert(err, qt.IsNil) - k = BigIntToBytes(bLen, big.NewInt(2)) - v = BigIntToBytes(bLen, big.NewInt(2)) + k = BigIntToBytesLE(bLen, big.NewInt(2)) + v = BigIntToBytesLE(bLen, big.NewInt(2)) invalids, err := tree.AddBatch([][]byte{k}, [][]byte{v}) c.Assert(err, qt.IsNil) c.Assert(len(invalids), qt.Equals, 0) @@ -884,8 +884,8 @@ func TestKeyLen(t *testing.T) { c.Assert(err, qt.IsNil) maxKeyLen := int(math.Ceil(float64(maxLevels) / float64(8))) - k = BigIntToBytes(maxKeyLen+1, big.NewInt(1)) - v = BigIntToBytes(maxKeyLen+1, big.NewInt(1)) + k = BigIntToBytesLE(maxKeyLen+1, big.NewInt(1)) + v = BigIntToBytesLE(maxKeyLen+1, big.NewInt(1)) expectedErrMsg := "len(k) can not be bigger than ceil(maxLevels/8)," + " where len(k): 5, maxLevels: 32, max key len=ceil(maxLevels/8): 4." + @@ -914,15 +914,15 @@ func TestKeyLen(t *testing.T) { nKVs := nCPU + 1 var ks, vs [][]byte for i := 0; i < nKVs; i++ { - ks = append(ks, BigIntToBytes(maxKeyLen+1, big.NewInt(1))) - vs = append(vs, BigIntToBytes(maxKeyLen+1, big.NewInt(1))) + ks = append(ks, BigIntToBytesLE(maxKeyLen+1, big.NewInt(1))) + vs = append(vs, BigIntToBytesLE(maxKeyLen+1, big.NewInt(1))) } invalids, err = tree.AddBatch(ks, vs) c.Assert(err, qt.IsNil) c.Assert(len(invalids), qt.Equals, nKVs) // check that with maxKeyLen it can be added - k = BigIntToBytes(maxKeyLen, big.NewInt(1)) + k = BigIntToBytesLE(maxKeyLen, big.NewInt(1)) err = tree.Add(k, v) c.Assert(err, qt.IsNil) @@ -983,16 +983,16 @@ func TestDelete(t *testing.T) { bLen := 32 // Add multiple key/value pairs to the tree keys := [][]byte{ - BigIntToBytes(bLen, big.NewInt(int64(1))), - BigIntToBytes(bLen, big.NewInt(int64(2))), - BigIntToBytes(bLen, big.NewInt(int64(3))), - BigIntToBytes(bLen, big.NewInt(int64(4))), + BigIntToBytesLE(bLen, big.NewInt(int64(1))), + BigIntToBytesLE(bLen, big.NewInt(int64(2))), + BigIntToBytesLE(bLen, big.NewInt(int64(3))), + BigIntToBytesLE(bLen, big.NewInt(int64(4))), } values := [][]byte{ - BigIntToBytes(bLen, big.NewInt(int64(10))), - BigIntToBytes(bLen, big.NewInt(int64(20))), - BigIntToBytes(bLen, big.NewInt(int64(30))), - BigIntToBytes(bLen, big.NewInt(int64(40))), + BigIntToBytesLE(bLen, big.NewInt(int64(10))), + BigIntToBytesLE(bLen, big.NewInt(int64(20))), + BigIntToBytesLE(bLen, big.NewInt(int64(30))), + BigIntToBytesLE(bLen, big.NewInt(int64(40))), } for i, key := range keys { err := tree.Add(key, values[i]) @@ -1038,8 +1038,8 @@ func BenchmarkAdd(b *testing.B) { // prepare inputs var ks, vs [][]byte for i := 0; i < 1000; i++ { - k := BigIntToBytes(bLen, big.NewInt(int64(i))) - v := BigIntToBytes(bLen, big.NewInt(int64(i))) + k := BigIntToBytesLE(bLen, big.NewInt(int64(i))) + v := BigIntToBytesLE(bLen, big.NewInt(int64(i))) ks = append(ks, k) vs = append(vs, v) } @@ -1216,20 +1216,20 @@ func TestGetLeavesFromSubPath(t *testing.T) { // Add keys with shared prefix and some without shared prefix keys := [][]byte{ // Shared prefix keys - append([]byte{0xFF, 0xFF}, BigIntToBytes(bLen-2, big.NewInt(int64(1)))...), - append([]byte{0xFF, 0xFF}, BigIntToBytes(bLen-2, big.NewInt(int64(2)))...), - append([]byte{0xFF, 0xFF}, BigIntToBytes(bLen-2, big.NewInt(int64(3)))...), + append([]byte{0xFF, 0xFF}, BigIntToBytesLE(bLen-2, big.NewInt(int64(1)))...), + append([]byte{0xFF, 0xFF}, BigIntToBytesLE(bLen-2, big.NewInt(int64(2)))...), + append([]byte{0xFF, 0xFF}, BigIntToBytesLE(bLen-2, big.NewInt(int64(3)))...), // Non-shared prefix keys - BigIntToBytes(bLen, big.NewInt(int64(4))), - BigIntToBytes(bLen, big.NewInt(int64(5))), + BigIntToBytesLE(bLen, big.NewInt(int64(4))), + BigIntToBytesLE(bLen, big.NewInt(int64(5))), } - sharedPrefixKeyNotAdded := append([]byte{0xFF, 0xFF}, BigIntToBytes(bLen-2, big.NewInt(int64(4)))...) + sharedPrefixKeyNotAdded := append([]byte{0xFF, 0xFF}, BigIntToBytesLE(bLen-2, big.NewInt(int64(4)))...) values := [][]byte{ - BigIntToBytes(bLen, big.NewInt(int64(10))), - BigIntToBytes(bLen, big.NewInt(int64(20))), - BigIntToBytes(bLen, big.NewInt(int64(30))), - BigIntToBytes(bLen, big.NewInt(int64(40))), - BigIntToBytes(bLen, big.NewInt(int64(50))), + BigIntToBytesLE(bLen, big.NewInt(int64(10))), + BigIntToBytesLE(bLen, big.NewInt(int64(20))), + BigIntToBytesLE(bLen, big.NewInt(int64(30))), + BigIntToBytesLE(bLen, big.NewInt(int64(40))), + BigIntToBytesLE(bLen, big.NewInt(int64(50))), } for i, key := range keys { err := tree.Add(key, values[i]) @@ -1487,7 +1487,7 @@ func generateKV(bLen, count, offset int) ([][]byte, [][]byte) { for i := 0; i < count; i++ { // Convert integer i to a byte slice - key := BigIntToBytes(bLen, big.NewInt(int64(offset+i))) + key := BigIntToBytesLE(bLen, big.NewInt(int64(offset+i))) value := randomBytes(bLen) keys = append(keys, key) diff --git a/tree/arbo/utils.go b/tree/arbo/utils.go index c183ff315..7d6aed2a6 100644 --- a/tree/arbo/utils.go +++ b/tree/arbo/utils.go @@ -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 diff --git a/tree/arbo/vt_test.go b/tree/arbo/vt_test.go index 705796b15..8ad4ee05c 100644 --- a/tree/arbo/vt_test.go +++ b/tree/arbo/vt_test.go @@ -51,16 +51,16 @@ func TestVirtualTreeTestVectors(t *testing.T) { maxLevels := 32 keyLen := int(math.Ceil(float64(maxLevels) / float64(8))) keys := [][]byte{ - BigIntToBytes(keyLen, big.NewInt(1)), - BigIntToBytes(keyLen, big.NewInt(33)), - BigIntToBytes(keyLen, big.NewInt(1234)), - BigIntToBytes(keyLen, big.NewInt(123456789)), + BigIntToBytesLE(keyLen, big.NewInt(1)), + BigIntToBytesLE(keyLen, big.NewInt(33)), + BigIntToBytesLE(keyLen, big.NewInt(1234)), + BigIntToBytesLE(keyLen, big.NewInt(123456789)), } values := [][]byte{ - BigIntToBytes(keyLen, big.NewInt(2)), - BigIntToBytes(keyLen, big.NewInt(44)), - BigIntToBytes(keyLen, big.NewInt(9876)), - BigIntToBytes(keyLen, big.NewInt(987654321)), + BigIntToBytesLE(keyLen, big.NewInt(2)), + BigIntToBytesLE(keyLen, big.NewInt(44)), + BigIntToBytesLE(keyLen, big.NewInt(9876)), + BigIntToBytesLE(keyLen, big.NewInt(987654321)), } // check the root for different batches of leafs @@ -159,7 +159,7 @@ func TestVirtualTreeAddBatchFullyUsed(t *testing.T) { var keys, values [][]byte for i := 0; i < 128; i++ { - k := BigIntToBytes(1, big.NewInt(int64(i))) + k := BigIntToBytesLE(1, big.NewInt(int64(i))) v := k keys = append(keys, k) diff --git a/vochain/transaction/proofs/arboproof/arboproof.go b/vochain/transaction/proofs/arboproof/arboproof.go index 7301c6c21..f9e933d4a 100644 --- a/vochain/transaction/proofs/arboproof/arboproof.go +++ b/vochain/transaction/proofs/arboproof/arboproof.go @@ -61,12 +61,12 @@ func (*ProofVerifierArbo) Verify(process *models.Process, envelope *models.VoteE return true, bigOne, nil } - availableWeight := arbo.BytesToBigInt(p.AvailableWeight) + availableWeight := arbo.BytesLEToBigInt(p.AvailableWeight) if p.VoteWeight == nil { return true, availableWeight, nil } - voteWeight := arbo.BytesToBigInt(p.VoteWeight) + voteWeight := arbo.BytesLEToBigInt(p.VoteWeight) if voteWeight.Cmp(availableWeight) == 1 { return false, nil, fmt.Errorf("assigned weight exceeded") } diff --git a/vochain/transaction_zk_test.go b/vochain/transaction_zk_test.go index 73d3839ab..89422d271 100644 --- a/vochain/transaction_zk_test.go +++ b/vochain/transaction_zk_test.go @@ -187,7 +187,7 @@ func TestMaxCensusSizeRegisterSIKTx(t *testing.T) { c.Assert(app.State.AddProcess(process), qt.IsNil) app.AdvanceTestBlock() - encWeight := arbo.BigIntToBytes(arbo.HashFunctionPoseidon.Len(), testWeight) + encWeight := arbo.BigIntToBytesLE(arbo.HashFunctionPoseidon.Len(), testWeight) tx := testBuildSignedRegisterSIKTx(t, accounts[0], pid, proofs[0], encWeight, app.chainID) _, _, _, _, err := app.TransactionHandler.RegisterSIKTxCheck(tx) c.Assert(err, qt.IsNil) diff --git a/vochain/vote_test.go b/vochain/vote_test.go index 0af7ba480..cf04fcec9 100644 --- a/vochain/vote_test.go +++ b/vochain/vote_test.go @@ -31,7 +31,7 @@ func testCreateKeysAndBuildWeightedZkCensus(t *testing.T, size int, weight *big. t.Fatal(err) } - encWeight := arbo.BigIntToBytes(arbo.HashFunctionPoseidon.Len(), weight) + encWeight := arbo.BigIntToBytesLE(arbo.HashFunctionPoseidon.Len(), weight) keys := ethereum.NewSignKeysBatch(size) for _, k := range keys { qt.Check(t, err, qt.IsNil)