Skip to content

Commit 8c6df36

Browse files
colin-axnerdamiannolanalexanderbez
authored
fix: change prehash value back to sha256 from noop (#19002)
Co-authored-by: Damian Nolan <[email protected]> Co-authored-by: Aleksandr Bezobchuk <[email protected]>
1 parent 5e6b988 commit 8c6df36

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

store/commit_info.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ func (ci *CommitInfo) Hash() []byte {
5050
return rootHash
5151
}
5252

53+
// GetStoreProof takes in a storeKey and returns a proof of the store key in addition
54+
// to the root hash it should be proved against. If an empty string is provided, the first
55+
// store based on lexographical ordering will be proved.
5356
func (ci *CommitInfo) GetStoreProof(storeKey string) ([]byte, *CommitmentOp, error) {
5457
sort.Slice(ci.StoreInfos, func(i, j int) bool {
5558
return ci.StoreInfos[i].Name < ci.StoreInfos[j].Name

store/proof.go

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var (
2424
LeafSpec: &ics23.LeafOp{
2525
Prefix: leafPrefix,
2626
PrehashKey: ics23.HashOp_NO_HASH,
27-
PrehashValue: ics23.HashOp_NO_HASH,
27+
PrehashValue: ics23.HashOp_SHA256,
2828
Hash: ics23.HashOp_SHA256,
2929
Length: ics23.LengthOp_VAR_PROTO,
3030
},
@@ -122,31 +122,70 @@ func (op CommitmentOp) Run(args [][]byte) ([][]byte, error) {
122122
return [][]byte{root}, nil
123123
}
124124

125-
// ProofFromByteSlices computes the proof from the given leaves.
125+
// ProofFromByteSlices computes the proof from the given leaves. An iteration will be
126+
// perfomed for each level of the tree, where each iteration hashes together the bottom most
127+
// nodes. If the length of the bottom most nodes is odd, then the last node will be saved
128+
// for the next iteration.
129+
//
130+
// Example:
131+
// Iteration 1:
132+
// n = 5
133+
// leaves = a, b, c, d, e.
134+
// index = 2 (prove c)
135+
//
136+
// Iteration 2:
137+
// n = 3
138+
// leaves = ab, cd, e
139+
// index = 1 (prove c, so index of cd)
140+
//
141+
// Iteration 3:
142+
// n = 2
143+
// leaves = abcd, e
144+
// index = 0 (prove c, so index of abcd)
145+
//
146+
// Final iteration:
147+
// n = 1
148+
// leaves = abcde
149+
// index = 0
150+
//
151+
// The bitwise & operator allows us to determine if the index or length is odd or even.
152+
// The bitwise ^ operator allows us to increment when the value is even and decrement when it is odd.
126153
func ProofFromByteSlices(leaves [][]byte, index int) (rootHash []byte, inners []*ics23.InnerOp) {
127154
if len(leaves) == 0 {
128155
return emptyHash(), nil
129156
}
130157

131158
n := len(leaves)
132159
for n > 1 {
160+
// Begin by constructing the proof for the inner node of the requested index.
161+
// A proof of the inner node is skipped only in the case where the requested index
162+
// is the last element and it does not have a leaf pair (resulting in it being
163+
// saved until the next iteration).
133164
if index < n-1 || index&1 == 1 {
134165
inner := &ics23.InnerOp{Hash: ics23.HashOp_SHA256}
166+
//Iif proof index is even then child is from left, suffix is populated
167+
// otherwise, child is from right and the prefix is populated.
135168
if index&1 == 0 {
169+
// inner op(prefix=0x01 | child | suffix=leaves[index+1])
136170
inner.Prefix = innerPrefix
137-
inner.Suffix = leaves[index^1]
171+
inner.Suffix = leaves[index^1] // XOR op is index+1 because index is even
138172
} else {
139-
inner.Prefix = append(innerPrefix, leaves[index^1]...)
173+
// inner op(prefix=0x01 | leaves[index-1] | child | suffix=nil)
174+
inner.Prefix = append(innerPrefix, leaves[index^1]...) // XOR op is index-1 because index is odd
140175
}
141176
inners = append(inners, inner)
142177
}
178+
179+
// hash together all leaf pairs
143180
for i := 0; i < n/2; i++ {
144181
leaves[i] = InnerHash(leaves[2*i], leaves[2*i+1])
145182
}
183+
184+
// save any leftover leaf for the next iteration
146185
if n&1 == 1 {
147186
leaves[n/2] = leaves[n-1]
148187
}
149-
n = (n + 1) / 2
188+
n = (n + 1) / 2 // n + 1 accounts for any leaves which are added to the next iteration
150189
index /= 2
151190
}
152191

@@ -178,7 +217,8 @@ func LeafHash(key, value []byte) ([]byte, error) {
178217
return SimpleMerkleSpec.LeafSpec.Apply(key, value)
179218
}
180219

181-
// InnerHash computes the hash of an inner node.
220+
// InnerHash computes the hash of an inner node as defined by ics23:
221+
// https://github.com/cosmos/ics23/blob/go/v0.10.0/proto/cosmos/ics23/v1/proofs.proto#L130
182222
func InnerHash(left, right []byte) []byte {
183223
data := make([]byte, len(innerPrefix)+len(left)+len(right))
184224
n := copy(data, innerPrefix)

store/proof_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,29 @@ func TestProofFromBytesSlices(t *testing.T) {
1414
want string
1515
}{
1616
{[]string{}, []string{}, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
17-
{[]string{"key1"}, []string{"value1"}, "09c468a07fe9bc1f14e754cff0acbad4faf9449449288be8e1d5d1199a247034"},
18-
{[]string{"key1"}, []string{"value2"}, "2131d85de3a8ded5d3a72bfc657f7324138540c520de7401ac8594785a3082fb"},
17+
{[]string{"key1"}, []string{"value1"}, "a44d3cc7daba1a4600b00a2434b30f8b970652169810d6dfa9fb1793a2189324"},
18+
{[]string{"key1"}, []string{"value2"}, "0638e99b3445caec9d95c05e1a3fc1487b4ddec6a952ff337080360b0dcc078c"},
1919
// swap order with 2 keys
2020
{
2121
[]string{"key1", "key2"},
2222
[]string{"value1", "value2"},
23-
"017788f37362dd0687beb59c0b3bfcc17a955120a4cb63dbdd4a0fdf9e07730e",
23+
"8fd19b19e7bb3f2b3ee0574027d8a5a4cec370464ea2db2fbfa5c7d35bb0cff3",
2424
},
2525
{
2626
[]string{"key2", "key1"},
2727
[]string{"value2", "value1"},
28-
"ad2b0c23dbd3376440a5347fba02ff35cfad7930daa5e733930315b6fbb03b26",
28+
"55d4bce1c53b7d394bd41bbfc2b239cc2e1c7e36423612a97181c47e79bb713c",
2929
},
3030
// swap order with 3 keys
3131
{
3232
[]string{"key1", "key2", "key3"},
3333
[]string{"value1", "value2", "value3"},
34-
"68f41a8a3508cb5f8eb3f1c7534a86fea9f59aa4898a5aac2f1bb92834ae2a36",
34+
"1dd674ec6782a0d586a903c9c63326a41cbe56b3bba33ed6ff5b527af6efb3dc",
3535
},
3636
{
3737
[]string{"key1", "key3", "key2"},
3838
[]string{"value1", "value3", "value2"},
39-
"92cd50420c22d0c79f64dd1b04bfd5f5d73265f7ac37e65cf622f3cf8b963805",
39+
"443382fbb629e0d50e86d6ea49e22aa4e27ba50262730b0122cec36860c903a2",
4040
},
4141
}
4242
for i, tc := range tests {

0 commit comments

Comments
 (0)