Remove `MemoBytes` usage from `zcash_primitives::sapling`

This commit is contained in:
Jack Grigg 2023-11-28 00:26:03 +00:00
parent 2f2401d144
commit 106f5a353a
9 changed files with 59 additions and 51 deletions

View File

@ -96,7 +96,7 @@ pub fn decrypt_transaction<P: consensus::Parameters>(
index, index,
note, note,
account, account,
memo, memo: MemoBytes::from_bytes(&memo).expect("correct length"),
transfer_type, transfer_type,
}) })
}) })

View File

@ -739,7 +739,7 @@ mod tests {
let encryptor = sapling_note_encryption( let encryptor = sapling_note_encryption(
Some(dfvk.fvk().ovk), Some(dfvk.fvk().ovk),
note.clone(), note.clone(),
MemoBytes::empty(), *MemoBytes::empty().as_array(),
&mut rng, &mut rng,
); );
let cmu = note.cmu().to_bytes().to_vec(); let cmu = note.cmu().to_bytes().to_vec();

View File

@ -779,7 +779,7 @@ pub(crate) fn fake_compact_block<P: consensus::Parameters>(
let encryptor = sapling_note_encryption( let encryptor = sapling_note_encryption(
Some(dfvk.fvk().ovk), Some(dfvk.fvk().ovk),
note.clone(), note.clone(),
MemoBytes::empty(), *MemoBytes::empty().as_array(),
&mut rng, &mut rng,
); );
let cmu = note.cmu().to_bytes().to_vec(); let cmu = note.cmu().to_bytes().to_vec();
@ -886,7 +886,7 @@ pub(crate) fn fake_compact_block_spending<P: consensus::Parameters>(
let encryptor = sapling_note_encryption( let encryptor = sapling_note_encryption(
Some(dfvk.fvk().ovk), Some(dfvk.fvk().ovk),
note.clone(), note.clone(),
MemoBytes::empty(), *MemoBytes::empty().as_array(),
&mut rng, &mut rng,
); );
let cmu = note.cmu().to_bytes().to_vec(); let cmu = note.cmu().to_bytes().to_vec();
@ -912,7 +912,7 @@ pub(crate) fn fake_compact_block_spending<P: consensus::Parameters>(
let encryptor = sapling_note_encryption( let encryptor = sapling_note_encryption(
Some(dfvk.fvk().ovk), Some(dfvk.fvk().ovk),
note.clone(), note.clone(),
MemoBytes::empty(), *MemoBytes::empty().as_array(),
&mut rng, &mut rng,
); );
let cmu = note.cmu().to_bytes().to_vec(); let cmu = note.cmu().to_bytes().to_vec();

View File

@ -1040,7 +1040,13 @@ pub(crate) mod tests {
); );
if result.is_some() { if result.is_some() {
return Ok(result); return Ok(result.map(|(note, addr, memo)| {
(
note,
addr,
MemoBytes::from_bytes(&memo).expect("correct length"),
)
}));
} }
} }

View File

@ -112,6 +112,8 @@ and this library adheres to Rust's notion of
- `builder::SaplingBuilder::new` now takes a `Zip212Enforcement` argument - `builder::SaplingBuilder::new` now takes a `Zip212Enforcement` argument
instead of a `P: consensus::Parameters` argument and a target height. instead of a `P: consensus::Parameters` argument and a target height.
- `builder::SaplingBuilder::add_spend` now takes `extsk` by reference. - `builder::SaplingBuilder::add_spend` now takes `extsk` by reference.
- `builder::SaplingBuilder::add_output` now takes an `Option<[u8; 512]>` memo
instead of a `MemoBytes`.
- `builder::SaplingBuilder::build` no longer takes a prover, proving context, - `builder::SaplingBuilder::build` no longer takes a prover, proving context,
progress notifier, or target height. Instead, it has `SpendProver, OutputProver` progress notifier, or target height. Instead, it has `SpendProver, OutputProver`
generic parameters and returns `(UnauthorizedBundle, SaplingMetadata)`. The generic parameters and returns `(UnauthorizedBundle, SaplingMetadata)`. The
@ -137,6 +139,13 @@ and this library adheres to Rust's notion of
- `try_sapling_compact_note_decryption` - `try_sapling_compact_note_decryption`
- `try_sapling_output_recovery_with_ock` - `try_sapling_output_recovery_with_ock`
- `try_sapling_output_recovery` - `try_sapling_output_recovery`
- `SaplingDomain::Memo` now has type `[u8; 512]` instead of `MemoBytes`.
- `sapling_note_encryption` now takes `memo` as a `[u8; 512]` instead of
`MemoBytes`.
- The following methods now return `[u8; 512]` instead of `MemoBytes`:
- `try_sapling_note_decryption`
- `try_sapling_output_recovery_with_ock`
- `try_sapling_output_recovery`
- `util::generate_random_rseed` now takes a `Zip212Enforcement` argument - `util::generate_random_rseed` now takes a `Zip212Enforcement` argument
instead of a `P: consensus::Parameters` argument and a height. instead of a `P: consensus::Parameters` argument and a height.
- `zcash_primitives::transaction`: - `zcash_primitives::transaction`:

View File

@ -6,7 +6,6 @@ use rand_core::OsRng;
use zcash_note_encryption::batch; use zcash_note_encryption::batch;
use zcash_primitives::{ use zcash_primitives::{
consensus::{sapling_zip212_enforcement, NetworkUpgrade::Canopy, Parameters, TEST_NETWORK}, consensus::{sapling_zip212_enforcement, NetworkUpgrade::Canopy, Parameters, TEST_NETWORK},
memo::MemoBytes,
sapling::{ sapling::{
builder::SaplingBuilder, builder::SaplingBuilder,
note_encryption::{ note_encryption::{
@ -38,13 +37,7 @@ fn bench_note_decryption(c: &mut Criterion) {
let mut builder = SaplingBuilder::new(zip212_enforcement); let mut builder = SaplingBuilder::new(zip212_enforcement);
builder builder
.add_output( .add_output(&mut rng, None, pa, NoteValue::from_raw(100), None)
&mut rng,
None,
pa,
NoteValue::from_raw(100),
MemoBytes::empty(),
)
.unwrap(); .unwrap();
let (bundle, _) = builder let (bundle, _) = builder
.build::<MockSpendProver, MockOutputProver, _, Amount>(&mut rng) .build::<MockSpendProver, MockOutputProver, _, Amount>(&mut rng)

View File

@ -9,7 +9,6 @@ use rand_core::CryptoRng;
use crate::{ use crate::{
keys::OutgoingViewingKey, keys::OutgoingViewingKey,
memo::MemoBytes,
sapling::{ sapling::{
self, self,
bundle::{ bundle::{
@ -165,7 +164,7 @@ struct SaplingOutputInfo {
/// `None` represents the `ovk = ⊥` case. /// `None` represents the `ovk = ⊥` case.
ovk: Option<OutgoingViewingKey>, ovk: Option<OutgoingViewingKey>,
note: Note, note: Note,
memo: MemoBytes, memo: Option<[u8; 512]>,
rcv: ValueCommitTrapdoor, rcv: ValueCommitTrapdoor,
} }
@ -188,7 +187,7 @@ impl SaplingOutputInfo {
None, None,
dummy_to, dummy_to,
NoteValue::from_raw(0), NoteValue::from_raw(0),
MemoBytes::empty(), None,
zip212_enforcement, zip212_enforcement,
) )
} }
@ -198,7 +197,7 @@ impl SaplingOutputInfo {
ovk: Option<OutgoingViewingKey>, ovk: Option<OutgoingViewingKey>,
to: PaymentAddress, to: PaymentAddress,
value: NoteValue, value: NoteValue,
memo: MemoBytes, memo: Option<[u8; 512]>,
zip212_enforcement: Zip212Enforcement, zip212_enforcement: Zip212Enforcement,
) -> Self { ) -> Self {
let rseed = generate_random_rseed_internal(zip212_enforcement, rng); let rseed = generate_random_rseed_internal(zip212_enforcement, rng);
@ -217,7 +216,16 @@ impl SaplingOutputInfo {
self, self,
rng: &mut R, rng: &mut R,
) -> OutputDescription<sapling::circuit::Output> { ) -> OutputDescription<sapling::circuit::Output> {
let encryptor = sapling_note_encryption::<R>(self.ovk, self.note.clone(), self.memo, rng); let encryptor = sapling_note_encryption::<R>(
self.ovk,
self.note.clone(),
self.memo.unwrap_or_else(|| {
let mut memo = [0; 512];
memo[0] = 0xf6;
memo
}),
rng,
);
// Construct the value commitment. // Construct the value commitment.
let cv = ValueCommitment::derive(self.note.value(), self.rcv.clone()); let cv = ValueCommitment::derive(self.note.value(), self.rcv.clone());
@ -396,7 +404,7 @@ impl SaplingBuilder {
ovk: Option<OutgoingViewingKey>, ovk: Option<OutgoingViewingKey>,
to: PaymentAddress, to: PaymentAddress,
value: NoteValue, value: NoteValue,
memo: MemoBytes, memo: Option<[u8; 512]>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let output = SaplingOutputInfo::new_internal( let output = SaplingOutputInfo::new_internal(
&mut rng, &mut rng,

View File

@ -15,9 +15,7 @@ use zcash_note_encryption::{
ENC_CIPHERTEXT_SIZE, NOTE_PLAINTEXT_SIZE, OUT_PLAINTEXT_SIZE, ENC_CIPHERTEXT_SIZE, NOTE_PLAINTEXT_SIZE, OUT_PLAINTEXT_SIZE,
}; };
use crate::{ use crate::sapling::{
memo::MemoBytes,
sapling::{
bundle::{GrothProofBytes, OutputDescription}, bundle::{GrothProofBytes, OutputDescription},
keys::{ keys::{
DiversifiedTransmissionKey, EphemeralPublicKey, EphemeralSecretKey, OutgoingViewingKey, DiversifiedTransmissionKey, EphemeralPublicKey, EphemeralSecretKey, OutgoingViewingKey,
@ -25,7 +23,6 @@ use crate::{
}, },
value::{NoteValue, ValueCommitment}, value::{NoteValue, ValueCommitment},
Diversifier, Note, PaymentAddress, Rseed, Diversifier, Note, PaymentAddress, Rseed,
},
}; };
use super::note::ExtractedNoteCommitment; use super::note::ExtractedNoteCommitment;
@ -145,7 +142,7 @@ impl Domain for SaplingDomain {
type ValueCommitment = ValueCommitment; type ValueCommitment = ValueCommitment;
type ExtractedCommitment = ExtractedNoteCommitment; type ExtractedCommitment = ExtractedNoteCommitment;
type ExtractedCommitmentBytes = [u8; 32]; type ExtractedCommitmentBytes = [u8; 32];
type Memo = MemoBytes; type Memo = [u8; 512];
fn derive_esk(note: &Self::Note) -> Option<Self::EphemeralSecretKey> { fn derive_esk(note: &Self::Note) -> Option<Self::EphemeralSecretKey> {
note.derive_esk() note.derive_esk()
@ -209,7 +206,7 @@ impl Domain for SaplingDomain {
} }
} }
input[COMPACT_NOTE_SIZE..NOTE_PLAINTEXT_SIZE].copy_from_slice(&memo.as_array()[..]); input[COMPACT_NOTE_SIZE..NOTE_PLAINTEXT_SIZE].copy_from_slice(&memo[..]);
NotePlaintextBytes(input) NotePlaintextBytes(input)
} }
@ -286,7 +283,9 @@ impl Domain for SaplingDomain {
} }
fn extract_memo(&self, plaintext: &NotePlaintextBytes) -> Self::Memo { fn extract_memo(&self, plaintext: &NotePlaintextBytes) -> Self::Memo {
MemoBytes::from_bytes(&plaintext.0[COMPACT_NOTE_SIZE..NOTE_PLAINTEXT_SIZE]).unwrap() plaintext.0[COMPACT_NOTE_SIZE..NOTE_PLAINTEXT_SIZE]
.try_into()
.expect("correct length")
} }
} }
@ -361,7 +360,6 @@ impl ShieldedOutput<SaplingDomain, COMPACT_NOTE_SIZE> for CompactOutputDescripti
/// use zcash_primitives::{ /// use zcash_primitives::{
/// keys::{OutgoingViewingKey, prf_expand}, /// keys::{OutgoingViewingKey, prf_expand},
/// consensus::{TEST_NETWORK, NetworkUpgrade, Parameters}, /// consensus::{TEST_NETWORK, NetworkUpgrade, Parameters},
/// memo::MemoBytes,
/// sapling::{ /// sapling::{
/// note_encryption::{sapling_note_encryption, Zip212Enforcement}, /// note_encryption::{sapling_note_encryption, Zip212Enforcement},
/// util::generate_random_rseed, /// util::generate_random_rseed,
@ -388,14 +386,14 @@ impl ShieldedOutput<SaplingDomain, COMPACT_NOTE_SIZE> for CompactOutputDescripti
/// let note = to.create_note(value, rseed); /// let note = to.create_note(value, rseed);
/// let cmu = note.cmu(); /// let cmu = note.cmu();
/// ///
/// let mut enc = sapling_note_encryption(ovk, note, MemoBytes::empty(), &mut rng); /// let mut enc = sapling_note_encryption(ovk, note, [0x37; 512], &mut rng);
/// let encCiphertext = enc.encrypt_note_plaintext(); /// let encCiphertext = enc.encrypt_note_plaintext();
/// let outCiphertext = enc.encrypt_outgoing_plaintext(&cv, &cmu, &mut rng); /// let outCiphertext = enc.encrypt_outgoing_plaintext(&cv, &cmu, &mut rng);
/// ``` /// ```
pub fn sapling_note_encryption<R: RngCore>( pub fn sapling_note_encryption<R: RngCore>(
ovk: Option<OutgoingViewingKey>, ovk: Option<OutgoingViewingKey>,
note: Note, note: Note,
memo: MemoBytes, memo: [u8; 512],
rng: &mut R, rng: &mut R,
) -> NoteEncryption<SaplingDomain> { ) -> NoteEncryption<SaplingDomain> {
let esk = note.generate_or_derive_esk_internal(rng); let esk = note.generate_or_derive_esk_internal(rng);
@ -416,7 +414,7 @@ pub fn try_sapling_note_decryption<Output: ShieldedOutput<SaplingDomain, ENC_CIP
ivk: &PreparedIncomingViewingKey, ivk: &PreparedIncomingViewingKey,
output: &Output, output: &Output,
zip212_enforcement: Zip212Enforcement, zip212_enforcement: Zip212Enforcement,
) -> Option<(Note, PaymentAddress, MemoBytes)> { ) -> Option<(Note, PaymentAddress, [u8; 512])> {
let domain = SaplingDomain::new(zip212_enforcement); let domain = SaplingDomain::new(zip212_enforcement);
try_note_decryption(&domain, ivk, output) try_note_decryption(&domain, ivk, output)
} }
@ -444,7 +442,7 @@ pub fn try_sapling_output_recovery_with_ock(
ock: &OutgoingCipherKey, ock: &OutgoingCipherKey,
output: &OutputDescription<GrothProofBytes>, output: &OutputDescription<GrothProofBytes>,
zip212_enforcement: Zip212Enforcement, zip212_enforcement: Zip212Enforcement,
) -> Option<(Note, PaymentAddress, MemoBytes)> { ) -> Option<(Note, PaymentAddress, [u8; 512])> {
let domain = SaplingDomain::new(zip212_enforcement); let domain = SaplingDomain::new(zip212_enforcement);
try_output_recovery_with_ock(&domain, ock, output, output.out_ciphertext()) try_output_recovery_with_ock(&domain, ock, output, output.out_ciphertext())
} }
@ -461,7 +459,7 @@ pub fn try_sapling_output_recovery(
ovk: &OutgoingViewingKey, ovk: &OutgoingViewingKey,
output: &OutputDescription<GrothProofBytes>, output: &OutputDescription<GrothProofBytes>,
zip212_enforcement: Zip212Enforcement, zip212_enforcement: Zip212Enforcement,
) -> Option<(Note, PaymentAddress, MemoBytes)> { ) -> Option<(Note, PaymentAddress, [u8; 512])> {
let domain = SaplingDomain::new(zip212_enforcement); let domain = SaplingDomain::new(zip212_enforcement);
try_output_recovery_with_ovk(&domain, ovk, output, output.cv(), output.out_ciphertext()) try_output_recovery_with_ovk(&domain, ovk, output, output.cv(), output.out_ciphertext())
} }
@ -492,7 +490,6 @@ mod tests {
use crate::{ use crate::{
keys::OutgoingViewingKey, keys::OutgoingViewingKey,
memo::MemoBytes,
sapling::{ sapling::{
bundle::{GrothProofBytes, OutputDescription}, bundle::{GrothProofBytes, OutputDescription},
keys::{DiversifiedTransmissionKey, EphemeralSecretKey}, keys::{DiversifiedTransmissionKey, EphemeralSecretKey},
@ -561,7 +558,7 @@ mod tests {
let cmu = note.cmu(); let cmu = note.cmu();
let ovk = OutgoingViewingKey([0; 32]); let ovk = OutgoingViewingKey([0; 32]);
let ne = sapling_note_encryption(Some(ovk), note, MemoBytes::empty(), &mut rng); let ne = sapling_note_encryption(Some(ovk), note, [0x37; 512], &mut rng);
let epk = ne.epk(); let epk = ne.epk();
let ock = prf_ock(&ovk, &cv, &cmu.to_bytes(), &epk.to_bytes()); let ock = prf_ock(&ovk, &cv, &cmu.to_bytes(), &epk.to_bytes());
@ -1415,7 +1412,7 @@ mod tests {
Some((decrypted_note, decrypted_to, decrypted_memo)) => { Some((decrypted_note, decrypted_to, decrypted_memo)) => {
assert_eq!(decrypted_note, note); assert_eq!(decrypted_note, note);
assert_eq!(decrypted_to, to); assert_eq!(decrypted_to, to);
assert_eq!(&decrypted_memo.as_array()[..], &tv.memo[..]); assert_eq!(&decrypted_memo[..], &tv.memo[..]);
} }
None => panic!("Note decryption failed"), None => panic!("Note decryption failed"),
} }
@ -1436,7 +1433,7 @@ mod tests {
Some((decrypted_note, decrypted_to, decrypted_memo)) => { Some((decrypted_note, decrypted_to, decrypted_memo)) => {
assert_eq!(decrypted_note, note); assert_eq!(decrypted_note, note);
assert_eq!(decrypted_to, to); assert_eq!(decrypted_to, to);
assert_eq!(&decrypted_memo.as_array()[..], &tv.memo[..]); assert_eq!(&decrypted_memo[..], &tv.memo[..]);
} }
None => panic!("Output recovery failed"), None => panic!("Output recovery failed"),
} }
@ -1449,7 +1446,7 @@ mod tests {
[Some(((decrypted_note, decrypted_to, decrypted_memo), i))] => { [Some(((decrypted_note, decrypted_to, decrypted_memo), i))] => {
assert_eq!(decrypted_note, &note); assert_eq!(decrypted_note, &note);
assert_eq!(decrypted_to, &to); assert_eq!(decrypted_to, &to);
assert_eq!(&decrypted_memo.as_array()[..], &tv.memo[..]); assert_eq!(&decrypted_memo[..], &tv.memo[..]);
assert_eq!(*i, 0); assert_eq!(*i, 0);
} }
_ => panic!("Note decryption failed"), _ => panic!("Note decryption failed"),
@ -1475,12 +1472,7 @@ mod tests {
// Test encryption // Test encryption
// //
let ne = NoteEncryption::<SaplingDomain>::new_with_esk( let ne = NoteEncryption::<SaplingDomain>::new_with_esk(esk, Some(ovk), note, tv.memo);
esk,
Some(ovk),
note,
MemoBytes::from_bytes(&tv.memo).unwrap(),
);
assert_eq!(ne.encrypt_note_plaintext().as_ref(), &tv.c_enc[..]); assert_eq!(ne.encrypt_note_plaintext().as_ref(), &tv.c_enc[..]);
assert_eq!( assert_eq!(

View File

@ -356,7 +356,7 @@ impl<'a, P: consensus::Parameters, R: RngCore + CryptoRng> Builder<'a, P, R> {
ovk, ovk,
to, to,
sapling::value::NoteValue::from_raw(value.into()), sapling::value::NoteValue::from_raw(value.into()),
memo, Some(*memo.as_array()),
) )
} }