Skip to content

Commit 5c3388a

Browse files
committed
perf(MerkleRoot): Simplify power-of-2 check
1 parent 1026308 commit 5c3388a

8 files changed

+44
-55
lines changed

tasm-lib/benchmarks/tasmlib_hashing_merkle_root.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
{
33
"name": "tasmlib_hashing_merkle_root",
44
"benchmark_result": {
5-
"clock_cycle_count": 4887,
6-
"hash_table_height": 3162,
7-
"u32_table_height": 48,
8-
"op_stack_table_height": 10472,
5+
"clock_cycle_count": 4879,
6+
"hash_table_height": 3156,
7+
"u32_table_height": 43,
8+
"op_stack_table_height": 10464,
99
"ram_table_height": 7673
1010
},
1111
"case": "CommonCase"
1212
},
1313
{
1414
"name": "tasmlib_hashing_merkle_root",
1515
"benchmark_result": {
16-
"clock_cycle_count": 9520,
17-
"hash_table_height": 6234,
18-
"u32_table_height": 49,
19-
"op_stack_table_height": 20734,
16+
"clock_cycle_count": 9512,
17+
"hash_table_height": 6228,
18+
"u32_table_height": 44,
19+
"op_stack_table_height": 20726,
2020
"ram_table_height": 15353
2121
},
2222
"case": "WorstCase"

tasm-lib/benchmarks/tasmlib_hashing_merkle_root_from_xfes.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
{
33
"name": "tasmlib_hashing_merkle_root_from_xfes",
44
"benchmark_result": {
5-
"clock_cycle_count": 7236,
6-
"hash_table_height": 3234,
7-
"u32_table_height": 104,
8-
"op_stack_table_height": 12046,
5+
"clock_cycle_count": 7228,
6+
"hash_table_height": 3228,
7+
"u32_table_height": 99,
8+
"op_stack_table_height": 12038,
99
"ram_table_height": 6653
1010
},
1111
"case": "CommonCase"
1212
},
1313
{
1414
"name": "tasmlib_hashing_merkle_root_from_xfes",
1515
"benchmark_result": {
16-
"clock_cycle_count": 14173,
17-
"hash_table_height": 6306,
18-
"u32_table_height": 107,
19-
"op_stack_table_height": 23844,
16+
"clock_cycle_count": 14165,
17+
"hash_table_height": 6300,
18+
"u32_table_height": 102,
19+
"op_stack_table_height": 23836,
2020
"ram_table_height": 13309
2121
},
2222
"case": "WorstCase"

tasm-lib/benchmarks/tasmlib_verifier_fri_verify.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
{
33
"name": "tasmlib_verifier_fri_verify",
44
"benchmark_result": {
5-
"clock_cycle_count": 43515,
6-
"hash_table_height": 14670,
7-
"u32_table_height": 18371,
8-
"op_stack_table_height": 40829,
5+
"clock_cycle_count": 43507,
6+
"hash_table_height": 14658,
7+
"u32_table_height": 18381,
8+
"op_stack_table_height": 40821,
99
"ram_table_height": 17564
1010
},
1111
"case": "CommonCase"
1212
},
1313
{
1414
"name": "tasmlib_verifier_fri_verify",
1515
"benchmark_result": {
16-
"clock_cycle_count": 43515,
17-
"hash_table_height": 14670,
18-
"u32_table_height": 17762,
19-
"op_stack_table_height": 40829,
16+
"clock_cycle_count": 43507,
17+
"hash_table_height": 14658,
18+
"u32_table_height": 17772,
19+
"op_stack_table_height": 40821,
2020
"ram_table_height": 17564
2121
},
2222
"case": "WorstCase"

tasm-lib/benchmarks/tasmlib_verifier_stark_verify_dynamic_inner_padded_height_256_fri_exp_4.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
{
33
"name": "tasmlib_verifier_stark_verify_dynamic_inner_padded_height_256_fri_exp_4",
44
"benchmark_result": {
5-
"clock_cycle_count": 187678,
6-
"hash_table_height": 138835,
7-
"u32_table_height": 25449,
8-
"op_stack_table_height": 165836,
5+
"clock_cycle_count": 187670,
6+
"hash_table_height": 138829,
7+
"u32_table_height": 25459,
8+
"op_stack_table_height": 165828,
99
"ram_table_height": 289170
1010
},
1111
"case": "CommonCase"

tasm-lib/benchmarks/tasmlib_verifier_stark_verify_dynamic_inner_padded_height_512_fri_exp_4.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
{
33
"name": "tasmlib_verifier_stark_verify_dynamic_inner_padded_height_512_fri_exp_4",
44
"benchmark_result": {
5-
"clock_cycle_count": 195182,
6-
"hash_table_height": 146527,
5+
"clock_cycle_count": 195174,
6+
"hash_table_height": 146521,
77
"u32_table_height": 34071,
8-
"op_stack_table_height": 170540,
8+
"op_stack_table_height": 170532,
99
"ram_table_height": 290321
1010
},
1111
"case": "CommonCase"

tasm-lib/benchmarks/tasmlib_verifier_stark_verify_static_inner_padded_height_256_fri_exp_4.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
{
33
"name": "tasmlib_verifier_stark_verify_static_inner_padded_height_256_fri_exp_4",
44
"benchmark_result": {
5-
"clock_cycle_count": 175975,
6-
"hash_table_height": 124789,
7-
"u32_table_height": 25449,
8-
"op_stack_table_height": 158030,
5+
"clock_cycle_count": 175967,
6+
"hash_table_height": 124783,
7+
"u32_table_height": 25459,
8+
"op_stack_table_height": 158022,
99
"ram_table_height": 285271
1010
},
1111
"case": "CommonCase"

tasm-lib/benchmarks/tasmlib_verifier_stark_verify_static_inner_padded_height_512_fri_exp_4.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
{
33
"name": "tasmlib_verifier_stark_verify_static_inner_padded_height_512_fri_exp_4",
44
"benchmark_result": {
5-
"clock_cycle_count": 183479,
6-
"hash_table_height": 132481,
5+
"clock_cycle_count": 183471,
6+
"hash_table_height": 132475,
77
"u32_table_height": 34071,
8-
"op_stack_table_height": 162734,
8+
"op_stack_table_height": 162726,
99
"ram_table_height": 286422
1010
},
1111
"case": "CommonCase"

tasm-lib/src/hashing/merkle_root.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use crate::twenty_first::prelude::Inverse;
3434
pub struct MerkleRoot;
3535

3636
impl MerkleRoot {
37-
pub const ZERO_LEAFS_ERROR_ID: i128 = 430;
3837
pub const NUM_LEAFS_NOT_POWER_OF_2_ERROR_ID: i128 = 431;
3938
}
4039

@@ -69,23 +68,13 @@ impl BasicSnippet for MerkleRoot {
6968
addi 1
7069
// _ leafs_len *leafs
7170

72-
/* assert the number of leafs is reasonable: some power of 2, not 0 */
71+
/* assert the number of leafs is some power of 2 */
7372
dup 1
74-
push 0
75-
eq
76-
push 0
77-
eq
78-
assert error_id {Self::ZERO_LEAFS_ERROR_ID}
79-
80-
dup 1 // _ leafs_len *leafs leafs_len
81-
log_2_floor
82-
push 2
83-
pow // _ leafs_len *leafs (2^⌊log₂(leafs_len)⌋)
84-
dup 2
73+
pop_count
74+
push 1
8575
eq
8676
assert error_id {Self::NUM_LEAFS_NOT_POWER_OF_2_ERROR_ID}
8777

88-
8978
call {dyn_malloc}
9079
// _ leafs_len *leafs *parent_level
9180

@@ -210,7 +199,7 @@ impl BasicSnippet for MerkleRoot {
210199

211200
fn sign_offs(&self) -> HashMap<Reviewer, SignOffFingerprint> {
212201
let mut sign_offs = HashMap::new();
213-
sign_offs.insert(Reviewer("ferdinand"), 0x2f0791d17407d7dc.into());
202+
sign_offs.insert(Reviewer("ferdinand"), 0x1c30ac983fdca9da.into());
214203
sign_offs
215204
}
216205
}
@@ -296,13 +285,13 @@ mod tests {
296285
test_assertion_failure(
297286
&ShadowedFunction::new(MerkleRoot),
298287
MerkleRoot.init_state(vec![], bfe!(0)).into(),
299-
&[MerkleRoot::ZERO_LEAFS_ERROR_ID],
288+
&[MerkleRoot::NUM_LEAFS_NOT_POWER_OF_2_ERROR_ID],
300289
);
301290
}
302291

303292
#[proptest(cases = 100)]
304293
fn computing_root_of_tree_of_height_not_power_of_2_crashes_vm(
305-
#[strategy(vec(arb(), 1..2048))]
294+
#[strategy(vec(arb(), 0..2048))]
306295
#[filter(!#leafs.len().is_power_of_two())]
307296
leafs: Vec<Digest>,
308297
#[strategy(arb())] address: BFieldElement,

0 commit comments

Comments
 (0)