From 75bede4bc8a8568cd0e6e8b66d930ebb58b167de Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Wed, 5 Jun 2019 14:58:00 +0100 Subject: [PATCH] Use fixed-length arrays instead of Vec --- zcash_primitives/src/note_encryption.rs | 28 +++++++++++++------------ 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/zcash_primitives/src/note_encryption.rs b/zcash_primitives/src/note_encryption.rs index 313a02f..dd1bf91 100644 --- a/zcash_primitives/src/note_encryption.rs +++ b/zcash_primitives/src/note_encryption.rs @@ -299,15 +299,18 @@ impl SaplingNoteEncryption { // Note plaintext encoding is defined in section 5.5 of the Zcash Protocol // Specification. - let mut input = Vec::with_capacity(NOTE_PLAINTEXT_SIZE); - input.push(1); - input.extend_from_slice(&self.to.diversifier.0); - (&mut input) + let mut input = [0; NOTE_PLAINTEXT_SIZE]; + input[0] = 1; + input[1..12].copy_from_slice(&self.to.diversifier.0); + (&mut input[12..20]) .write_u64::(self.note.value) .unwrap(); - self.note.r.into_repr().write_le(&mut input).unwrap(); - input.extend_from_slice(&self.memo.0); - assert_eq!(input.len(), NOTE_PLAINTEXT_SIZE); + self.note + .r + .into_repr() + .write_le(&mut input[20..COMPACT_NOTE_SIZE]) + .unwrap(); + input[COMPACT_NOTE_SIZE..NOTE_PLAINTEXT_SIZE].copy_from_slice(&self.memo.0); let mut output = [0u8; ENC_CIPHERTEXT_SIZE]; assert_eq!( @@ -395,7 +398,7 @@ pub fn try_sapling_note_decryption( let shared_secret = sapling_ka_agree(ivk, epk); let key = kdf_sapling(&shared_secret, &epk); - let mut plaintext = vec![0; ENC_CIPHERTEXT_SIZE]; + let mut plaintext = [0; ENC_CIPHERTEXT_SIZE]; assert_eq!( ChachaPolyIetf::aead_cipher() .open_to( @@ -439,9 +442,8 @@ pub fn try_sapling_compact_note_decryption( // Prefix plaintext with 64 zero-bytes to skip over Poly1305 keying output const CHACHA20_BLOCK_SIZE: usize = 64; - let mut plaintext = Vec::with_capacity(CHACHA20_BLOCK_SIZE + COMPACT_NOTE_SIZE); - plaintext.extend_from_slice(&[0; CHACHA20_BLOCK_SIZE]); - plaintext.extend_from_slice(&enc_ciphertext[0..COMPACT_NOTE_SIZE]); + let mut plaintext = [0; CHACHA20_BLOCK_SIZE + COMPACT_NOTE_SIZE]; + plaintext[CHACHA20_BLOCK_SIZE..].copy_from_slice(&enc_ciphertext[0..COMPACT_NOTE_SIZE]); assert_eq!( ChaCha20Ietf::cipher() .decrypt( @@ -477,7 +479,7 @@ pub fn try_sapling_output_recovery( let ock = prf_ock(&ovk, &cv, &cmu, &epk); - let mut op = vec![0; OUT_CIPHERTEXT_SIZE]; + let mut op = [0; OUT_CIPHERTEXT_SIZE]; assert_eq!( ChachaPolyIetf::aead_cipher() .open_to(&mut op, &out_ciphertext, &[], ock.as_bytes(), &[0u8; 12]) @@ -496,7 +498,7 @@ pub fn try_sapling_output_recovery( let shared_secret = sapling_ka_agree(&esk, &pk_d); let key = kdf_sapling(&shared_secret, &epk); - let mut plaintext = vec![0; ENC_CIPHERTEXT_SIZE]; + let mut plaintext = [0; ENC_CIPHERTEXT_SIZE]; assert_eq!( ChachaPolyIetf::aead_cipher() .open_to(