forked from zcash/orchard
-
Notifications
You must be signed in to change notification settings - Fork 0
Constant-time note commitment #52
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -63,29 +63,34 @@ impl NoteCommitment { | |
| .chain(rho_bits.iter().by_vals().take(L_ORCHARD_BASE)) | ||
| .chain(psi_bits.iter().by_vals().take(L_ORCHARD_BASE)); | ||
|
|
||
| // TODO: make this constant-time. | ||
| let type_bits = BitArray::<_, Lsb0>::new(asset.to_bytes()); | ||
| let zsa_note_bits = iter::empty() | ||
| .chain(g_d_bits.iter().by_vals()) | ||
| .chain(pk_d_bits.iter().by_vals()) | ||
| .chain(v_bits.iter().by_vals()) | ||
| .chain(rho_bits.iter().by_vals().take(L_ORCHARD_BASE)) | ||
| .chain(psi_bits.iter().by_vals().take(L_ORCHARD_BASE)) | ||
| .chain(type_bits.iter().by_vals()); | ||
|
|
||
| let zec_domain = sinsemilla::CommitDomain::new(NOTE_COMMITMENT_PERSONALIZATION); | ||
| let zsa_domain = sinsemilla::CommitDomain::new(NOTE_ZSA_COMMITMENT_PERSONALIZATION); | ||
|
|
||
| let zec_hash_point = zec_domain.hash_to_point_inner(zec_note_bits); | ||
| let zsa_hash_point = zsa_domain.hash_to_point_inner(zsa_note_bits); | ||
|
|
||
| let zec_blind = zec_domain.blinding_factor(&rcm.0); | ||
| let zsa_blind = zsa_domain.blinding_factor(&rcm.0); | ||
|
|
||
| if asset.is_native().into() { | ||
| // Commit to ZEC notes as per the Orchard protocol. | ||
| Self::commit(NOTE_COMMITMENT_PERSONALIZATION, zec_note_bits, rcm) | ||
| CtOption::<pallas::Point>::from(zec_hash_point) | ||
| .map(|p| p + zec_blind) | ||
| .map(NoteCommitment) | ||
| } else { | ||
| // Commit to non-ZEC notes as per the ZSA protocol. | ||
| // Append the note type to the Orchard note encoding. | ||
| let type_bits = BitArray::<_, Lsb0>::new(asset.to_bytes()); | ||
| let zsa_note_bits = zec_note_bits.chain(type_bits.iter().by_vals()); | ||
|
|
||
| // Commit in a different domain than Orchard notes. | ||
| Self::commit(NOTE_ZSA_COMMITMENT_PERSONALIZATION, zsa_note_bits, rcm) | ||
| CtOption::<pallas::Point>::from(zsa_hash_point) | ||
| .map(|p| p + zsa_blind) | ||
| .map(NoteCommitment) | ||
| } | ||
| } | ||
|
|
||
| fn commit( | ||
| personalization: &str, | ||
| bits: impl Iterator<Item = bool>, | ||
| rcm: NoteCommitTrapdoor, | ||
| ) -> CtOption<Self> { | ||
| let domain = sinsemilla::CommitDomain::new(personalization); | ||
| domain.commit(bits, &rcm.0).map(NoteCommitment) | ||
| } | ||
| } | ||
|
|
||
| /// The x-coordinate of the commitment to a note. | ||
|
|
@@ -140,3 +145,34 @@ impl PartialEq for ExtractedNoteCommitment { | |
| } | ||
|
|
||
| impl Eq for ExtractedNoteCommitment {} | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::constants::fixed_bases::NOTE_COMMITMENT_PERSONALIZATION; | ||
| use crate::note::commitment::NoteCommitTrapdoor; | ||
| use ff::Field; | ||
| use halo2_gadgets::sinsemilla::primitives as sinsemilla; | ||
| use pasta_curves::pallas; | ||
| use rand::{rngs::OsRng, Rng}; | ||
| use subtle::CtOption; | ||
|
|
||
| #[test] | ||
| fn test_note_commit() { | ||
|
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 test does not test the You can keep this test but please describe what is being tested here. |
||
| let mut os_rng = OsRng::default(); | ||
| let msg: Vec<bool> = (0..36).map(|_| os_rng.gen::<bool>()).collect(); | ||
|
|
||
| let rcm = NoteCommitTrapdoor(pallas::Scalar::random(&mut os_rng)); | ||
|
|
||
| let domain = sinsemilla::CommitDomain::new(NOTE_COMMITMENT_PERSONALIZATION); | ||
|
|
||
| let expected_commit = domain.commit(msg.clone().into_iter(), &rcm.0); | ||
|
|
||
| let commit = { | ||
| let hash_point = domain.hash_to_point_inner(msg.into_iter()); | ||
| let blind_factor = domain.blinding_factor(&rcm.0); | ||
| CtOption::<pallas::Point>::from(hash_point).map(|p| p + blind_factor) | ||
| }; | ||
|
|
||
| assert_eq!(expected_commit.unwrap(), commit.unwrap()); | ||
| } | ||
| } | ||
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.
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.
can return the comments to emphasis the changes between the cases.