Skip to content

Commit

Permalink
Merge pull request #46 from pacu/randomized-tests
Browse files Browse the repository at this point in the history
Fix Randomized tests
  • Loading branch information
pacu authored Jun 21, 2024
2 parents 8e33091 + 62c6079 commit 3fea7f6
Show file tree
Hide file tree
Showing 9 changed files with 307 additions and 103 deletions.
18 changes: 17 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,20 @@ jobs:
- uses: actions/checkout@v4
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
- run: cargo build --verbose --all-features
- run: cargo test --verbose
- run: cargo test --verbose

build_and_test_redpallas:
if: ${{ ! startsWith(github.event.pull_request.head.ref, 'release-') }}
name: Rust project - latest
runs-on: ubuntu-latest
strategy:
matrix:
toolchain:
- stable
- beta
- nightly
steps:
- uses: actions/checkout@v4
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
- run: cargo build --verbose --features redpallas
- run: cargo test --verbose --features redpallas
4 changes: 2 additions & 2 deletions frost-uniffi-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ pub fn trusted_dealer_keygen_with_identifiers(
let trust_dealt_keys =
trusted_dealer_keygen(&configuration, list, &mut rng).map_err(FrostError::map_err)?;

let pubkey =
FrostPublicKeyPackage::from_public_key_package::<E>(trust_dealt_keys.public_keys).map_err(FrostError::map_err)?;
let pubkey = FrostPublicKeyPackage::from_public_key_package::<E>(trust_dealt_keys.public_keys)
.map_err(FrostError::map_err)?;

let mut hash_map: HashMap<ParticipantIdentifier, FrostSecretKeyShare> = HashMap::new();

Expand Down
39 changes: 38 additions & 1 deletion frost-uniffi-sdk/src/randomized/coordinator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(feature = "redpallas")]
use reddsa::frost::redpallas as frost;
use reddsa::frost::redpallas::RandomizedParams;

#[cfg(feature = "redpallas")]
use crate::randomized::randomizer::FrostRandomizer;
Expand All @@ -10,7 +11,10 @@ type E = reddsa::frost::redpallas::PallasBlake2b512;
type E = frost_ed25519::Ed25519Sha512;

use crate::{
coordinator::{CoordinationError, FrostSignature, FrostSigningPackage},
coordinator::{
CoordinationError, FrostSignature, FrostSignatureVerificationError, FrostSigningPackage,
Message,
},
participant::FrostSignatureShare,
FrostPublicKeyPackage,
};
Expand Down Expand Up @@ -66,3 +70,36 @@ pub fn aggregate(
})
.map(FrostSignature::from_signature)
}

#[uniffi::export]
pub fn verify_randomized_signature(
randomizer: FrostRandomizer,
message: Message,
signature: FrostSignature,
pubkey: FrostPublicKeyPackage,
) -> Result<(), FrostSignatureVerificationError> {
let randomizer = randomizer
.into_randomizer::<E>()
.map_err(|_| FrostSignatureVerificationError::InvalidPublicKeyPackage)?;

let signature = signature.to_signature::<E>().map_err(|e| {
FrostSignatureVerificationError::ValidationFailed {
reason: e.to_string(),
}
})?;

let pubkey = pubkey
.into_public_key_package()
.map_err(|_| FrostSignatureVerificationError::InvalidPublicKeyPackage)?;

let verifying_key = pubkey.verifying_key();

let randomizer_params = RandomizedParams::from_randomizer(verifying_key, randomizer);

randomizer_params
.randomized_verifying_key()
.verify(&message.data, &signature)
.map_err(|e| FrostSignatureVerificationError::ValidationFailed {
reason: e.to_string(),
})
}
2 changes: 1 addition & 1 deletion frost-uniffi-sdk/src/randomized/randomizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use uniffi;
type E = reddsa::frost::redpallas::PallasBlake2b512;

#[cfg(feature = "redpallas")]
#[derive(uniffi::Record)]
#[derive(uniffi::Record, Clone)]
pub struct FrostRandomizer {
data: Vec<u8>,
}
Expand Down
43 changes: 25 additions & 18 deletions frost-uniffi-sdk/src/randomized/tests/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,61 @@
use std::collections::HashMap;

use rand::rngs::ThreadRng;
use reddsa::frost::redpallas as frost;
#[cfg(feature = "redpallas")]
type E = reddsa::frost::redpallas::PallasBlake2b512;
#[cfg(not(feature = "redpallas"))]
type E = frost_ed25519::Ed25519Sha512;

use crate::coordinator::new_signing_package;
use crate::randomized::participant::sign;
use crate::randomized::randomizer::FrostRandomizer;
use crate::FrostPublicKeyPackage;
use crate::{
coordinator::{FrostSigningPackage, Message},
participant::{FrostSignatureShare, FrostSigningCommitments, FrostSigningNonces},
FrostKeyPackage, ParticipantIdentifier,
};
use frost::RandomizedParams;
use rand::rngs::ThreadRng;
use reddsa::frost::redpallas as frost;
use std::collections::HashMap;

pub fn round_2(
rng: &mut ThreadRng,
nonces_map: &HashMap<ParticipantIdentifier, FrostSigningNonces>,
key_packages: &HashMap<ParticipantIdentifier, FrostKeyPackage>,
commitments_map: HashMap<ParticipantIdentifier, FrostSigningCommitments>,
pub_key: FrostPublicKeyPackage,
message: Message,
randomizer: Option<FrostRandomizer>,
) -> (
FrostSigningPackage,
HashMap<ParticipantIdentifier, FrostSignatureShare>,
Option<FrostRandomizer>,
RandomizedParams,
) {
let commitments = commitments_map.into_iter().map(|c| c.1).collect();
let signing_package = new_signing_package(message, commitments).unwrap();
let mut signature_shares = HashMap::new();

let randomizer = match randomizer {
Some(r) => r,
None => {
let randomizer =
frost::round2::Randomizer::new(rng, &signing_package.to_signing_package().unwrap())
.unwrap();
let pub_keys = pub_key.into_public_key_package().unwrap();
let pallas_signing_package = signing_package.to_signing_package().unwrap();
let randomized_params =
RandomizedParams::new(pub_keys.verifying_key(), &pallas_signing_package, rng).unwrap();
let randomizer = randomized_params.randomizer();

FrostRandomizer::from_randomizer::<frost::PallasBlake2b512>(randomizer).unwrap()
}
};
let frost_randomizer = FrostRandomizer::from_randomizer::<E>(*randomizer).unwrap();

for participant_identifier in nonces_map.keys() {
let key_package = key_packages[participant_identifier].clone();

let nonces = nonces_map[participant_identifier].clone();

let signature_share =
sign(signing_package.clone(), nonces, key_package, &randomizer).unwrap();
let signature_share = sign(
signing_package.clone(),
nonces,
key_package,
&frost_randomizer,
)
.unwrap();

signature_shares.insert(participant_identifier.clone(), signature_share);
}

(signing_package, signature_shares, Some(randomizer))
(signing_package, signature_shares, randomized_params)
}
29 changes: 12 additions & 17 deletions frost-uniffi-sdk/src/trusted_dealer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ pub fn trusted_dealer_keygen_from_configuration<C: Ciphersuite>(

let trusted_dealt_keys = keygen?;



let pubkey = FrostPublicKeyPackage::from_public_key_package::<C>(trusted_dealt_keys.public_keys)?;
let pubkey =
FrostPublicKeyPackage::from_public_key_package::<C>(trusted_dealt_keys.public_keys)?;

let mut hash_map: HashMap<ParticipantIdentifier, FrostSecretKeyShare> = HashMap::new();

Expand All @@ -45,8 +44,8 @@ pub fn trusted_dealer_keygen_from_configuration<C: Ciphersuite>(
}

pub struct TrustDealtKeys<C: Ciphersuite> {
pub secret_shares: BTreeMap<Identifier<C>,SecretShare<C>>,
pub public_keys: PublicKeyPackage<C>
pub secret_shares: BTreeMap<Identifier<C>, SecretShare<C>>,
pub public_keys: PublicKeyPackage<C>,
}

pub fn trusted_dealer_keygen<C: Ciphersuite>(
Expand All @@ -65,12 +64,10 @@ pub fn trusted_dealer_keygen<C: Ciphersuite>(
frost::keys::KeyPackage::try_from(v)?;
}

Ok(
TrustDealtKeys {
secret_shares: shares,
public_keys: pubkeys
}
)
Ok(TrustDealtKeys {
secret_shares: shares,
public_keys: pubkeys,
})
}

fn split_secret<C: Ciphersuite>(
Expand All @@ -97,12 +94,10 @@ fn split_secret<C: Ciphersuite>(
frost::keys::KeyPackage::try_from(v)?;
}

Ok(
TrustDealtKeys {
secret_shares: shares,
public_keys: pubkeys
}
)
Ok(TrustDealtKeys {
secret_shares: shares,
public_keys: pubkeys,
})
}
#[cfg(test)]
mod tests {
Expand Down
52 changes: 38 additions & 14 deletions frost-uniffi-sdk/tests/dkg_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ use frost_core::Identifier;
use frost_uniffi_sdk::coordinator::{aggregate, verify_signature};

#[cfg(feature = "redpallas")]
use frost_uniffi_sdk::{
coordinator::verify_signature,
randomized::{coordinator::aggregate, tests::helpers::round_2},
use frost_uniffi_sdk::randomized::{
coordinator::{aggregate, verify_randomized_signature},
randomizer::FrostRandomizer,
tests::helpers::round_2,
};

mod helpers;
use helpers::round_1;

use rand::thread_rng;
use std::{collections::HashMap, sync::Arc};

Expand All @@ -24,8 +28,6 @@ use frost_uniffi_sdk::dkg::lib::{
part_1, part_2, part_3, DKGRound1Package, DKGRound1SecretPackage, DKGRound2Package,
DKGRound2SecretPackage,
};
mod helpers;
use helpers::round_1;

#[cfg(not(feature = "redpallas"))]
use helpers::round_2;
Expand Down Expand Up @@ -198,36 +200,58 @@ fn test_dkg_from_3_participants() {
};

#[cfg(feature = "redpallas")]
let (signing_package, signature_shares, randomizer) = round_2(
let pubkey = pubkeys.get(&p1_identifier).unwrap();

#[cfg(feature = "redpallas")]
let (signing_package, signature_shares, randomized_params) = round_2(
&mut rng,
&nonces,
&key_packages,
commitments,
pubkey.clone(),
message.clone(),
None,
);

#[cfg(not(feature = "redpallas"))]
let (signing_package, signature_shares) =
round_2(&nonces, &key_packages, commitments, message.clone());

#[cfg(feature = "redpallas")]
let randomizer =
FrostRandomizer::from_randomizer::<E>(*randomized_params.randomizer()).unwrap();

let p1identifier = p1_identifier.clone();
let pubkey = pubkeys.get(&p1identifier).unwrap().clone();
let group_signature = aggregate(
signing_package,
signature_shares.into_iter().map(|s| s.1).collect(),
pubkey.clone(),
#[cfg(feature = "redpallas")]
randomizer.unwrap(),
randomizer.clone(),
)
.unwrap();

let verify_signature = verify_signature(message, group_signature, pubkey.clone());

match verify_signature {
Ok(()) => assert!(true),
Err(e) => {
assert!(false, "signature verification failed with error: {e:?}")
#[cfg(feature = "redpallas")]
{
let verify_signature =
verify_randomized_signature(randomizer, message, group_signature, pubkey);

match verify_signature {
Ok(()) => assert!(true),
Err(e) => {
assert!(false, "signature verification failed with error: {e:?}")
}
}
}
#[cfg(not(feature = "redpallas"))]
{
let verify_signature = verify_signature(message, group_signature, pubkey.clone());

match verify_signature {
Ok(()) => assert!(true),
Err(e) => {
assert!(false, "signature verification failed with error: {e:?}")
}
}
}
}
Loading

0 comments on commit 3fea7f6

Please sign in to comment.