@@ -23,22 +23,22 @@ module pyth::merkle_tree {
23
23
bytes20::from_bytes (bytes20)
24
24
}
25
25
26
- fun emptyLeafHash (): Bytes20 {
26
+ fun empty_leaf_hash (): Bytes20 {
27
27
let v = vector <u8 >[MERKLE_EMPTY_LEAF_PREFIX ];
28
28
hash (&v)
29
29
}
30
30
31
- fun leafHash (data: &vector <u8 >): Bytes20 {
31
+ fun leaf_hash (data: &vector <u8 >): Bytes20 {
32
32
let v = vector <u8 >[MERKLE_LEAF_PREFIX ];
33
33
vector ::append (&mut v, *data);
34
34
hash (&v)
35
35
}
36
36
37
- fun nodeHash (
37
+ fun node_hash (
38
38
childA: Bytes20 ,
39
39
childB: Bytes20
40
40
): Bytes20 {
41
- if (greaterThan (childA, childB)) {
41
+ if (greater_than (childA, childB)) {
42
42
(childA, childB) = (childB, childA);
43
43
};
44
44
// append data_B to data_A
@@ -53,9 +53,9 @@ module pyth::merkle_tree {
53
53
hash (&v)
54
54
}
55
55
56
- // greaterThan returns whether a is strictly greater than b
57
- // note that data(&a) and data(&b) are both vectors of length 20
58
- fun greaterThan (a: Bytes20 , b: Bytes20 ): bool {
56
+ // greater_than returns whether a is strictly greater than b
57
+ // note that data(&a) and data(&b) are both vector<u8>s of length 20
58
+ fun greater_than (a: Bytes20 , b: Bytes20 ): bool {
59
59
// aa and bb both have length 20
60
60
let aa = data (&a);
61
61
let bb = data (&b);
@@ -72,38 +72,38 @@ module pyth::merkle_tree {
72
72
}
73
73
74
74
// The Sui Move stdlb insert function shifts v[i] and subsequent elements to the right.
75
- // We don't want this behavior, so we define our own setElement function that instead replaces the ith element.
75
+ // We don't want this behavior, so we define our own set_element function that instead replaces the ith element.
76
76
// Reference: https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/packages/move-stdlib/sources/vector.move
77
- fun setElement <T : drop >(a: &mut vector <T >, value: T , index: u64 ){
77
+ fun set_element <T : drop >(a: &mut vector <T >, value: T , index: u64 ){
78
78
vector ::push_back <T >(a, value); // push value to end
79
79
vector ::swap_remove (a, index); // swap value to correct position and pop last value
80
80
}
81
81
82
- // isProofValid returns whether a merkle proof is valid
83
- public fun isProofValid (
84
- encodedProof : &mut Cursor <u8 >,
82
+ // is_proof_valid returns whether a merkle proof is valid
83
+ public fun is_proof_valid (
84
+ encoded_proof : &mut Cursor <u8 >,
85
85
root: Bytes20 ,
86
- leafData : vector <u8 >,
86
+ leaf_data : vector <u8 >,
87
87
): bool {
88
- let currentDigest : Bytes20 = leafHash(&leafData );
89
- let proofSize: u8 = deserialize::deserialize_u8 (encodedProof );
88
+ let current_digest : Bytes20 = leaf_hash (&leaf_data );
89
+ let proofSize: u8 = deserialize::deserialize_u8 (encoded_proof );
90
90
while (proofSize > 0 ){
91
- let siblingDigest : Bytes20 = bytes20::new (
92
- deserialize::deserialize_vector (encodedProof , 20 )
91
+ let sibling_digest : Bytes20 = bytes20::new (
92
+ deserialize::deserialize_vector (encoded_proof , 20 )
93
93
);
94
94
95
- currentDigest = nodeHash (
96
- currentDigest ,
97
- siblingDigest
95
+ current_digest = node_hash (
96
+ current_digest ,
97
+ sibling_digest
98
98
);
99
99
proofSize = proofSize - 1 ;
100
100
};
101
- bytes20::data (¤tDigest ) == bytes20::data (&root)
101
+ bytes20::data (¤t_digest ) == bytes20::data (&root)
102
102
}
103
103
104
- // constructProofs constructs a merkle tree and returns the root of the tree as
104
+ // construct_proofs constructs a merkle tree and returns the root of the tree as
105
105
// a Bytes20 as well as the vector of encoded proofs
106
- public fun constructProofs (
106
+ public fun construct_proofs (
107
107
messages: &vector <vector <u8 >>,
108
108
depth: u8
109
109
) : (Bytes20 , vector <u8 >) {
@@ -124,7 +124,7 @@ module pyth::merkle_tree {
124
124
let tree = vector ::empty <Bytes20 >();
125
125
126
126
// empty leaf hash
127
- let cachedEmptyLeafHash: Bytes20 = emptyLeafHash ();
127
+ let cachedEmptyLeafHash: Bytes20 = empty_leaf_hash ();
128
128
129
129
// Instantiate tree to be a full binary tree with the appropriate depth.
130
130
// Add an entry at the end for swapping
@@ -137,7 +137,7 @@ module pyth::merkle_tree {
137
137
// Fill in bottom row with leaf hashes
138
138
let j: u64 = 0 ;
139
139
while (j < vector ::length (messages)){
140
- setElement <Bytes20 >(&mut tree, leafHash (vector ::borrow (messages, j)), (1 << depth) + j);
140
+ set_element <Bytes20 >(&mut tree, leaf_hash (vector ::borrow (messages, j)), (1 << depth) + j);
141
141
j = j + 1 ;
142
142
};
143
143
@@ -149,8 +149,8 @@ module pyth::merkle_tree {
149
149
let i: u64 = 0 ;
150
150
while (i < levelNumNodes ){
151
151
let id = (1 << level) + i;
152
- let nodeHash = nodeHash (*vector ::borrow (&tree, id * 2 ), *vector ::borrow (&tree, id * 2 + 1 ));
153
- setElement <Bytes20 >(&mut tree, nodeHash , id);
152
+ let node_hash = node_hash (*vector ::borrow (&tree, id * 2 ), *vector ::borrow (&tree, id * 2 + 1 ));
153
+ set_element <Bytes20 >(&mut tree, node_hash , id);
154
154
i = i + 1 ;
155
155
};
156
156
k = k - 1 ;
@@ -183,28 +183,28 @@ module pyth::merkle_tree {
183
183
// test 1
184
184
let x = bytes20::new (x"0000000000000000000000000000000000001000 ");
185
185
let y = bytes20::new (x"0000000000000000000000000000000000000001 ");
186
- let res = greaterThan (x, y);
186
+ let res = greater_than (x, y);
187
187
assert !(res==true , 0 );
188
- res = greaterThan (y, x);
188
+ res = greater_than (y, x);
189
189
assert !(res==false , 0 );
190
190
191
191
// test 2
192
192
x = bytes20::new (x"1100000000000000000000000000000000001000 ");
193
193
y = bytes20::new (x"1100000000000000000000000000000000000001 ");
194
- res = greaterThan (x, y);
194
+ res = greater_than (x, y);
195
195
assert !(res==true , 0 );
196
196
197
197
// equality case
198
198
x = bytes20::new (x"1100000000000000000000000000000000001001 ");
199
199
y = bytes20::new (x"1100000000000000000000000000000000001001 ");
200
- res = greaterThan (x, y);
200
+ res = greater_than (x, y);
201
201
assert !(res==false , 0 );
202
202
}
203
203
204
204
#[test]
205
205
fun test_hash_leaf () {
206
206
let data: vector <u8 > = x"00640000000000000000000000000000000000000000000000000000000000000000000000000000640000000000000064000000640000000000000064000000000000006400000000000000640000000000000064 ";
207
- let hash = leafHash (&data);
207
+ let hash = leaf_hash (&data);
208
208
let expected = bytes20::new (x"afc6a8ac466430f35895055f8a4c951785dad5ce ");
209
209
assert !(hash == expected, 1 );
210
210
}
@@ -213,7 +213,7 @@ module pyth::merkle_tree {
213
213
fun test_hash_node () {
214
214
let h1 = bytes20::new (x"05c51b04b820c0f704e3fdd2e4fc1e70aff26dff ");
215
215
let h2 = bytes20::new (x"1e108841c8d21c7a5c4860c8c3499c918ea9e0ac ");
216
- let hash = nodeHash (h1, h2);
216
+ let hash = node_hash (h1, h2);
217
217
let expected = bytes20::new (x"2d0e4fde68184c7ce8af426a0865bd41ef84dfa4 ");
218
218
assert !(hash == expected, 1 );
219
219
}
@@ -223,14 +223,14 @@ module pyth::merkle_tree {
223
223
let messages = vector ::empty <vector <u8 >>();
224
224
vector ::push_back (&mut messages, x"1234 ");
225
225
226
- let (root, proofs) = constructProofs (&messages, 1 );
226
+ let (root, proofs) = construct_proofs (&messages, 1 );
227
227
228
- let proofsCursor = cursor::new (proofs);
229
- let valid = isProofValid (&mut proofsCursor , root, x"1234 ");
228
+ let proofs_cursor = cursor::new (proofs);
229
+ let valid = is_proof_valid (&mut proofs_cursor , root, x"1234 ");
230
230
assert !(valid==true , 0 );
231
231
232
232
// destroy cursor
233
- cursor::take_rest <u8 >(proofsCursor );
233
+ cursor::take_rest <u8 >(proofs_cursor );
234
234
}
235
235
236
236
#[test]
@@ -241,20 +241,20 @@ module pyth::merkle_tree {
241
241
vector ::push_back (&mut messages, x"11 ");
242
242
vector ::push_back (&mut messages, x"22 ");
243
243
244
- let (root, proofs) = constructProofs (&messages, 2 );
244
+ let (root, proofs) = construct_proofs (&messages, 2 );
245
245
246
- let proofsCursor = cursor::new (proofs);
247
- assert !(isProofValid (&mut proofsCursor , root, x"1234 ")==true , 0 );
248
- assert !(isProofValid (&mut proofsCursor , root, x"4321 ")==true , 0 );
249
- assert !(isProofValid (&mut proofsCursor , root, x"11 ")==true , 0 );
250
- assert !(isProofValid (&mut proofsCursor , root, x"22 ")==true , 0 );
246
+ let proofs_cursor = cursor::new (proofs);
247
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"1234 ")==true , 0 );
248
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"4321 ")==true , 0 );
249
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"11 ")==true , 0 );
250
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"22 ")==true , 0 );
251
251
252
252
// destroy cursor
253
- cursor::take_rest <u8 >(proofsCursor );
253
+ cursor::take_rest <u8 >(proofs_cursor );
254
254
}
255
255
256
256
#[test]
257
- fun testMerkleTreeDepth3 (){
257
+ fun test_merkle_tree_depth_3 (){
258
258
let messages = vector ::empty <vector <u8 >>();
259
259
vector ::push_back (&mut messages, x"00 ");
260
260
vector ::push_back (&mut messages, x"4321 ");
@@ -265,65 +265,65 @@ module pyth::merkle_tree {
265
265
vector ::push_back (&mut messages, x"100000 ");
266
266
vector ::push_back (&mut messages, x"eeeeee ");
267
267
268
- let (root, proofs) = constructProofs (&messages, 3 );
268
+ let (root, proofs) = construct_proofs (&messages, 3 );
269
269
270
- let proofsCursor = cursor::new (proofs);
271
- assert !(isProofValid (&mut proofsCursor , root, x"00 ")==true , 0 );
272
- assert !(isProofValid (&mut proofsCursor , root, x"4321 ")==true , 0 );
273
- assert !(isProofValid (&mut proofsCursor , root, x"444444 ")==true , 0 );
274
- assert !(isProofValid (&mut proofsCursor , root, x"22222222 ")==true , 0 );
275
- assert !(isProofValid (&mut proofsCursor , root, x"22 ")==true , 0 );
276
- assert !(isProofValid (&mut proofsCursor , root, x"11 ")==true , 0 );
277
- assert !(isProofValid (&mut proofsCursor , root, x"100000 ")==true , 0 );
278
- assert !(isProofValid (&mut proofsCursor , root, x"eeeeee ")==true , 0 );
270
+ let proofs_cursor = cursor::new (proofs);
271
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"00 ")==true , 0 );
272
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"4321 ")==true , 0 );
273
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"444444 ")==true , 0 );
274
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"22222222 ")==true , 0 );
275
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"22 ")==true , 0 );
276
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"11 ")==true , 0 );
277
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"100000 ")==true , 0 );
278
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"eeeeee ")==true , 0 );
279
279
280
280
// destroy cursor
281
- cursor::take_rest <u8 >(proofsCursor );
281
+ cursor::take_rest <u8 >(proofs_cursor );
282
282
}
283
283
284
284
#[test]
285
- fun testMerkleTreeDepth1InvalidProofs (){
285
+ fun test_merkle_tree_depth_1_invalid_proofs (){
286
286
let messages = vector ::empty <vector <u8 >>();
287
287
vector ::push_back (&mut messages, x"1234 ");
288
288
289
- let (root, proofs) = constructProofs (&messages, 1 );
289
+ let (root, proofs) = construct_proofs (&messages, 1 );
290
290
291
- let proofsCursor = cursor::new (proofs);
291
+ let proofs_cursor = cursor::new (proofs);
292
292
293
293
// use wrong leaf data (it is not included in the tree)
294
- let valid = isProofValid (&mut proofsCursor , root, x"432222 ");
294
+ let valid = is_proof_valid (&mut proofs_cursor , root, x"432222 ");
295
295
assert !(valid==false , 0 );
296
296
297
297
// destroy cursor
298
- cursor::take_rest <u8 >(proofsCursor );
298
+ cursor::take_rest <u8 >(proofs_cursor );
299
299
}
300
300
301
301
#[test]
302
- fun testMerkleTreeDepth2InvalidProofs (){
302
+ fun test_merkle_tree_depth_2_invalid_proofs (){
303
303
let messages = vector ::empty <vector <u8 >>();
304
304
vector ::push_back (&mut messages, x"1234 ");
305
305
vector ::push_back (&mut messages, x"4321 ");
306
306
vector ::push_back (&mut messages, x"11 ");
307
307
vector ::push_back (&mut messages, x"22 ");
308
308
309
- let (root, proofs) = constructProofs (&messages, 2 );
309
+ let (root, proofs) = construct_proofs (&messages, 2 );
310
310
311
- let proofsCursor = cursor::new (proofs);
311
+ let proofs_cursor = cursor::new (proofs);
312
312
// proof fails because we used the proof of x"1234" to try to prove that x"4321" is in the tree
313
- assert !(isProofValid (&mut proofsCursor , root, x"4321 ")==false , 0 );
313
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"4321 ")==false , 0 );
314
314
// proof succeeds
315
- assert !(isProofValid (&mut proofsCursor , root, x"4321 ")==true , 0 );
315
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"4321 ")==true , 0 );
316
316
// proof fails because we used the proof of x"11" to try to prove that x"22" is in the tree
317
- assert !(isProofValid (&mut proofsCursor , root, x"22 ")==false , 0 );
317
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"22 ")==false , 0 );
318
318
// proof succeeds
319
- assert !(isProofValid (&mut proofsCursor , root, x"22 ")==true , 0 );
319
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"22 ")==true , 0 );
320
320
321
321
// destroy cursor
322
- cursor::take_rest <u8 >(proofsCursor );
322
+ cursor::take_rest <u8 >(proofs_cursor );
323
323
}
324
324
325
325
#[test]
326
- fun testMerkleTreeDepth3InvalidProofs (){
326
+ fun test_merkle_tree_depth_3_invalid_proofs (){
327
327
let messages = vector ::empty <vector <u8 >>();
328
328
vector ::push_back (&mut messages, x"00 ");
329
329
vector ::push_back (&mut messages, x"4321 ");
@@ -334,46 +334,46 @@ module pyth::merkle_tree {
334
334
vector ::push_back (&mut messages, x"100000 ");
335
335
vector ::push_back (&mut messages, x"eeeeee ");
336
336
337
- let (root, proofs) = constructProofs (&messages, 3 );
337
+ let (root, proofs) = construct_proofs (&messages, 3 );
338
338
339
- let proofsCursor = cursor::new (proofs);
339
+ let proofs_cursor = cursor::new (proofs);
340
340
341
341
// test various proof failure cases (because of mismatch between proof and leaf data)
342
- assert !(isProofValid (&mut proofsCursor , root, x"00 ")==true , 0 );
343
- assert !(isProofValid (&mut proofsCursor , root, x"22 ")==false , 0 );
344
- assert !(isProofValid (&mut proofsCursor , root, x"22222222 ")==false , 0 );
345
- assert !(isProofValid (&mut proofsCursor , root, x"22222222 ")==true , 0 );
346
- assert !(isProofValid (&mut proofsCursor , root, x"22 ")==true , 0 );
347
- assert !(isProofValid (&mut proofsCursor , root, x"eeeeee ")==false , 0 );
348
- assert !(isProofValid (&mut proofsCursor , root, x"4321 ")==false , 0 );
349
- assert !(isProofValid (&mut proofsCursor , root, x"eeeeee ")==true , 0 );
342
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"00 ")==true , 0 );
343
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"22 ")==false , 0 );
344
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"22222222 ")==false , 0 );
345
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"22222222 ")==true , 0 );
346
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"22 ")==true , 0 );
347
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"eeeeee ")==false , 0 );
348
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"4321 ")==false , 0 );
349
+ assert !(is_proof_valid (&mut proofs_cursor , root, x"eeeeee ")==true , 0 );
350
350
351
351
// destroy cursor
352
- cursor::take_rest <u8 >(proofsCursor );
352
+ cursor::take_rest <u8 >(proofs_cursor );
353
353
}
354
354
355
355
#[test]
356
356
#[expected_failure(abort_code = pyth::merkle_tree::E_DEPTH_NOT_LARGE_ENOUGH_FOR_MESSAGES)]
357
- fun testMerkleTreeDepthExceeded1 (){
357
+ fun test_merkle_tree_depth_exceeded_1 (){
358
358
let messages = vector ::empty <vector <u8 >>();
359
359
vector ::push_back (&mut messages, x"00 ");
360
360
vector ::push_back (&mut messages, x"4321 ");
361
361
vector ::push_back (&mut messages, x"444444 ");
362
362
363
- constructProofs (&messages, 1 ); //depth 1
363
+ construct_proofs (&messages, 1 ); //depth 1
364
364
}
365
365
366
366
#[test]
367
367
#[expected_failure(abort_code = pyth::merkle_tree::E_DEPTH_NOT_LARGE_ENOUGH_FOR_MESSAGES)]
368
- fun testMerkleTreeDepthExceeded2 (){
368
+ fun test_merkle_tree_depth_exceeded_2 (){
369
369
let messages = vector ::empty <vector <u8 >>();
370
370
vector ::push_back (&mut messages, x"00 ");
371
371
vector ::push_back (&mut messages, x"4321 ");
372
372
vector ::push_back (&mut messages, x"444444 ");
373
373
vector ::push_back (&mut messages, x"22222222 ");
374
374
vector ::push_back (&mut messages, x"22 ");
375
375
376
- constructProofs (&messages, 2 ); // depth 2
376
+ construct_proofs (&messages, 2 ); // depth 2
377
377
}
378
378
379
379
}
0 commit comments