From 3a54c7281bacf59fe8dcffc6d9b82db60ae465f6 Mon Sep 17 00:00:00 2001 From: Dmitry Demin Date: Thu, 8 Aug 2024 12:32:46 +0200 Subject: [PATCH 1/8] Refactor enc_ciphertext to return reference instead of copy These changes were discussed and suggested in PR zcash_note_encryption#2 --- src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4d18376..292bc9a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -374,7 +374,7 @@ pub trait ShieldedOutput { fn cmstar_bytes(&self) -> D::ExtractedCommitmentBytes; /// Exposes the note ciphertext of the output. Returns `None` if the output is compact. - fn enc_ciphertext(&self) -> Option; + fn enc_ciphertext(&self) -> Option<&D::NoteCiphertextBytes>; // FIXME: Should we return `Option` or // `&D::CompactNoteCiphertextBytes` instead? (complexity)? @@ -383,8 +383,7 @@ pub trait ShieldedOutput { //// Splits the AEAD tag from the ciphertext. fn split_ciphertext_at_tag(&self) -> Option<(D::NotePlaintextBytes, [u8; AEAD_TAG_SIZE])> { - let enc_ciphertext = self.enc_ciphertext()?; - let enc_ciphertext_bytes = enc_ciphertext.as_ref(); + let enc_ciphertext_bytes = self.enc_ciphertext()?.as_ref(); let (plaintext, tail) = enc_ciphertext_bytes .len() From 606c82c86a13ce4806a5feb39167249a9f51fdaa Mon Sep 17 00:00:00 2001 From: Dmitry Demin Date: Thu, 8 Aug 2024 13:15:59 +0200 Subject: [PATCH 2/8] Remove extra spaces in rust-toolchain.toml --- rust-toolchain.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-toolchain.toml b/rust-toolchain.toml index bd93f58..57237a5 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] channel = "1.56.1" -components = [ "clippy", "rustfmt" ] +components = ["clippy", "rustfmt"] From e0cfd5b13494f0f2e5f6a93ee11649464ac7c630 Mon Sep 17 00:00:00 2001 From: Dmitry Demin Date: Thu, 8 Aug 2024 13:16:43 +0200 Subject: [PATCH 3/8] Restore the original order of const definition to reduce PR diff --- src/lib.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 292bc9a..421529b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,28 +44,22 @@ pub mod note_bytes; use note_bytes::NoteBytes; -/// The size of a compact note for Sapling and Orchard Vanilla. +/// The size of a compact note for Sapling and pre-ZSA Orchard. pub const COMPACT_NOTE_SIZE: usize = 1 + // version 11 + // diversifier 8 + // value 32; // rseed (or rcm prior to ZIP 212) -/// The size of `NotePlaintextBytes` for Sapling and Orchard Vanilla. +/// The size of [`NotePlaintextBytes`] for Sapling and pre-ZSA Orchard. pub const NOTE_PLAINTEXT_SIZE: usize = COMPACT_NOTE_SIZE + 512; - -/// The size of the memo. -pub const MEMO_SIZE: usize = 512; -/// The size of the authentication tag used for note encryption. -pub const AEAD_TAG_SIZE: usize = 16; - /// The size of [`OutPlaintextBytes`]. pub const OUT_PLAINTEXT_SIZE: usize = 32 + // pk_d 32; // esk +const AEAD_TAG_SIZE: usize = 16; +/// The size of an encrypted note plaintext for Sapling and pre-ZSA Orchard. +pub const ENC_CIPHERTEXT_SIZE: usize = NOTE_PLAINTEXT_SIZE + AEAD_TAG_SIZE; /// The size of an encrypted outgoing plaintext. pub const OUT_CIPHERTEXT_SIZE: usize = OUT_PLAINTEXT_SIZE + AEAD_TAG_SIZE; -/// The size of an encrypted note plaintext for Sapling and Orchard Vanilla. -pub const ENC_CIPHERTEXT_SIZE: usize = NOTE_PLAINTEXT_SIZE + AEAD_TAG_SIZE; - /// A symmetric key that can be used to recover a single Sapling or Orchard output. pub struct OutgoingCipherKey(pub [u8; 32]); From 445d10914c936174d0bb991d3503d16a9787923b Mon Sep 17 00:00:00 2001 From: Dmitry Demin Date: Thu, 8 Aug 2024 13:21:19 +0200 Subject: [PATCH 4/8] Fix the comment for split_plaintext_at_memo --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 421529b..25a7490 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -264,7 +264,8 @@ pub trait Domain { plaintext: &Self::CompactNotePlaintextBytes, ) -> Option<(Self::Note, Self::Recipient)>; - /// Splits the memo field from the given note plaintext. + /// Splits the given note plaintext into the compact part (containing the note) and + /// the memo field. /// /// # Compatibility /// From b7221e552c8ed88d0a46c3e5f663a3684f5450cd Mon Sep 17 00:00:00 2001 From: Dmitry Demin Date: Thu, 8 Aug 2024 13:29:44 +0200 Subject: [PATCH 5/8] Fix docstring for NOTE_PLAINTEXT_SIZE --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 25a7490..e04b8d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,7 +49,7 @@ pub const COMPACT_NOTE_SIZE: usize = 1 + // version 11 + // diversifier 8 + // value 32; // rseed (or rcm prior to ZIP 212) -/// The size of [`NotePlaintextBytes`] for Sapling and pre-ZSA Orchard. +/// The size of [`Domain::NotePlaintextBytes`] for Sapling and pre-ZSA Orchard. pub const NOTE_PLAINTEXT_SIZE: usize = COMPACT_NOTE_SIZE + 512; /// The size of [`OutPlaintextBytes`]. pub const OUT_PLAINTEXT_SIZE: usize = 32 + // pk_d From c66c8a234c4e816ffabe7b2e83e2bc73bd557616 Mon Sep 17 00:00:00 2001 From: Dmitry Demin Date: Thu, 8 Aug 2024 14:40:25 +0200 Subject: [PATCH 6/8] Update CHANGELOG --- CHANGELOG.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b464b50..e682b2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,8 +11,22 @@ and this library adheres to Rust's notion of abstract types `NotePlaintextBytes`, `NoteCiphertextBytes`, `CompactNotePlaintextBytes`, and `CompactNoteCiphertextBytes` to the `Domain` trait. -- Moved the specific constants into the `Domain` trait implementations. - +- Removed the separate `NotePlaintextBytes` type definition (as it is now an + associated type). +- Added new `parse_note_plaintext_bytes`, `parse_note_ciphertext_bytes`, and + `parse_compact_note_plaintext_bytes` methods to the `Domain` trait. +- Updated the `note_plaintext_bytes` method of the `Domain` trait to return the + `NotePlaintextBytes` associated type. +- Updated the `encrypt_note_plaintext` method of `NoteEncryption` to return the + `NoteCiphertextBytes` associated type of the `Domain` instead of the explicit + array. +- Updated the `enc_ciphertext` method of the `ShieldedOutput` trait to return an + `Option` of a reference instead of a copy. +- Moved the specific constants into the `Domain` trait implementations, while + keeping the original constants for backward compatibility. +- Added new `note_bytes` module with helper trait and struct to deal with note + bytes data with abstracted underlying array size. + ## [0.4.0] - 2023-06-06 ### Changed - The `esk` and `ephemeral_key` arguments have been removed from From ded868d5d808086f80883ac3a243571bce18ef8e Mon Sep 17 00:00:00 2001 From: Dmitry Demin Date: Mon, 12 Aug 2024 15:57:14 +0200 Subject: [PATCH 7/8] Remove unused constants COMPACT_NOTE_SIZE, NOTE_PLAINTEXT_SIZE, ENC_CIPHERTEXT_SIZE, and update CHANGELOG accordingly --- CHANGELOG.md | 6 ++++-- src/lib.rs | 9 --------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e682b2b..2bf28b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this library adheres to Rust's notion of ## [Unreleased] ### Changed +- Breaking change: removed the constants `COMPACT_NOTE_SIZE`, + `NOTE_PLAINTEXT_SIZE`, and `ENC_CIPHERTEXT_SIZE` as they are no longer used + in the `zcash_note_encryption`, orchard`, or `sapling-crypto` crates. - Generalized the note plaintext size to support variable sizes by adding the abstract types `NotePlaintextBytes`, `NoteCiphertextBytes`, `CompactNotePlaintextBytes`, and `CompactNoteCiphertextBytes` to the `Domain` @@ -22,8 +25,7 @@ and this library adheres to Rust's notion of array. - Updated the `enc_ciphertext` method of the `ShieldedOutput` trait to return an `Option` of a reference instead of a copy. -- Moved the specific constants into the `Domain` trait implementations, while - keeping the original constants for backward compatibility. +- Moved the specific constants into the `Domain` trait implementations. - Added new `note_bytes` module with helper trait and struct to deal with note bytes data with abstracted underlying array size. diff --git a/src/lib.rs b/src/lib.rs index e04b8d5..3e28501 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,19 +44,10 @@ pub mod note_bytes; use note_bytes::NoteBytes; -/// The size of a compact note for Sapling and pre-ZSA Orchard. -pub const COMPACT_NOTE_SIZE: usize = 1 + // version - 11 + // diversifier - 8 + // value - 32; // rseed (or rcm prior to ZIP 212) -/// The size of [`Domain::NotePlaintextBytes`] for Sapling and pre-ZSA Orchard. -pub const NOTE_PLAINTEXT_SIZE: usize = COMPACT_NOTE_SIZE + 512; /// The size of [`OutPlaintextBytes`]. pub const OUT_PLAINTEXT_SIZE: usize = 32 + // pk_d 32; // esk const AEAD_TAG_SIZE: usize = 16; -/// The size of an encrypted note plaintext for Sapling and pre-ZSA Orchard. -pub const ENC_CIPHERTEXT_SIZE: usize = NOTE_PLAINTEXT_SIZE + AEAD_TAG_SIZE; /// The size of an encrypted outgoing plaintext. pub const OUT_CIPHERTEXT_SIZE: usize = OUT_PLAINTEXT_SIZE + AEAD_TAG_SIZE; From 6c1f21bca45adf6b1a0385ba26aa6756d0523573 Mon Sep 17 00:00:00 2001 From: Dmitry Demin Date: Mon, 12 Aug 2024 18:51:30 +0200 Subject: [PATCH 8/8] Minor improvement in CHANGELOG.md --- CHANGELOG.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bf28b7..9ec5ec9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,9 @@ and this library adheres to Rust's notion of ## [Unreleased] ### Changed -- Breaking change: removed the constants `COMPACT_NOTE_SIZE`, - `NOTE_PLAINTEXT_SIZE`, and `ENC_CIPHERTEXT_SIZE` as they are no longer used - in the `zcash_note_encryption`, orchard`, or `sapling-crypto` crates. +- **Breaking change:** removed the constants `COMPACT_NOTE_SIZE`, + `NOTE_PLAINTEXT_SIZE`, and `ENC_CIPHERTEXT_SIZE` as they are now + implementation spesific (located in `orchard` and `sapling-crypto` crates). - Generalized the note plaintext size to support variable sizes by adding the abstract types `NotePlaintextBytes`, `NoteCiphertextBytes`, `CompactNotePlaintextBytes`, and `CompactNoteCiphertextBytes` to the `Domain` @@ -25,8 +25,7 @@ and this library adheres to Rust's notion of array. - Updated the `enc_ciphertext` method of the `ShieldedOutput` trait to return an `Option` of a reference instead of a copy. -- Moved the specific constants into the `Domain` trait implementations. -- Added new `note_bytes` module with helper trait and struct to deal with note +- Added a new `note_bytes` module with helper trait and struct to deal with note bytes data with abstracted underlying array size. ## [0.4.0] - 2023-06-06