Skip to content

Commit

Permalink
UIP 4: make backreference encryption infallible
Browse files Browse the repository at this point in the history
This is safe because we know the arrays that have been allocated
are the correct size
  • Loading branch information
redshiftzero committed Nov 16, 2024
1 parent 97630a7 commit 3ad5fa4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 12 deletions.
12 changes: 4 additions & 8 deletions crates/core/component/shielded-pool/src/backref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ impl Backref {
Self { note_commitment }
}

pub fn encrypt(
&self,
brk: &BackreferenceKey,
nullifier: &Nullifier,
) -> Result<EncryptedBackref> {
pub fn encrypt(&self, brk: &BackreferenceKey, nullifier: &Nullifier) -> EncryptedBackref {
let cipher = ChaCha20Poly1305::new(&brk.0);

// Nonce is the first 12 bytes of the nullifier
Expand All @@ -41,9 +37,9 @@ impl Backref {

let ciphertext = cipher
.encrypt(nonce, plaintext.as_ref())
.map_err(|_| anyhow::anyhow!("encryption error"))?;
.expect("encryption should succeed ");

Ok(EncryptedBackref { bytes: ciphertext })
EncryptedBackref { bytes: ciphertext }
}
}

Expand Down Expand Up @@ -211,7 +207,7 @@ mod tests {
let nullifier = Nullifier::derive(&nk, state_commitment_proof.position(), &note_commitment);

let backref = Backref::new(note_commitment);
let encrypted_backref = backref.encrypt(&brk, &nullifier).unwrap();
let encrypted_backref = backref.encrypt(&brk, &nullifier);

let decrypted_backref = encrypted_backref.decrypt(&brk, &nullifier).unwrap();

Expand Down
5 changes: 1 addition & 4 deletions crates/core/component/shielded-pool/src/spend/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ impl SpendPlan {
pub fn spend_body(&self, fvk: &FullViewingKey) -> Body {
// Construct the backreference for this spend.
let backref = Backref::new(self.note.commit());
// TODO: This is fallible
let encrypted_backref = backref
.encrypt(&fvk.backref_key(), &self.nullifier(fvk))
.expect("can encrypt");
let encrypted_backref = backref.encrypt(&fvk.backref_key(), &self.nullifier(fvk));
Body {
balance_commitment: self.balance().commit(self.value_blinding),
nullifier: self.nullifier(fvk),
Expand Down

0 comments on commit 3ad5fa4

Please sign in to comment.