Skip to content

Commit

Permalink
Reverted Hash3 and comms change
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-barrett committed Jan 3, 2024
1 parent bfc78a0 commit aad6df6
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/cli/repl/meta_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ impl MetaCmd<F> {
.store
.open(hash)
.expect("data must have been committed");
repl.hide(secret, fun)
repl.hide(*secret, *fun)
},
};
}
Expand Down
6 changes: 0 additions & 6 deletions src/cli/zstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ impl<F: LurkField> ZDag<F> {
self.0.insert(z_ptr, ZPtrType::Atom);
z_ptr
}
RawPtr::Hash3(_) => {
// `Hash3` is currently only used for commit caches, and in particular, they
// never appear as part of expressions, since commits are not pointers, but
// field elements
unreachable!()
}
RawPtr::Hash4(idx) => {
let [a, b] = expect_ptrs!(store, 2, *idx);
let a = self.populate_with(&a, store, cache);
Expand Down
8 changes: 4 additions & 4 deletions src/lem/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,13 @@ impl Block {
let Some((secret, ptr)) = store.open(hash) else {
bail!("No committed data for hash {}", &hash.hex_digits())
};
bindings.insert_ptr(tgt_ptr.clone(), ptr);
bindings.insert_ptr(tgt_ptr.clone(), *ptr);
bindings.insert_ptr(
tgt_secret.clone(),
store.intern_atom(Tag::Expr(Num), secret),
store.intern_atom(Tag::Expr(Num), *secret),
);
let secret_idx = store.intern_f(secret).0;
let vals = vec![Val::Num(secret_idx), Val::Pointer(ptr)];
let secret_idx = store.intern_f(*secret).0;
let vals = vec![Val::Num(secret_idx), Val::Pointer(*ptr)];
hints.commitment.push(Some(SlotData { vals }));
}
Op::Unit(f) => f(),
Expand Down
11 changes: 1 addition & 10 deletions src/lem/pointers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use super::Tag;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub enum RawPtr {
Atom(usize),
Hash3(usize),
Hash4(usize),
Hash6(usize),
Hash8(usize),
Expand All @@ -24,7 +23,7 @@ impl RawPtr {
pub fn is_hash(&self) -> bool {
matches!(
self,
RawPtr::Hash3(..) | RawPtr::Hash4(..) | RawPtr::Hash6(..) | RawPtr::Hash8(..)
RawPtr::Hash4(..) | RawPtr::Hash6(..) | RawPtr::Hash8(..)
)
}

Expand All @@ -36,14 +35,6 @@ impl RawPtr {
}
}

#[inline]
pub fn get_hash3(&self) -> Option<usize> {
match self {
RawPtr::Hash3(x) => Some(*x),
_ => None,
}
}

#[inline]
pub fn get_hash4(&self) -> Option<usize> {
match self {
Expand Down
46 changes: 8 additions & 38 deletions src/lem/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ use super::pointers::{Ptr, RawPtr, ZPtr};
#[derive(Debug)]
pub struct Store<F: LurkField> {
f_elts: FrozenIndexSet<Box<FWrap<F>>>,
hash3: FrozenIndexSet<Box<[RawPtr; 3]>>,
hash4: FrozenIndexSet<Box<[RawPtr; 4]>>,
hash6: FrozenIndexSet<Box<[RawPtr; 6]>>,
hash8: FrozenIndexSet<Box<[RawPtr; 8]>>,
Expand All @@ -53,6 +52,8 @@ pub struct Store<F: LurkField> {
ptr_string_cache: FrozenMap<Ptr, String>,
ptr_symbol_cache: FrozenMap<Ptr, Box<Symbol>>,

comms: FrozenMap<FWrap<F>, Box<(F, Ptr)>>, // hash -> (secret, src)

pub poseidon_cache: PoseidonCache<F>,
pub inverse_poseidon_cache: InversePoseidonCache<F>,

Expand Down Expand Up @@ -83,14 +84,14 @@ impl<F: LurkField> Default for Store<F> {

Self {
f_elts,
hash3: Default::default(),
hash4: Default::default(),
hash6: Default::default(),
hash8: Default::default(),
string_ptr_cache: Default::default(),
symbol_ptr_cache: Default::default(),
ptr_string_cache: Default::default(),
ptr_symbol_cache: Default::default(),
comms: Default::default(),
poseidon_cache,
inverse_poseidon_cache: Default::default(),
dehydrated: Default::default(),
Expand Down Expand Up @@ -273,7 +274,6 @@ impl<F: LurkField> Store<F> {
}};
}
match N {
3 => intern!(Hash3, hash3, 3),
4 => intern!(Hash4, hash4, 4),
6 => intern!(Hash6, hash6, 6),
8 => intern!(Hash8, hash8, 8),
Expand Down Expand Up @@ -317,7 +317,6 @@ impl<F: LurkField> Store<F> {
}};
}
match N {
3 => fetch!(hash3, 3),
4 => fetch!(hash4, 4),
6 => fetch!(hash6, 6),
8 => fetch!(hash8, 8),
Expand Down Expand Up @@ -599,21 +598,13 @@ impl<F: LurkField> Store<F> {

#[inline]
pub fn add_comm(&self, hash: F, secret: F, payload: Ptr) {
let ptrs = [
self.intern_raw_atom(secret),
self.tag(*payload.tag()),
*payload.raw(),
];
let (idx, _) = self.hash3.insert_probe(Box::new(ptrs));
let ptr = RawPtr::Hash3(idx);
let z = FWrap(hash);
self.z_cache.insert(ptr, Box::new(z));
self.inverse_z_cache.insert(z, Box::new(ptr));
self.comms
.insert(FWrap::<F>(hash), Box::new((secret, payload)));
}

#[inline]
pub fn hide(&self, secret: F, payload: Ptr) -> Ptr {
self.comm(self.hide_ptr(secret, payload))
self.comm(self.hide_and_return_z_payload(secret, payload).0)
}

pub fn hide_and_return_z_payload(&self, secret: F, payload: Ptr) -> (F, ZPtr<F>) {
Expand All @@ -631,22 +622,8 @@ impl<F: LurkField> Store<F> {
}

#[inline]
pub fn open(&self, hash: F) -> Option<(F, Ptr)> {
let cached = self.inverse_z_cache.get(&FWrap(hash))?;
let [f, tag, pay] = self.fetch_raw_ptrs::<3>(cached.get_hash3()?)?;
let f = self.fetch_f(f.get_atom()?)?;
let ptr = self.raw_to_ptr(tag, pay)?;
Some((*f, ptr))
}

pub fn hide_ptr(&self, secret: F, payload: Ptr) -> F {
let hash = self.poseidon_cache.hash3(&[
secret,
payload.tag().to_field(),
self.hash_raw_ptr(payload.raw()).0,
]);
self.add_comm(hash, secret, payload);
hash
pub fn open(&self, hash: F) -> Option<&(F, Ptr)> {
self.comms.get(&FWrap(hash))
}

#[inline]
Expand Down Expand Up @@ -856,7 +833,6 @@ impl<F: LurkField> Store<F> {
}
match ptr {
RawPtr::Atom(idx) => FWrap(*self.expect_f(*idx)),
RawPtr::Hash3(idx) => hash_raw!(hash3, 3, *idx),
RawPtr::Hash4(idx) => hash_raw!(hash4, 4, *idx),
RawPtr::Hash6(idx) => hash_raw!(hash6, 6, *idx),
RawPtr::Hash8(idx) => hash_raw!(hash8, 8, *idx),
Expand Down Expand Up @@ -922,12 +898,6 @@ impl<F: LurkField> Store<F> {
while let Some(ptr) = stack.pop() {
match ptr {
RawPtr::Atom(..) => (),
RawPtr::Hash3(idx) => {
let ptrs = self.expect_raw_ptrs::<3>(*idx);
for ptr in ptrs {
feed_loop!(ptr)
}
}
RawPtr::Hash4(idx) => {
let ptrs = self.expect_raw_ptrs::<4>(*idx);
for ptr in ptrs {
Expand Down

0 comments on commit aad6df6

Please sign in to comment.