Skip to content

Commit

Permalink
UIP 4: Implement Backreference decryption
Browse files Browse the repository at this point in the history
  • Loading branch information
redshiftzero committed Nov 13, 2024
1 parent b1b51e7 commit 789ee1b
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions crates/core/component/shielded-pool/src/backref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,25 @@ impl Backref {
Ok(EncryptedBackref { bytes: ciphertext })
}
}

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

let nonce_bytes = &nullifier.to_bytes()[..12];
let nonce = Nonce::from_slice(&nonce_bytes);

let plaintext = cipher
.decrypt(nonce, self.bytes.as_ref())
.map_err(|_| anyhow::anyhow!("decryption error"))?;

let note_commitment_bytes: [u8; 32] = plaintext
.try_into()
.map_err(|_| anyhow::anyhow!("decryption error"))?;

Ok(Backref {
note_commitment: tct::StateCommitment::try_from(note_commitment_bytes)
.map_err(|_| anyhow::anyhow!("decryption error"))?,
})
}
}

0 comments on commit 789ee1b

Please sign in to comment.