Skip to content

Commit eb53c84

Browse files
committed
refactor!: Remove trait DeprecatedSnippet
Remove the trait `DeprecatedSnippet` and all infrastructure surrounding it. Use `BasicSnippet` and the infrastructure surrounding _it_ instead. 💃🕺🕺🕺💃🕺💃🕺💃💃💃🕺💃💃🕺🕺🕺💃💃🕺💃💃💃🕺💃💃🕺💃💃💃🕺💃 💃🕺💃💃💃🕺💃🕺🕺💃💃🕺💃💃🕺💃🕺💃💃🕺💃💃💃🕺💃💃🕺💃💃💃🕺💃 💃🕺🕺💃💃🕺💃🕺💃🕺💃🕺💃🕺🕺🕺🕺🕺💃🕺💃💃💃🕺💃💃💃🕺🕺🕺💃💃 💃🕺💃💃💃🕺💃🕺💃💃🕺🕺💃🕺💃💃💃🕺💃🕺💃💃💃🕺💃💃💃💃🕺💃💃💃 💃🕺💃💃💃🕺💃🕺💃💃💃🕺💃🕺💃💃💃🕺💃🕺🕺🕺💃🕺🕺🕺💃💃🕺💃💃💃
1 parent c05a897 commit eb53c84

File tree

9 files changed

+61
-920
lines changed

9 files changed

+61
-920
lines changed

tasm-lib/src/lib.rs

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@ use std::collections::HashMap;
1616
use std::io::Write;
1717
use std::time::SystemTime;
1818

19-
use anyhow::bail;
2019
use itertools::Itertools;
2120
use memory::dyn_malloc;
2221
use num_traits::Zero;
23-
use snippet_bencher::BenchmarkResult;
2422
use triton_vm::isa::op_stack::NUM_OP_STACK_REGISTERS;
2523
use triton_vm::prelude::*;
2624

@@ -119,54 +117,6 @@ pub(crate) fn pop_encodable<T: BFieldCodec>(stack: &mut Vec<BFieldElement>) -> T
119117
*T::decode(&limbs).unwrap()
120118
}
121119

122-
/// Execute a Triton-VM program and return its output and execution trace length
123-
pub fn execute_bench_deprecated(
124-
code: &[LabelledInstruction],
125-
stack: &mut Vec<BFieldElement>,
126-
expected_stack_diff: isize,
127-
std_in: Vec<BFieldElement>,
128-
nondeterminism: NonDeterminism,
129-
) -> anyhow::Result<BenchmarkResult> {
130-
let init_stack = stack.to_owned();
131-
let initial_stack_height = init_stack.len() as isize;
132-
let public_input = PublicInput::new(std_in.clone());
133-
let program = Program::new(code);
134-
135-
let mut vm_state = VMState::new(program.clone(), public_input, nondeterminism.clone());
136-
vm_state.op_stack.stack.clone_from(&init_stack);
137-
138-
let (simulation_trace, terminal_state) = VM::trace_execution_of_state(vm_state)?;
139-
140-
let jump_stack = &terminal_state.jump_stack;
141-
if !jump_stack.is_empty() {
142-
bail!("Jump stack must be unchanged after code execution but was {jump_stack:?}")
143-
}
144-
145-
let final_stack_height = terminal_state.op_stack.stack.len() as isize;
146-
if expected_stack_diff != final_stack_height - initial_stack_height {
147-
bail!(
148-
"Code must grow/shrink stack with expected number of elements.\n
149-
init height: {initial_stack_height}\n
150-
end height: {final_stack_height}\n
151-
expected difference: {expected_stack_diff}\n\n
152-
final stack: {}",
153-
terminal_state.op_stack.stack.iter().join(",")
154-
)
155-
}
156-
157-
// If this environment variable is set, all programs, including the code to prepare the state,
158-
// will be proven and then verified.
159-
// Notice that this is only done after the successful execution of the program above, so all
160-
// produced proofs here should be valid.
161-
// If you run this, make sure `opt-level` is set to 3.
162-
if std::env::var("DYING_TO_PROVE").is_ok() {
163-
prove_and_verify(program, &std_in, &nondeterminism, Some(init_stack));
164-
}
165-
166-
stack.clone_from(&terminal_state.op_stack.stack);
167-
Ok(BenchmarkResult::new(&simulation_trace))
168-
}
169-
170120
/// Execute a Triton-VM program and test correct behavior indicators.
171121
/// Modify stack and memory. Panic if anything goes wrong.
172122
pub(crate) fn execute_test(
@@ -375,8 +325,7 @@ pub fn generate_full_profile(
375325

376326
/// Glob-import this module to reduce the number of imports in a test module.
377327
///
378-
/// Feel free to add anything you frequently `use` in a test module. It is
379-
/// discouraged to add deprecated types or functions – get rid of them instead.
328+
/// Feel free to add anything you frequently `use` in a test module.
380329
#[cfg(test)]
381330
pub mod test_prelude {
382331
pub use std::collections::HashMap;

tasm-lib/src/list/higher_order/all.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use itertools::Itertools;
2-
use triton_vm::isa::parser::tokenize;
32
use triton_vm::prelude::*;
43

54
use super::inner_function::InnerFunction;
@@ -44,12 +43,6 @@ impl BasicSnippet for All {
4443

4544
let inner_function_name = match &self.f {
4645
InnerFunction::RawCode(rc) => rc.entrypoint(),
47-
InnerFunction::DeprecatedSnippet(sn) => {
48-
let fn_body = sn.function_code(library);
49-
let (_, instructions) = tokenize(&fn_body).unwrap();
50-
let labelled_instructions = isa::parser::to_labelled_instructions(&instructions);
51-
library.explicit_import(&sn.entrypoint_name(), &labelled_instructions)
52-
}
5346
InnerFunction::NoFunctionBody(_) => todo!(),
5447
InnerFunction::BasicSnippet(bs) => {
5548
let labelled_instructions = bs.annotated_code(library);
@@ -61,7 +54,6 @@ impl BasicSnippet for All {
6154
// body. Otherwise, `library` handles the imports.
6255
let maybe_inner_function_body_raw = match &self.f {
6356
InnerFunction::RawCode(rc) => rc.function.iter().map(|x| x.to_string()).join("\n"),
64-
InnerFunction::DeprecatedSnippet(_) => Default::default(),
6557
InnerFunction::NoFunctionBody(_) => todo!(),
6658
InnerFunction::BasicSnippet(_) => Default::default(),
6759
};

tasm-lib/src/list/higher_order/filter.rs

Lines changed: 48 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use itertools::Itertools;
2-
use triton_vm::isa::parser::tokenize;
32
use triton_vm::prelude::*;
43

54
use super::inner_function::InnerFunction;
@@ -52,12 +51,6 @@ impl BasicSnippet for Filter {
5251

5352
let inner_function_name = match &self.f {
5453
InnerFunction::RawCode(rc) => rc.entrypoint(),
55-
InnerFunction::DeprecatedSnippet(sn) => {
56-
let fn_body = sn.function_code(library);
57-
let (_, instructions) = tokenize(&fn_body).unwrap();
58-
let labelled_instructions = isa::parser::to_labelled_instructions(&instructions);
59-
library.explicit_import(&sn.entrypoint_name(), &labelled_instructions)
60-
}
6154
InnerFunction::NoFunctionBody(_) => todo!(),
6255
InnerFunction::BasicSnippet(bs) => {
6356
let labelled_instructions = bs.annotated_code(library);
@@ -71,7 +64,6 @@ impl BasicSnippet for Filter {
7164
// body. Otherwise, `library` handles the imports.
7265
let maybe_inner_function_body_raw = match &self.f {
7366
InnerFunction::RawCode(rc) => rc.function.iter().join("\n"),
74-
InnerFunction::DeprecatedSnippet(_) => String::default(),
7567
InnerFunction::NoFunctionBody(_) => todo!(),
7668
InnerFunction::BasicSnippet(_) => String::default(),
7769
};
@@ -167,7 +159,6 @@ mod tests {
167159
use crate::list::higher_order::inner_function::RawCode;
168160
use crate::rust_shadowing_helper_functions;
169161
use crate::test_prelude::*;
170-
use crate::traits::deprecated_snippet::DeprecatedSnippet;
171162

172163
impl Function for Filter {
173164
fn rust_shadow(
@@ -272,152 +263,63 @@ mod tests {
272263
#[derive(Debug, Clone)]
273264
pub struct TestHashXFieldElementLsb;
274265

275-
impl DeprecatedSnippet for TestHashXFieldElementLsb {
276-
fn entrypoint_name(&self) -> String {
277-
"test_hash_xfield_element_lsb".to_string()
266+
impl BasicSnippet for TestHashXFieldElementLsb {
267+
fn inputs(&self) -> Vec<(DataType, String)> {
268+
vec![(DataType::Xfe, "element".to_string())]
278269
}
279270

280-
fn input_field_names(&self) -> Vec<String>
281-
where
282-
Self: Sized,
283-
{
284-
vec![
285-
"elem2".to_string(),
286-
"elem1".to_string(),
287-
"elem0".to_string(),
288-
]
271+
fn outputs(&self) -> Vec<(DataType, String)> {
272+
vec![(DataType::Bool, "b".to_string())]
289273
}
290274

291-
fn input_types(&self) -> Vec<DataType> {
292-
vec![DataType::Xfe]
293-
}
294-
295-
fn output_field_names(&self) -> Vec<String>
296-
where
297-
Self: Sized,
298-
{
299-
vec!["bool".to_string()]
300-
}
301-
302-
fn output_types(&self) -> Vec<DataType> {
303-
vec![DataType::Bool]
304-
}
305-
306-
fn stack_diff(&self) -> isize
307-
where
308-
Self: Sized,
309-
{
310-
-2
275+
fn entrypoint(&self) -> String {
276+
"test_hash_xfield_element_lsb".to_string()
311277
}
312278

313-
fn function_code(&self, library: &mut Library) -> String {
314-
let entrypoint = self.entrypoint_name();
279+
fn code(&self, library: &mut Library) -> Vec<LabelledInstruction> {
280+
let entrypoint = self.entrypoint();
315281
let unused_import = library.import(Box::new(arithmetic::u32::safe_add::SafeAdd));
316-
format!(
317-
"
318-
// BEFORE: _ x2 x1 x0
319-
// AFTER: _ b
320-
{entrypoint}:
321-
// Useless additions, to ensure that dependencies are accepted inside the filter generated code
322-
push 0
323-
push 0
324-
call {unused_import}
325-
pop 1
326-
327-
push 0
328-
push 0
329-
push 0
330-
push 1 // _ x2 x1 x0 0 0 0 1
331-
push 0 swap 7 // _ 0 x1 x0 0 0 0 1 x2
332-
push 0 swap 7 // _ 0 0 x0 0 0 0 1 x2 x1
333-
push 0 swap 7 // _ 0 0 0 0 0 0 1 x2 x1 x0
334-
335-
sponge_init
336-
sponge_absorb
337-
sponge_squeeze // _ d9 d8 d7 d6 d5 d4 d3 d2 d1 d0
338-
swap 5 pop 1 // _ d9 d8 d7 d6 d0 d4 d3 d2 d1
339-
swap 5 pop 1 // _ d9 d8 d7 d1 d0 d4 d3 d2
340-
swap 5 pop 1
341-
swap 5 pop 1
342-
swap 5 pop 1
343-
344-
// _ d4 d3 d2 d1 d0
345-
346-
split // _ d4 d3 d2 d1 hi lo
347-
push 2 // _ d4 d3 d2 d1 hi lo 2
348-
swap 1
349-
div_mod // _ d4 d3 d2 d1 hi q r
350-
swap 6
351-
pop 5 pop 1
352-
return
353-
"
354-
)
355-
}
356-
357-
fn crash_conditions(&self) -> Vec<String>
358-
where
359-
Self: Sized,
360-
{
361-
vec![]
362-
}
363-
364-
fn gen_input_states(&self) -> Vec<InitVmState>
365-
where
366-
Self: Sized,
367-
{
368-
let mut stack = empty_stack();
369-
stack.extend(random::<[BFieldElement; 3]>());
370-
371-
vec![InitVmState::with_stack(stack)]
372-
}
373-
374-
fn common_case_input_state(&self) -> InitVmState
375-
where
376-
Self: Sized,
377-
{
378-
let mut stack = empty_stack();
379-
stack.extend(random::<[BFieldElement; 3]>());
380-
381-
InitVmState::with_stack(stack)
382-
}
383-
384-
fn worst_case_input_state(&self) -> InitVmState
385-
where
386-
Self: Sized,
387-
{
388-
let mut stack = empty_stack();
389-
stack.extend(random::<[BFieldElement; 3]>());
390-
391-
InitVmState::with_stack(stack)
392-
}
393-
394-
fn rust_shadowing(
395-
&self,
396-
stack: &mut Vec<BFieldElement>,
397-
_: Vec<BFieldElement>,
398-
_: Vec<BFieldElement>,
399-
_: &mut HashMap<BFieldElement, BFieldElement>,
400-
) where
401-
Self: Sized,
402-
{
403-
let mut xfield_element = vec![];
404-
for _ in 0..3 {
405-
xfield_element.push(stack.pop().unwrap());
406-
}
407-
let digest = Tip5::hash_varlen(&xfield_element).values().to_vec();
408-
let b = digest[0].value() % 2;
409-
stack.push(BFieldElement::new(b));
282+
triton_asm!(
283+
// BEFORE: _ x2 x1 x0
284+
// AFTER: _ b
285+
{entrypoint}:
286+
// Useless additions, to ensure that dependencies are accepted inside
287+
// the filter-generated code
288+
push 0
289+
push 0
290+
call {unused_import}
291+
pop 1
292+
293+
push 0
294+
push 0
295+
push 0
296+
push 1 // _ x2 x1 x0 0 0 0 1
297+
push 0 swap 7 // _ 0 x1 x0 0 0 0 1 x2
298+
push 0 swap 7 // _ 0 0 x0 0 0 0 1 x2 x1
299+
push 0 swap 7 // _ 0 0 0 0 0 0 1 x2 x1 x0
300+
301+
sponge_init
302+
sponge_absorb
303+
sponge_squeeze // _ d9 d8 d7 d6 d5 d4 d3 d2 d1 d0
304+
swap 5 pop 1 // _ d9 d8 d7 d6 d0 d4 d3 d2 d1
305+
swap 5 pop 1 // _ d9 d8 d7 d1 d0 d4 d3 d2
306+
swap 5 pop 1
307+
swap 5 pop 1
308+
swap 5 pop 1
309+
310+
// _ d4 d3 d2 d1 d0
311+
312+
split // _ d4 d3 d2 d1 hi lo
313+
push 2 // _ d4 d3 d2 d1 hi lo 2
314+
swap 1
315+
div_mod // _ d4 d3 d2 d1 hi q r
316+
swap 6
317+
pop 5 pop 1
318+
return
319+
)
410320
}
411321
}
412322

413-
#[test]
414-
fn prop_test() {
415-
ShadowedFunction::new(Filter {
416-
f: InnerFunction::DeprecatedSnippet(Box::new(TestHashXFieldElementLsb)),
417-
})
418-
.test();
419-
}
420-
421323
#[test]
422324
fn test_with_raw_function_lsb_on_bfe() {
423325
let rawcode = RawCode::new(
@@ -472,7 +374,7 @@ mod benches {
472374
#[test]
473375
fn benchmark() {
474376
ShadowedFunction::new(Filter {
475-
f: InnerFunction::DeprecatedSnippet(Box::new(TestHashXFieldElementLsb)),
377+
f: InnerFunction::BasicSnippet(Box::new(TestHashXFieldElementLsb)),
476378
})
477379
.bench();
478380
}

0 commit comments

Comments
 (0)