diff --git a/Cargo.toml b/Cargo.toml index 0d23a678f..05ecd466b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ codegen-units = 1 [patch.crates-io] # In development. -orchard = { git = "https://github.com/zcash/orchard.git", rev = "2c8241f25b943aa05203eacf9905db117c69bd29" } +orchard = { git = "https://github.com/zcash/orchard.git", rev = "8c018eff7e795b16fc68aed22d0fd4eebe2710ec" } incrementalmerkletree = { git = "https://github.com/zcash/incrementalmerkletree.git", rev = "b7bd6246122a6e9ace8edb51553fbf5228906cbb" } zcash_encoding = { path = "components/zcash_encoding" } zcash_note_encryption = { path = "components/zcash_note_encryption" } diff --git a/zcash_primitives/src/sapling/note_encryption.rs b/zcash_primitives/src/sapling/note_encryption.rs index a1de00bf8..e56be70b2 100644 --- a/zcash_primitives/src/sapling/note_encryption.rs +++ b/zcash_primitives/src/sapling/note_encryption.rs @@ -9,9 +9,9 @@ use std::convert::TryInto; use zcash_note_encryption::{ try_compact_note_decryption, try_note_decryption, try_output_recovery_with_ock, - try_output_recovery_with_ovk, Domain, EphemeralKeyBytes, NoteEncryption, NotePlaintextBytes, - NoteValidity, OutPlaintextBytes, OutgoingCipherKey, ShieldedOutput, COMPACT_NOTE_SIZE, - NOTE_PLAINTEXT_SIZE, OUT_PLAINTEXT_SIZE, + try_output_recovery_with_ovk, BatchDomain, Domain, EphemeralKeyBytes, NoteEncryption, + NotePlaintextBytes, NoteValidity, OutPlaintextBytes, OutgoingCipherKey, ShieldedOutput, + COMPACT_NOTE_SIZE, NOTE_PLAINTEXT_SIZE, OUT_PLAINTEXT_SIZE, }; use crate::{ @@ -185,37 +185,6 @@ impl Domain for SaplingDomain

{ kdf_sapling(dhsecret, epk) } - fn batch_kdf<'a>( - items: impl Iterator, &'a EphemeralKeyBytes)>, - ) -> Vec> { - let (shared_secrets, ephemeral_keys): (Vec<_>, Vec<_>) = items.unzip(); - - let secrets: Vec<_> = shared_secrets - .iter() - .filter_map(|s| s.map(ExtendedPoint::from)) - .collect(); - let mut secrets_affine = vec![AffinePoint::identity(); shared_secrets.len()]; - group::Curve::batch_normalize(&secrets, &mut secrets_affine); - - let mut secrets_affine = secrets_affine.into_iter(); - shared_secrets - .into_iter() - .map(|s| s.and_then(|_| secrets_affine.next())) - .zip(ephemeral_keys.into_iter()) - .map(|(secret, ephemeral_key)| { - secret.map(|dhsecret| { - Blake2bParams::new() - .hash_length(32) - .personal(KDF_SAPLING_PERSONALIZATION) - .to_state() - .update(&dhsecret.to_bytes()) - .update(ephemeral_key.as_ref()) - .finalize() - }) - }) - .collect() - } - fn note_plaintext_bytes( note: &Self::Note, to: &Self::Recipient, @@ -278,19 +247,6 @@ impl Domain for SaplingDomain

{ jubjub::ExtendedPoint::from_bytes(&ephemeral_key.0).into() } - fn batch_epk( - ephemeral_keys: impl Iterator, - ) -> Vec<(Option, EphemeralKeyBytes)> { - let ephemeral_keys: Vec<_> = ephemeral_keys.collect(); - let epks = jubjub::AffinePoint::batch_from_bytes(ephemeral_keys.iter().map(|b| b.0)); - epks.into_iter() - .zip(ephemeral_keys.into_iter()) - .map(|(epk, ephemeral_key)| { - (epk.map(jubjub::ExtendedPoint::from).into(), ephemeral_key) - }) - .collect() - } - fn check_epk_bytes NoteValidity>( note: &Note, check: F, @@ -359,6 +315,52 @@ impl Domain for SaplingDomain

{ } } +impl BatchDomain for SaplingDomain

{ + fn batch_kdf<'a>( + items: impl Iterator, &'a EphemeralKeyBytes)>, + ) -> Vec> { + let (shared_secrets, ephemeral_keys): (Vec<_>, Vec<_>) = items.unzip(); + + let secrets: Vec<_> = shared_secrets + .iter() + .filter_map(|s| s.map(ExtendedPoint::from)) + .collect(); + let mut secrets_affine = vec![AffinePoint::identity(); shared_secrets.len()]; + group::Curve::batch_normalize(&secrets, &mut secrets_affine); + + let mut secrets_affine = secrets_affine.into_iter(); + shared_secrets + .into_iter() + .map(|s| s.and_then(|_| secrets_affine.next())) + .zip(ephemeral_keys.into_iter()) + .map(|(secret, ephemeral_key)| { + secret.map(|dhsecret| { + Blake2bParams::new() + .hash_length(32) + .personal(KDF_SAPLING_PERSONALIZATION) + .to_state() + .update(&dhsecret.to_bytes()) + .update(ephemeral_key.as_ref()) + .finalize() + }) + }) + .collect() + } + + fn batch_epk( + ephemeral_keys: impl Iterator, + ) -> Vec<(Option, EphemeralKeyBytes)> { + let ephemeral_keys: Vec<_> = ephemeral_keys.collect(); + let epks = jubjub::AffinePoint::batch_from_bytes(ephemeral_keys.iter().map(|b| b.0)); + epks.into_iter() + .zip(ephemeral_keys.into_iter()) + .map(|(epk, ephemeral_key)| { + (epk.map(jubjub::ExtendedPoint::from).into(), ephemeral_key) + }) + .collect() + } +} + /// Creates a new encryption context for the given note. /// /// Setting `ovk` to `None` represents the `ovk = ⊥` case, where the note cannot be