Skip to content

Commit 8efe4f3

Browse files
committed
signoff: xfe::to_the_power_of_power_of_2
1 parent c35e889 commit 8efe4f3

7 files changed

+78
-74
lines changed

tasm-lib/benchmarks/tasmlib_arithmetic_xfe_to_the_power_of_power_of_2.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
{
33
"name": "tasmlib_arithmetic_xfe_to_the_power_of_power_of_2",
44
"benchmark_result": {
5-
"clock_cycle_count": 273,
5+
"clock_cycle_count": 251,
66
"hash_table_height": 24,
77
"u32_table_height": 0,
8-
"op_stack_table_height": 245,
8+
"op_stack_table_height": 205,
99
"ram_table_height": 0
1010
},
1111
"case": "CommonCase"
1212
},
1313
{
1414
"name": "tasmlib_arithmetic_xfe_to_the_power_of_power_of_2",
1515
"benchmark_result": {
16-
"clock_cycle_count": 416,
16+
"clock_cycle_count": 383,
1717
"hash_table_height": 24,
1818
"u32_table_height": 0,
19-
"op_stack_table_height": 377,
19+
"op_stack_table_height": 315,
2020
"ram_table_height": 0
2121
},
2222
"case": "WorstCase"

tasm-lib/benchmarks/tasmlib_verifier_master_table_divide_out_zerofiers.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
{
33
"name": "tasmlib_verifier_master_table_divide_out_zerofiers",
44
"benchmark_result": {
5-
"clock_cycle_count": 7473,
5+
"clock_cycle_count": 7451,
66
"hash_table_height": 8298,
77
"u32_table_height": 22,
8-
"op_stack_table_height": 8639,
8+
"op_stack_table_height": 8599,
99
"ram_table_height": 5379
1010
},
1111
"case": "CommonCase"
1212
},
1313
{
1414
"name": "tasmlib_verifier_master_table_divide_out_zerofiers",
1515
"benchmark_result": {
16-
"clock_cycle_count": 7473,
16+
"clock_cycle_count": 7451,
1717
"hash_table_height": 8298,
1818
"u32_table_height": 22,
19-
"op_stack_table_height": 8639,
19+
"op_stack_table_height": 8599,
2020
"ram_table_height": 5379
2121
},
2222
"case": "WorstCase"

tasm-lib/benchmarks/tasmlib_verifier_stark_verify_dynamic_inner_padded_height_256_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_256_fri_exp_4",
44
"benchmark_result": {
5-
"clock_cycle_count": 188458,
6-
"hash_table_height": 138907,
5+
"clock_cycle_count": 188448,
6+
"hash_table_height": 138901,
77
"u32_table_height": 25078,
8-
"op_stack_table_height": 167396,
8+
"op_stack_table_height": 167380,
99
"ram_table_height": 289169
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": 195969,
6-
"hash_table_height": 146599,
5+
"clock_cycle_count": 195958,
6+
"hash_table_height": 146593,
77
"u32_table_height": 33679,
8-
"op_stack_table_height": 172110,
8+
"op_stack_table_height": 172092,
99
"ram_table_height": 290320
1010
},
1111
"case": "CommonCase"

tasm-lib/benchmarks/tasmlib_verifier_stark_verify_static_inner_padded_height_256_fri_exp_4.json

Lines changed: 2 additions & 2 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": 176755,
5+
"clock_cycle_count": 176745,
66
"hash_table_height": 124861,
77
"u32_table_height": 25078,
8-
"op_stack_table_height": 159590,
8+
"op_stack_table_height": 159574,
99
"ram_table_height": 285270
1010
},
1111
"case": "CommonCase"

tasm-lib/benchmarks/tasmlib_verifier_stark_verify_static_inner_padded_height_512_fri_exp_4.json

Lines changed: 2 additions & 2 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": 184266,
5+
"clock_cycle_count": 184255,
66
"hash_table_height": 132553,
77
"u32_table_height": 33679,
8-
"op_stack_table_height": 164304,
8+
"op_stack_table_height": 164286,
99
"ram_table_height": 286421
1010
},
1111
"case": "CommonCase"

tasm-lib/src/arithmetic/xfe/to_the_power_of_power_of_2.rs

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
1+
use std::collections::HashMap;
2+
13
use triton_vm::prelude::*;
24

35
use crate::prelude::*;
4-
6+
use crate::traits::basic_snippet::Reviewer;
7+
use crate::traits::basic_snippet::SignOffFingerprint;
8+
9+
/// ### Behavior
10+
///
11+
/// ```text
12+
/// BEFORE: _ [exp: u32] [base: XFieldElement]
13+
/// AFTER: _ [base^(2^exp): XFieldElement]
14+
/// ```
15+
///
16+
/// ### Preconditions
17+
///
18+
/// - all input arguments are properly [`BFieldCodec`] encoded
19+
///
20+
/// ### Postconditions
21+
///
22+
/// None.
23+
//
24+
// todo: Explain the precondition. It seems to be a bit arbitrary. The tests
25+
// only check `exp` in range 0..32, while the assembly should work fine with
26+
// any number.
527
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
628
pub struct ToThePowerOfPowerOf2;
729

@@ -21,11 +43,19 @@ impl BasicSnippet for ToThePowerOfPowerOf2 {
2143
"tasmlib_arithmetic_xfe_to_the_power_of_power_of_2".to_owned()
2244
}
2345

24-
fn code(&self, _library: &mut Library) -> Vec<LabelledInstruction> {
46+
fn code(&self, _: &mut Library) -> Vec<LabelledInstruction> {
2547
let entrypoint = self.entrypoint();
26-
2748
let loop_label = format!("{}_loop", entrypoint);
28-
let loop_code = triton_asm!(
49+
50+
triton_asm!(
51+
{entrypoint}:
52+
call {loop_label}
53+
54+
pick 3
55+
pop 1
56+
57+
return
58+
2959
// INVARIANT: _ remainder [acc]
3060
{loop_label}:
3161
// end condition: remainder == 0
@@ -42,30 +72,20 @@ impl BasicSnippet for ToThePowerOfPowerOf2 {
4272
xx_mul
4373
// _ remainder [acc^2]
4474

45-
swap 3
46-
push -1
47-
add
48-
swap 3
75+
pick 3
76+
addi -1
77+
place 3
4978
// _ (remainder - 1) [acc^2]
5079

5180
recurse
52-
53-
);
54-
55-
triton_asm!(
56-
{entrypoint}:
57-
call {loop_label}
58-
59-
swap 1
60-
swap 2
61-
swap 3
62-
pop 1
63-
64-
return
65-
66-
{&loop_code}
6781
)
6882
}
83+
84+
fn sign_offs(&self) -> HashMap<Reviewer, SignOffFingerprint> {
85+
let mut sign_offs = HashMap::new();
86+
sign_offs.insert(Reviewer("ferdinand"), 0x7768250498bfbb26.into());
87+
sign_offs
88+
}
6989
}
7090

7191
#[cfg(test)]
@@ -81,7 +101,7 @@ mod tests {
81101

82102
fn rust_shadow(&self, stack: &mut Vec<BFieldElement>) {
83103
let (exponent_log_2, base) = pop_encodable::<Self::Args>(stack);
84-
let result = base.mod_pow_u32(2u32.pow(exponent_log_2));
104+
let result = base.mod_pow_u32(2_u32.pow(exponent_log_2));
85105
push_encodable(stack, &result);
86106
}
87107

@@ -109,56 +129,40 @@ mod tests {
109129
}
110130

111131
#[test]
112-
fn to_the_power_of_power_of_2_pbt() {
132+
fn rust_shadow() {
113133
ShadowedClosure::new(ToThePowerOfPowerOf2).test()
114134
}
115135

116136
#[test]
117137
fn compare_to_generic_pow_u32() {
118-
let base: XFieldElement = random();
138+
let base = random();
119139
for log_2_exponent in 0..32 {
120-
let init_stack_pow_pow = [
121-
ToThePowerOfPowerOf2.init_stack_for_isolated_run(),
122-
vec![BFieldElement::new(log_2_exponent)],
123-
base.coefficients.into_iter().rev().collect_vec(),
124-
]
125-
.concat();
126-
let final_state_pow_pow = test_rust_equivalence_given_complete_state(
140+
let final_pow_pow_stack = test_rust_equivalence_given_complete_state(
127141
&ShadowedClosure::new(ToThePowerOfPowerOf2),
128-
&init_stack_pow_pow,
142+
&ToThePowerOfPowerOf2.set_up_test_stack((log_2_exponent, base)),
129143
&[],
130144
&NonDeterminism::default(),
131145
&None,
132146
None,
133-
);
134-
135-
// Run snippet for generic pow
136-
let init_stack_to_generic = [
137-
XfeModPowU32Generic.init_stack_for_isolated_run(),
138-
vec![BFieldElement::new(
139-
2u64.pow(log_2_exponent.try_into().unwrap()),
140-
)],
141-
base.coefficients.into_iter().rev().collect_vec(),
142-
]
143-
.concat();
144-
let final_state_from_generic = test_rust_equivalence_given_complete_state(
147+
)
148+
.op_stack
149+
.stack;
150+
151+
let final_generic_stack = test_rust_equivalence_given_complete_state(
145152
&ShadowedClosure::new(XfeModPowU32Generic),
146-
&init_stack_to_generic,
153+
&XfeModPowU32Generic.set_up_test_stack((2_u32.pow(log_2_exponent), base)),
147154
&[],
148155
&NonDeterminism::default(),
149156
&None,
150157
None,
151-
);
158+
)
159+
.op_stack
160+
.stack;
152161

153162
// Assert that height agrees, and the top-3 elements agree
154-
assert_eq!(
155-
final_state_from_generic.op_stack.stack.len(),
156-
final_state_pow_pow.op_stack.stack.len()
157-
);
158-
assert_eq!(
159-
final_state_from_generic.op_stack.stack[16..=18],
160-
final_state_pow_pow.op_stack.stack[16..=18],
161-
);
163+
assert_eq!(19, final_pow_pow_stack.len());
164+
assert_eq!(19, final_generic_stack.len());
165+
assert_eq!(final_pow_pow_stack[16..=18], final_generic_stack[16..=18],);
162166
}
163167
}
164168
}

0 commit comments

Comments
 (0)