From 3cb513b7a70dbea1f557fd38566fa53d9208b77b Mon Sep 17 00:00:00 2001 From: Arthur Paulino Date: Thu, 16 Nov 2023 12:51:21 -0300 Subject: [PATCH] add Op::Unit for general purpose debugging or data collection (#896) --- src/lem/circuit.rs | 4 ++-- src/lem/interpreter.rs | 1 + src/lem/macros.rs | 13 +++++++++++++ src/lem/mod.rs | 5 +++++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/lem/circuit.rs b/src/lem/circuit.rs index 023a7b66a8..a6009dd0bb 100644 --- a/src/lem/circuit.rs +++ b/src/lem/circuit.rs @@ -984,7 +984,7 @@ fn synthesize_block, C: Coprocessor>( bound_allocations.insert_ptr(tgt[0].clone(), div_ptr); bound_allocations.insert_ptr(tgt[1].clone(), rem_ptr); } - Op::Emit(_) => (), + Op::Emit(_) | Op::Unit(_) => (), Op::Hide(tgt, sec, pay) => { let sec = bound_allocations.get_ptr(sec)?; let pay = bound_allocations.get_ptr(pay)?; @@ -1501,7 +1501,7 @@ impl Func { // three implies_u64, one sub and one linear num_constraints += 197; } - Op::Not(..) | Op::Emit(_) | Op::Cproc(..) | Op::Copy(..) => (), + Op::Not(..) | Op::Emit(_) | Op::Cproc(..) | Op::Copy(..) | Op::Unit(_) => (), Op::Cons2(_, tag, _) => { // tag for the image globals.insert(FWrap(tag.to_field())); diff --git a/src/lem/interpreter.rs b/src/lem/interpreter.rs index 19a84d8394..8f3dd6b8ca 100644 --- a/src/lem/interpreter.rs +++ b/src/lem/interpreter.rs @@ -434,6 +434,7 @@ impl Block { bindings.insert_ptr(tgt_secret.clone(), Ptr::Atom(Tag::Expr(Num), *secret)); hints.commitment.push(Some(SlotData::FPtr(*secret, *ptr))) } + Op::Unit(f) => f(), } } match &self.ctrl { diff --git a/src/lem/macros.rs b/src/lem/macros.rs index 275bcaab5d..e45e81a077 100644 --- a/src/lem/macros.rs +++ b/src/lem/macros.rs @@ -208,6 +208,9 @@ macro_rules! op { let func = Box::new($func.clone()); $crate::lem::Op::Call(out, func, inp) } + }; + ( unit($f:expr) ) => { + $crate::lem::Op::Unit($f) } } @@ -591,6 +594,16 @@ macro_rules! block { $($tail)* ) }; + (@seq {$($limbs:expr)*}, unit($f:expr) ; $($tail:tt)*) => { + $crate::block! ( + @seq + { + $($limbs)* + $crate::op!(unit($f)) + }, + $($tail)* + ) + }; (@seq {$($limbs:expr)*}, let ($($tgt:ident),*) = $func:ident($($arg:ident),*) ; $($tail:tt)*) => { $crate::block! ( @seq diff --git a/src/lem/mod.rs b/src/lem/mod.rs index 854925a808..1e918e054f 100644 --- a/src/lem/mod.rs +++ b/src/lem/mod.rs @@ -296,6 +296,7 @@ pub enum Op { /// `Open(s, p, h)` binds `s` and `p` to the secret and payload (respectively) /// of the commitment that resulted on (num or comm) `h` Open(Var, Var, Var), + Unit(fn()), } impl Func { @@ -451,6 +452,7 @@ impl Func { is_unique(tgt_secret, map); is_unique(tgt_ptr, map); } + Op::Unit(_) => (), } } match &block.ctrl { @@ -736,6 +738,9 @@ impl Block { let pay = insert_one(map, uniq, &pay); ops.push(Op::Open(sec, pay, comm_or_num)) } + Op::Unit(x) => { + ops.push(Op::Unit(x)); + } } } let ctrl = match self.ctrl {