diff --git a/tasm-lib/Cargo.toml b/tasm-lib/Cargo.toml index 4e35039f..a664a2b1 100644 --- a/tasm-lib/Cargo.toml +++ b/tasm-lib/Cargo.toml @@ -29,7 +29,7 @@ rand = "0.8.5" serde = { version = "1", features = ["derive"] } serde_json = "1" strum = { version = "0.26", features = ["derive"] } -triton-vm = "0.42.0-alpha.2" +triton-vm = "0.42.0-alpha.3" [dev-dependencies.cargo-husky] version = "1" diff --git a/tasm-lib/benchmarks/tasmlib_array_inner_product_of_452_xfes.json b/tasm-lib/benchmarks/tasmlib_array_inner_product_of_452_xfes.json new file mode 100644 index 00000000..b0a72eb5 --- /dev/null +++ b/tasm-lib/benchmarks/tasmlib_array_inner_product_of_452_xfes.json @@ -0,0 +1,24 @@ +[ + { + "name": "tasmlib_array_inner_product_of_452_xfes", + "benchmark_result": { + "clock_cycle_count": 462, + "hash_table_height": 288, + "u32_table_height": 0, + "op_stack_table_height": 5, + "ram_table_height": 2712 + }, + "case": "CommonCase" + }, + { + "name": "tasmlib_array_inner_product_of_452_xfes", + "benchmark_result": { + "clock_cycle_count": 462, + "hash_table_height": 288, + "u32_table_height": 0, + "op_stack_table_height": 5, + "ram_table_height": 2712 + }, + "case": "WorstCase" + } +] \ No newline at end of file diff --git a/tasm-lib/benchmarks/tasmlib_array_inner_product_of_609_xfes.json b/tasm-lib/benchmarks/tasmlib_array_inner_product_of_609_xfes.json new file mode 100644 index 00000000..453f2c73 --- /dev/null +++ b/tasm-lib/benchmarks/tasmlib_array_inner_product_of_609_xfes.json @@ -0,0 +1,24 @@ +[ + { + "name": "tasmlib_array_inner_product_of_609_xfes", + "benchmark_result": { + "clock_cycle_count": 619, + "hash_table_height": 378, + "u32_table_height": 0, + "op_stack_table_height": 5, + "ram_table_height": 3654 + }, + "case": "CommonCase" + }, + { + "name": "tasmlib_array_inner_product_of_609_xfes", + "benchmark_result": { + "clock_cycle_count": 619, + "hash_table_height": 378, + "u32_table_height": 0, + "op_stack_table_height": 5, + "ram_table_height": 3654 + }, + "case": "WorstCase" + } +] \ No newline at end of file diff --git a/tasm-lib/benchmarks/tasmlib_array_inner_product_of_three_rows_with_weights_Bfe_baserowelem.json b/tasm-lib/benchmarks/tasmlib_array_inner_product_of_three_rows_with_weights_Bfe_baserowelem.json index bb7a8df2..9651dea0 100644 --- a/tasm-lib/benchmarks/tasmlib_array_inner_product_of_three_rows_with_weights_Bfe_baserowelem.json +++ b/tasm-lib/benchmarks/tasmlib_array_inner_product_of_three_rows_with_weights_Bfe_baserowelem.json @@ -2,22 +2,22 @@ { "name": "tasmlib_array_inner_product_of_three_rows_with_weights_Bfe_baserowelem", "benchmark_result": { - "clock_cycle_count": 463, + "clock_cycle_count": 466, "hash_table_height": 288, "u32_table_height": 0, "op_stack_table_height": 6, - "ram_table_height": 1962 + "ram_table_height": 1974 }, "case": "CommonCase" }, { "name": "tasmlib_array_inner_product_of_three_rows_with_weights_Bfe_baserowelem", "benchmark_result": { - "clock_cycle_count": 463, + "clock_cycle_count": 466, "hash_table_height": 288, "u32_table_height": 0, "op_stack_table_height": 6, - "ram_table_height": 1962 + "ram_table_height": 1974 }, "case": "WorstCase" } diff --git a/tasm-lib/benchmarks/tasmlib_array_inner_product_of_three_rows_with_weights_Xfe_baserowelem.json b/tasm-lib/benchmarks/tasmlib_array_inner_product_of_three_rows_with_weights_Xfe_baserowelem.json index 1eb2dd54..b771345d 100644 --- a/tasm-lib/benchmarks/tasmlib_array_inner_product_of_three_rows_with_weights_Xfe_baserowelem.json +++ b/tasm-lib/benchmarks/tasmlib_array_inner_product_of_three_rows_with_weights_Xfe_baserowelem.json @@ -2,22 +2,22 @@ { "name": "tasmlib_array_inner_product_of_three_rows_with_weights_Xfe_baserowelem", "benchmark_result": { - "clock_cycle_count": 463, + "clock_cycle_count": 466, "hash_table_height": 288, "u32_table_height": 0, "op_stack_table_height": 6, - "ram_table_height": 2688 + "ram_table_height": 2706 }, "case": "CommonCase" }, { "name": "tasmlib_array_inner_product_of_three_rows_with_weights_Xfe_baserowelem", "benchmark_result": { - "clock_cycle_count": 463, + "clock_cycle_count": 466, "hash_table_height": 288, "u32_table_height": 0, "op_stack_table_height": 6, - "ram_table_height": 2688 + "ram_table_height": 2706 }, "case": "WorstCase" } diff --git a/tasm-lib/benchmarks/tasmlib_hashing_merkle_verify.json b/tasm-lib/benchmarks/tasmlib_hashing_merkle_verify.json index 524a6d56..6bd61290 100644 --- a/tasm-lib/benchmarks/tasmlib_hashing_merkle_verify.json +++ b/tasm-lib/benchmarks/tasmlib_hashing_merkle_verify.json @@ -2,10 +2,10 @@ { "name": "tasmlib_hashing_merkle_verify", "benchmark_result": { - "clock_cycle_count": 69, + "clock_cycle_count": 63, "hash_table_height": 66, "u32_table_height": 12, - "op_stack_table_height": 108, + "op_stack_table_height": 48, "ram_table_height": 0 }, "case": "CommonCase" @@ -13,10 +13,10 @@ { "name": "tasmlib_hashing_merkle_verify", "benchmark_result": { - "clock_cycle_count": 167, + "clock_cycle_count": 147, "hash_table_height": 150, "u32_table_height": 28, - "op_stack_table_height": 304, + "op_stack_table_height": 104, "ram_table_height": 0 }, "case": "WorstCase" diff --git a/tasm-lib/benchmarks/tasmlib_mmr_verify_from_secret_in_leaf_index_on_stack.json b/tasm-lib/benchmarks/tasmlib_mmr_verify_from_secret_in_leaf_index_on_stack.json index cc2954e9..517dd58d 100644 --- a/tasm-lib/benchmarks/tasmlib_mmr_verify_from_secret_in_leaf_index_on_stack.json +++ b/tasm-lib/benchmarks/tasmlib_mmr_verify_from_secret_in_leaf_index_on_stack.json @@ -2,10 +2,10 @@ { "name": "tasmlib_mmr_verify_from_secret_in_leaf_index_on_stack", "benchmark_result": { - "clock_cycle_count": 1096, + "clock_cycle_count": 1064, "hash_table_height": 378, "u32_table_height": 166, - "op_stack_table_height": 884, + "op_stack_table_height": 564, "ram_table_height": 5 }, "case": "CommonCase" @@ -13,10 +13,10 @@ { "name": "tasmlib_mmr_verify_from_secret_in_leaf_index_on_stack", "benchmark_result": { - "clock_cycle_count": 1966, + "clock_cycle_count": 1904, "hash_table_height": 558, "u32_table_height": 835, - "op_stack_table_height": 1604, + "op_stack_table_height": 984, "ram_table_height": 5 }, "case": "WorstCase" diff --git a/tasm-lib/benchmarks/tasmlib_mmr_verify_from_secret_in_secret_leaf_index.json b/tasm-lib/benchmarks/tasmlib_mmr_verify_from_secret_in_secret_leaf_index.json index eabc42ee..294a5890 100644 --- a/tasm-lib/benchmarks/tasmlib_mmr_verify_from_secret_in_secret_leaf_index.json +++ b/tasm-lib/benchmarks/tasmlib_mmr_verify_from_secret_in_secret_leaf_index.json @@ -2,10 +2,10 @@ { "name": "tasmlib_mmr_verify_from_secret_in_secret_leaf_index", "benchmark_result": { - "clock_cycle_count": 1104, + "clock_cycle_count": 1072, "hash_table_height": 390, "u32_table_height": 166, - "op_stack_table_height": 890, + "op_stack_table_height": 570, "ram_table_height": 5 }, "case": "CommonCase" @@ -13,10 +13,10 @@ { "name": "tasmlib_mmr_verify_from_secret_in_secret_leaf_index", "benchmark_result": { - "clock_cycle_count": 1974, + "clock_cycle_count": 1912, "hash_table_height": 570, "u32_table_height": 933, - "op_stack_table_height": 1610, + "op_stack_table_height": 990, "ram_table_height": 5 }, "case": "WorstCase" diff --git a/tasm-lib/benchmarks/tasmlib_verifier_fri_verify.json b/tasm-lib/benchmarks/tasmlib_verifier_fri_verify.json index 55c435b8..7c693ee2 100644 --- a/tasm-lib/benchmarks/tasmlib_verifier_fri_verify.json +++ b/tasm-lib/benchmarks/tasmlib_verifier_fri_verify.json @@ -2,10 +2,10 @@ { "name": "tasmlib_verifier_fri_verify", "benchmark_result": { - "clock_cycle_count": 126151, + "clock_cycle_count": 124551, "hash_table_height": 14838, "u32_table_height": 12046, - "op_stack_table_height": 125233, + "op_stack_table_height": 109233, "ram_table_height": 19397 }, "case": "CommonCase" @@ -13,10 +13,10 @@ { "name": "tasmlib_verifier_fri_verify", "benchmark_result": { - "clock_cycle_count": 126151, + "clock_cycle_count": 124551, "hash_table_height": 14838, "u32_table_height": 11671, - "op_stack_table_height": 125233, + "op_stack_table_height": 109233, "ram_table_height": 19397 }, "case": "WorstCase" diff --git a/tasm-lib/benchmarks/tasmlib_verifier_master_ext_table_air_constraint_evaluation.json b/tasm-lib/benchmarks/tasmlib_verifier_master_ext_table_air_constraint_evaluation.json index 1a9db319..6324596a 100644 --- a/tasm-lib/benchmarks/tasmlib_verifier_master_ext_table_air_constraint_evaluation.json +++ b/tasm-lib/benchmarks/tasmlib_verifier_master_ext_table_air_constraint_evaluation.json @@ -2,22 +2,22 @@ { "name": "tasmlib_verifier_master_ext_table_air_constraint_evaluation", "benchmark_result": { - "clock_cycle_count": 34392, - "hash_table_height": 37020, + "clock_cycle_count": 34566, + "hash_table_height": 37200, "u32_table_height": 0, - "op_stack_table_height": 63661, - "ram_table_height": 22644 + "op_stack_table_height": 63999, + "ram_table_height": 22746 }, "case": "CommonCase" }, { "name": "tasmlib_verifier_master_ext_table_air_constraint_evaluation", "benchmark_result": { - "clock_cycle_count": 34392, - "hash_table_height": 37020, + "clock_cycle_count": 34566, + "hash_table_height": 37200, "u32_table_height": 0, - "op_stack_table_height": 63661, - "ram_table_height": 22644 + "op_stack_table_height": 63999, + "ram_table_height": 22746 }, "case": "WorstCase" } diff --git a/tasm-lib/benchmarks/tasmlib_verifier_master_ext_table_quotient_summands.json b/tasm-lib/benchmarks/tasmlib_verifier_master_ext_table_quotient_summands.json index 95d8bd88..4bcd1174 100644 --- a/tasm-lib/benchmarks/tasmlib_verifier_master_ext_table_quotient_summands.json +++ b/tasm-lib/benchmarks/tasmlib_verifier_master_ext_table_quotient_summands.json @@ -2,22 +2,22 @@ { "name": "tasmlib_verifier_master_ext_table_quotient_summands", "benchmark_result": { - "clock_cycle_count": 41932, - "hash_table_height": 45450, - "u32_table_height": 18, - "op_stack_table_height": 72392, - "ram_table_height": 28113 + "clock_cycle_count": 42337, + "hash_table_height": 45672, + "u32_table_height": 33, + "op_stack_table_height": 72952, + "ram_table_height": 28242 }, "case": "CommonCase" }, { "name": "tasmlib_verifier_master_ext_table_quotient_summands", "benchmark_result": { - "clock_cycle_count": 42075, - "hash_table_height": 45450, - "u32_table_height": 29, - "op_stack_table_height": 72524, - "ram_table_height": 28113 + "clock_cycle_count": 42233, + "hash_table_height": 45672, + "u32_table_height": 25, + "op_stack_table_height": 72856, + "ram_table_height": 28242 }, "case": "WorstCase" } diff --git a/tasm-lib/benchmarks/tasmlib_verifier_master_ext_table_verify_Base_table_rows.json b/tasm-lib/benchmarks/tasmlib_verifier_master_ext_table_verify_Base_table_rows.json index 91744708..6540f714 100644 --- a/tasm-lib/benchmarks/tasmlib_verifier_master_ext_table_verify_Base_table_rows.json +++ b/tasm-lib/benchmarks/tasmlib_verifier_master_ext_table_verify_Base_table_rows.json @@ -2,22 +2,22 @@ { "name": "tasmlib_verifier_master_ext_table_verify_Base_table_rows", "benchmark_result": { - "clock_cycle_count": 1345, + "clock_cycle_count": 1288, "hash_table_height": 1353, "u32_table_height": 6, - "op_stack_table_height": 3543, - "ram_table_height": 1098 + "op_stack_table_height": 3033, + "ram_table_height": 1107 }, "case": "CommonCase" }, { "name": "tasmlib_verifier_master_ext_table_verify_Base_table_rows", "benchmark_result": { - "clock_cycle_count": 1450, + "clock_cycle_count": 1378, "hash_table_height": 1443, "u32_table_height": 6, - "op_stack_table_height": 3753, - "ram_table_height": 1098 + "op_stack_table_height": 3093, + "ram_table_height": 1107 }, "case": "WorstCase" } diff --git a/tasm-lib/benchmarks/tasmlib_verifier_master_ext_table_verify_Extension_table_rows.json b/tasm-lib/benchmarks/tasmlib_verifier_master_ext_table_verify_Extension_table_rows.json index 67dd2328..cc11e2f9 100644 --- a/tasm-lib/benchmarks/tasmlib_verifier_master_ext_table_verify_Extension_table_rows.json +++ b/tasm-lib/benchmarks/tasmlib_verifier_master_ext_table_verify_Extension_table_rows.json @@ -2,10 +2,10 @@ { "name": "tasmlib_verifier_master_ext_table_verify_Extension_table_rows", "benchmark_result": { - "clock_cycle_count": 1102, + "clock_cycle_count": 1051, "hash_table_height": 1071, "u32_table_height": 6, - "op_stack_table_height": 2751, + "op_stack_table_height": 2241, "ram_table_height": 783 }, "case": "CommonCase" @@ -13,10 +13,10 @@ { "name": "tasmlib_verifier_master_ext_table_verify_Extension_table_rows", "benchmark_result": { - "clock_cycle_count": 1207, + "clock_cycle_count": 1141, "hash_table_height": 1161, "u32_table_height": 6, - "op_stack_table_height": 2961, + "op_stack_table_height": 2301, "ram_table_height": 783 }, "case": "WorstCase" diff --git a/tasm-lib/benchmarks/tasmlib_verifier_master_ext_table_verify_Quotient_table_rows.json b/tasm-lib/benchmarks/tasmlib_verifier_master_ext_table_verify_Quotient_table_rows.json index b2218075..32f43375 100644 --- a/tasm-lib/benchmarks/tasmlib_verifier_master_ext_table_verify_Quotient_table_rows.json +++ b/tasm-lib/benchmarks/tasmlib_verifier_master_ext_table_verify_Quotient_table_rows.json @@ -2,10 +2,10 @@ { "name": "tasmlib_verifier_master_ext_table_verify_Quotient_table_rows", "benchmark_result": { - "clock_cycle_count": 613, + "clock_cycle_count": 562, "hash_table_height": 471, "u32_table_height": 6, - "op_stack_table_height": 1023, + "op_stack_table_height": 513, "ram_table_height": 45 }, "case": "CommonCase" @@ -13,10 +13,10 @@ { "name": "tasmlib_verifier_master_ext_table_verify_Quotient_table_rows", "benchmark_result": { - "clock_cycle_count": 718, + "clock_cycle_count": 652, "hash_table_height": 561, "u32_table_height": 6, - "op_stack_table_height": 1233, + "op_stack_table_height": 573, "ram_table_height": 45 }, "case": "WorstCase" diff --git a/tasm-lib/benchmarks/tasmlib_verifier_stark_verify_inner_padded_height_256_fri_exp_4.json b/tasm-lib/benchmarks/tasmlib_verifier_stark_verify_inner_padded_height_256_fri_exp_4.json index 507a6967..67fd5374 100644 --- a/tasm-lib/benchmarks/tasmlib_verifier_stark_verify_inner_padded_height_256_fri_exp_4.json +++ b/tasm-lib/benchmarks/tasmlib_verifier_stark_verify_inner_padded_height_256_fri_exp_4.json @@ -2,11 +2,11 @@ { "name": "tasmlib_verifier_stark_verify_inner_padded_height_256_fri_exp_4", "benchmark_result": { - "clock_cycle_count": 433499, - "hash_table_height": 173611, - "u32_table_height": 17504, - "op_stack_table_height": 611436, - "ram_table_height": 490763 + "clock_cycle_count": 426014, + "hash_table_height": 173863, + "u32_table_height": 17137, + "op_stack_table_height": 533424, + "ram_table_height": 493384 }, "case": "CommonCase" } diff --git a/tasm-lib/benchmarks/tasmlib_verifier_stark_verify_inner_padded_height_512_fri_exp_4.json b/tasm-lib/benchmarks/tasmlib_verifier_stark_verify_inner_padded_height_512_fri_exp_4.json index 374cf5c2..2248b2eb 100644 --- a/tasm-lib/benchmarks/tasmlib_verifier_stark_verify_inner_padded_height_512_fri_exp_4.json +++ b/tasm-lib/benchmarks/tasmlib_verifier_stark_verify_inner_padded_height_512_fri_exp_4.json @@ -2,11 +2,11 @@ { "name": "tasmlib_verifier_stark_verify_inner_padded_height_512_fri_exp_4", "benchmark_result": { - "clock_cycle_count": 457442, - "hash_table_height": 182743, - "u32_table_height": 23128, - "op_stack_table_height": 644954, - "ram_table_height": 493245 + "clock_cycle_count": 448437, + "hash_table_height": 182995, + "u32_table_height": 22603, + "op_stack_table_height": 551742, + "ram_table_height": 495866 }, "case": "CommonCase" } diff --git a/tasm-lib/benchmarks/tasmlib_verifier_verify_authentication_paths_for_leaf_and_index_list.json b/tasm-lib/benchmarks/tasmlib_verifier_verify_authentication_paths_for_leaf_and_index_list.json index 5c682642..de9c1be1 100644 --- a/tasm-lib/benchmarks/tasmlib_verifier_verify_authentication_paths_for_leaf_and_index_list.json +++ b/tasm-lib/benchmarks/tasmlib_verifier_verify_authentication_paths_for_leaf_and_index_list.json @@ -2,10 +2,10 @@ { "name": "tasmlib_verifier_verify_authentication_paths_for_leaf_and_index_list", "benchmark_result": { - "clock_cycle_count": 8195, + "clock_cycle_count": 7395, "hash_table_height": 4872, "u32_table_height": 5, - "op_stack_table_height": 13463, + "op_stack_table_height": 5463, "ram_table_height": 321 }, "case": "CommonCase" @@ -13,10 +13,10 @@ { "name": "tasmlib_verifier_verify_authentication_paths_for_leaf_and_index_list", "benchmark_result": { - "clock_cycle_count": 13795, + "clock_cycle_count": 12195, "hash_table_height": 9672, "u32_table_height": 6, - "op_stack_table_height": 24663, + "op_stack_table_height": 8663, "ram_table_height": 321 }, "case": "WorstCase" diff --git a/tasm-lib/src/hashing.rs b/tasm-lib/src/hashing.rs index a0ed720d..7d18b4fb 100644 --- a/tasm-lib/src/hashing.rs +++ b/tasm-lib/src/hashing.rs @@ -1,9 +1,9 @@ pub mod absorb_multiple; pub mod absorb_multiple_static_size; pub mod algebraic_hasher; -pub mod divine_sibling_u64_index; pub mod eq_digest; pub mod merkle_root; +pub mod merkle_step_u64_index; pub mod merkle_verify; pub mod reverse_digest; pub mod sponge_hasher; diff --git a/tasm-lib/src/hashing/divine_sibling_u64_index.rs b/tasm-lib/src/hashing/merkle_step_u64_index.rs similarity index 68% rename from tasm-lib/src/hashing/divine_sibling_u64_index.rs rename to tasm-lib/src/hashing/merkle_step_u64_index.rs index 28d6b1ec..a1884c04 100644 --- a/tasm-lib/src/hashing/divine_sibling_u64_index.rs +++ b/tasm-lib/src/hashing/merkle_step_u64_index.rs @@ -12,73 +12,56 @@ use crate::snippet_bencher::BenchmarkCase; use crate::traits::basic_snippet::BasicSnippet; use crate::traits::procedure::Procedure; use crate::traits::procedure::ProcedureInitialState; +use crate::twenty_first::prelude::AlgebraicHasher; -/// `divine_sibling` but for index of type `u64` +/// `merkle_step` but for index of type `u64` #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] -pub struct DivineSiblingU64Index; +pub struct MerkleStepU64Index; -impl BasicSnippet for DivineSiblingU64Index { +impl BasicSnippet for MerkleStepU64Index { fn inputs(&self) -> Vec<(DataType, String)> { vec![ - (DataType::U64, "Mekle tree index".to_owned()), - (DataType::Digest, "previous digest".to_owned()), + (DataType::U64, "Merkle tree node index".to_owned()), + (DataType::Digest, "node digest".to_owned()), ] } fn outputs(&self) -> Vec<(DataType, String)> { vec![ - (DataType::U64, "Parent Mekle tree index".to_owned()), - (DataType::Digest, "Right child digest".to_owned()), - (DataType::Digest, "Left child digest".to_owned()), + (DataType::U64, "Merkle tree node index".to_owned()), + (DataType::Digest, "parent digest".to_owned()), ] } fn entrypoint(&self) -> String { - "tasmlib_hashing_divine_sibling_u64_index".to_owned() + "tasmlib_hashing_merkle_step_u64_index".to_owned() } fn code(&self, _library: &mut Library) -> Vec { - const TWO_POW_31: u32 = 1u32 << 31; - let entrypoint = self.entrypoint(); - triton_asm!( - {entrypoint}: - divine_sibling - - push 2 - push 0 - // _ mt_index_hi (mt_index_lo / 2) [digest (right_node)] [digest (left_node)] 2 0 - - swap 13 - // _ 0 (mt_index_lo / 2) [digest (right_node)] [digest (left_node)] 2 mt_index_hi - - div_mod - // _ 0 (mt_index_lo / 2) [digest (right_node)] [digest (left_node)] (mt_index_hi / 2) (mt_index_hi % 2) - - push {TWO_POW_31} - mul - // _ 0 (mt_index_lo / 2) [digest (right_node)] [digest (left_node)] (mt_index_hi / 2) carry - - swap 1 - swap 13 - // _ (mt_index_hi / 2) (mt_index_lo / 2) [digest (right_node)] [digest (left_node)] carry 0 - - swap 12 - // _ (mt_index_hi / 2) 0 [digest (right_node)] [digest (left_node)] carry (mt_index_lo / 2) - - add - // _ (mt_index_hi / 2) 0 [digest (right_node)] [digest (left_node)] (mt_index / 2)_lo - - swap 11 - pop 1 - // _ (mt_index_hi / 2) (mt_index / 2)_lo [digest (right_node)] [digest (left_node)] - + {self.entrypoint()}: + // _ mt_index_hi mt_index_lo [digest'; 5] + merkle_step // _ mt_index_hi (mt_index_lo / 2) [digest; 5] + push 2 // _ mt_index_hi (mt_index_lo / 2) [digest; 5] 2 + push 0 // _ mt_index_hi (mt_index_lo / 2) [digest; 5] 2 0 + swap 8 // _ 0 (mt_index_lo / 2) [digest; 5] 2 mt_index_hi + div_mod // _ 0 (mt_index_lo / 2) [digest; 5] (mt_index_hi / 2) (mt_index_hi % 2) + + push {1u32 << 31} + hint two_pow_31: u32 = stack[0] + mul // _ 0 (mt_index_lo / 2) [digest; 5] (mt_index_hi / 2) carry + swap 1 // _ 0 (mt_index_lo / 2) [digest; 5] carry (mt_index_hi / 2) + swap 8 // _ (mt_index / 2)_hi (mt_index_lo / 2) [digest; 5] carry 0 + swap 7 // _ (mt_index / 2)_hi 0 [digest; 5] carry (mt_index_lo / 2) + add // _ (mt_index / 2)_hi 0 [digest; 5] (mt_index / 2)_lo + swap 6 // _ (mt_index / 2)_hi (mt_index / 2)_lo [digest; 5] 0 + pop 1 // _ (mt_index / 2)_hi (mt_index / 2)_lo [digest; 5] return ) } } -impl Procedure for DivineSiblingU64Index { +impl Procedure for MerkleStepU64Index { fn rust_shadow( &self, stack: &mut Vec, @@ -99,18 +82,19 @@ impl Procedure for DivineSiblingU64Index { let leaf_index_lo: u32 = stack.pop().unwrap().try_into().unwrap(); let leaf_index_hi: u32 = stack.pop().unwrap().try_into().unwrap(); let leaf_index: u64 = ((leaf_index_hi as u64) << 32) | (leaf_index_lo as u64); - let (left_digest, right_digest) = if leaf_index % 2 == 1 { - (ap_digest, stack_digest) - } else { + let stack_digest_is_left_sibling = leaf_index % 2 == 0; + let (left_digest, right_digest) = if stack_digest_is_left_sibling { (stack_digest, ap_digest) + } else { + (ap_digest, stack_digest) }; + let parent_digest = Tip5::hash_pair(left_digest, right_digest); let parent_index = leaf_index / 2; stack.push(BFieldElement::new(parent_index >> 32)); stack.push(BFieldElement::new(parent_index & u32::MAX as u64)); - push_encodable(stack, &right_digest); - push_encodable(stack, &left_digest); + push_encodable(stack, &parent_digest); vec![] } @@ -137,7 +121,7 @@ impl Procedure for DivineSiblingU64Index { } } -impl DivineSiblingU64Index { +impl MerkleStepU64Index { fn prepare_stack_and_non_determinism( &self, leaf_index: u64, @@ -171,7 +155,7 @@ mod tests { #[test] fn prop() { for _ in 0..10 { - ShadowedProcedure::new(DivineSiblingU64Index).test(); + ShadowedProcedure::new(MerkleStepU64Index).test(); } } @@ -184,9 +168,9 @@ mod tests { prop(u64::MAX, (1u64 << 63) - 1); fn prop(mt_index: u64, expected_parent_index: u64) { - let shadowed_procedure = ShadowedProcedure::new(DivineSiblingU64Index); + let shadowed_procedure = ShadowedProcedure::new(MerkleStepU64Index); let (init_stack, non_determinism) = - DivineSiblingU64Index.prepare_stack_and_non_determinism(mt_index); + MerkleStepU64Index.prepare_stack_and_non_determinism(mt_index); let tasm_final_state = crate::test_helpers::tasm_final_state( &shadowed_procedure, &init_stack, @@ -201,8 +185,8 @@ mod tests { let mut final_stack = tasm_final_state.op_stack.stack; - // Pop and ignore digests - for _ in 0..2 * DIGEST_LENGTH { + // Pop and ignore digest + for _ in 0..DIGEST_LENGTH { final_stack.pop(); } diff --git a/tasm-lib/src/hashing/merkle_verify.rs b/tasm-lib/src/hashing/merkle_verify.rs index 51e1eed1..eaa0a77d 100644 --- a/tasm-lib/src/hashing/merkle_verify.rs +++ b/tasm-lib/src/hashing/merkle_verify.rs @@ -72,7 +72,7 @@ impl BasicSnippet for MerkleVerify { // AFTER: _ 1 [root'; 5] {traverse_tree}: dup 5 push 1 eq skiz return // break loop if node_index is 1 - divine_sibling hash recurse // move up one level in the Merkle tree + merkle_step recurse // move up one level in the Merkle tree ) } } @@ -113,6 +113,7 @@ mod tests { let allowed_error_codes = match i { 0 => { + println!("now testing: too high height"); init_state.nondeterminism.digests.push(random()); init_state.stack[len - 1].increment(); // height vec![ @@ -121,6 +122,7 @@ mod tests { ] } 1 => { + println!("now testing: too small height"); init_state.nondeterminism.digests.push(random()); init_state.stack[len - 1].decrement(); // height vec![ @@ -129,18 +131,22 @@ mod tests { ] } 2 => { + println!("now testing: corrupt leaf"); init_state.stack[len - 2].increment(); // leaf vec![InstructionError::VectorAssertionFailed(0)] } 3 => { + println!("now testing: too high leaf index"); init_state.stack[len - 6].increment(); // leaf index vec![InstructionError::VectorAssertionFailed(0)] } 4 => { + println!("now testing: too small leaf index"); init_state.stack[len - 6].decrement(); // leaf index vec![InstructionError::VectorAssertionFailed(0)] } 5 => { + println!("now testing: corrupt root"); init_state.stack[len - 7].increment(); // root vec![InstructionError::VectorAssertionFailed(0)] } @@ -206,7 +212,7 @@ mod tests { let tree_height = match maybe_bench_case { Some(BenchmarkCase::CommonCase) => 6, Some(BenchmarkCase::WorstCase) => 20, - None => rng.gen_range(0..20), + None => rng.gen_range(1..20), }; // sample unconstrained inputs directly diff --git a/tasm-lib/src/mmr/verify_from_secret_in_leaf_index_on_stack.rs b/tasm-lib/src/mmr/verify_from_secret_in_leaf_index_on_stack.rs index 0ab84ace..60c6c4f9 100644 --- a/tasm-lib/src/mmr/verify_from_secret_in_leaf_index_on_stack.rs +++ b/tasm-lib/src/mmr/verify_from_secret_in_leaf_index_on_stack.rs @@ -2,7 +2,7 @@ use triton_vm::prelude::*; use crate::arithmetic::u64::eq_u64::EqU64; use crate::data_type::DataType; -use crate::hashing::divine_sibling_u64_index::DivineSiblingU64Index; +use crate::hashing::merkle_step_u64_index::MerkleStepU64Index; use crate::library::Library; use crate::list::get::Get; use crate::traits::basic_snippet::BasicSnippet; @@ -41,7 +41,7 @@ impl BasicSnippet for MmrVerifyFromSecretInLeafIndexOnStack { let leaf_index_to_mt_index = library.import(Box::new(MmrLeafIndexToMtIndexAndPeakIndex)); let eq_u64 = library.import(Box::new(EqU64)); - let divine_digest_u64_index = library.import(Box::new(DivineSiblingU64Index)); + let merkle_step_u64_index = library.import(Box::new(MerkleStepU64Index)); let list_get = library.import(Box::new(Get::new(DataType::Digest))); let auth_path_loop_code = triton_asm!( @@ -52,12 +52,9 @@ impl BasicSnippet for MmrVerifyFromSecretInLeafIndexOnStack { skiz return // __ mt_index_hi mt_index_lo [acc_hash] - // read next auth path element from secret in, and calculate Merkle tree parent index - call {divine_digest_u64_index} + // move up one layer in the Merkle tree + call {merkle_step_u64_index} - // __ (mt_index / 2)_hi (mt_index / 2)_lo [left_digest] [right_digest] - - hash // _ (mt_index / 2)_hi (mt_index / 2)_lo [digest (acc_hash)] recurse diff --git a/tasm-lib/src/mmr/verify_from_secret_in_secret_leaf_index.rs b/tasm-lib/src/mmr/verify_from_secret_in_secret_leaf_index.rs index c1502898..cb90eb8d 100644 --- a/tasm-lib/src/mmr/verify_from_secret_in_secret_leaf_index.rs +++ b/tasm-lib/src/mmr/verify_from_secret_in_secret_leaf_index.rs @@ -2,7 +2,7 @@ use triton_vm::prelude::*; use crate::arithmetic::u64::eq_u64::EqU64; use crate::data_type::DataType; -use crate::hashing::divine_sibling_u64_index::DivineSiblingU64Index; +use crate::hashing::merkle_step_u64_index::MerkleStepU64Index; use crate::library::Library; use crate::list::get::Get; use crate::traits::basic_snippet::BasicSnippet; @@ -42,7 +42,7 @@ impl BasicSnippet for MmrVerifyFromSecretInSecretLeafIndex { let leaf_index_to_mt_index = library.import(Box::new(MmrLeafIndexToMtIndexAndPeakIndex)); let eq_u64 = library.import(Box::new(EqU64)); - let divine_digest_u64_index = library.import(Box::new(DivineSiblingU64Index)); + let merkle_step_u64_index = library.import(Box::new(MerkleStepU64Index)); let list_get = library.import(Box::new(Get::new(DataType::Digest))); // BEFORE: _ *peaks leaf_count_hi leaf_count_lo [digest (leaf_digest)] @@ -95,12 +95,9 @@ impl BasicSnippet for MmrVerifyFromSecretInSecretLeafIndex { skiz return // __ mt_index_hi mt_index_lo [digest (acc_hash)] - // read next auth path element from secret in, and calculate Merkle tree parent index - call {divine_digest_u64_index} + // move up one layer in the Merkle tree + call {merkle_step_u64_index} - // __ (mt_index / 2)_hi (mt_index / 2)_lo [left_digest] [right_digest] - - hash // _ mt_index_hi (mt_index_lo / 2) [digest (acc_hash)] recurse diff --git a/tasm-lib/src/verifier/master_ext_table/verify_table_rows.rs b/tasm-lib/src/verifier/master_ext_table/verify_table_rows.rs index 751c122d..3c0648af 100644 --- a/tasm-lib/src/verifier/master_ext_table/verify_table_rows.rs +++ b/tasm-lib/src/verifier/master_ext_table/verify_table_rows.rs @@ -79,7 +79,7 @@ impl BasicSnippet for VerifyTableRows { let loop_over_auth_paths_code = triton_asm!( {loop_over_auth_path_digests_label}: dup 5 push 1 eq skiz return // break loop if node_index is 1 - divine_sibling hash recurse // move up one level in the Merkle tree + merkle_step recurse // move up one level in the Merkle tree ); let loop_over_rows_label = format!("{entrypoint}_loop_over_rows"); diff --git a/tasm-lib/src/verifier/verify_authentication_paths_for_leaf_and_index_list.rs b/tasm-lib/src/verifier/verify_authentication_paths_for_leaf_and_index_list.rs index bcb7d4d7..f91617e9 100644 --- a/tasm-lib/src/verifier/verify_authentication_paths_for_leaf_and_index_list.rs +++ b/tasm-lib/src/verifier/verify_authentication_paths_for_leaf_and_index_list.rs @@ -61,7 +61,7 @@ impl BasicSnippet for VerifyAuthenticationPathForLeafAndIndexList { let loop_over_auth_paths_code = triton_asm!( {loop_over_auth_paths_label}: dup 5 push 1 eq skiz return // break loop if node_index is 1 - divine_sibling hash recurse // move up one level in the Merkle tree + merkle_step recurse // move up one level in the Merkle tree ); const LIST_ELEMENT_SIZE: usize = EXTENSION_DEGREE + 1;