Skip to content

Commit

Permalink
Refactor/modify lib.rs to avoid Vec usage and fix CI build failure (#3)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitry Demin <[email protected]>
  • Loading branch information
dmidem and dmidem authored Apr 17, 2024
1 parent e14dab6 commit 8b6b31d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
- name: Add target
run: rustup target add ${{ matrix.target }}
- name: Build crate
run: cargo build --features=alloc --no-default-features --verbose --target ${{ matrix.target }}
run: cargo build --no-default-features --verbose --target ${{ matrix.target }}

bitrot:
name: Bitrot check
Expand Down
18 changes: 10 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use core::fmt::{self, Write};
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "alloc")]
use alloc::{borrow::ToOwned, vec::Vec};
use alloc::vec::Vec;

use chacha20::{
cipher::{StreamCipher, StreamCipherSeek},
Expand Down Expand Up @@ -139,7 +139,7 @@ pub trait Domain {
type Memo;

type NotePlaintextBytes: AsMut<[u8]> + for<'a> From<&'a [u8]>;
type NoteCiphertextBytes: AsRef<[u8]> + for<'a> From<&'a [u8]>;
type NoteCiphertextBytes: AsMut<[u8]> + for<'a> From<(&'a [u8], &'a [u8])>;
type CompactNotePlaintextBytes: AsMut<[u8]> + for<'a> From<&'a [u8]>;
type CompactNoteCiphertextBytes: AsRef<[u8]>;

Expand Down Expand Up @@ -410,7 +410,7 @@ impl<D: Domain> NoteEncryption<D> {
let tag = ChaCha20Poly1305::new(key.as_ref().into())
.encrypt_in_place_detached([0u8; 12][..].into(), &[], output)
.unwrap();
D::NoteCiphertextBytes::from(&[output, tag.as_ref()].concat())
D::NoteCiphertextBytes::from((output, tag.as_ref()))
}

/// Generates `outCiphertext` for this note.
Expand Down Expand Up @@ -476,9 +476,10 @@ fn try_note_decryption_inner<D: Domain, Output: ShieldedOutput<D>>(
output: &Output,
key: &D::SymmetricKey,
) -> Option<(D::Note, D::Recipient, D::Memo)> {
let mut enc_ciphertext = output.enc_ciphertext()?.as_ref().to_owned();
let mut enc_ciphertext = output.enc_ciphertext()?;
let enc_ciphertext_ref = enc_ciphertext.as_mut();

let (plaintext, tag) = extract_tag(&mut enc_ciphertext);
let (plaintext, tag) = extract_tag(enc_ciphertext_ref);

ChaCha20Poly1305::new(key.as_ref().into())
.decrypt_in_place_detached([0u8; 12][..].into(), &[], plaintext, &tag.into())
Expand Down Expand Up @@ -643,9 +644,10 @@ pub fn try_output_recovery_with_ock<D: Domain, Output: ShieldedOutput<D>>(
// be okay.
let key = D::kdf(shared_secret, &ephemeral_key);

let mut enc_ciphertext = output.enc_ciphertext()?.as_ref().to_owned();
let mut enc_ciphertext = output.enc_ciphertext()?;
let enc_ciphertext_ref = enc_ciphertext.as_mut();

let (plaintext, tag) = extract_tag(&mut enc_ciphertext);
let (plaintext, tag) = extract_tag(enc_ciphertext_ref);

ChaCha20Poly1305::new(key.as_ref().into())
.decrypt_in_place_detached([0u8; 12][..].into(), &[], plaintext, &tag.into())
Expand Down Expand Up @@ -674,7 +676,7 @@ pub fn try_output_recovery_with_ock<D: Domain, Output: ShieldedOutput<D>>(
}

// Splits the AEAD tag from the ciphertext.
fn extract_tag(enc_ciphertext: &mut Vec<u8>) -> (&mut [u8], [u8; AEAD_TAG_SIZE]) {
fn extract_tag(enc_ciphertext: &mut [u8]) -> (&mut [u8], [u8; AEAD_TAG_SIZE]) {
let tag_loc = enc_ciphertext.len() - AEAD_TAG_SIZE;

let (plaintext, tail) = enc_ciphertext.split_at_mut(tag_loc);
Expand Down

0 comments on commit 8b6b31d

Please sign in to comment.