-
Notifications
You must be signed in to change notification settings - Fork 4
feat(Merkle): Tree and Node IDs for Verify
#960
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,9 +36,17 @@ use octez_riscv_data::foldable::Fold; | |
| use octez_riscv_data::foldable::Foldable; | ||
| use octez_riscv_data::hash::Hash; | ||
| use octez_riscv_data::hash::HashFold; | ||
| use octez_riscv_data::merkle_proof::Deserialiser; | ||
| use octez_riscv_data::merkle_proof::DeserialiserNode; | ||
| use octez_riscv_data::merkle_proof::FromProof; | ||
| use octez_riscv_data::merkle_proof::Partial; | ||
| use octez_riscv_data::merkle_proof::Suspended; | ||
| use octez_riscv_data::merkle_proof::SuspendedResult; | ||
| use octez_riscv_data::mode::Mode; | ||
| use octez_riscv_data::mode::Normal; | ||
| use octez_riscv_data::mode::Prove; | ||
| use octez_riscv_data::mode::Verify; | ||
| use octez_riscv_data::mode::utils::not_found; | ||
| use octez_riscv_data::serialisation::deserialise; | ||
| use trait_set::trait_set; | ||
|
|
||
|
|
@@ -515,6 +523,110 @@ impl<R: Resolver<LazyTreeId, Tree<LazyNodeId>>> Resolver<ProveTreeId, Tree<Prove | |
| } | ||
| } | ||
|
|
||
| /// Identifier for a node resolved in [`Verify`] mode. | ||
| pub enum VerifyNodeId { | ||
| Present(Node<VerifyTreeId, Verify>), | ||
| Blind(Hash), | ||
| } | ||
|
|
||
| impl FromProof for VerifyNodeId { | ||
| fn from_proof<Proof: Deserialiser>(proof: Proof) -> SuspendedResult<Proof, Self> { | ||
| let ctx = proof.into_node()?; | ||
| match ctx.presence() { | ||
| Partial::Blinded(hash) => ctx.done(VerifyNodeId::Blind(hash)), | ||
| Partial::Present(()) => { | ||
| let (ctx, node) = Node::from_branches(ctx)?; | ||
| ctx.done(VerifyNodeId::Present(node)) | ||
| } | ||
| // SAFETY: called only in `Verify` mode | ||
| Partial::Absent => unsafe { not_found() }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Identifier for a tree resolved in [`Verify`] mode. | ||
| pub enum VerifyTreeId { | ||
| Present(Tree<Arc<VerifyNodeId>>), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep the reference counting in the node/node ID, like |
||
| Blind(Hash), | ||
| Absent, | ||
| } | ||
|
|
||
| impl FromProof for VerifyTreeId { | ||
| fn from_proof<Proof: Deserialiser>(proof: Proof) -> SuspendedResult<Proof, Self> { | ||
| let ctx = proof.into_node()?; | ||
| match ctx.presence() { | ||
| Partial::Absent => ctx.done(VerifyTreeId::Absent), | ||
| Partial::Blinded(hash) => ctx.done(VerifyTreeId::Blind(hash)), | ||
| Partial::Present(()) => { | ||
| let (ctx, present) = ctx.next_branch_with(|proof| proof.into_leaf::<bool>())?; | ||
| match present { | ||
| Partial::Present(true) => { | ||
| let (ctx, node_id) = ctx.next_branch_with(|proof| { | ||
| let suspended = VerifyNodeId::from_proof(proof)?; | ||
| Ok(suspended.map(Arc::new)) | ||
| })?; | ||
| ctx.done(VerifyTreeId::Present(Tree::from(Some(node_id)))) | ||
| } | ||
| Partial::Present(false) => ctx.done(VerifyTreeId::Present(Tree::default())), | ||
| // SAFETY: called only in `Verify` mode | ||
| Partial::Blinded(_) | Partial::Absent => unsafe { not_found() }, | ||
| } | ||
|
Comment on lines
+561
to
+573
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This portion should be done by the parser (not necessarily |
||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Adapter that projects in-memory AVL identifiers into verify-mode values. | ||
| pub struct VerifyResolver; | ||
|
|
||
| impl Resolver<VerifyNodeId, Node<VerifyTreeId, Verify>> for VerifyResolver { | ||
| fn resolve<'a>( | ||
| &self, | ||
| id: &'a VerifyNodeId, | ||
| ) -> Result<&'a Node<VerifyTreeId, Verify>, OperationalError> { | ||
| match id { | ||
| VerifyNodeId::Present(inner) => Ok(inner), | ||
| // SAFETY: called only in `Verify` mode | ||
| VerifyNodeId::Blind(_) => unsafe { not_found() }, | ||
| } | ||
| } | ||
|
|
||
| fn resolve_mut<'a>( | ||
| &mut self, | ||
| id: &'a mut VerifyNodeId, | ||
| ) -> Result<&'a mut Node<VerifyTreeId, Verify>, OperationalError> { | ||
| match id { | ||
| VerifyNodeId::Present(inner) => Ok(inner), | ||
| // SAFETY: called only in `Verify` mode | ||
| VerifyNodeId::Blind(_) => unsafe { not_found() }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Resolver<VerifyTreeId, Tree<Arc<VerifyNodeId>>> for VerifyResolver { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something doesn't add up here. Resolving |
||
| fn resolve<'a>( | ||
| &self, | ||
| id: &'a VerifyTreeId, | ||
| ) -> Result<&'a Tree<Arc<VerifyNodeId>>, OperationalError> { | ||
| match id { | ||
| VerifyTreeId::Present(inner) => Ok(inner), | ||
| // SAFETY: called only in `Verify` mode | ||
| VerifyTreeId::Blind(_) | VerifyTreeId::Absent => unsafe { not_found() }, | ||
| } | ||
| } | ||
|
|
||
| fn resolve_mut<'a>( | ||
| &mut self, | ||
| id: &'a mut VerifyTreeId, | ||
| ) -> Result<&'a mut Tree<Arc<VerifyNodeId>>, OperationalError> { | ||
| match id { | ||
| VerifyTreeId::Present(inner) => Ok(inner), | ||
| // SAFETY: called only in `Verify` mode | ||
| VerifyTreeId::Blind(_) | VerifyTreeId::Absent => unsafe { not_found() }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::sync::Arc; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps worth a comment about why
Absentdoesn't exist