serai/coins/monero/tests/key_image.rs

52 lines
1.6 KiB
Rust
Raw Normal View History

#![cfg(feature = "multisig")]
2022-04-29 01:47:25 +00:00
use rand::{RngCore, rngs::OsRng};
2022-04-29 19:28:04 +00:00
use curve25519_dalek::{traits::Identity, edwards::EdwardsPoint};
2022-04-30 08:32:19 +00:00
use monero_serai::key_image;
2022-04-29 19:28:04 +00:00
mod frost;
2022-04-30 08:32:19 +00:00
use crate::frost::{THRESHOLD, PARTICIPANTS, generate_keys};
#[test]
2022-04-30 08:32:19 +00:00
fn test() {
2022-04-29 01:47:25 +00:00
let (keys, group_private) = generate_keys();
let image = key_image::generate(&group_private);
2022-04-29 01:47:25 +00:00
let mut included = (1 ..= PARTICIPANTS).into_iter().collect::<Vec<usize>>();
while included.len() > THRESHOLD {
included.swap_remove((OsRng.next_u64() as usize) % included.len());
}
included.sort();
2022-04-29 19:28:04 +00:00
let mut views = vec![];
let mut shares = vec![];
for i in 1 ..= PARTICIPANTS {
if included.contains(&i) {
// If they were included, include their view
2022-04-30 08:32:19 +00:00
views.push(keys[i - 1].view(&included).unwrap());
2022-04-29 19:28:04 +00:00
let share = key_image::generate_share(&mut OsRng, &views[i - 1]);
2022-04-30 08:32:19 +00:00
let mut serialized = share.0.compress().to_bytes().to_vec();
2022-04-29 19:28:04 +00:00
serialized.extend(b"abc");
serialized.extend(&share.1);
shares.push(serialized);
} else {
2022-04-30 08:32:19 +00:00
// If they weren't included, include dummy data to fill the Vec
2022-04-29 19:28:04 +00:00
// Uses the view of someone actually included as Params::new verifies inclusion
2022-04-30 08:32:19 +00:00
views.push(keys[included[0] - 1].view(&included).unwrap());
2022-04-29 19:28:04 +00:00
shares.push(vec![]);
}
}
2022-04-29 19:28:04 +00:00
for i in &included {
let mut multi_image = EdwardsPoint::identity();
for l in &included {
let share = key_image::verify_share(&views[i - 1], *l, &shares[l - 1]).unwrap();
assert_eq!(share.1, b"abc");
multi_image += share.0;
}
assert_eq!(image, multi_image);
}
}