From 49147e170a36738075f731c0cc03b1dc2a4419b0 Mon Sep 17 00:00:00 2001 From: h5law <53987565+h5law@users.noreply.github.com> Date: Mon, 1 Jan 2024 00:40:49 +0000 Subject: [PATCH] chore: replace BigEndian encoding with LittleEndian in the SMST --- benchmarks/bench_utils_test.go | 8 ++++---- benchmarks/proof_sizes_test.go | 8 ++++---- docs/MerkleSumTrie.md | 2 +- fuzz_test.go | 2 +- hasher.go | 6 +++--- proofs.go | 4 ++-- smst.go | 6 +++--- smst_proofs_test.go | 8 ++++---- smst_test.go | 2 +- smst_utils_test.go | 4 ++-- utils.go | 4 ++-- 11 files changed, 27 insertions(+), 27 deletions(-) diff --git a/benchmarks/bench_utils_test.go b/benchmarks/bench_utils_test.go index 5a360aa..0083ffa 100644 --- a/benchmarks/bench_utils_test.go +++ b/benchmarks/bench_utils_test.go @@ -30,24 +30,24 @@ var ( updSMST = func(s *smt.SMST, i uint64) error { b := make([]byte, 8) - binary.BigEndian.PutUint64(b, i) + binary.LittleEndian.PutUint64(b, i) return s.Update(b, b, i) } getSMST = func(s *smt.SMST, i uint64) error { b := make([]byte, 8) - binary.BigEndian.PutUint64(b, i) + binary.LittleEndian.PutUint64(b, i) _, _, err := s.Get(b) return err } proSMST = func(s *smt.SMST, i uint64) error { b := make([]byte, 8) - binary.BigEndian.PutUint64(b, i) + binary.LittleEndian.PutUint64(b, i) _, err := s.Prove(b) return err } delSMST = func(s *smt.SMST, i uint64) error { b := make([]byte, 8) - binary.BigEndian.PutUint64(b, i) + binary.LittleEndian.PutUint64(b, i) return s.Delete(b) } ) diff --git a/benchmarks/proof_sizes_test.go b/benchmarks/proof_sizes_test.go index e020fad..d103abe 100644 --- a/benchmarks/proof_sizes_test.go +++ b/benchmarks/proof_sizes_test.go @@ -43,7 +43,7 @@ func TestSMT_ProofSizes(t *testing.T) { t.Run(tc.name, func(t *testing.T) { for i := 0; i < tc.trieSize; i++ { b := make([]byte, 8) - binary.BigEndian.PutUint64(b, uint64(i)) + binary.LittleEndian.PutUint64(b, uint64(i)) require.NoError(t, trie.Update(b, b)) } require.NoError(t, trie.Commit()) @@ -55,7 +55,7 @@ func TestSMT_ProofSizes(t *testing.T) { minCompact := uint64(0) for i := 0; i < tc.trieSize; i++ { b := make([]byte, 8) - binary.BigEndian.PutUint64(b, uint64(i)) + binary.LittleEndian.PutUint64(b, uint64(i)) proof, err := trie.Prove(b) require.NoError(t, err) require.NotNil(t, proof) @@ -132,7 +132,7 @@ func TestSMST_ProofSizes(t *testing.T) { t.Run(tc.name, func(t *testing.T) { for i := 0; i < tc.trieSize; i++ { b := make([]byte, 8) - binary.BigEndian.PutUint64(b, uint64(i)) + binary.LittleEndian.PutUint64(b, uint64(i)) require.NoError(t, trie.Update(b, b, uint64(i))) } require.NoError(t, trie.Commit()) @@ -144,7 +144,7 @@ func TestSMST_ProofSizes(t *testing.T) { minCompact := uint64(0) for i := 0; i < tc.trieSize; i++ { b := make([]byte, 8) - binary.BigEndian.PutUint64(b, uint64(i)) + binary.LittleEndian.PutUint64(b, uint64(i)) proof, err := trie.Prove(b) require.NoError(t, err) require.NotNil(t, proof) diff --git a/docs/MerkleSumTrie.md b/docs/MerkleSumTrie.md index a72017c..cecf46e 100644 --- a/docs/MerkleSumTrie.md +++ b/docs/MerkleSumTrie.md @@ -61,7 +61,7 @@ The majority of the code relating to the SMST can be found in: The sum for any node is encoded in a byte array with a fixed size (`[8]byte`) this allows for the sum to fully represent a `uint64` value in binary form. The golang `encoding/binary` package is used to encode the sum with -`binary.BigEndian.PutUint64(sumBz[:], sum)` into a byte array `sumBz`. +`binary.LittleEndian.PutUint64(sumBz[:], sum)` into a byte array `sumBz`. In order for the SMST to include the sum into a leaf node the SMT the SMST initialises the SMT with the `WithValueHasher(nil)` option so that the SMT does diff --git a/fuzz_test.go b/fuzz_test.go index 98ce5dc..91a2233 100644 --- a/fuzz_test.go +++ b/fuzz_test.go @@ -72,7 +72,7 @@ func FuzzSMT_DetectUnexpectedFailures(f *testing.F) { require.Equal(t, originalRoot, newRoot, "root changed while getting") case Update: value := make([]byte, 32) - binary.BigEndian.PutUint64(value, uint64(i)) + binary.LittleEndian.PutUint64(value, uint64(i)) err := trie.Update(key(), value) require.NoErrorf(t, err, "unknown error occured while updating") newRoot := trie.Root() diff --git a/hasher.go b/hasher.go index 1b9b2cd..08b3b2c 100644 --- a/hasher.go +++ b/hasher.go @@ -167,12 +167,12 @@ func encodeSumInner(leftData []byte, rightData []byte) []byte { leftSumBz := leftData[len(leftData)-sumSize:] rightSumBz := rightData[len(rightData)-sumSize:] if !bytes.Equal(leftSumBz, defaultSum[:]) { - leftSum = binary.BigEndian.Uint64(leftSumBz) + leftSum = binary.LittleEndian.Uint64(leftSumBz) } if !bytes.Equal(rightSumBz, defaultSum[:]) { - rightSum = binary.BigEndian.Uint64(rightSumBz) + rightSum = binary.LittleEndian.Uint64(rightSumBz) } - binary.BigEndian.PutUint64(sum[:], leftSum+rightSum) + binary.LittleEndian.PutUint64(sum[:], leftSum+rightSum) value = append(value, sum[:]...) return value } diff --git a/proofs.go b/proofs.go index 32983a9..d7e2f64 100644 --- a/proofs.go +++ b/proofs.go @@ -285,7 +285,7 @@ func VerifyProof(proof *SparseMerkleProof, root, key, value []byte, spec *TrieSp // VerifySumProof verifies a Merkle proof for a sum trie. func VerifySumProof(proof *SparseMerkleProof, root, key, value []byte, sum uint64, spec *TrieSpec) (bool, error) { var sumBz [sumSize]byte - binary.BigEndian.PutUint64(sumBz[:], sum) + binary.LittleEndian.PutUint64(sumBz[:], sum) valueHash := spec.digestValue(value) valueHash = append(valueHash, sumBz[:]...) if bytes.Equal(value, defaultValue) && sum == 0 { @@ -318,7 +318,7 @@ func VerifyClosestProof(proof *SparseMerkleClosestProof, root []byte, spec *Trie return VerifySumProof(proof.ClosestProof, root, proof.ClosestPath, nil, 0, spec) } sumBz := proof.ClosestValueHash[len(proof.ClosestValueHash)-sumSize:] - sum := binary.BigEndian.Uint64(sumBz) + sum := binary.LittleEndian.Uint64(sumBz) valueHash := proof.ClosestValueHash[:len(proof.ClosestValueHash)-sumSize] return VerifySumProof(proof.ClosestProof, root, proof.ClosestPath, valueHash, sum, spec) } diff --git a/smst.go b/smst.go index 9f571cc..0e90422 100644 --- a/smst.go +++ b/smst.go @@ -59,7 +59,7 @@ func (smst *SMST) Get(key []byte) ([]byte, uint64, error) { } var weightBz [sumSize]byte copy(weightBz[:], valueHash[len(valueHash)-sumSize:]) - weight := binary.BigEndian.Uint64(weightBz[:]) + weight := binary.LittleEndian.Uint64(weightBz[:]) return valueHash[:len(valueHash)-sumSize], weight, nil } @@ -69,7 +69,7 @@ func (smst *SMST) Get(key []byte) ([]byte, uint64, error) { func (smst *SMST) Update(key, value []byte, weight uint64) error { valueHash := smst.digestValue(value) var weightBz [sumSize]byte - binary.BigEndian.PutUint64(weightBz[:], weight) + binary.LittleEndian.PutUint64(weightBz[:], weight) valueHash = append(valueHash, weightBz[:]...) return smst.SMT.Update(key, valueHash) } @@ -109,5 +109,5 @@ func (smst *SMST) Sum() uint64 { var sumBz [sumSize]byte digest := smst.Root() copy(sumBz[:], digest[len(digest)-sumSize:]) - return binary.BigEndian.Uint64(sumBz[:]) + return binary.LittleEndian.Uint64(sumBz[:]) } diff --git a/smst_proofs_test.go b/smst_proofs_test.go index f113a0b..b9c7625 100644 --- a/smst_proofs_test.go +++ b/smst_proofs_test.go @@ -101,7 +101,7 @@ func TestSMST_Proof_Operations(t *testing.T) { // Try proving a default value for a non-default leaf. var sum [sumSize]byte - binary.BigEndian.PutUint64(sum[:], 5) + binary.LittleEndian.PutUint64(sum[:], 5) tval := base.digestValue([]byte("testValue")) tval = append(tval, sum[:]...) _, leafData := base.th.digestSumLeaf(base.ph.Path([]byte("testKey2")), tval) @@ -321,7 +321,7 @@ func TestSMST_ProveClosest(t *testing.T) { require.NotEqual(t, proof, &SparseMerkleClosestProof{}) closestPath := sha256.Sum256([]byte("testKey2")) closestValueHash := []byte("testValue2") - binary.BigEndian.PutUint64(sumBz[:], 24) + binary.LittleEndian.PutUint64(sumBz[:], 24) closestValueHash = append(closestValueHash, sumBz[:]...) require.Equal(t, proof, &SparseMerkleClosestProof{ Path: path[:], @@ -347,7 +347,7 @@ func TestSMST_ProveClosest(t *testing.T) { require.NotEqual(t, proof, &SparseMerkleClosestProof{}) closestPath = sha256.Sum256([]byte("testKey4")) closestValueHash = []byte("testValue4") - binary.BigEndian.PutUint64(sumBz[:], 30) + binary.LittleEndian.PutUint64(sumBz[:], 30) closestValueHash = append(closestValueHash, sumBz[:]...) require.Equal(t, proof, &SparseMerkleClosestProof{ Path: path2[:], @@ -418,7 +418,7 @@ func TestSMST_ProveClosest_OneNode(t *testing.T) { closestPath := sha256.Sum256([]byte("foo")) closestValueHash := []byte("bar") var sumBz [sumSize]byte - binary.BigEndian.PutUint64(sumBz[:], 5) + binary.LittleEndian.PutUint64(sumBz[:], 5) closestValueHash = append(closestValueHash, sumBz[:]...) require.Equal(t, proof, &SparseMerkleClosestProof{ Path: path[:], diff --git a/smst_test.go b/smst_test.go index ca40dfa..5246883 100644 --- a/smst_test.go +++ b/smst_test.go @@ -460,7 +460,7 @@ func TestSMST_TotalSum(t *testing.T) { // Check root hash contains the correct hex sum root1 := smst.Root() sumBz := root1[len(root1)-sumSize:] - rootSum := binary.BigEndian.Uint64(sumBz) + rootSum := binary.LittleEndian.Uint64(sumBz) require.NoError(t, err) // Calculate total sum of the trie diff --git a/smst_utils_test.go b/smst_utils_test.go index 9accaa1..ca37941 100644 --- a/smst_utils_test.go +++ b/smst_utils_test.go @@ -23,7 +23,7 @@ func (smst *SMSTWithStorage) Update(key, value []byte, sum uint64) error { } valueHash := smst.digestValue(value) var sumBz [sumSize]byte - binary.BigEndian.PutUint64(sumBz[:], sum) + binary.LittleEndian.PutUint64(sumBz[:], sum) value = append(value, sumBz[:]...) return smst.preimages.Set(valueHash, value) } @@ -54,7 +54,7 @@ func (smst *SMSTWithStorage) GetValueSum(key []byte) ([]byte, uint64, error) { } var sumBz [sumSize]byte copy(sumBz[:], value[len(value)-sumSize:]) - storedSum := binary.BigEndian.Uint64(sumBz[:]) + storedSum := binary.LittleEndian.Uint64(sumBz[:]) if storedSum != sum { return nil, 0, fmt.Errorf("sum mismatch for %s: got %d, expected %d", string(key), storedSum, sum) } diff --git a/utils.go b/utils.go index cc5bca8..ea4466f 100644 --- a/utils.go +++ b/utils.go @@ -103,7 +103,7 @@ func minBytes(i int) int { // intToBytes converts an int to a byte slice func intToBytes(i int) []byte { b := make([]byte, 8) - binary.BigEndian.PutUint64(b, uint64(i)) + binary.LittleEndian.PutUint64(b, uint64(i)) d := minBytes(i) return b[8-d:] } @@ -113,7 +113,7 @@ func bytesToInt(bz []byte) int { b := make([]byte, 8) // allocate space for a 64-bit unsigned integer d := 8 - len(bz) // determine how much padding is necessary copy(b[d:], bz) // copy over the non-zero bytes - u := binary.BigEndian.Uint64(b) + u := binary.LittleEndian.Uint64(b) return int(u) }