Skip to content

Commit

Permalink
Extract NoteBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeykoren committed Jul 19, 2024
1 parent 39b479e commit d81026b
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 51 deletions.
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.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ reddsa = "0.5"
nonempty = "0.7"
serde = { version = "1.0", features = ["derive"] }
subtle = "2.3"
zcash_note_encryption_zsa = { package = "zcash_note_encryption", version = "0.4", git = "https://github.com/QED-it/zcash_note_encryption", branch = "zsa1" }
zcash_note_encryption_zsa = { package = "zcash_note_encryption", version = "0.4", git = "https://github.com/QED-it/zcash_note_encryption", branch = "note-bytes" }
incrementalmerkletree = "0.5"
zcash_spec = "0.1"
zip32 = "0.1"
Expand All @@ -60,7 +60,7 @@ bridgetree = "0.4"
criterion = "0.4" # 0.5 depends on clap 4 which has MSRV 1.70
halo2_gadgets = { git = "https://github.com/QED-it/halo2", rev = "7f5c0babd61f8ca46c9165a1adfac298d3fd3a11", features = ["test-dependencies"] }
proptest = "1.0.0"
zcash_note_encryption_zsa = { package = "zcash_note_encryption", version = "0.4", git = "https://github.com/QED-it/zcash_note_encryption", branch = "zsa1", features = ["pre-zip-212"] }
zcash_note_encryption_zsa = { package = "zcash_note_encryption", version = "0.4", git = "https://github.com/QED-it/zcash_note_encryption", branch = "note-bytes", features = ["pre-zip-212"] }
incrementalmerkletree = { version = "0.5", features = ["test-dependencies"] }
ahash = "=0.8.6" #Pinned: 0.8.7 depends on Rust 1.72

Expand Down
43 changes: 1 addition & 42 deletions src/note_encryption/orchard_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use core::fmt;

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

use crate::{
action::Action,
Expand All @@ -13,48 +14,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
7 changes: 4 additions & 3 deletions src/note_encryption/orchard_domain_vanilla.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! 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 +10,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 @@ -43,7 +44,7 @@ mod tests {
try_compact_note_decryption, try_note_decryption, try_output_recovery_with_ovk, Domain,
EphemeralKeyBytes,
};

use zcash_note_encryption_zsa::note_bytes::NoteBytesData;
use crate::{
action::Action,
address::Address,
Expand All @@ -63,7 +64,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 Down
7 changes: 4 additions & 3 deletions src/note_encryption/orchard_domain_zsa.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! This module implements the note encryption logic specific for the `OrchardZSA` flavor.