You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.1 KiB

crypto: Proof of Concept for iterative version of SimpleHashFromByteSlices (#2611) (#3530) (#2611) had suggested that an iterative version of SimpleHashFromByteSlice would be faster, presumably because we can envision some overhead accumulating from stack frames and function calls. Additionally, a recursive algorithm risks hitting the stack limit and causing a stack overflow should the tree be too large. Provided here is an iterative alternative, a simple test to assert correctness and a benchmark. On the performance side, there appears to be no overall difference: ``` BenchmarkSimpleHashAlternatives/recursive-4 20000 77677 ns/op BenchmarkSimpleHashAlternatives/iterative-4 20000 76802 ns/op ``` On the surface it might seem that the additional overhead is due to the different allocation patterns of the implementations. The recursive version uses a single `[][]byte` slices which it then re-slices at each level of the tree. The iterative version reproduces `[][]byte` once within the function and then rewrites sub-slices of that array at each level of the tree. Eexperimenting by modifying the code to simply calculate the hash and not store the result show little to no difference in performance. These preliminary results suggest: 1. The performance of the current implementation is pretty good 2. Go has low overhead for recursive functions 3. The performance of the SimpleHashFromByteSlice routine is dominated by the actual hashing of data Although this work is in no way exhaustive, point #3 suggests that optimizations of this routine would need to take an alternative approach to make significant improvements on the current performance. Finally, considering that the recursive implementation is easier to read, it might not be worthwhile to switch to a less intuitive implementation for so little benefit. * re-add slice re-writing * [crypto] Document SimpleHashFromByteSlicesIterative
6 years ago
  1. package merkle
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. cmn "github.com/tendermint/tendermint/libs/common"
  6. . "github.com/tendermint/tendermint/libs/test"
  7. "github.com/tendermint/tendermint/crypto/tmhash"
  8. )
  9. type testItem []byte
  10. func (tI testItem) Hash() []byte {
  11. return []byte(tI)
  12. }
  13. func TestSimpleProof(t *testing.T) {
  14. total := 100
  15. items := make([][]byte, total)
  16. for i := 0; i < total; i++ {
  17. items[i] = testItem(cmn.RandBytes(tmhash.Size))
  18. }
  19. rootHash := SimpleHashFromByteSlices(items)
  20. rootHash2, proofs := SimpleProofsFromByteSlices(items)
  21. require.Equal(t, rootHash, rootHash2, "Unmatched root hashes: %X vs %X", rootHash, rootHash2)
  22. // For each item, check the trail.
  23. for i, item := range items {
  24. proof := proofs[i]
  25. // Check total/index
  26. require.Equal(t, proof.Index, i, "Unmatched indicies: %d vs %d", proof.Index, i)
  27. require.Equal(t, proof.Total, total, "Unmatched totals: %d vs %d", proof.Total, total)
  28. // Verify success
  29. err := proof.Verify(rootHash, item)
  30. require.NoError(t, err, "Verification failed: %v.", err)
  31. // Trail too long should make it fail
  32. origAunts := proof.Aunts
  33. proof.Aunts = append(proof.Aunts, cmn.RandBytes(32))
  34. err = proof.Verify(rootHash, item)
  35. require.Error(t, err, "Expected verification to fail for wrong trail length")
  36. proof.Aunts = origAunts
  37. // Trail too short should make it fail
  38. proof.Aunts = proof.Aunts[0 : len(proof.Aunts)-1]
  39. err = proof.Verify(rootHash, item)
  40. require.Error(t, err, "Expected verification to fail for wrong trail length")
  41. proof.Aunts = origAunts
  42. // Mutating the itemHash should make it fail.
  43. err = proof.Verify(rootHash, MutateByteSlice(item))
  44. require.Error(t, err, "Expected verification to fail for mutated leaf hash")
  45. // Mutating the rootHash should make it fail.
  46. err = proof.Verify(MutateByteSlice(rootHash), item)
  47. require.Error(t, err, "Expected verification to fail for mutated root hash")
  48. }
  49. }
  50. func TestSimpleHashAlternatives(t *testing.T) {
  51. total := 100
  52. items := make([][]byte, total)
  53. for i := 0; i < total; i++ {
  54. items[i] = testItem(cmn.RandBytes(tmhash.Size))
  55. }
  56. rootHash1 := SimpleHashFromByteSlicesIterative(items)
  57. rootHash2 := SimpleHashFromByteSlices(items)
  58. require.Equal(t, rootHash1, rootHash2, "Unmatched root hashes: %X vs %X", rootHash1, rootHash2)
  59. }
  60. func BenchmarkSimpleHashAlternatives(b *testing.B) {
  61. total := 100
  62. items := make([][]byte, total)
  63. for i := 0; i < total; i++ {
  64. items[i] = testItem(cmn.RandBytes(tmhash.Size))
  65. }
  66. b.ResetTimer()
  67. b.Run("recursive", func(b *testing.B) {
  68. for i := 0; i < b.N; i++ {
  69. _ = SimpleHashFromByteSlices(items)
  70. }
  71. })
  72. b.Run("iterative", func(b *testing.B) {
  73. for i := 0; i < b.N; i++ {
  74. _ = SimpleHashFromByteSlicesIterative(items)
  75. }
  76. })
  77. }
  78. func Test_getSplitPoint(t *testing.T) {
  79. tests := []struct {
  80. length int
  81. want int
  82. }{
  83. {1, 0},
  84. {2, 1},
  85. {3, 2},
  86. {4, 2},
  87. {5, 4},
  88. {10, 8},
  89. {20, 16},
  90. {100, 64},
  91. {255, 128},
  92. {256, 128},
  93. {257, 256},
  94. }
  95. for _, tt := range tests {
  96. got := getSplitPoint(tt.length)
  97. require.Equal(t, tt.want, got, "getSplitPoint(%d) = %v, want %v", tt.length, got, tt.want)
  98. }
  99. }