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

Refactor enc_ciphertext to return reference instead of copy #13

Merged
merged 8 commits into from
Aug 12, 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
19 changes: 17 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,27 @@ 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 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`
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.
- 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
### Changed
- The `esk` and `ephemeral_key` arguments have been removed from
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.56.1"
components = [ "clippy", "rustfmt" ]
components = ["clippy", "rustfmt"]
25 changes: 5 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,13 @@ pub mod note_bytes;

use note_bytes::NoteBytes;

/// The size of a compact note for Sapling and Orchard Vanilla.
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.
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 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]);

Expand Down Expand Up @@ -270,7 +255,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
///
Expand Down Expand Up @@ -374,7 +360,7 @@ pub trait ShieldedOutput<D: Domain> {
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<D::NoteCiphertextBytes>;
fn enc_ciphertext(&self) -> Option<&D::NoteCiphertextBytes>;

// FIXME: Should we return `Option<D::CompactNoteCiphertextBytes>` or
// `&D::CompactNoteCiphertextBytes` instead? (complexity)?
Expand All @@ -383,8 +369,7 @@ pub trait ShieldedOutput<D: Domain> {

//// 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()
Expand Down