Skip to content

Commit

Permalink
Add MEMO_SIZE constant and use it instead of the hardcoded 512 for th…
Browse files Browse the repository at this point in the history
…e memo array size
  • Loading branch information
dmidem committed Aug 13, 2024
1 parent 9101e2c commit 335bb34
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
},
circuit,
keys::{OutgoingViewingKey, SpendAuthorizingKey, SpendValidatingKey},
note_encryption::{sapling_note_encryption, Zip212Enforcement},
note_encryption::{sapling_note_encryption, Zip212Enforcement, MEMO_SIZE},
prover::{OutputProver, SpendProver},
util::generate_random_rseed_internal,
value::{
Expand Down Expand Up @@ -281,7 +281,7 @@ pub struct OutputInfo {
ovk: Option<OutgoingViewingKey>,
to: PaymentAddress,
value: NoteValue,
memo: [u8; 512],
memo: [u8; MEMO_SIZE],
}

impl OutputInfo {
Expand All @@ -290,14 +290,14 @@ impl OutputInfo {
ovk: Option<OutgoingViewingKey>,
to: PaymentAddress,
value: NoteValue,
memo: Option<[u8; 512]>,
memo: Option<[u8; MEMO_SIZE]>,
) -> Self {
Self {
ovk,
to,
value,
memo: memo.unwrap_or_else(|| {
let mut memo = [0; 512];
let mut memo = [0; MEMO_SIZE];
memo[0] = 0xf6;
memo
}),
Expand Down Expand Up @@ -353,7 +353,7 @@ struct PreparedOutputInfo {
/// `None` represents the `ovk = ⊥` case.
ovk: Option<OutgoingViewingKey>,
note: Note,
memo: [u8; 512],
memo: [u8; MEMO_SIZE],
rcv: ValueCommitTrapdoor,
}

Expand Down Expand Up @@ -523,7 +523,7 @@ impl Builder {
ovk: Option<OutgoingViewingKey>,
to: PaymentAddress,
value: NoteValue,
memo: Option<[u8; 512]>,
memo: Option<[u8; MEMO_SIZE]>,
) -> Result<(), Error> {
let output = OutputInfo::new(ovk, to, value, memo);

Expand Down
21 changes: 12 additions & 9 deletions src/note_encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ use zcash_note_encryption::{
OutPlaintextBytes, OutgoingCipherKey, ShieldedOutput, AEAD_TAG_SIZE, OUT_PLAINTEXT_SIZE,
};

/// The size of the memo.
pub(crate) const MEMO_SIZE: usize = 512;

/// The size of a compact note.
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`].
pub const NOTE_PLAINTEXT_SIZE: usize = COMPACT_NOTE_SIZE + 512;
pub const NOTE_PLAINTEXT_SIZE: usize = COMPACT_NOTE_SIZE + MEMO_SIZE;

/// The size of an encrypted note plaintext.
pub const ENC_CIPHERTEXT_SIZE: usize = NOTE_PLAINTEXT_SIZE + AEAD_TAG_SIZE;
Expand Down Expand Up @@ -154,7 +157,7 @@ impl Domain for SaplingDomain {
type ValueCommitment = ValueCommitment;
type ExtractedCommitment = ExtractedNoteCommitment;
type ExtractedCommitmentBytes = [u8; 32];
type Memo = [u8; 512];
type Memo = [u8; MEMO_SIZE];

type NotePlaintextBytes = NoteBytesData<{ NOTE_PLAINTEXT_SIZE }>;
type NoteCiphertextBytes = NoteBytesData<{ ENC_CIPHERTEXT_SIZE }>;
Expand Down Expand Up @@ -408,14 +411,14 @@ impl ShieldedOutput<SaplingDomain> for CompactOutputDescription {
/// let note = to.create_note(value, rseed);
/// let cmu = note.cmu();
///
/// let mut enc = sapling_note_encryption(ovk, note, [0x37; 512], &mut rng);
/// let mut enc = sapling_note_encryption(ovk, note, [0x37; MEMO_SIZE], &mut rng);
/// let encCiphertext = enc.encrypt_note_plaintext();
/// let outCiphertext = enc.encrypt_outgoing_plaintext(&cv, &cmu, &mut rng);
/// ```
pub fn sapling_note_encryption<R: RngCore>(
ovk: Option<OutgoingViewingKey>,
note: Note,
memo: [u8; 512],
memo: [u8; MEMO_SIZE],
rng: &mut R,
) -> NoteEncryption<SaplingDomain> {
let esk = note.generate_or_derive_esk_internal(rng);
Expand All @@ -436,7 +439,7 @@ pub fn try_sapling_note_decryption<Output: ShieldedOutput<SaplingDomain>>(
ivk: &PreparedIncomingViewingKey,
output: &Output,
zip212_enforcement: Zip212Enforcement,
) -> Option<(Note, PaymentAddress, [u8; 512])> {
) -> Option<(Note, PaymentAddress, [u8; MEMO_SIZE])> {
let domain = SaplingDomain::new(zip212_enforcement);
try_note_decryption(&domain, ivk, output)
}
Expand All @@ -462,7 +465,7 @@ pub fn try_sapling_output_recovery_with_ock(
ock: &OutgoingCipherKey,
output: &OutputDescription<GrothProofBytes>,
zip212_enforcement: Zip212Enforcement,
) -> Option<(Note, PaymentAddress, [u8; 512])> {
) -> Option<(Note, PaymentAddress, [u8; MEMO_SIZE])> {
let domain = SaplingDomain::new(zip212_enforcement);
try_output_recovery_with_ock(&domain, ock, output, output.out_ciphertext())
}
Expand All @@ -479,7 +482,7 @@ pub fn try_sapling_output_recovery(
ovk: &OutgoingViewingKey,
output: &OutputDescription<GrothProofBytes>,
zip212_enforcement: Zip212Enforcement,
) -> Option<(Note, PaymentAddress, [u8; 512])> {
) -> Option<(Note, PaymentAddress, [u8; MEMO_SIZE])> {
let domain = SaplingDomain::new(zip212_enforcement);
try_output_recovery_with_ovk(&domain, ovk, output, output.cv(), output.out_ciphertext())
}
Expand All @@ -505,7 +508,7 @@ mod tests {
prf_ock, sapling_note_encryption, try_sapling_compact_note_decryption,
try_sapling_note_decryption, try_sapling_output_recovery,
try_sapling_output_recovery_with_ock, CompactOutputDescription, NoteBytesData,
SaplingDomain, Zip212Enforcement,
SaplingDomain, Zip212Enforcement, MEMO_SIZE,
};

use crate::{
Expand Down Expand Up @@ -575,7 +578,7 @@ mod tests {
let cmu = note.cmu();

let ovk = OutgoingViewingKey([0; 32]);
let ne = sapling_note_encryption(Some(ovk), note, [0x37; 512], &mut rng);
let ne = sapling_note_encryption(Some(ovk), note, [0x37; MEMO_SIZE], &mut rng);
let epk = ne.epk();
let ock = prf_ock(&ovk, &cv, &cmu.to_bytes(), &epk.to_bytes());

Expand Down

0 comments on commit 335bb34

Please sign in to comment.