Skip to content
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

Synchronize Orchard with updates from zcash_note_encryption for zcash PR #2 issues resolve #111

Merged
merged 7 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ rustdoc-args = ["--cfg", "docsrs", "--html-in-header", "katex-header.html"]
[dependencies]
aes = "0.8"
bitvec = "1"
blake2b_simd = "=1.0.1" # Last version required rust 1.66
blake2b_simd = "1"
half = "=2.2.1" # Last version requires Rust 1.70
ff = "0.13"
fpe = "0.6"
Expand Down
18 changes: 12 additions & 6 deletions src/note_encryption/compact_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::fmt;

use zcash_note_encryption_zsa::{EphemeralKeyBytes, ShieldedOutput};
use zcash_note_encryption_zsa::{note_bytes::NoteBytes, EphemeralKeyBytes, ShieldedOutput};

use crate::{
action::Action,
Expand All @@ -25,7 +25,10 @@ impl<A, D: OrchardDomainCommon> ShieldedOutput<OrchardDomain<D>> for Action<A, D
}

fn enc_ciphertext_compact(&self) -> D::CompactNoteCiphertextBytes {
self.encrypted_note().enc_ciphertext.as_ref()[..D::COMPACT_NOTE_SIZE].into()
D::CompactNoteCiphertextBytes::from_slice(
&self.encrypted_note().enc_ciphertext.as_ref()[..D::COMPACT_NOTE_SIZE],
)
.unwrap()
}
}

Expand Down Expand Up @@ -71,7 +74,7 @@ impl<D: OrchardDomainCommon> ShieldedOutput<OrchardDomain<D>> for CompactAction<
}

fn enc_ciphertext_compact(&self) -> D::CompactNoteCiphertextBytes {
self.enc_ciphertext
D::CompactNoteCiphertextBytes::from_slice(self.enc_ciphertext.as_ref()).unwrap()
}
}

Expand Down Expand Up @@ -112,7 +115,7 @@ impl<D: OrchardDomainCommon> CompactAction<D> {
pub mod testing {
use rand::RngCore;

use zcash_note_encryption_zsa::{Domain, NoteEncryption};
use zcash_note_encryption_zsa::{note_bytes::NoteBytes, Domain, NoteEncryption, MEMO_SIZE};

use crate::{
address::Address,
Expand Down Expand Up @@ -145,7 +148,7 @@ pub mod testing {
}
};
let note = Note::from_parts(recipient, value, AssetBase::native(), rho, rseed).unwrap();
let encryptor = NoteEncryption::<OrchardDomain<D>>::new(ovk, note, [0u8; 512]);
let encryptor = NoteEncryption::<OrchardDomain<D>>::new(ovk, note, [0u8; MEMO_SIZE]);
let cmx = ExtractedNoteCommitment::from(note.commitment());
let ephemeral_key = OrchardDomain::<D>::epk_bytes(encryptor.epk());
let enc_ciphertext = encryptor.encrypt_note_plaintext();
Expand All @@ -155,7 +158,10 @@ pub mod testing {
nullifier: nf_old,
cmx,
ephemeral_key,
enc_ciphertext: enc_ciphertext.as_ref()[..52].try_into().unwrap(),
enc_ciphertext: D::CompactNoteCiphertextBytes::from_slice(
&enc_ciphertext.as_ref()[..D::COMPACT_NOTE_SIZE],
)
.unwrap(),
},
note,
)
Expand Down
13 changes: 8 additions & 5 deletions src/note_encryption/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use group::ff::PrimeField;
use blake2b_simd::Params;

use zcash_note_encryption_zsa::{
BatchDomain, Domain, EphemeralKeyBytes, OutPlaintextBytes, OutgoingCipherKey, MEMO_SIZE,
OUT_PLAINTEXT_SIZE,
note_bytes::NoteBytes, BatchDomain, Domain, EphemeralKeyBytes, OutPlaintextBytes,
OutgoingCipherKey, MEMO_SIZE, OUT_PLAINTEXT_SIZE,
};

use crate::{
Expand Down Expand Up @@ -255,12 +255,15 @@ impl<D: OrchardDomainCommon> Domain for OrchardDomain<D> {
parse_note_plaintext_without_memo::<D, _>(self.rho, plaintext, |_| Some(*pk_d))
}

fn extract_memo(
fn split_plaintext_at_memo(
&self,
plaintext: &D::NotePlaintextBytes,
) -> (Self::CompactNotePlaintextBytes, Self::Memo) {
) -> Option<(Self::CompactNotePlaintextBytes, Self::Memo)> {
let (compact, memo) = plaintext.as_ref().split_at(D::COMPACT_NOTE_SIZE);
(compact.into(), memo.try_into().unwrap())
Some((
Self::CompactNotePlaintextBytes::from_slice(compact)?,
memo.try_into().ok()?,
))
}

fn extract_pk_d(out_plaintext: &OutPlaintextBytes) -> Option<Self::DiversifiedTransmissionKey> {
Expand Down
44 changes: 1 addition & 43 deletions src/note_encryption/orchard_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use core::fmt;

use zcash_note_encryption_zsa::{AEAD_TAG_SIZE, MEMO_SIZE};
use zcash_note_encryption_zsa::{note_bytes::NoteBytes, AEAD_TAG_SIZE, MEMO_SIZE};

use crate::{
action::Action,
Expand All @@ -13,48 +13,6 @@ use crate::{

use super::{compact_action::CompactAction, domain::Memo};

/// Represents a fixed-size array of bytes for note components.
#[derive(Clone, Copy, Debug)]
pub struct NoteBytesData<const N: usize>(pub [u8; N]);

impl<const N: usize> AsRef<[u8]> for NoteBytesData<N> {
fn as_ref(&self) -> &[u8] {
&self.0
}
}

impl<const N: usize> AsMut<[u8]> for NoteBytesData<N> {
fn as_mut(&mut self) -> &mut [u8] {
&mut self.0
}
}

impl<const N: usize> From<&[u8]> for NoteBytesData<N> {
fn from(s: &[u8]) -> Self {
Self(s.try_into().unwrap())
}
}

impl<const N: usize> From<(&[u8], &[u8])> for NoteBytesData<N> {
fn from(s: (&[u8], &[u8])) -> Self {
Self([s.0, s.1].concat().try_into().unwrap())
}
}

/// Provides a unified interface for handling fixed-size byte arrays used in Orchard note encryption.
pub trait NoteBytes:
AsRef<[u8]>
+ AsMut<[u8]>
+ for<'a> From<&'a [u8]>
+ for<'a> From<(&'a [u8], &'a [u8])>
+ Clone
+ Copy
+ Send
{
}

impl<const N: usize> NoteBytes for NoteBytesData<N> {}

/// Represents the Orchard protocol domain specifics required for note encryption and decryption.
pub trait OrchardDomainCommon: fmt::Debug + Clone {
/// The size of a compact note, specific to the Orchard protocol.
Expand Down
12 changes: 7 additions & 5 deletions src/note_encryption/orchard_domain_vanilla.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! This module implements the note encryption logic specific for the `OrchardVanilla` flavor.

use zcash_note_encryption_zsa::note_bytes::NoteBytesData;

use crate::{
note::{AssetBase, Note},
orchard_flavor::OrchardVanilla,
Expand All @@ -9,7 +11,7 @@ use super::{
domain::{
build_base_note_plaintext_bytes, Memo, COMPACT_NOTE_SIZE_VANILLA, NOTE_VERSION_BYTE_V2,
},
orchard_domain::{NoteBytesData, OrchardDomainCommon},
orchard_domain::OrchardDomainCommon,
};

impl OrchardDomainCommon for OrchardVanilla {
Expand Down Expand Up @@ -40,8 +42,8 @@ mod tests {
use rand::rngs::OsRng;

use zcash_note_encryption_zsa::{
try_compact_note_decryption, try_note_decryption, try_output_recovery_with_ovk, Domain,
EphemeralKeyBytes,
note_bytes::NoteBytesData, try_compact_note_decryption, try_note_decryption,
try_output_recovery_with_ovk, Domain, EphemeralKeyBytes,
};

use crate::{
Expand All @@ -63,7 +65,7 @@ mod tests {
use super::super::{
compact_action::CompactAction,
domain::{parse_note_plaintext_without_memo, parse_note_version, prf_ock_orchard},
orchard_domain::{NoteBytesData, OrchardDomain},
orchard_domain::OrchardDomain,
};

type OrchardDomainVanilla = OrchardDomain<OrchardVanilla>;
Expand All @@ -85,7 +87,7 @@ mod tests {

// Decode.
let domain = OrchardDomainVanilla::for_rho(rho);
let (compact, parsed_memo) = domain.extract_memo(&plaintext);
let (compact, parsed_memo) = domain.split_plaintext_at_memo(&plaintext).unwrap();

assert!(parse_note_version(compact.as_ref()).is_some());

Expand Down
12 changes: 7 additions & 5 deletions src/note_encryption/orchard_domain_zsa.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! This module implements the note encryption logic specific for the `OrchardZSA` flavor.

use zcash_note_encryption_zsa::note_bytes::NoteBytesData;

use crate::{
note::{AssetBase, Note},
orchard_flavor::OrchardZSA,
Expand All @@ -10,7 +12,7 @@ use super::{
build_base_note_plaintext_bytes, Memo, COMPACT_NOTE_SIZE_VANILLA, COMPACT_NOTE_SIZE_ZSA,
NOTE_VERSION_BYTE_V3,
},
orchard_domain::{NoteBytesData, OrchardDomainCommon},
orchard_domain::OrchardDomainCommon,
};

impl OrchardDomainCommon for OrchardZSA {
Expand Down Expand Up @@ -47,8 +49,8 @@ mod tests {
use rand::rngs::OsRng;

use zcash_note_encryption_zsa::{
try_compact_note_decryption, try_note_decryption, try_output_recovery_with_ovk, Domain,
EphemeralKeyBytes,
note_bytes::NoteBytesData, try_compact_note_decryption, try_note_decryption,
try_output_recovery_with_ovk, Domain, EphemeralKeyBytes,
};

use crate::{
Expand All @@ -70,7 +72,7 @@ mod tests {
use super::super::{
compact_action::CompactAction,
domain::{parse_note_plaintext_without_memo, parse_note_version, prf_ock_orchard},
orchard_domain::{NoteBytesData, OrchardDomain},
orchard_domain::OrchardDomain,
};

type OrchardDomainZSA = OrchardDomain<OrchardZSA>;
Expand All @@ -92,7 +94,7 @@ mod tests {

// Decode.
let domain = OrchardDomainZSA::for_rho(rho);
let (compact, parsed_memo) = domain.extract_memo(&plaintext);
let (compact, parsed_memo) = domain.split_plaintext_at_memo(&plaintext).unwrap();

assert!(parse_note_version(compact.as_ref()).is_some());

Expand Down
Loading