-
Notifications
You must be signed in to change notification settings - Fork 52
refactor(halo2): Decouple circuit from Midnight types #3076
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
Merged
hjeljeli32
merged 7 commits into
main
from
hjeljeli32/3037-refactor-midnight-decoupling
Mar 11, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7915c43
refactor(halo2): decouple circuit layer from Midnight types and simpl…
hjeljeli32 e4cf0b1
chore(stm): fix conflicts of rebase
hjeljeli32 da910d4
refactor(halo2): use circuit base aliases consistently and clean up t…
hjeljeli32 fddb627
chore(stm): fix conflicts of rebase
hjeljeli32 0215bbf
refactor(halo2): fix adapter exposure, cleanup type-conversion, impro…
hjeljeli32 4bf9ee0
refactor(halo2): align lottery index with STM and add lottery index g…
hjeljeli32 fbf9b8d
chore: update changelog and bump crate version
hjeljeli32 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| //! Adapters for converting STM-side structures to Halo2 circuit witness structures. | ||
|
|
||
| use digest::Digest; | ||
| use thiserror::Error; | ||
|
|
||
| use crate::circuits::halo2::types::{MerklePath as Halo2MerklePath, Position}; | ||
| use crate::membership_commitment::MerklePath as StmMerklePath; | ||
| use crate::signature_scheme::BaseFieldElement; | ||
|
|
||
| /// Errors returned when adapting STM Merkle paths to Halo2 witness paths. | ||
| #[derive(Debug, Error)] | ||
| pub enum MerklePathAdapterError { | ||
| #[error("invalid merkle digest length")] | ||
| InvalidDigestLength, | ||
| #[error("non-canonical merkle digest")] | ||
| NonCanonicalDigest, | ||
| } | ||
|
|
||
| impl<D: Digest> TryFrom<&StmMerklePath<D>> for Halo2MerklePath { | ||
| type Error = MerklePathAdapterError; | ||
|
|
||
| fn try_from(stm_path: &StmMerklePath<D>) -> Result<Self, Self::Error> { | ||
| let mut siblings = Vec::with_capacity(stm_path.values.len()); | ||
|
|
||
| for (i, value) in stm_path.values.iter().enumerate() { | ||
| let bytes: [u8; 32] = value | ||
| .as_slice() | ||
| .try_into() | ||
| .map_err(|_| MerklePathAdapterError::InvalidDigestLength)?; | ||
| let node = BaseFieldElement::from_bytes(&bytes) | ||
| .ok() | ||
| .map(|base| base.into()) | ||
| .ok_or(MerklePathAdapterError::NonCanonicalDigest)?; | ||
| let bit = (stm_path.index >> i) & 1; | ||
| // At level `i`, `bit = (index >> i) & 1`: `0` means current is left, `1` means right. | ||
| // STM uses `H(current || sibling)` for `bit == 0`, else `H(sibling || current)`. | ||
| // Map `0 -> Position::Right` and `1 -> Position::Left` so Halo2 folds identically. | ||
| let position = if bit == 0 { | ||
| Position::Right | ||
| } else { | ||
| Position::Left | ||
| }; | ||
| siblings.push((position, node)); | ||
| } | ||
|
|
||
| Ok(Halo2MerklePath::new(siblings)) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.