Skip to content

Commit

Permalink
test: add decryption for 0 length UIP 4 encrypted backrefs
Browse files Browse the repository at this point in the history
  • Loading branch information
redshiftzero committed Nov 14, 2024
1 parent 79ff0fe commit 8b2f7fe
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion crates/core/component/shielded-pool/src/backref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,44 @@ mod tests {

use crate::Note;

#[test]
fn encrypted_backref_zero_length() {
let rng = OsRng;

let seed_phrase = SeedPhrase::generate(rng);
let sk = SpendKey::from_seed_phrase_bip44(seed_phrase, &Bip44Path::new(0));
let fvk = sk.full_viewing_key();
let brk = fvk.backref_key();

let ivk = fvk.incoming();
let (sender, _dtk_d) = ivk.payment_address(0u32.into());

let value_to_send = Value {
amount: 1u64.into(),
asset_id: asset::Cache::with_known_assets()
.get_unit("upenumbra")
.unwrap()
.id(),
};

let note = Note::generate(&mut OsRng, &sender, value_to_send);
let note_commitment: penumbra_tct::StateCommitment = note.commit();
let nk = *sk.nullifier_key();
let mut sct = tct::Tree::new();

sct.insert(tct::Witness::Keep, note_commitment).unwrap();
let state_commitment_proof = sct.witness(note_commitment).unwrap();
let nullifier = Nullifier::derive(&nk, state_commitment_proof.position(), &note_commitment);

let encrypted_backref = EncryptedBackref::dummy();
assert!(encrypted_backref.is_empty());
assert_eq!(encrypted_backref.len(), 0);

// Decrypting a zero-length encrypted backref should return `None`.
let decrypted_backref = encrypted_backref.decrypt(&brk, &nullifier).unwrap();
assert_eq!(decrypted_backref, None);
}

#[test]
fn encrypted_backref_round_trip() {
let rng = OsRng;
Expand Down Expand Up @@ -176,6 +214,6 @@ mod tests {

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

assert_eq!(backref, decrypted_backref);
assert_eq!(Some(backref), decrypted_backref);
}
}

0 comments on commit 8b2f7fe

Please sign in to comment.