From d37c925c132d1399efd3fcff4a1e00bf0c4f0d8c Mon Sep 17 00:00:00 2001 From: Gabriel Barreto Date: Mon, 15 Jan 2024 13:39:47 -0300 Subject: [PATCH] remove LEM path --- src/lem/eval.rs | 2 +- src/lem/interpreter.rs | 80 +++++++++--------------- src/lem/mod.rs | 1 - src/lem/path.rs | 135 ----------------------------------------- src/lem/tests/misc.rs | 2 +- 5 files changed, 31 insertions(+), 189 deletions(-) delete mode 100644 src/lem/path.rs diff --git a/src/lem/eval.rs b/src/lem/eval.rs index ff44e14660..da2f4540a1 100644 --- a/src/lem/eval.rs +++ b/src/lem/eval.rs @@ -71,7 +71,7 @@ fn compute_frame>( }; assert_eq!(func.input_params.len(), input.len()); let preimages = Hints::new_from_func(func); - let (frame, _) = func.call(input, store, preimages, emitted, lang, pc)?; + let frame = func.call(input, store, preimages, emitted, lang, pc)?; let must_break = matches!(frame.output[2].tag(), Tag::Cont(Terminal | Error)); Ok((frame, must_break)) } diff --git a/src/lem/interpreter.rs b/src/lem/interpreter.rs index 0dc2b097b5..820aa5e97b 100644 --- a/src/lem/interpreter.rs +++ b/src/lem/interpreter.rs @@ -1,7 +1,6 @@ use anyhow::{anyhow, bail, Context, Result}; use super::{ - path::Path, pointers::{Ptr, RawPtr}, slot::{SlotData, Val}, store::{fetch_ptrs, intern_ptrs, Store}, @@ -140,11 +139,10 @@ impl Block { store: &Store, mut bindings: VarMap, mut hints: Hints, - mut path: Path, emitted: &mut Vec, lang: &Lang, pc: usize, - ) -> Result<(Frame, Path)> { + ) -> Result { for op in &self.ops { match op { Op::Cproc(out, sym, inp) => { @@ -164,10 +162,7 @@ impl Block { Op::Call(out, func, inp) => { // Get the argument values let inp_ptrs = bindings.get_many_ptr(inp)?; - let (frame, func_path) = - func.call(&inp_ptrs, store, hints, emitted, lang, pc)?; - // Extend the path - path.extend_from_path(&func_path); + let frame = func.call(&inp_ptrs, store, hints, emitted, lang, pc)?; // Bind the output variables to the output values hints = frame.hints; for (var, ptr) in out.iter().zip(frame.output.into_iter()) { @@ -476,14 +471,12 @@ impl Block { let ptr = bindings.get_ptr(match_var)?; let tag = ptr.tag(); if let Some(block) = cases.get(tag) { - path.push_tag_inplace(*tag); - block.run(input, store, bindings, hints, path, emitted, lang, pc) + block.run(input, store, bindings, hints, emitted, lang, pc) } else { - path.push_default_inplace(); let Some(def) = def else { bail!("No match for tag {}", tag) }; - def.run(input, store, bindings, hints, path, emitted, lang, pc) + def.run(input, store, bindings, hints, emitted, lang, pc) } } Ctrl::MatchSymbol(match_var, cases, def) => { @@ -495,23 +488,20 @@ impl Block { bail!("Symbol bound to {match_var} wasn't interned"); }; if let Some(block) = cases.get(&sym) { - path.push_symbol_inplace(sym); - block.run(input, store, bindings, hints, path, emitted, lang, pc) + block.run(input, store, bindings, hints, emitted, lang, pc) } else { - path.push_default_inplace(); let Some(def) = def else { bail!("No match for symbol {sym}") }; - def.run(input, store, bindings, hints, path, emitted, lang, pc) + def.run(input, store, bindings, hints, emitted, lang, pc) } } Ctrl::If(b, true_block, false_block) => { let b = bindings.get_bool(b)?; - path.push_bool_inplace(b); if b { - true_block.run(input, store, bindings, hints, path, emitted, lang, pc) + true_block.run(input, store, bindings, hints, emitted, lang, pc) } else { - false_block.run(input, store, bindings, hints, path, emitted, lang, pc) + false_block.run(input, store, bindings, hints, emitted, lang, pc) } } Ctrl::Return(output_vars) => { @@ -520,17 +510,14 @@ impl Block { output.push(bindings.get_ptr(var)?) } let input = input.to_vec(); - Ok(( - Frame { - input, - output, - emitted: emitted.clone(), - hints, - blank: false, - pc, - }, - path, - )) + Ok(Frame { + input, + output, + emitted: emitted.clone(), + hints, + blank: false, + pc, + }) } } } @@ -545,7 +532,7 @@ impl Func { emitted: &mut Vec, lang: &Lang, pc: usize, - ) -> Result<(Frame, Path)> { + ) -> Result { let mut bindings = VarMap::new(); for (i, param) in self.input_params.iter().enumerate() { bindings.insert_ptr(param.clone(), args[i]); @@ -559,17 +546,10 @@ impl Func { let commitment_init = hints.commitment.len(); let bit_decomp_init = hints.bit_decomp.len(); - let mut res = self.body.run( - args, - store, - bindings, - hints, - Path::default(), - emitted, - lang, - pc, - )?; - let hints = &mut res.0.hints; + let mut res = self + .body + .run(args, store, bindings, hints, emitted, lang, pc)?; + let hints = &mut res.hints; let hash4_used = hints.hash4.len() - hash4_init; let hash6_used = hints.hash6.len() - hash6_init; @@ -604,15 +584,13 @@ impl Func { lang: &Lang, pc: usize, ) -> Result { - Ok(self - .call( - args, - store, - Hints::new_from_func(self), - &mut vec![], - lang, - pc, - )? - .0) + self.call( + args, + store, + Hints::new_from_func(self), + &mut vec![], + lang, + pc, + ) } } diff --git a/src/lem/mod.rs b/src/lem/mod.rs index 0ba5fc1003..595080ab31 100644 --- a/src/lem/mod.rs +++ b/src/lem/mod.rs @@ -64,7 +64,6 @@ pub mod eval; pub(crate) mod interpreter; mod macros; pub mod multiframe; -mod path; pub mod pointers; mod slot; pub mod store; diff --git a/src/lem/path.rs b/src/lem/path.rs deleted file mode 100644 index cb09570880..0000000000 --- a/src/lem/path.rs +++ /dev/null @@ -1,135 +0,0 @@ -use std::collections::HashSet; - -use crate::Symbol; - -use super::{Block, Ctrl, Func, Op, Tag}; - -#[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub(crate) enum PathNode { - Tag(Tag), - Symbol(Symbol), - Bool(bool), - Default, -} - -impl std::fmt::Display for PathNode { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::Tag(tag) => write!(f, "Tag({})", tag), - Self::Symbol(sym) => write!(f, "Symbol({})", sym), - Self::Bool(b) => write!(f, "Bool({})", b), - Self::Default => write!(f, "Default"), - } - } -} - -#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)] -pub struct Path(Vec); - -impl std::fmt::Display for Path { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let strings = self.0.iter().map(|x| format!("{}", x)).collect::>(); - write!(f, "{}", strings.join(".")) - } -} - -impl Path { - pub fn push_tag(&self, tag: &Tag) -> Path { - let mut path = self.0.clone(); - path.push(PathNode::Tag(*tag)); - Path(path) - } - - pub fn push_bool(&self, b: bool) -> Path { - let mut path = self.0.clone(); - path.push(PathNode::Bool(b)); - Path(path) - } - - pub fn push_symbol(&self, sym: Symbol) -> Path { - let mut path = self.0.clone(); - path.push(PathNode::Symbol(sym)); - Path(path) - } - - pub fn push_default(&self) -> Path { - let mut path = self.0.clone(); - path.push(PathNode::Default); - Path(path) - } - - #[inline] - pub fn push_tag_inplace(&mut self, tag: Tag) { - self.0.push(PathNode::Tag(tag)); - } - - #[inline] - pub fn push_bool_inplace(&mut self, b: bool) { - self.0.push(PathNode::Bool(b)); - } - - #[inline] - pub fn push_symbol_inplace(&mut self, sym: Symbol) { - self.0.push(PathNode::Symbol(sym)); - } - - #[inline] - pub fn push_default_inplace(&mut self) { - self.0.push(PathNode::Default); - } - - #[inline] - pub fn extend_from_path(&mut self, path: &Path) { - self.0.extend_from_slice(&path.0) - } - - /// Computes the number of different paths taken given a list of paths - pub fn num_paths_taken(paths: &[Self]) -> usize { - let mut all_paths: HashSet<&Self> = HashSet::default(); - all_paths.extend(paths); - all_paths.len() - } -} - -impl Func { - /// Computes the number of possible paths in a `Func` can take - pub fn num_paths(&self) -> usize { - self.body.num_paths() - } - - /// Asserts that all paths were visited by a set of frames. This is mostly - /// for testing purposes. - pub fn assert_all_paths_taken(&self, paths: &[Path]) { - assert_eq!(Path::num_paths_taken(paths), self.num_paths()); - } -} - -impl Block { - fn num_paths(&self) -> usize { - let mut num_paths = 1; - for op in &self.ops { - if let Op::Call(_, func, _) = op { - num_paths *= func.num_paths() - } - } - num_paths *= match &self.ctrl { - Ctrl::MatchTag(_, cases, def) => { - let init = def.as_ref().map_or(0, |def| def.num_paths()); - cases - .values() - .fold(init, |acc, block| acc + block.num_paths()) - } - Ctrl::MatchSymbol(_, cases, def) => { - let init = def.as_ref().map_or(0, |def| def.num_paths()); - cases - .values() - .fold(init, |acc, block| acc + block.num_paths()) - } - Ctrl::If(_, true_block, false_block) => { - true_block.num_paths() + false_block.num_paths() - } - Ctrl::Return(..) => 1, - }; - num_paths - } -} diff --git a/src/lem/tests/misc.rs b/src/lem/tests/misc.rs index 5ac6d024fd..16181fe5a0 100644 --- a/src/lem/tests/misc.rs +++ b/src/lem/tests/misc.rs @@ -33,7 +33,7 @@ fn synthesize_test_helper( let mut cs_prev = None; for input in inputs { let input = [input, nil, outermost]; - let (frame, _) = func + let frame = func .call(&input, store, Default::default(), &mut vec![], &lang, 0) .unwrap();