Skip to content
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ff = "0.12"
fpe = "0.5"
group = { version = "0.12.1", features = ["wnaf-memuse"] }
halo2_gadgets = { git = "https://github.com/QED-it/halo2", branch = "zsa1" }
halo2_proofs = { git = "https://github.com/QED-it/halo2", branch = "zsa1" }
halo2_proofs = { git = "https://github.com/QED-it/halo2", branch = "zsa1"}
hex = "0.4"
lazy_static = "1"
memuse = { version = "0.2.1", features = ["nonempty"] }
Expand All @@ -52,7 +52,7 @@ plotters = { version = "0.3.0", optional = true }

[dev-dependencies]
criterion = "0.3"
halo2_gadgets = { git = "https://github.com/QED-it/halo2", branch = "zsa1" , features = ["test-dependencies"] }
halo2_gadgets = { git = "https://github.com/QED-it/halo2", branch = "zsa1", features = ["test-dependencies"] }
hex = "0.4"
proptest = "1.0.0"
zcash_note_encryption = { version = "0.2", features = ["pre-zip-212"] }
Expand Down
96 changes: 70 additions & 26 deletions src/note/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bitvec::{array::BitArray, order::Lsb0};
use group::ff::{PrimeField, PrimeFieldBits};
use halo2_gadgets::sinsemilla::primitives as sinsemilla;
use pasta_curves::pallas;
use subtle::{ConstantTimeEq, CtOption};
use subtle::{ConditionallySelectable, ConstantTimeEq, CtOption};

use crate::{
constants::{
Expand Down Expand Up @@ -56,35 +56,42 @@ impl NoteCommitment {
let rho_bits = rho.to_le_bits();
let psi_bits = psi.to_le_bits();

let zec_note_bits = iter::empty()
let common_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));

// TODO: make this constant-time.
if asset.is_native().into() {
// Commit to ZEC notes as per the Orchard protocol.
Self::commit(NOTE_COMMITMENT_PERSONALIZATION, zec_note_bits, rcm)
} 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)
}
}

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)
.chain(psi_bits.iter().by_vals().take(L_ORCHARD_BASE))
.collect::<Vec<bool>>();

let zec_note_bits = common_note_bits.clone().into_iter();

let type_bits = BitArray::<_, Lsb0>::new(asset.to_bytes());
let zsa_note_bits = common_note_bits
.into_iter()
.chain(type_bits.iter().by_vals());

let zec_domain = sinsemilla::CommitDomain::new(NOTE_COMMITMENT_PERSONALIZATION);
let zsa_domain = sinsemilla::CommitDomain::new_with_personalization(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should rename it to sinsemilla::CommitDomain::new_with_blind_personalization(). Either now or remember to do it after ECC review for our PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I rename it, I have also to rename it in qedit/halo2 but the PR in qedit/halo2 has already been merged on zsa1.
I prefer to rename it after ECC review for our PR.

NOTE_ZSA_COMMITMENT_PERSONALIZATION,
NOTE_COMMITMENT_PERSONALIZATION,
);

let zec_hash_point = zec_domain.M.hash_to_point(zec_note_bits);
let zsa_hash_point = zsa_domain.M.hash_to_point(zsa_note_bits);

// Select the desired hash point in constant-time
let hash_point = zsa_hash_point.and_then(|zsa_hash| {
zec_hash_point.map(|zec_hash| {
pallas::Point::conditional_select(&zsa_hash, &zec_hash, asset.is_native())
})
});

// To evaluate the commitment from the hash_point, we could use either zec_domain or
// zsa_domain because they have both the same `R` constant.
zec_domain
.commit_from_hash_point(hash_point, &rcm.0)
.map(NoteCommitment)
}
}

Expand Down Expand Up @@ -140,3 +147,40 @@ impl PartialEq for ExtractedNoteCommitment {
}

impl Eq for ExtractedNoteCommitment {}

#[cfg(test)]
mod tests {
use crate::constants::fixed_bases::{
NOTE_COMMITMENT_PERSONALIZATION, NOTE_ZSA_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};

#[test]
fn test_commit_in_several_steps() {
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_zec = sinsemilla::CommitDomain::new(NOTE_COMMITMENT_PERSONALIZATION);
let domain_zsa = sinsemilla::CommitDomain::new_with_personalization(
NOTE_ZSA_COMMITMENT_PERSONALIZATION,
NOTE_COMMITMENT_PERSONALIZATION,
);

let expected_commit = domain_zsa.commit(msg.clone().into_iter(), &rcm.0);

// Evaluating the commitment in one step with `commit` or in two steps with `hash_to_point`
// and `commit_from_hash_point` must give the same commitment.
let hash_point = domain_zsa.M.hash_to_point(msg.into_iter());
let commit_r_zsa = domain_zsa.commit_from_hash_point(hash_point, &rcm.0);
assert_eq!(expected_commit.unwrap(), commit_r_zsa.unwrap());

// ZEC and ZSA note commitments must use the same R constant
assert_eq!(domain_zec.R(), domain_zsa.R());
}
}
63 changes: 31 additions & 32 deletions src/test_vectors/keys.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Test vectors for Orchard key components.
// From https://github.com/zcash-hackworks/zcash-test-vectors/ (orchard_key_components)

pub(crate) struct TestVector {
pub(crate) sk: [u8; 32],
Expand Down Expand Up @@ -26,7 +26,6 @@ pub(crate) struct TestVector {
}

pub(crate) fn test_vectors() -> Vec<TestVector> {
// From https://github.com/zcash-hackworks/zcash-test-vectors/blob/master/orchard_key_components.py
vec![
TestVector {
sk: [
Expand Down Expand Up @@ -654,14 +653,14 @@ pub(crate) fn test_vectors() -> Vec<TestVector> {
0xcd, 0x08, 0xcb, 0x05,
],
note_cmx: [
0x71, 0x99, 0x1e, 0xeb, 0x4b, 0x51, 0x7e, 0xd9, 0xd4, 0x31, 0xdc, 0xa9, 0xa2, 0xbb,
0xcf, 0x1c, 0xe8, 0x75, 0xe8, 0x55, 0xdf, 0xf4, 0x8c, 0x69, 0xf4, 0xef, 0x51, 0x01,
0x1e, 0x86, 0x1e, 0x07,
0xf1, 0x34, 0x33, 0x5c, 0x60, 0xf3, 0x17, 0xec, 0x48, 0xd6, 0x7b, 0x49, 0x34, 0x58,
0xde, 0x26, 0xb4, 0x06, 0xfd, 0xd2, 0x10, 0xf8, 0x60, 0xb1, 0x81, 0xaf, 0x7b, 0x82,
0xcd, 0xe9, 0x4e, 0x37,
],
note_nf: [
0xdf, 0x79, 0xd4, 0xa4, 0x6e, 0xca, 0xfd, 0x34, 0x50, 0x9d, 0x41, 0xfc, 0xfb, 0x61,
0x3e, 0x1f, 0xdc, 0xbc, 0x0d, 0x3b, 0xa0, 0xaa, 0x8d, 0xe5, 0x04, 0xd6, 0xf2, 0x54,
0xcd, 0x55, 0x12, 0x32,
0x75, 0x6c, 0x1f, 0x4b, 0xfa, 0xae, 0xc5, 0x5b, 0x42, 0x0a, 0x4f, 0x88, 0x12, 0xdb,
0x6c, 0x43, 0x2e, 0x48, 0x61, 0x6f, 0xb8, 0xd1, 0xbb, 0x2b, 0x1d, 0xd1, 0xe7, 0x2c,
0x0a, 0xa3, 0xdb, 0x3a,
],
},
TestVector {
Expand Down Expand Up @@ -760,14 +759,14 @@ pub(crate) fn test_vectors() -> Vec<TestVector> {
0xa0, 0xd4, 0x70, 0x19,
],
note_cmx: [
0x94, 0x81, 0xcb, 0xac, 0x4e, 0xb0, 0xc0, 0x8d, 0x12, 0x83, 0x47, 0x13, 0xb5, 0x15,
0xfd, 0x8c, 0x61, 0x0d, 0x44, 0xc1, 0x29, 0x50, 0x97, 0xcc, 0x20, 0x13, 0x4d, 0xe2,
0x1c, 0x63, 0x6c, 0x29,
0x07, 0x1c, 0x49, 0x8b, 0x02, 0x6b, 0x8b, 0x1a, 0x7a, 0x1e, 0x84, 0x00, 0x13, 0x87,
0x20, 0x82, 0x3d, 0x80, 0xec, 0xa4, 0x99, 0xac, 0xd2, 0x91, 0xfc, 0x07, 0xfd, 0x2f,
0x41, 0x6e, 0x83, 0x38,
],
note_nf: [
0x44, 0xc8, 0x8d, 0x03, 0x10, 0x21, 0x56, 0x11, 0x25, 0x6a, 0xad, 0x07, 0x34, 0x99,
0x43, 0x08, 0x94, 0x59, 0x73, 0x17, 0x15, 0x75, 0xae, 0x7e, 0x89, 0xf5, 0x8d, 0x44,
0xde, 0xa2, 0x67, 0x10,
0x22, 0x55, 0x5e, 0xe7, 0x85, 0x46, 0x8c, 0xc7, 0x70, 0xca, 0xf9, 0x0f, 0x82, 0x61,
0x1b, 0x80, 0x15, 0xb0, 0x3d, 0xd8, 0x85, 0x6c, 0xc9, 0xa2, 0x7c, 0xbf, 0x7f, 0x5c,
0x31, 0x3d, 0x96, 0x0e,
],
},
TestVector {
Expand Down Expand Up @@ -866,14 +865,14 @@ pub(crate) fn test_vectors() -> Vec<TestVector> {
0xe5, 0xe1, 0x71, 0x1c,
],
note_cmx: [
0xfe, 0x09, 0xf5, 0x89, 0x7c, 0xed, 0xc7, 0x67, 0x7c, 0xf5, 0x80, 0xc5, 0xf0, 0xd1,
0x83, 0xed, 0x3b, 0xc0, 0x94, 0x5c, 0xb1, 0xc3, 0x85, 0xf3, 0x88, 0xf9, 0x3f, 0x94,
0x3e, 0xff, 0x2a, 0x2f,
0x7b, 0xc1, 0xdd, 0xba, 0x52, 0x06, 0x14, 0xdd, 0x4e, 0x44, 0x3e, 0xaa, 0xf2, 0x5b,
0xea, 0xa0, 0x14, 0xc2, 0x78, 0x6d, 0x15, 0x0a, 0x3b, 0x6f, 0xea, 0x97, 0x6a, 0x9b,
0xdf, 0x27, 0x02, 0x04,
],
note_nf: [
0x80, 0xdf, 0x21, 0xbf, 0xeb, 0x46, 0x06, 0x16, 0x06, 0x29, 0x61, 0x03, 0x86, 0xc7,
0x99, 0x02, 0xef, 0x49, 0x8b, 0x61, 0xb1, 0x55, 0x0c, 0xbd, 0x10, 0x55, 0xf8, 0x26,
0x94, 0xfa, 0x5b, 0x03,
0x78, 0x31, 0xe9, 0xc5, 0x1c, 0xc1, 0xdc, 0xa6, 0xda, 0x89, 0x1a, 0xde, 0xad, 0x3a,
0x79, 0x24, 0xff, 0x26, 0x4b, 0xe4, 0x25, 0x0b, 0xec, 0x05, 0xc5, 0x92, 0xd3, 0x14,
0x52, 0x6b, 0x1a, 0x2f,
],
},
TestVector {
Expand Down Expand Up @@ -972,14 +971,14 @@ pub(crate) fn test_vectors() -> Vec<TestVector> {
0xcd, 0xdb, 0x41, 0x87,
],
note_cmx: [
0x5e, 0x54, 0xfe, 0x7e, 0x59, 0x52, 0xa6, 0xea, 0x26, 0xba, 0xbc, 0x32, 0xcf, 0xed,
0xf5, 0xe6, 0x0b, 0x56, 0x82, 0x58, 0x98, 0x0d, 0x92, 0xc1, 0xe5, 0xdd, 0x3f, 0xdf,
0x3e, 0x48, 0x7e, 0x1d,
0xea, 0xf9, 0x49, 0x1c, 0xab, 0xca, 0x20, 0x07, 0xba, 0x91, 0xea, 0xc9, 0xb3, 0xc0,
0xa1, 0x1c, 0x58, 0xef, 0x5f, 0xe0, 0x71, 0x86, 0x8c, 0x66, 0xf1, 0xc5, 0xf5, 0x9c,
0x7c, 0x99, 0x25, 0x14,
],
note_nf: [
0x45, 0x5f, 0xd0, 0xe5, 0x2e, 0x59, 0x33, 0x62, 0x57, 0x2d, 0x12, 0xdb, 0x44, 0xbb,
0xf0, 0xe0, 0x95, 0x8a, 0xf9, 0xd5, 0x8f, 0xbd, 0xf9, 0x7b, 0x85, 0x22, 0x8e, 0xca,
0xd0, 0xde, 0x7f, 0x05,
0x3a, 0xbc, 0x17, 0xad, 0xee, 0x2a, 0xa8, 0x8d, 0xf8, 0x93, 0x99, 0x4f, 0x1a, 0xf2,
0x71, 0x51, 0xc1, 0x8b, 0xa6, 0xa9, 0x89, 0xc4, 0xbf, 0xfb, 0xb9, 0xa9, 0x18, 0xd5,
0x50, 0x71, 0x95, 0x02,
],
},
TestVector {
Expand Down Expand Up @@ -1078,14 +1077,14 @@ pub(crate) fn test_vectors() -> Vec<TestVector> {
0xca, 0x26, 0xfa, 0xc4,
],
note_cmx: [
0xdf, 0x1e, 0x28, 0x2c, 0x9b, 0xf3, 0x09, 0x72, 0xb2, 0x8c, 0x46, 0xed, 0x8a, 0x54,
0x42, 0x6d, 0xae, 0xc1, 0xae, 0x38, 0x1a, 0x38, 0x5d, 0xf2, 0x5f, 0xff, 0xfa, 0x66,
0x79, 0x53, 0xd3, 0x33,
0x22, 0x0c, 0xf6, 0xb7, 0xbd, 0x7d, 0xf1, 0x1a, 0xeb, 0x46, 0xb8, 0xf6, 0x52, 0x19,
0x71, 0xe4, 0xf5, 0xce, 0xc6, 0x0e, 0x2c, 0x3a, 0xd1, 0x9e, 0x3b, 0x3f, 0x40, 0x7e,
0x69, 0x4b, 0xe8, 0x01,
],
note_nf: [
0x97, 0x0a, 0xff, 0xd7, 0xf1, 0x0d, 0x5b, 0x71, 0x23, 0x27, 0xe8, 0x39, 0x7e, 0x51,
0x45, 0x3f, 0x85, 0xf4, 0xc2, 0x0f, 0x80, 0x1e, 0x97, 0xe5, 0xe6, 0x40, 0x0e, 0xb0,
0xed, 0xc4, 0xbb, 0x03,
0x0d, 0x35, 0x9b, 0x02, 0xf6, 0x2d, 0x72, 0x18, 0xc8, 0x93, 0xd0, 0x4e, 0xc0, 0xed,
0xd4, 0x89, 0xb2, 0x33, 0xa3, 0xc6, 0x60, 0x15, 0xaf, 0x93, 0x93, 0x0b, 0x23, 0x30,
0x35, 0x82, 0x07, 0x3f,
],
},
]
Expand Down
Loading