Skip to content

Commit

Permalink
Restore parse_note_version function and remove NOTE_VERSION_BYTE asso…
Browse files Browse the repository at this point in the history
…ciated constant
  • Loading branch information
Dmitry Demin committed Jul 16, 2024
1 parent b20ff2f commit 4aff14d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
25 changes: 14 additions & 11 deletions src/note_encryption/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ const ZSA_ASSET_SIZE: usize = 32;
/// The size of a ZSA compact note.
pub(super) const COMPACT_NOTE_SIZE_ZSA: usize = COMPACT_NOTE_SIZE_VANILLA + ZSA_ASSET_SIZE;

/// The version byte for Vanilla.
pub(super) const NOTE_VERSION_BYTE_V2: u8 = 0x02;

/// The version byte for ZSA.
pub(super) const NOTE_VERSION_BYTE_V3: u8 = 0x03;

pub(super) type Memo = [u8; MEMO_SIZE];

/// Defined in [Zcash Protocol Spec § 5.4.2: Pseudo Random Functions][concreteprfs].
Expand Down Expand Up @@ -72,14 +78,13 @@ pub(super) fn prf_ock_orchard(
)
}

/// Checks if the version of the note plaintext matches the expected version for the domain.
pub(super) fn validate_note_version<D: OrchardDomainCommon>(
plaintext: &D::CompactNotePlaintextBytes,
) -> bool {
plaintext
.as_ref()
.first()
.map_or(false, |version| *version == D::NOTE_VERSION_BYTE)
/// Retrieves the version of the note plaintext.
/// Returns `Some(u8)` if the version is recognized, otherwise `None`.
pub(super) fn parse_note_version(plaintext: &[u8]) -> Option<u8> {
plaintext.first().and_then(|version| match *version {
NOTE_VERSION_BYTE_V2 | NOTE_VERSION_BYTE_V3 => Some(*version),
_ => None,
})
}

/// Parses the note plaintext (excluding the memo) and extracts the note and address if valid.
Expand All @@ -93,9 +98,7 @@ pub(super) fn parse_note_plaintext_without_memo<D: OrchardDomainCommon, F>(
where
F: FnOnce(&Diversifier) -> Option<DiversifiedTransmissionKey>,
{
if !validate_note_version::<D>(plaintext) {
return None;
}
parse_note_version(plaintext.as_ref())?;

// The unwraps below are guaranteed to succeed
let diversifier = Diversifier::from_bytes(
Expand Down
3 changes: 0 additions & 3 deletions src/note_encryption/orchard_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ pub trait OrchardDomainCommon: fmt::Debug + Clone {
/// The size of an encrypted note ciphertext, accounting for additional AEAD tag space.
const ENC_CIPHERTEXT_SIZE: usize = Self::NOTE_PLAINTEXT_SIZE + AEAD_TAG_SIZE;

/// The note version byte.
const NOTE_VERSION_BYTE: u8;

/// The raw bytes of a note plaintext.
type NotePlaintextBytes: NoteBytes;
/// The raw bytes of an encrypted note plaintext.
Expand Down
12 changes: 6 additions & 6 deletions src/note_encryption/orchard_domain_vanilla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ use crate::{
};

use super::{
domain::{build_base_note_plaintext_bytes, Memo, COMPACT_NOTE_SIZE_VANILLA},
domain::{
build_base_note_plaintext_bytes, Memo, COMPACT_NOTE_SIZE_VANILLA, NOTE_VERSION_BYTE_V2,
},
orchard_domain::{NoteBytesData, OrchardDomainCommon},
};

impl OrchardDomainCommon for OrchardVanilla {
const COMPACT_NOTE_SIZE: usize = COMPACT_NOTE_SIZE_VANILLA;

const NOTE_VERSION_BYTE: u8 = 0x02;

type NotePlaintextBytes = NoteBytesData<{ Self::NOTE_PLAINTEXT_SIZE }>;
type NoteCiphertextBytes = NoteBytesData<{ Self::ENC_CIPHERTEXT_SIZE }>;
type CompactNotePlaintextBytes = NoteBytesData<{ Self::COMPACT_NOTE_SIZE }>;
type CompactNoteCiphertextBytes = NoteBytesData<{ Self::COMPACT_NOTE_SIZE }>;

fn build_note_plaintext_bytes(note: &Note, memo: &Memo) -> Self::NotePlaintextBytes {
let mut np = build_base_note_plaintext_bytes(Self::NOTE_VERSION_BYTE, note);
let mut np = build_base_note_plaintext_bytes(NOTE_VERSION_BYTE_V2, note);

np[COMPACT_NOTE_SIZE_VANILLA..].copy_from_slice(memo);

Expand Down Expand Up @@ -62,7 +62,7 @@ mod tests {

use super::super::{
compact_action::CompactAction,
domain::{parse_note_plaintext_without_memo, prf_ock_orchard, validate_note_version},
domain::{parse_note_plaintext_without_memo, parse_note_version, prf_ock_orchard},
orchard_domain::{NoteBytesData, OrchardDomain},
};

Expand All @@ -87,7 +87,7 @@ mod tests {
let domain = OrchardDomainVanilla::for_rho(rho);
let (compact, parsed_memo) = domain.extract_memo(&plaintext);

assert!(validate_note_version::<OrchardVanilla>(&compact));
assert!(parse_note_version(compact.as_ref()).is_some());

let (parsed_note, parsed_recipient) = parse_note_plaintext_without_memo::<OrchardVanilla, _>(rho, &compact,
|diversifier| {
Expand Down
9 changes: 4 additions & 5 deletions src/note_encryption/orchard_domain_zsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
use super::{
domain::{
build_base_note_plaintext_bytes, Memo, COMPACT_NOTE_SIZE_VANILLA, COMPACT_NOTE_SIZE_ZSA,
NOTE_VERSION_BYTE_V3,
},
orchard_domain::{NoteBytesData, OrchardDomainCommon},
};
Expand All @@ -20,10 +21,8 @@ impl OrchardDomainCommon for OrchardZSA {
type CompactNotePlaintextBytes = NoteBytesData<{ Self::COMPACT_NOTE_SIZE }>;
type CompactNoteCiphertextBytes = NoteBytesData<{ Self::COMPACT_NOTE_SIZE }>;

const NOTE_VERSION_BYTE: u8 = 0x03;

fn build_note_plaintext_bytes(note: &Note, memo: &Memo) -> Self::NotePlaintextBytes {
let mut np = build_base_note_plaintext_bytes(Self::NOTE_VERSION_BYTE, note);
let mut np = build_base_note_plaintext_bytes(NOTE_VERSION_BYTE_V3, note);

np[COMPACT_NOTE_SIZE_VANILLA..COMPACT_NOTE_SIZE_ZSA]
.copy_from_slice(&note.asset().to_bytes());
Expand Down Expand Up @@ -70,7 +69,7 @@ mod tests {

use super::super::{
compact_action::CompactAction,
domain::{parse_note_plaintext_without_memo, prf_ock_orchard, validate_note_version},
domain::{parse_note_plaintext_without_memo, parse_note_version, prf_ock_orchard},
orchard_domain::{NoteBytesData, OrchardDomain},
};

Expand All @@ -95,7 +94,7 @@ mod tests {
let domain = OrchardDomainZSA::for_rho(rho);
let (compact, parsed_memo) = domain.extract_memo(&plaintext);

assert!(validate_note_version::<OrchardZSA>(&compact));
assert!(parse_note_version(compact.as_ref()).is_some());

let (parsed_note, parsed_recipient) = parse_note_plaintext_without_memo::<OrchardZSA, _>(rho, &compact,
|diversifier| {
Expand Down

0 comments on commit 4aff14d

Please sign in to comment.