Skip to content

Commit

Permalink
make Ptr hashing functions pure
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurpaulino committed Oct 31, 2023
1 parent ab4ff04 commit a791459
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 124 deletions.
8 changes: 4 additions & 4 deletions src/circuit/circuit_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ impl<F: LurkField> EvaluationStore for Store<F> {
self.hydrate_scalar_cache()
}

fn ptr_eq(&self, left: &Self::Ptr, right: &Self::Ptr) -> Result<bool, Self::Error> {
self.ptr_eq(left, right)
fn ptr_eq(&self, left: &Self::Ptr, right: &Self::Ptr) -> bool {
self.ptr_eq(left, right).unwrap()
}
}

Expand Down Expand Up @@ -178,8 +178,8 @@ impl<'a, F: LurkField, C: Coprocessor<F> + 'a> MultiFrameTrait<'a, F, C> for Mul
fn io_to_scalar_vector(
store: &Self::Store,
io: &<Self::EvalFrame as FrameLike<Ptr<F>, ContPtr<F>>>::FrameIO,
) -> Result<Vec<F>, Self::StoreError> {
io.to_vector(store)
) -> Vec<F> {
io.to_vector(store).unwrap()
}

fn compute_witness(&self, s: &Self::Store) -> WitnessCS<F> {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<F: LurkField> HasFieldModulus for Commitment<F> {
impl<F: LurkField> Commitment<F> {
pub(crate) fn new(secret: Option<F>, payload: Ptr<F>, store: &Store<F>) -> Result<Self> {
let secret = secret.unwrap_or(F::NON_HIDING_COMMITMENT_SECRET);
let (hash, z_payload) = store.hide_and_return_z_payload(secret, payload)?;
let (hash, z_payload) = store.hide_and_return_z_payload(secret, payload);

Check warning on line 38 in src/cli/commitment.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/commitment.rs#L38

Added line #L38 was not covered by tests
let mut z_store = ZStore::<F>::default();
populate_z_store(&mut z_store, &payload, store, &mut HashMap::default())?;
z_store.add_comm(hash, secret, z_payload);
Expand Down
2 changes: 1 addition & 1 deletion src/cli/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ impl Repl<F> {
&commitment.z_store,
&mut Default::default(),
)?;
self.store.hide(*secret, payload)?;
self.store.hide(*secret, payload);

Check warning on line 318 in src/cli/repl.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/repl.rs#L318

Added line #L318 was not covered by tests
if print_data {
println!(
"{}",
Expand Down
2 changes: 1 addition & 1 deletion src/cli/repl/meta_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl MetaCmd<F> {
.eval_expr(second)
.with_context(|| "evaluating second arg")?;
let (first_io_expr, second_io_expr) = (&first_io[0], &second_io[0]);
if !&repl.store.ptr_eq(first_io_expr, second_io_expr)? {
if !repl.store.ptr_eq(first_io_expr, second_io_expr) {
eprintln!(
"`assert-eq` failed. Expected:\n {} = {}\nGot:\n {} ≠ {}",
first.fmt_to_string(&repl.store, &repl.state.borrow()),
Expand Down
2 changes: 1 addition & 1 deletion src/coprocessor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ pub(crate) mod test {
.get_allocated_const(LEMTag::Expr(ExprTag::Num).to_field())
.expect("Num tag should have been allocated");

let err_cont_z_ptr = s.hash_ptr(&s.cont_error()).expect("hash_ptr failed");
let err_cont_z_ptr = s.hash_ptr(&s.cont_error());

Check warning on line 350 in src/coprocessor/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/coprocessor/mod.rs#L350

Added line #L350 was not covered by tests
let cont_err = g
.get_allocated_ptr_from_z_ptr(&err_cont_z_ptr)
.expect("Error pointer should have been allocated");
Expand Down
5 changes: 1 addition & 4 deletions src/coprocessor/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,7 @@ impl<F: LurkField> Coprocessor<F> for Sha256Coprocessor<F> {
}

fn evaluate_lem_simple(&self, s: &LEMStore<F>, args: &[LEMPtr<F>]) -> LEMPtr<F> {
let z_ptrs = args
.iter()
.map(|ptr| s.hash_ptr(ptr).unwrap())
.collect::<Vec<_>>();
let z_ptrs = args.iter().map(|ptr| s.hash_ptr(ptr)).collect::<Vec<_>>();

Check warning on line 146 in src/coprocessor/sha256.rs

View check run for this annotation

Codecov / codecov/patch

src/coprocessor/sha256.rs#L146

Added line #L146 was not covered by tests
LEMPtr::num(compute_sha256(self.n, &z_ptrs))
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/coprocessor/trie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl<F: LurkField> CoCircuit<F> for NewCoprocessor<F> {

// TODO: Use a custom type.
let root = LEMPtr::num(trie.root);
let root_z_ptr = s.hash_ptr(&root).unwrap();
let root_z_ptr = s.hash_ptr(&root);

Check warning on line 136 in src/coprocessor/trie/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/coprocessor/trie/mod.rs#L136

Added line #L136 was not covered by tests

AllocatedPtr::alloc_constant(cs, root_z_ptr)
}
Expand Down Expand Up @@ -169,8 +169,8 @@ impl<F: LurkField> Coprocessor<F> for LookupCoprocessor<F> {
let key_ptr = &args[1];

// TODO: Check tags.
let root_scalar = *s.hash_ptr(root_ptr).unwrap().value();
let key_scalar = *s.hash_ptr(key_ptr).unwrap().value();
let root_scalar = *s.hash_ptr(root_ptr).value();
let key_scalar = *s.hash_ptr(key_ptr).value();

Check warning on line 173 in src/coprocessor/trie/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/coprocessor/trie/mod.rs#L172-L173

Added lines #L172 - L173 were not covered by tests
let trie: StandardTrie<'_, F> =
Trie::new_with_root(&s.poseidon_cache, &s.inverse_poseidon_cache, root_scalar);

Expand Down Expand Up @@ -320,9 +320,9 @@ impl<F: LurkField> Coprocessor<F> for InsertCoprocessor<F> {
let root_ptr = &args[0];
let key_ptr = &args[1];
let val_ptr = &args[2];
let root_scalar = *s.hash_ptr(root_ptr).unwrap().value();
let key_scalar = *s.hash_ptr(key_ptr).unwrap().value();
let val_scalar = *s.hash_ptr(val_ptr).unwrap().value();
let root_scalar = *s.hash_ptr(root_ptr).value();
let key_scalar = *s.hash_ptr(key_ptr).value();
let val_scalar = *s.hash_ptr(val_ptr).value();

Check warning on line 325 in src/coprocessor/trie/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/coprocessor/trie/mod.rs#L323-L325

Added lines #L323 - L325 were not covered by tests
let mut trie: StandardTrie<'_, F> =
Trie::new_with_root(&s.poseidon_cache, &s.inverse_poseidon_cache, root_scalar);
trie.insert(key_scalar, val_scalar).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/eval/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl<F: LurkField, C: Coprocessor<F>> Lang<F, C> {

// TODO: this `z_ptr` is not really needed by LEM
let ptr = store.intern_symbol(&name);
let z_ptr = store.hash_ptr(&ptr).unwrap();
let z_ptr = store.hash_ptr(&ptr);

Check warning on line 143 in src/eval/lang.rs

View check run for this annotation

Codecov / codecov/patch

src/eval/lang.rs#L143

Added line #L143 was not covered by tests
let z_ptr = ZExprPtr::from_parts(lurk::tag::ExprTag::Sym, *z_ptr.value());

self.coprocessors.insert(name, (cproc.into(), z_ptr));
Expand Down
22 changes: 11 additions & 11 deletions src/lem/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<F: LurkField> GlobalAllocator<F> {
#[inline]
pub fn get_allocated_ptr_from_z_ptr(&self, z_ptr: &ZPtr<F>) -> Result<AllocatedPtr<F>> {
Ok(AllocatedPtr::from_parts(
self.get_allocated_const_cloned(z_ptr.tag().to_field())?,
self.get_allocated_const_cloned(z_ptr.tag_field())?,

Check warning on line 126 in src/lem/circuit.rs

View check run for this annotation

Codecov / codecov/patch

src/lem/circuit.rs#L126

Added line #L126 was not covered by tests
self.get_allocated_const_cloned(*z_ptr.value())?,
))
}
Expand Down Expand Up @@ -217,7 +217,7 @@ pub(crate) fn allocate_slot<F: LurkField, CS: ConstraintSystem<F>>(
SlotData::PtrVec(ptr_vec) => {
let mut component_idx = 0;
for ptr in ptr_vec {
let z_ptr = store.hash_ptr(ptr)?;
let z_ptr = store.hash_ptr(ptr);

// allocate pointer tag
preallocated_preimg.push(AllocatedNum::alloc_infallible(
Expand All @@ -237,7 +237,7 @@ pub(crate) fn allocate_slot<F: LurkField, CS: ConstraintSystem<F>>(
}
}
SlotData::FPtr(f, ptr) => {
let z_ptr = store.hash_ptr(ptr)?;
let z_ptr = store.hash_ptr(ptr);
// allocate first component
preallocated_preimg.push(AllocatedNum::alloc_infallible(
cs.namespace(|| format!("component 0 slot {slot}")),
Expand Down Expand Up @@ -382,7 +382,7 @@ impl Block {
}
Op::Lit(_, lit) => {
let lit_ptr = lit.to_ptr(store);
let lit_z_ptr = store.hash_ptr(&lit_ptr).unwrap();
let lit_z_ptr = store.hash_ptr(&lit_ptr);
g.new_const(cs, lit_z_ptr.tag_field());
g.new_const(cs, *lit_z_ptr.value());
}
Expand Down Expand Up @@ -579,7 +579,7 @@ fn synthesize_block<F: LurkField, CS: ConstraintSystem<F>, C: Coprocessor<F>>(
}
collected_ptrs
.iter()
.map(|ptr| ctx.store.hash_ptr(ptr).expect("hash_ptr failed"))
.map(|ptr| ctx.store.hash_ptr(ptr))

Check warning on line 582 in src/lem/circuit.rs

View check run for this annotation

Codecov / codecov/patch

src/lem/circuit.rs#L582

Added line #L582 was not covered by tests
.collect::<Vec<_>>()
} else {
vec![ZPtr::dummy(); out.len()]
Expand Down Expand Up @@ -642,7 +642,7 @@ fn synthesize_block<F: LurkField, CS: ConstraintSystem<F>, C: Coprocessor<F>>(
let output_z_ptrs = if not_dummy_and_not_blank {
let z_ptrs = ctx.call_outputs[ctx.call_idx]
.iter()
.map(|ptr| ctx.store.hash_ptr(ptr).expect("hash_ptr failed"))
.map(|ptr| ctx.store.hash_ptr(ptr))
.collect::<Vec<_>>();
ctx.call_idx += 1;
assert_eq!(z_ptrs.len(), out.len());
Expand Down Expand Up @@ -740,7 +740,7 @@ fn synthesize_block<F: LurkField, CS: ConstraintSystem<F>, C: Coprocessor<F>>(
let allocated_tag = ctx.global_allocator.get_allocated_const_cloned(lit_tag)?;
let allocated_hash = ctx
.global_allocator
.get_allocated_const_cloned(*ctx.store.hash_ptr(&lit_ptr)?.value())?;
.get_allocated_const_cloned(*ctx.store.hash_ptr(&lit_ptr).value())?;
let allocated_ptr = AllocatedPtr::from_parts(allocated_tag, allocated_hash);
bound_allocations.insert_ptr(tgt.clone(), allocated_ptr);
}
Expand Down Expand Up @@ -1217,7 +1217,7 @@ fn synthesize_block<F: LurkField, CS: ConstraintSystem<F>, C: Coprocessor<F>>(
let mut cases_vec = Vec::with_capacity(cases.len());
for (sym, block) in cases {
let sym_ptr = ctx.store.intern_symbol(sym);
let sym_hash = *ctx.store.hash_ptr(&sym_ptr)?.value();
let sym_hash = *ctx.store.hash_ptr(&sym_ptr).value();
cases_vec.push((sym_hash, block));
}

Expand Down Expand Up @@ -1268,7 +1268,7 @@ impl Func {
assert_eq!(self.output_size, frame.output.len());
let mut output = Vec::with_capacity(frame.output.len());
for (i, ptr) in frame.output.iter().enumerate() {
let zptr = store.hash_ptr(ptr)?;
let zptr = store.hash_ptr(ptr);
output.push(AllocatedPtr::alloc(
&mut cs.namespace(|| format!("var: output[{}]", i)),
|| Ok(zptr),
Expand All @@ -1287,7 +1287,7 @@ impl Func {
) -> Result<()> {
for (i, ptr) in frame.input.iter().enumerate() {
let param = &self.input_params[i];
let zptr = store.hash_ptr(ptr)?;
let zptr = store.hash_ptr(ptr);

Check warning on line 1290 in src/lem/circuit.rs

View check run for this annotation

Codecov / codecov/patch

src/lem/circuit.rs#L1290

Added line #L1290 was not covered by tests
let ptr =
AllocatedPtr::alloc(&mut cs.namespace(|| format!("var: {param}")), || Ok(zptr))?;
bound_allocations.insert_ptr(param.clone(), ptr);
Expand Down Expand Up @@ -1468,7 +1468,7 @@ impl Func {
}
Op::Lit(_, lit) => {
let lit_ptr = lit.to_ptr(store);
let lit_z_ptr = store.hash_ptr(&lit_ptr).unwrap();
let lit_z_ptr = store.hash_ptr(&lit_ptr);

Check warning on line 1471 in src/lem/circuit.rs

View check run for this annotation

Codecov / codecov/patch

src/lem/circuit.rs#L1471

Added line #L1471 was not covered by tests
globals.insert(FWrap(lit_z_ptr.tag_field()));
globals.insert(FWrap(*lit_z_ptr.value()));
}
Expand Down
4 changes: 2 additions & 2 deletions src/lem/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl Block {
let b = bindings.get_ptr(b)?;
// In order to compare Ptrs, we *must* resolve the hashes. Otherwise, we risk failing to recognize equality of
// compound data with opaque data in either element's transitive closure.
let c = store.hash_ptr(&a)?.value() == store.hash_ptr(&b)?.value();
let c = store.hash_ptr(&a).value() == store.hash_ptr(&b).value();
bindings.insert_bool(tgt.clone(), c);
}
Op::Not(tgt, a) => {
Expand Down Expand Up @@ -419,7 +419,7 @@ impl Block {
let Ptr::Atom(Tag::Expr(Num), secret) = bindings.get_ptr(sec)? else {
bail!("{sec} is not a numeric pointer")
};
let tgt_ptr = store.hide(secret, src_ptr)?;
let tgt_ptr = store.hide(secret, src_ptr);
hints.commitment.push(Some(SlotData::FPtr(secret, src_ptr)));
bindings.insert_ptr(tgt.clone(), tgt_ptr);
}
Expand Down
24 changes: 12 additions & 12 deletions src/lem/multiframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ impl<F: LurkField> EvaluationStore for Store<F> {
self.hydrate_z_cache()
}

fn ptr_eq(&self, left: &Self::Ptr, right: &Self::Ptr) -> Result<bool, Self::Error> {
Ok(self.hash_ptr(left)? == self.hash_ptr(right)?)
fn ptr_eq(&self, left: &Self::Ptr, right: &Self::Ptr) -> bool {
self.ptr_eq(left, right)
}
}

Expand All @@ -127,7 +127,7 @@ fn assert_eq_ptrs_aptrs<F: LurkField>(
assert_eq!(ptrs.len(), aptrs.len());
if !blank {
for (aptr, ptr) in aptrs.iter().zip(ptrs) {
let z_ptr = store.hash_ptr(ptr).expect("hash_ptr failed");
let z_ptr = store.hash_ptr(ptr);
let (Some(alloc_ptr_tag), Some(alloc_ptr_hash)) =
(aptr.tag().get_value(), aptr.hash().get_value())
else {
Expand Down Expand Up @@ -274,7 +274,7 @@ fn synthesize_frames_parallel<F: LurkField, CS: ConstraintSystem<F>, C: Coproces
.input
.iter()
.map(|input_ptr| {
let z_ptr = store.hash_ptr(input_ptr).expect("hash_ptr failed");
let z_ptr = store.hash_ptr(input_ptr);

Check warning on line 277 in src/lem/multiframe.rs

View check run for this annotation

Codecov / codecov/patch

src/lem/multiframe.rs#L277

Added line #L277 was not covered by tests
AllocatedPtr::alloc(&mut frame_cs, || Ok(z_ptr)).expect("allocation failed")
})
.collect::<Vec<_>>()
Expand Down Expand Up @@ -339,14 +339,14 @@ impl<'a, F: LurkField, C: Coprocessor<F> + 'a> MultiFrameTrait<'a, F, C> for Mul
fn io_to_scalar_vector(
store: &Self::Store,
io: &<Self::EvalFrame as FrameLike<Ptr<F>, Ptr<F>>>::FrameIO,
) -> Result<Vec<F>, Self::StoreError> {
store.to_vector(io).map_err(|e| store::Error(e.to_string()))
) -> Vec<F> {
store.to_scalar_vector(io)
}

fn compute_witness(&self, s: &Store<F>) -> WitnessCS<F> {
let mut wcs = WitnessCS::new();

let z_scalar = s.to_vector(self.input.as_ref().unwrap()).unwrap();
let z_scalar = s.to_scalar_vector(self.input.as_ref().unwrap());

Check warning on line 349 in src/lem/multiframe.rs

View check run for this annotation

Codecov / codecov/patch

src/lem/multiframe.rs#L349

Added line #L349 was not covered by tests

let mut bogus_cs = WitnessCS::<F>::new();
let z: Vec<AllocatedNum<F>> = z_scalar
Expand Down Expand Up @@ -665,7 +665,7 @@ impl<'a, F: LurkField, C: Coprocessor<F>> Circuit<F> for MultiFrame<'a, F, C> {
|store: &Store<F>, frames: &[Frame<F>], input: &[Ptr<F>], output: &[Ptr<F>]| {
let mut allocated_input = Vec::with_capacity(input.len());
for (i, ptr) in input.iter().enumerate() {
let z_ptr = store.hash_ptr(ptr).expect("pointer hashing failed");
let z_ptr = store.hash_ptr(ptr);

let allocated_tag = AllocatedNum::alloc_infallible(
&mut cs.namespace(|| format!("allocated tag for input {i}")),
Expand All @@ -686,7 +686,7 @@ impl<'a, F: LurkField, C: Coprocessor<F>> Circuit<F> for MultiFrame<'a, F, C> {

let mut allocated_output = Vec::with_capacity(output.len());
for (i, ptr) in output.iter().enumerate() {
let z_ptr = store.hash_ptr(ptr).expect("pointer hashing failed");
let z_ptr = store.hash_ptr(ptr);

let allocated_tag = AllocatedNum::alloc_infallible(
&mut cs.namespace(|| format!("allocated tag for output {i}")),
Expand Down Expand Up @@ -758,12 +758,12 @@ impl<'a, F: LurkField, C: Coprocessor<F>> Provable<F> for MultiFrame<'a, F, C> {
let store = self.store.expect("store missing");
let mut res = Vec::with_capacity(self.public_input_size());
for ptr in input {
let z_ptr = store.hash_ptr(ptr).expect("pointer hashing failed");
let z_ptr = store.hash_ptr(ptr);
res.push(z_ptr.tag().to_field());
res.push(*z_ptr.value());
}
for ptr in output {
let z_ptr = store.hash_ptr(ptr).expect("pointer hashing failed");
let z_ptr = store.hash_ptr(ptr);
res.push(z_ptr.tag().to_field());
res.push(*z_ptr.value());
}
Expand Down Expand Up @@ -971,7 +971,7 @@ mod tests {
let frame = frames.first().unwrap();
let mut input = Vec::with_capacity(frame.input.len());
for ptr in &frame.input {
let z_ptr = store.hash_ptr(ptr).unwrap();
let z_ptr = store.hash_ptr(ptr);

Check warning on line 974 in src/lem/multiframe.rs

View check run for this annotation

Codecov / codecov/patch

src/lem/multiframe.rs#L974

Added line #L974 was not covered by tests
let allocated_tag = AllocatedNum::alloc_infallible(&mut cs, || z_ptr.tag_field());
allocated_tag.inputize(&mut cs).unwrap();
let allocated_hash = AllocatedNum::alloc_infallible(&mut cs, || *z_ptr.value());
Expand Down
Loading

0 comments on commit a791459

Please sign in to comment.