Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Ptr hashing functions pure #813

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> 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 @@
&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 Expand Up @@ -434,33 +434,33 @@
}

#[inline]
fn input_marker(&self) -> String {
format!(
"{}> ",
self.state
.borrow()
.fmt_to_string(self.state.borrow().get_current_package_name())
)
}

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

View check run for this annotation

Codecov / codecov/patch

src/cli/repl.rs#L437-L444

Added lines #L437 - L444 were not covered by tests

fn handle_form<'a>(&mut self, input: parser::Span<'a>, demo: bool) -> Result<parser::Span<'a>> {
let (syntax_start, mut new_input, ptr, is_meta) =
self.store.read_maybe_meta(self.state.clone(), &input)?;
if demo {
let potential_commentaries = &input[..syntax_start];
let actual_syntax = &input[syntax_start..new_input.location_offset()];
let input_marker = &self.input_marker();
if actual_syntax.contains('\n') {
// print the expression on a new line to avoid messing with the user's formatting
print!("{potential_commentaries}{input_marker}\n{actual_syntax}");
} else {
print!("{potential_commentaries}{input_marker}{actual_syntax}");
}
std::io::stdout().flush()?;

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

View check run for this annotation

Codecov / codecov/patch

src/cli/repl.rs#L450-L459

Added lines #L450 - L459 were not covered by tests
// wait for ENTER to be pressed
std::io::stdin().read_line(&mut String::new())?;

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

View check run for this annotation

Codecov / codecov/patch

src/cli/repl.rs#L461

Added line #L461 was not covered by tests
// ENTER already prints a new line so we can remove it from the start of incoming input
new_input = parser::Span::new(new_input.trim_start_matches('\n'));

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

View check run for this annotation

Codecov / codecov/patch

src/cli/repl.rs#L463

Added line #L463 was not covered by tests
}
if is_meta {
self.handle_meta(ptr)?;
Expand All @@ -473,7 +473,7 @@
pub fn load_file(&mut self, file_path: &Utf8Path, demo: bool) -> Result<()> {
let input = read_to_string(file_path)?;
if demo {
println!("Loading {file_path} in demo mode");

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

View check run for this annotation

Codecov / codecov/patch

src/cli/repl.rs#L476

Added line #L476 was not covered by tests
} else {
println!("Loading {file_path}");
}
Expand Down Expand Up @@ -515,11 +515,11 @@
}

loop {
match editor.readline(&self.input_marker()) {

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

View check run for this annotation

Codecov / codecov/patch

src/cli/repl.rs#L518

Added line #L518 was not covered by tests
Ok(line) => {
editor.save_history(history_path)?;
match self.store.read_maybe_meta(self.state.clone(), &line) {
Ok((.., expr_ptr, is_meta)) => {

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

View check run for this annotation

Codecov / codecov/patch

src/cli/repl.rs#L522

Added line #L522 was not covered by tests
if is_meta {
if let Err(e) = self.handle_meta(expr_ptr) {
println!("!Error: {e}");
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 @@
.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 @@
}

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 @@

// 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 @@
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 @@
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 @@

// 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 @@
#[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 @@
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 @@
}
}
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 @@
}
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 @@
}
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 @@
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 @@
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 @@
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 @@
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 @@
) -> 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 @@
}
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 @@
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 @@
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 @@
.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 @@
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 @@
|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 @@

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 @@
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 @@
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
Loading