From d81026bc8196cea93a95746204e6b1b7854f0df6 Mon Sep 17 00:00:00 2001 From: alexeykoren <2365507+alexeykoren@users.noreply.github.com> Date: Fri, 19 Jul 2024 23:14:31 +0200 Subject: [PATCH] Extract NoteBytes --- Cargo.lock | 2 +- Cargo.toml | 4 +- src/note_encryption/orchard_domain.rs | 43 +------------------ src/note_encryption/orchard_domain_vanilla.rs | 7 +-- src/note_encryption/orchard_domain_zsa.rs | 7 +-- 5 files changed, 12 insertions(+), 51 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ff6675ce4..132a27d26 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2552,7 +2552,7 @@ dependencies = [ [[package]] name = "zcash_note_encryption" version = "0.4.0" -source = "git+https://github.com/QED-it/zcash_note_encryption?branch=zsa1#b8bd2a186fc04ec4f55b2db44df7374f03ab5725" +source = "git+https://github.com/QED-it/zcash_note_encryption?branch=note-bytes#e6023a923194cf27668702c88e6dc5ceeb361304" dependencies = [ "chacha20", "chacha20poly1305", diff --git a/Cargo.toml b/Cargo.toml index 53da31cb3..7a98c5f96 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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 diff --git a/src/note_encryption/orchard_domain.rs b/src/note_encryption/orchard_domain.rs index aa70fded6..5dfe12c76 100644 --- a/src/note_encryption/orchard_domain.rs +++ b/src/note_encryption/orchard_domain.rs @@ -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, @@ -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(pub [u8; N]); - -impl AsRef<[u8]> for NoteBytesData { - fn as_ref(&self) -> &[u8] { - &self.0 - } -} - -impl AsMut<[u8]> for NoteBytesData { - fn as_mut(&mut self) -> &mut [u8] { - &mut self.0 - } -} - -impl From<&[u8]> for NoteBytesData { - fn from(s: &[u8]) -> Self { - Self(s.try_into().unwrap()) - } -} - -impl From<(&[u8], &[u8])> for NoteBytesData { - 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 NoteBytes for NoteBytesData {} - /// 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. diff --git a/src/note_encryption/orchard_domain_vanilla.rs b/src/note_encryption/orchard_domain_vanilla.rs index d49c361da..02da495cf 100644 --- a/src/note_encryption/orchard_domain_vanilla.rs +++ b/src/note_encryption/orchard_domain_vanilla.rs @@ -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, @@ -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 { @@ -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, @@ -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; diff --git a/src/note_encryption/orchard_domain_zsa.rs b/src/note_encryption/orchard_domain_zsa.rs index 4979e8c91..baa3a349d 100644 --- a/src/note_encryption/orchard_domain_zsa.rs +++ b/src/note_encryption/orchard_domain_zsa.rs @@ -1,5 +1,6 @@ //! 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, @@ -10,7 +11,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 { @@ -50,7 +51,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, @@ -70,7 +71,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;