From e56e8209eef1e48578893627b18d3bdf4e974fc3 Mon Sep 17 00:00:00 2001 From: Arthur Paulino Date: Mon, 16 Oct 2023 21:09:25 -0300 Subject: [PATCH] rename 'advices' to 'hints' --- src/lem/circuit.rs | 14 ++--- src/lem/eval.rs | 4 +- src/lem/interpreter.rs | 116 ++++++++++++++++++++--------------------- src/lem/mod.rs | 6 +-- 4 files changed, 69 insertions(+), 71 deletions(-) diff --git a/src/lem/circuit.rs b/src/lem/circuit.rs index 043aa42a90..2311706aa2 100644 --- a/src/lem/circuit.rs +++ b/src/lem/circuit.rs @@ -438,7 +438,7 @@ impl Func { // that's why they are filled with dummies let preallocated_hash4_slots = allocate_slots( cs, - &frame.advices.hash4, + &frame.hints.hash4, SlotType::Hash4, self.slot.hash4, store, @@ -446,7 +446,7 @@ impl Func { let preallocated_hash6_slots = allocate_slots( cs, - &frame.advices.hash6, + &frame.hints.hash6, SlotType::Hash6, self.slot.hash6, store, @@ -454,7 +454,7 @@ impl Func { let preallocated_hash8_slots = allocate_slots( cs, - &frame.advices.hash8, + &frame.hints.hash8, SlotType::Hash8, self.slot.hash8, store, @@ -462,7 +462,7 @@ impl Func { let preallocated_commitment_slots = allocate_slots( cs, - &frame.advices.commitment, + &frame.hints.commitment, SlotType::Commitment, self.slot.commitment, store, @@ -470,7 +470,7 @@ impl Func { let preallocated_bit_decomp_slots = allocate_slots( cs, - &frame.advices.bit_decomp, + &frame.hints.bit_decomp, SlotType::BitDecomp, self.slot.bit_decomp, store, @@ -1274,9 +1274,9 @@ impl Func { } } - let call_outputs = &frame.advices.call_outputs; + let call_outputs = &frame.hints.call_outputs; let call_idx = 0; - let cproc_outputs = &frame.advices.cproc_outputs; + let cproc_outputs = &frame.hints.cproc_outputs; recurse( cs, &self.body, diff --git a/src/lem/eval.rs b/src/lem/eval.rs index b561ab90dd..ff9875eb7e 100644 --- a/src/lem/eval.rs +++ b/src/lem/eval.rs @@ -19,7 +19,7 @@ use crate::{ }; use super::{ - interpreter::{Advices, Frame}, + interpreter::{Frame, Hints}, pointers::Ptr, store::Store, Ctrl, Func, Op, Tag, Var, @@ -72,7 +72,7 @@ fn compute_frame>( .expect("Program counter outside range") }; assert_eq!(func.input_params.len(), input.len()); - let preimages = Advices::new_from_func(func); + let preimages = Hints::new_from_func(func); 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 5266c5c218..14c2b26a2d 100644 --- a/src/lem/interpreter.rs +++ b/src/lem/interpreter.rs @@ -49,11 +49,11 @@ impl VarMap> { } #[derive(Clone, Debug, Default)] -/// `Advices` hold the non-deterministic advices for hashes and `Func` calls. +/// `Hints` hold the non-deterministic hints for hashes and `Func` calls. /// The hash preimages must have the same shape as the allocated slots for the /// `Func`, and the `None` values are used to fill the unused slots, which are /// later filled by dummy values. -pub struct Advices { +pub struct Hints { pub hash4: Vec>>, pub hash6: Vec>>, pub hash8: Vec>>, @@ -63,8 +63,8 @@ pub struct Advices { pub cproc_outputs: Vec>>, } -impl Advices { - pub fn new_from_func(func: &Func) -> Advices { +impl Hints { + pub fn new_from_func(func: &Func) -> Hints { let slot = func.slot; let hash4 = Vec::with_capacity(slot.hash4); let hash6 = Vec::with_capacity(slot.hash6); @@ -73,7 +73,7 @@ impl Advices { let bit_decomp = Vec::with_capacity(slot.bit_decomp); let call_outputs = VecDeque::new(); let cproc_outputs = Vec::new(); - Advices { + Hints { hash4, hash6, hash8, @@ -84,7 +84,7 @@ impl Advices { } } - pub fn blank(func: &Func) -> Advices { + pub fn blank(func: &Func) -> Hints { let slot = func.slot; let hash4 = vec![None; slot.hash4]; let hash6 = vec![None; slot.hash6]; @@ -93,7 +93,7 @@ impl Advices { let bit_decomp = vec![None; slot.bit_decomp]; let call_outputs = VecDeque::new(); let cproc_outputs = Vec::new(); - Advices { + Hints { hash4, hash6, hash8, @@ -115,7 +115,7 @@ pub struct Frame { pub input: Vec>, pub output: Vec>, pub emitted: Vec>, - pub advices: Advices, + pub hints: Hints, pub blank: bool, pub pc: usize, } @@ -124,12 +124,12 @@ impl Frame { pub fn blank(func: &Func, pc: usize) -> Frame { let input = vec![Ptr::null(Tag::Expr(Nil)); func.input_params.len()]; let output = vec![Ptr::null(Tag::Expr(Nil)); func.output_size]; - let advices = Advices::blank(func); + let hints = Hints::blank(func); Frame { input, output, emitted: Vec::default(), - advices, + hints, blank: true, pc, } @@ -145,7 +145,7 @@ impl Block { input: &[Ptr], store: &Store, mut bindings: VarMap>, - mut advices: Advices, + mut hints: Hints, mut path: Path, emitted: &mut Vec>, lang: &Lang, @@ -165,7 +165,7 @@ impl Block { for (var, ptr) in out.iter().zip(&out_ptrs) { bindings.insert(var.clone(), Val::Pointer(*ptr)); } - advices.cproc_outputs.push(out_ptrs); + hints.cproc_outputs.push(out_ptrs); } Op::Call(out, func, inp) => { // Get the argument values @@ -176,10 +176,10 @@ impl Block { // save all the inner call outputs, push the output of the call in front // of it, then extend `call_outputs` let mut inner_call_outputs = VecDeque::new(); - std::mem::swap(&mut inner_call_outputs, &mut advices.call_outputs); + std::mem::swap(&mut inner_call_outputs, &mut hints.call_outputs); let (mut frame, func_path) = - func.call(&inp_ptrs, store, advices, emitted, lang, pc)?; - std::mem::swap(&mut inner_call_outputs, &mut frame.advices.call_outputs); + func.call(&inp_ptrs, store, hints, emitted, lang, pc)?; + std::mem::swap(&mut inner_call_outputs, &mut frame.hints.call_outputs); // Extend the path and bind the output variables to the output values path.extend_from_path(&func_path); @@ -187,10 +187,10 @@ impl Block { bindings.insert_ptr(var.clone(), *ptr); } - // Update `advices` correctly + // Update `hints` correctly inner_call_outputs.push_front(frame.output); - advices = frame.advices; - advices.call_outputs.extend(inner_call_outputs); + hints = frame.hints; + hints.call_outputs.extend(inner_call_outputs); } Op::Null(tgt, tag) => { bindings.insert_ptr(tgt.clone(), Ptr::null(*tag)); @@ -279,9 +279,9 @@ impl Block { let b = bindings.get_ptr(b)?; let c = if let (Ptr::Atom(_, f), Ptr::Atom(_, g)) = (a, b) { let diff = f - g; - advices.bit_decomp.push(Some(SlotData::F(f + f))); - advices.bit_decomp.push(Some(SlotData::F(g + g))); - advices.bit_decomp.push(Some(SlotData::F(diff + diff))); + hints.bit_decomp.push(Some(SlotData::F(f + f))); + hints.bit_decomp.push(Some(SlotData::F(g + g))); + hints.bit_decomp.push(Some(SlotData::F(diff + diff))); let f = BaseNum::Scalar(f); let g = BaseNum::Scalar(g); f < g @@ -294,7 +294,7 @@ impl Block { assert!(*n <= 64); let a = bindings.get_ptr(a)?; let c = if let Ptr::Atom(_, f) = a { - advices.bit_decomp.push(Some(SlotData::F(f))); + hints.bit_decomp.push(Some(SlotData::F(f))); let b = if *n < 64 { (1 << *n) - 1 } else { u64::MAX }; Ptr::Atom(Tag::Expr(Num), F::from_u64(f.to_u64_unchecked() & b)) } else { @@ -329,14 +329,14 @@ impl Block { let preimg_ptrs = bindings.get_many_ptr(preimg)?; let tgt_ptr = store.intern_2_ptrs(*tag, preimg_ptrs[0], preimg_ptrs[1]); bindings.insert_ptr(img.clone(), tgt_ptr); - advices.hash4.push(Some(SlotData::PtrVec(preimg_ptrs))); + hints.hash4.push(Some(SlotData::PtrVec(preimg_ptrs))); } Op::Cons3(img, tag, preimg) => { let preimg_ptrs = bindings.get_many_ptr(preimg)?; let tgt_ptr = store.intern_3_ptrs(*tag, preimg_ptrs[0], preimg_ptrs[1], preimg_ptrs[2]); bindings.insert_ptr(img.clone(), tgt_ptr); - advices.hash6.push(Some(SlotData::PtrVec(preimg_ptrs))); + hints.hash6.push(Some(SlotData::PtrVec(preimg_ptrs))); } Op::Cons4(img, tag, preimg) => { let preimg_ptrs = bindings.get_many_ptr(preimg)?; @@ -348,7 +348,7 @@ impl Block { preimg_ptrs[3], ); bindings.insert_ptr(img.clone(), tgt_ptr); - advices.hash8.push(Some(SlotData::PtrVec(preimg_ptrs))); + hints.hash8.push(Some(SlotData::PtrVec(preimg_ptrs))); } Op::Decons2(preimg, img) => { let img_ptr = bindings.get_ptr(img)?; @@ -362,7 +362,7 @@ impl Block { for (var, ptr) in preimg.iter().zip(preimg_ptrs.iter()) { bindings.insert_ptr(var.clone(), *ptr); } - advices + hints .hash4 .push(Some(SlotData::PtrVec(preimg_ptrs.to_vec()))); } @@ -378,7 +378,7 @@ impl Block { for (var, ptr) in preimg.iter().zip(preimg_ptrs.iter()) { bindings.insert_ptr(var.clone(), *ptr); } - advices + hints .hash6 .push(Some(SlotData::PtrVec(preimg_ptrs.to_vec()))); } @@ -394,7 +394,7 @@ impl Block { for (var, ptr) in preimg.iter().zip(preimg_ptrs.iter()) { bindings.insert_ptr(var.clone(), *ptr); } - advices + hints .hash8 .push(Some(SlotData::PtrVec(preimg_ptrs.to_vec()))); } @@ -404,9 +404,7 @@ impl Block { bail!("{sec} is not a numeric pointer") }; let tgt_ptr = store.hide(secret, src_ptr)?; - advices - .commitment - .push(Some(SlotData::FPtr(secret, src_ptr))); + hints.commitment.push(Some(SlotData::FPtr(secret, src_ptr))); bindings.insert_ptr(tgt.clone(), tgt_ptr); } Op::Open(tgt_secret, tgt_ptr, comm) => { @@ -418,7 +416,7 @@ impl Block { }; bindings.insert_ptr(tgt_ptr.clone(), *ptr); bindings.insert_ptr(tgt_secret.clone(), Ptr::Atom(Tag::Expr(Num), *secret)); - advices.commitment.push(Some(SlotData::FPtr(*secret, *ptr))) + hints.commitment.push(Some(SlotData::FPtr(*secret, *ptr))) } } } @@ -428,13 +426,13 @@ impl Block { let tag = ptr.tag(); if let Some(block) = cases.get(tag) { path.push_tag_inplace(*tag); - block.run(input, store, bindings, advices, path, emitted, lang, pc) + block.run(input, store, bindings, hints, path, emitted, lang, pc) } else { path.push_default_inplace(); let Some(def) = def else { bail!("No match for tag {}", tag) }; - def.run(input, store, bindings, advices, path, emitted, lang, pc) + def.run(input, store, bindings, hints, path, emitted, lang, pc) } } Ctrl::MatchSymbol(match_var, cases, def) => { @@ -447,22 +445,22 @@ impl Block { }; if let Some(block) = cases.get(&sym) { path.push_symbol_inplace(sym); - block.run(input, store, bindings, advices, path, emitted, lang, pc) + block.run(input, store, bindings, hints, path, emitted, lang, pc) } else { path.push_default_inplace(); let Some(def) = def else { bail!("No match for symbol {sym}") }; - def.run(input, store, bindings, advices, path, emitted, lang, pc) + def.run(input, store, bindings, hints, path, 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, advices, path, emitted, lang, pc) + true_block.run(input, store, bindings, hints, path, emitted, lang, pc) } else { - false_block.run(input, store, bindings, advices, path, emitted, lang, pc) + false_block.run(input, store, bindings, hints, path, emitted, lang, pc) } } Ctrl::Return(output_vars) => { @@ -476,7 +474,7 @@ impl Block { input, output, emitted: emitted.clone(), - advices, + hints, blank: false, pc, }, @@ -492,7 +490,7 @@ impl Func { &self, args: &[Ptr], store: &Store, - advices: Advices, + hints: Hints, emitted: &mut Vec>, lang: &Lang, pc: usize, @@ -503,45 +501,45 @@ impl Func { } // We must fill any unused slots with `None` values so we save - // the initial size of advices, which might not be zero - let hash4_init = advices.hash4.len(); - let hash6_init = advices.hash6.len(); - let hash8_init = advices.hash8.len(); - let commitment_init = advices.commitment.len(); - let bit_decomp_init = advices.bit_decomp.len(); + // the initial size of hints, which might not be zero + let hash4_init = hints.hash4.len(); + let hash6_init = hints.hash6.len(); + let hash8_init = hints.hash8.len(); + let commitment_init = hints.commitment.len(); + let bit_decomp_init = hints.bit_decomp.len(); let mut res = self.body.run( args, store, bindings, - advices, + hints, Path::default(), emitted, lang, pc, )?; - let advices = &mut res.0.advices; + let hints = &mut res.0.hints; - let hash4_used = advices.hash4.len() - hash4_init; - let hash6_used = advices.hash6.len() - hash6_init; - let hash8_used = advices.hash8.len() - hash8_init; - let commitment_used = advices.commitment.len() - commitment_init; - let bit_decomp_used = advices.bit_decomp.len() - bit_decomp_init; + let hash4_used = hints.hash4.len() - hash4_init; + let hash6_used = hints.hash6.len() - hash6_init; + let hash8_used = hints.hash8.len() - hash8_init; + let commitment_used = hints.commitment.len() - commitment_init; + let bit_decomp_used = hints.bit_decomp.len() - bit_decomp_init; for _ in hash4_used..self.slot.hash4 { - advices.hash4.push(None); + hints.hash4.push(None); } for _ in hash6_used..self.slot.hash6 { - advices.hash6.push(None); + hints.hash6.push(None); } for _ in hash8_used..self.slot.hash8 { - advices.hash8.push(None); + hints.hash8.push(None); } for _ in commitment_used..self.slot.commitment { - advices.commitment.push(None); + hints.commitment.push(None); } for _ in bit_decomp_used..self.slot.bit_decomp { - advices.bit_decomp.push(None); + hints.bit_decomp.push(None); } Ok(res) @@ -559,7 +557,7 @@ impl Func { .call( args, store, - Advices::new_from_func(self), + Hints::new_from_func(self), &mut vec![], lang, pc, diff --git a/src/lem/mod.rs b/src/lem/mod.rs index 5a444d81fc..7eb1b24a6f 100644 --- a/src/lem/mod.rs +++ b/src/lem/mod.rs @@ -6,8 +6,8 @@ //! The motivation behind LEM is the fact that hand-writing the circuit is a //! fragile process that hinders experimentation and safety. Thus we would like //! to bootstrap the circuit automatically, as well as an interpretation -//! algorithm that computes all non-deterministic advices for the circuit, -//! given a higher level description of the step function. +//! algorithm that computes all non-deterministic hints for the circuit, given a +//! higher level description of the step function. //! //! LEM also allows the `Store` API to be completely abstracted away from the //! responsibilities of LEM authors. Indeed, we want the implementation details @@ -35,7 +35,7 @@ //! //! The interpreter runs a LEM function given input values. Interpreting a LEM //! function will compute the values of each variable in the path of execution. -//! In particular, it will compute all the non-deterministic advices that are +//! In particular, it will compute all the non-deterministic hints that are //! needed to solve the circuit. //! //! ### Synthesizing