Skip to content

Commit

Permalink
test: update tests for dkgs with relaxed constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jan 31, 2024
1 parent 514221e commit 7e57d2a
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 149 deletions.
217 changes: 118 additions & 99 deletions ferveo-wasm/tests/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ type TestSetup = (

const TAU: u32 = 0;

fn setup_dkg(shares_num: u32, security_threshold: u32) -> TestSetup {
let validator_keypairs = (0..shares_num as usize)
fn setup_dkg(
shares_num: u32,
validators_num: u32,
security_threshold: u32,
) -> TestSetup {
let validator_keypairs = (0..validators_num as usize)
.map(gen_keypair)
.collect::<Vec<Keypair>>();
let validators = validator_keypairs
Expand All @@ -32,15 +36,15 @@ fn setup_dkg(shares_num: u32, security_threshold: u32) -> TestSetup {
// Each validator holds their own DKG instance and generates a transcript every
// validator, including themselves
let messages = validators.iter().map(|sender| {
let mut dkg = Dkg::new(
let mut validator_dkg = Dkg::new(
TAU,
shares_num,
security_threshold,
&validators_js,
sender,
)
.unwrap();
let transcript = dkg.generate_transcript().unwrap();
let transcript = validator_dkg.generate_transcript().unwrap();

ValidatorMessage::new(sender, &transcript).unwrap()
});
Expand All @@ -61,12 +65,16 @@ fn setup_dkg(shares_num: u32, security_threshold: u32) -> TestSetup {

// Server can aggregate the transcripts and verify them
let server_aggregate = dkg.aggregate_transcripts(&messages_js).unwrap();
let is_valid = server_aggregate.verify(shares_num, &messages_js).unwrap();
let is_valid = server_aggregate
.verify(validators_num, &messages_js)
.unwrap();
assert!(is_valid);

// Client can also aggregate the transcripts and verify them
let client_aggregate = AggregatedTranscript::new(&messages_js).unwrap();
let is_valid = client_aggregate.verify(shares_num, &messages_js).unwrap();
let is_valid = client_aggregate
.verify(validators_num, &messages_js)
.unwrap();
assert!(is_valid);

// In the meantime, the client creates a ciphertext and decryption request
Expand All @@ -88,105 +96,116 @@ fn setup_dkg(shares_num: u32, security_threshold: u32) -> TestSetup {
#[wasm_bindgen_test]
fn tdec_simple() {
let shares_num = 16;
let security_threshold = 10;
let (
validator_keypairs,
validators,
validators_js,
messages_js,
msg,
aad,
ciphertext,
) = setup_dkg(shares_num, security_threshold);

// Having aggregated the transcripts, the validators can now create decryption shares
let decryption_shares = zip_eq(validators, validator_keypairs)
.map(|(validator, keypair)| {
let mut dkg = Dkg::new(
TAU,
shares_num,
security_threshold,
&validators_js,
&validator,
)
.unwrap();
let aggregate = dkg.aggregate_transcripts(&messages_js).unwrap();
let is_valid = aggregate.verify(shares_num, &messages_js).unwrap();
assert!(is_valid);

aggregate
.create_decryption_share_simple(
&dkg,
&ciphertext.header().unwrap(),
&aad,
&keypair,
let security_threshold = shares_num / 2;
for validators_num in [shares_num, shares_num + 2] {
let (
validator_keypairs,
validators,
validators_js,
messages_js,
msg,
aad,
ciphertext,
) = setup_dkg(shares_num, validators_num, security_threshold);

// Having aggregated the transcripts, the validators can now create decryption shares
let decryption_shares = zip_eq(validators, validator_keypairs)
.map(|(validator, keypair)| {
let mut dkg = Dkg::new(
TAU,
shares_num,
security_threshold,
&validators_js,
&validator,
)
.unwrap()
})
.collect::<Vec<DecryptionShareSimple>>();
let decryption_shares_js = into_js_array(decryption_shares);

// Now, the decryption share can be used to decrypt the ciphertext
// This part is in the client API

let shared_secret =
combine_decryption_shares_simple(&decryption_shares_js).unwrap();

// The client should have access to the public parameters of the DKG
let plaintext =
decrypt_with_shared_secret(&ciphertext, &aad, &shared_secret).unwrap();
assert_eq!(msg, plaintext);
.unwrap();
let aggregate =
dkg.aggregate_transcripts(&messages_js).unwrap();
let is_valid =
aggregate.verify(validators_num, &messages_js).unwrap();
assert!(is_valid);

aggregate
.create_decryption_share_simple(
&dkg,
&ciphertext.header().unwrap(),
&aad,
&keypair,
)
.unwrap()
})
.collect::<Vec<DecryptionShareSimple>>();
let decryption_shares_js = into_js_array(decryption_shares);

// Now, the decryption share can be used to decrypt the ciphertext
// This part is in the client API

let shared_secret =
combine_decryption_shares_simple(&decryption_shares_js).unwrap();

// The client should have access to the public parameters of the DKG
let plaintext =
decrypt_with_shared_secret(&ciphertext, &aad, &shared_secret)
.unwrap();
assert_eq!(msg, plaintext);
}
}

#[wasm_bindgen_test]
fn tdec_precomputed() {
let shares_num = 16;
let security_threshold = shares_num; // Must be equal to shares_num in precomputed variant
let (
validator_keypairs,
validators,
validators_js,
messages_js,
msg,
aad,
ciphertext,
) = setup_dkg(shares_num, security_threshold);

// Having aggregated the transcripts, the validators can now create decryption shares
let decryption_shares = zip_eq(validators, validator_keypairs)
.map(|(validator, keypair)| {
let mut dkg = Dkg::new(
TAU,
shares_num,
security_threshold,
&validators_js,
&validator,
)
.unwrap();
let aggregate = dkg.aggregate_transcripts(&messages_js).unwrap();
let is_valid = aggregate.verify(shares_num, &messages_js).unwrap();
assert!(is_valid);

aggregate
.create_decryption_share_precomputed(
&dkg,
&ciphertext.header().unwrap(),
&aad,
&keypair,
for validators_num in [shares_num, shares_num + 2] {
let (
validator_keypairs,
validators,
validators_js,
messages_js,
msg,
aad,
ciphertext,
) = setup_dkg(shares_num, validators_num, security_threshold);

// Having aggregated the transcripts, the validators can now create decryption shares
let decryption_shares = zip_eq(validators, validator_keypairs)
.map(|(validator, keypair)| {
let mut dkg = Dkg::new(
TAU,
shares_num,
security_threshold,
&validators_js,
&validator,
)
.unwrap()
})
.collect::<Vec<DecryptionSharePrecomputed>>();
let decryption_shares_js = into_js_array(decryption_shares);

// Now, the decryption share can be used to decrypt the ciphertext
// This part is in the client API

let shared_secret =
combine_decryption_shares_precomputed(&decryption_shares_js).unwrap();

// The client should have access to the public parameters of the DKG
let plaintext =
decrypt_with_shared_secret(&ciphertext, &aad, &shared_secret).unwrap();
assert_eq!(msg, plaintext);
.unwrap();
let aggregate =
dkg.aggregate_transcripts(&messages_js).unwrap();
let is_valid =
aggregate.verify(validators_num, &messages_js).unwrap();
assert!(is_valid);

aggregate
.create_decryption_share_precomputed(
&dkg,
&ciphertext.header().unwrap(),
&aad,
&keypair,
)
.unwrap()
})
.collect::<Vec<DecryptionSharePrecomputed>>();
let decryption_shares_js = into_js_array(decryption_shares);

// Now, the decryption share can be used to decrypt the ciphertext
// This part is in the client API

let shared_secret =
combine_decryption_shares_precomputed(&decryption_shares_js)
.unwrap();

// The client should have access to the public parameters of the DKG
let plaintext =
decrypt_with_shared_secret(&ciphertext, &aad, &shared_secret)
.unwrap();
assert_eq!(msg, plaintext);
}
}
Loading

0 comments on commit 7e57d2a

Please sign in to comment.