use umbral_rs::pre::*;
fn vec_to_hex(v: &[u8]) -> String {
v.iter().map(|b| format!("{:02x}", b)).collect()
}
fn play(alice: &KeyPair) {
let params = new_standard_params();
println!("Alice public key: {:?}", alice.public_key().to_bytes());
println!(
"Alice public key (hex version): {}",
vec_to_hex(&alice.public_key().to_bytes())
);
let alice_copy = KeyPair::from_bytes(
&alice.public_key().to_bytes(),
&alice.private_key().to_bytes(),
¶ms,
)
.unwrap();
assert_eq!(
alice.public_key().to_bytes(),
alice_copy.public_key().to_bytes()
);
assert_eq!(
alice.private_key().to_bytes(),
alice_copy.private_key().to_bytes()
);
println!(
"Alice copy public key (hex version): {}",
vec_to_hex(&alice_copy.public_key().to_bytes())
);
}
fn swap_keypairs(a: &KeyPair, b: &KeyPair) -> (KeyPair, KeyPair) {
let a_copy = KeyPair::from_bytes(
&a.public_key().to_bytes(),
&a.private_key().to_bytes(),
&new_standard_params(),
)
.unwrap();
let b_copy = KeyPair::from_bytes(
&b.public_key().to_bytes(),
&b.private_key().to_bytes(),
&new_standard_params(),
)
.unwrap();
(b_copy, a_copy)
}
fn main() {
let params = new_standard_params();
let mut alice = KeyPair::new(¶ms);
let signer = Signer::new(¶ms);
let mut bob = KeyPair::new(¶ms);
let plaintext = b"Hello, umbral!".to_vec();
let (ciphertext, mut capsule) = encrypt(&alice.public_key(), &plaintext).unwrap();
capsule.set_correctness_keys(&alice.public_key(), &bob.public_key(), &signer.public_key());
let threshold = 2;
let nodes_number = 5;
let kfrags = generate_kfrags(
&alice,
&bob.public_key(),
threshold,
nodes_number,
&signer,
KFragMode::DelegatingAndReceiving,
)
.unwrap();
for kfrag in kfrags {
let cfrag = reencrypt(&kfrag, &capsule, true, None, true).unwrap();
capsule.attach_cfrag(&cfrag).unwrap();
}
let plaintext_bob = decrypt(ciphertext, &capsule, &bob, true).unwrap();
assert_eq!(plaintext, plaintext_bob);
println!("{:?}", String::from_utf8(plaintext_bob.to_owned()).unwrap());
play(&alice);
println!();
println!();
println!("Swapping keypairs");
println!("Alice: {:?}", alice.public_key().to_bytes());
println!("Bob: {:?}", bob.public_key().to_bytes());
let (alice, bob) = swap_keypairs(&alice, &bob);
println!("Alice: {:?}", alice.public_key().to_bytes());
println!("Bob: {:?}", bob.public_key().to_bytes());
}