-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1915514
commit 20cedea
Showing
4 changed files
with
110 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub(crate) mod signer; | ||
pub(crate) mod threshold_signature; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use blst::min_sig::{PublicKey, SecretKey}; | ||
use rand_core::{CryptoRng, RngCore}; | ||
|
||
pub(crate) struct Signer { | ||
signing_key: SecretKey, | ||
pub(crate) verification_key: PublicKey, | ||
} | ||
|
||
impl Signer { | ||
pub(crate) fn new(rng: &mut (impl RngCore + CryptoRng)) -> Self { | ||
let mut ikm = [0u8; 32]; | ||
rng.fill_bytes(&mut ikm); | ||
let sk = SecretKey::key_gen(&ikm, &[]) | ||
.expect("Error occurs when the length of ikm < 32. This will not happen here."); | ||
let pk: PublicKey = sk.sk_to_pk(); | ||
Self { | ||
signing_key: sk, | ||
verification_key: pk, | ||
} | ||
} | ||
|
||
pub(crate) fn sign<const N: usize>(&self, msg: &[u8]) -> [u8; N] { | ||
let mut signature_to_byte = [0u8; N]; | ||
signature_to_byte.copy_from_slice(&self.signing_key.sign(msg, &[], &[]).to_bytes()); | ||
signature_to_byte | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
examples/simple_aggregate_signature/threshold_signature.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use crate::Element; | ||
use alba::centralized_telescope::params::Params; | ||
use alba::centralized_telescope::proof::Proof; | ||
use alba::centralized_telescope::CentralizedTelescope; | ||
use blst::min_sig::{AggregatePublicKey, AggregateSignature, PublicKey, Signature}; | ||
use blst::BLST_ERROR; | ||
use std::collections::HashMap; | ||
|
||
pub(crate) struct ThresholdSignature { | ||
proof: Proof, | ||
key_list: Vec<PublicKey>, | ||
} | ||
|
||
impl ThresholdSignature { | ||
pub(crate) fn aggregate<const N: usize>( | ||
alba_signatures: &HashMap<Element, usize>, | ||
params: &Params, | ||
key_list: &HashMap<usize, PublicKey>, | ||
) -> Self { | ||
let prover_set: Vec<Element> = alba_signatures.keys().copied().collect(); | ||
let alba = CentralizedTelescope::create(params); | ||
let proof = alba.prove(&prover_set).unwrap(); | ||
let signatures = proof.element_sequence.clone(); | ||
let mut public_keys = Vec::with_capacity(signatures.len()); | ||
|
||
for sig in signatures { | ||
public_keys.push( | ||
*key_list | ||
.get(alba_signatures.get(sig.as_slice()).unwrap()) | ||
.unwrap(), | ||
); | ||
} | ||
Self { | ||
proof, | ||
key_list: public_keys, | ||
} | ||
} | ||
|
||
/// Validates individual signatures in the threshold signature | ||
fn validate_signatures(&self, msg: &[u8]) -> bool { | ||
let mut signatures = Vec::with_capacity(self.proof.element_sequence.len()); | ||
for sig_bytes in &self.proof.element_sequence { | ||
let Ok(signature) = Signature::from_bytes(sig_bytes.as_slice()) else { | ||
return false; | ||
}; | ||
signatures.push(signature); | ||
} | ||
let signature_refs: Vec<&Signature> = signatures.iter().collect(); | ||
let Ok(aggregate_signature) = | ||
AggregateSignature::aggregate(signature_refs.as_slice(), false) | ||
else { | ||
return false; | ||
}; | ||
let final_signature = aggregate_signature.to_signature(); | ||
|
||
let public_key_refs: Vec<&PublicKey> = self.key_list.iter().collect(); | ||
let Ok(aggregate_verification_key) = | ||
AggregatePublicKey::aggregate(public_key_refs.as_slice(), false) | ||
else { | ||
return false; | ||
}; | ||
let final_verification_key = aggregate_verification_key.to_public_key(); | ||
|
||
let result = final_signature.verify(false, msg, &[], &[], &final_verification_key, false); | ||
result == BLST_ERROR::BLST_SUCCESS | ||
} | ||
|
||
pub(crate) fn verify(&self, msg: &[u8], params: &Params) -> bool { | ||
if self.validate_signatures(msg) { | ||
let alba = CentralizedTelescope::create(params); | ||
return alba.verify(&self.proof); | ||
} | ||
false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters