|
| 1 | +from typing import List |
| 2 | + |
| 3 | +import pytest |
| 4 | +from poseidon_py.poseidon_hash import poseidon_hash |
| 5 | + |
| 6 | +from starknet_py.hash.hash_method import HashMethod |
| 7 | +from starknet_py.hash.utils import pedersen_hash |
| 8 | +from starknet_py.utils.merkle_tree import MerkleTree |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize( |
| 12 | + "leaves, hash_method, expected_root_hash", |
| 13 | + [ |
| 14 | + ( |
| 15 | + ["0x12", "0xa"], |
| 16 | + HashMethod.PEDERSEN, |
| 17 | + "0x586699e3ba6f118227e094ad423313a2d51871507dcbc23116f11cdd79d80f2", |
| 18 | + ), |
| 19 | + ( |
| 20 | + ["0x12", "0xa"], |
| 21 | + HashMethod.POSEIDON, |
| 22 | + "0x6257f1f60f7c9fd49e2718c8ad19cd8dce6b1ba4b553b2123113f22b1e9c379", |
| 23 | + ), |
| 24 | + ( |
| 25 | + [ |
| 26 | + "0x5bb9440e27889a364bcb678b1f679ecd1347acdedcbf36e83494f857cc58026", |
| 27 | + "0x3", |
| 28 | + ], |
| 29 | + HashMethod.PEDERSEN, |
| 30 | + "0x551b4adb6c35d49c686a00b9192da9332b18c9b262507cad0ece37f3b6918d2", |
| 31 | + ), |
| 32 | + ( |
| 33 | + [ |
| 34 | + "0x5bb9440e27889a364bcb678b1f679ecd1347acdedcbf36e83494f857cc58026", |
| 35 | + "0x3", |
| 36 | + ], |
| 37 | + HashMethod.POSEIDON, |
| 38 | + "0xc118a3963c12777b0717d1dc89baa8b3ceed84dfd713a6bd1354676f03f021", |
| 39 | + ), |
| 40 | + ], |
| 41 | +) |
| 42 | +def test_calculate_hash( |
| 43 | + leaves: List[str], hash_method: HashMethod, expected_root_hash: str |
| 44 | +): |
| 45 | + if hash_method == HashMethod.PEDERSEN: |
| 46 | + apply_hash = pedersen_hash |
| 47 | + elif hash_method == HashMethod.POSEIDON: |
| 48 | + apply_hash = poseidon_hash |
| 49 | + else: |
| 50 | + raise ValueError(f"Unsupported hash method: {hash_method}.") |
| 51 | + |
| 52 | + a, b = int(leaves[0], 16), int(leaves[1], 16) |
| 53 | + merkle_hash = hash_method.hash(*sorted([b, a])) |
| 54 | + raw_hash = apply_hash(*sorted([b, a])) |
| 55 | + |
| 56 | + assert raw_hash == merkle_hash |
| 57 | + assert int(expected_root_hash, 16) == merkle_hash |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize( |
| 61 | + "hash_method", |
| 62 | + [ |
| 63 | + HashMethod.PEDERSEN, |
| 64 | + HashMethod.POSEIDON, |
| 65 | + ], |
| 66 | +) |
| 67 | +def test_build_from_0_elements(hash_method: HashMethod): |
| 68 | + with pytest.raises( |
| 69 | + ValueError, match="Cannot build Merkle tree from an empty list of leaves." |
| 70 | + ): |
| 71 | + MerkleTree([], hash_method) |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.parametrize( |
| 75 | + "leaves, hash_method, expected_root_hash, expected_levels_count", |
| 76 | + [ |
| 77 | + (["0x1"], HashMethod.PEDERSEN, "0x1", 1), |
| 78 | + (["0x1"], HashMethod.POSEIDON, "0x1", 1), |
| 79 | + ( |
| 80 | + ["0x1", "0x2"], |
| 81 | + HashMethod.PEDERSEN, |
| 82 | + "0x5bb9440e27889a364bcb678b1f679ecd1347acdedcbf36e83494f857cc58026", |
| 83 | + 2, |
| 84 | + ), |
| 85 | + ( |
| 86 | + ["0x1", "0x2"], |
| 87 | + HashMethod.POSEIDON, |
| 88 | + "0x5d44a3decb2b2e0cc71071f7b802f45dd792d064f0fc7316c46514f70f9891a", |
| 89 | + 2, |
| 90 | + ), |
| 91 | + ( |
| 92 | + ["0x1", "0x2", "0x3", "0x4"], |
| 93 | + HashMethod.PEDERSEN, |
| 94 | + "0x38118a340bbba28e678413cd3b07a9436a5e60fd6a7cbda7db958a6d501e274", |
| 95 | + 3, |
| 96 | + ), |
| 97 | + ( |
| 98 | + ["0x1", "0x2", "0x3", "0x4"], |
| 99 | + HashMethod.POSEIDON, |
| 100 | + "0xa4d02f1e82fc554b062b754d3a4995e0ed8fc7e5016a7ca2894a451a4bae64", |
| 101 | + 3, |
| 102 | + ), |
| 103 | + ( |
| 104 | + ["0x1", "0x2", "0x3", "0x4", "0x5", "0x6"], |
| 105 | + HashMethod.PEDERSEN, |
| 106 | + "0x329d5b51e352537e8424bfd85b34d0f30b77d213e9b09e2976e6f6374ecb59", |
| 107 | + 4, |
| 108 | + ), |
| 109 | + ( |
| 110 | + ["0x1", "0x2", "0x3", "0x4", "0x5", "0x6"], |
| 111 | + HashMethod.POSEIDON, |
| 112 | + "0x34d525f018d8d6b3e492b1c9cda9bbdc3bc7834b408a30a417186c698c34766", |
| 113 | + 4, |
| 114 | + ), |
| 115 | + ( |
| 116 | + ["0x1", "0x2", "0x3", "0x4", "0x5", "0x6", "0x7"], |
| 117 | + HashMethod.PEDERSEN, |
| 118 | + "0x7f748c75e5bdb7ae28013f076b8ab650c4e01d3530c6e5ab665f9f1accbe7d4", |
| 119 | + 4, |
| 120 | + ), |
| 121 | + ( |
| 122 | + ["0x1", "0x2", "0x3", "0x4", "0x5", "0x6", "0x7"], |
| 123 | + HashMethod.POSEIDON, |
| 124 | + "0x3308a3c50c25883753f82b21f14c644ec375b88ea5b0f83d1e6afe74d0ed790", |
| 125 | + 4, |
| 126 | + ), |
| 127 | + ], |
| 128 | +) |
| 129 | +def test_build_from_elements( |
| 130 | + leaves: List[str], |
| 131 | + hash_method: HashMethod, |
| 132 | + expected_root_hash: str, |
| 133 | + expected_levels_count: int, |
| 134 | +): |
| 135 | + tree = MerkleTree([int(leaf, 16) for leaf in leaves], hash_method) |
| 136 | + |
| 137 | + assert tree.root_hash is not None |
| 138 | + assert tree.levels is not None |
| 139 | + assert tree.root_hash == int(expected_root_hash, 16) |
| 140 | + assert len(tree.levels) == expected_levels_count |
0 commit comments