Skip to content

Commit 2e98a3f

Browse files
committed
Pass on stm module
* StmSig does not require the Verification key + PoP, as in the registration PoP is already verified. * PartyId is no longer linked to a signature. * Introduced the StmAggrVerificationKey, which essentially is the root of the merkle tree and the full stake. * Renamed StmMultiSig -> StmAggrSig * Minor optimisation in StmAggrSig. Instead of individually verifying `k` signatures (= to evaluating `k` pairings), we first aggregate the signatures, and the keys, and verify the aggregate (= `k` additions in G1 and G2 and one pairing evaluation)
1 parent 9cfa79c commit 2e98a3f

File tree

6 files changed

+218
-175
lines changed

6 files changed

+218
-175
lines changed

mithril-core/examples/key_registration.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
//! run presented in `tests/integration.rs`, we explicitly treat each party individually.
44
55
use mithril::key_reg::{ClosedKeyReg, KeyReg};
6-
use mithril::stm::{
7-
Stake, StmClerk, StmInitializer, StmParameters, StmSig, StmSigner, StmVerificationKey,
8-
StmVerifier,
9-
};
6+
use mithril::stm::{Stake, StmClerk, StmInitializer, StmParameters, StmSig, StmSigner, StmVerificationKeyPoP, StmVerifier};
107

118
use rand_chacha::ChaCha20Rng;
129
use rand_core::{RngCore, SeedableRng};
@@ -46,7 +43,7 @@ fn main() {
4643
let party_3_init = StmInitializer::setup(params, parties[3].0, parties[3].1, &mut rng);
4744

4845
// The public keys are broadcast. All participants will have the same keys.
49-
let parties_pks: Vec<StmVerificationKey> = vec![
46+
let parties_pks: Vec<StmVerificationKeyPoP> = vec![
5047
party_0_init.verification_key(),
5148
party_1_init.verification_key(),
5249
party_2_init.verification_key(),
@@ -113,7 +110,7 @@ fn main() {
113110
let closed_registration = local_reg(&parties, &parties_pks);
114111
let clerk = StmClerk::from_registration(params, closed_registration.clone());
115112
let verifier = StmVerifier::new(
116-
closed_registration.avk.to_commitment(),
113+
closed_registration.merkle_tree.to_commitment(),
117114
params,
118115
closed_registration.total_stake,
119116
);
@@ -146,7 +143,7 @@ fn try_signatures(party: &StmSigner<H>, msg: &[u8], m: u64) -> Vec<StmSig<H>> {
146143
.collect()
147144
}
148145

149-
fn local_reg(ids: &[(u64, u64)], pks: &[StmVerificationKey]) -> ClosedKeyReg<H> {
146+
fn local_reg(ids: &[(u64, u64)], pks: &[StmVerificationKeyPoP]) -> ClosedKeyReg<H> {
150147
let mut local_keyreg = KeyReg::init(ids);
151148
// todo: maybe its cleaner to have a `StmPublic` instance that covers the "shareable"
152149
// data, such as the public key, stake and id.

mithril-core/src/c_api.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use std::os::raw::c_char;
99
pub const NULLPOINTERERR: i64 = -99;
1010

1111
type H = blake2::Blake2b;
12-
type StmVerificationKeyPtr = *mut StmVerificationKey;
12+
type StmVerificationKeyPoPPtr = *mut StmVerificationKeyPoP;
1313
type SigPtr = *mut StmSig<H>;
14-
type MultiSigPtr = *mut StmMultiSig<H>;
14+
type MultiSigPtr = *mut StmAggrSig<H>;
1515
type StmInitializerPtr = *mut StmInitializer;
1616
type StmSignerPtr = *mut StmSigner<H>;
1717
type StmClerkPtr = *mut StmClerk<H>;
@@ -315,7 +315,7 @@ mod initializer {
315315
#[no_mangle]
316316
pub extern "C" fn stm_initializer_verification_key(
317317
me: StmInitializerPtr,
318-
pk: *mut StmVerificationKeyPtr,
318+
pk: *mut StmVerificationKeyPoPPtr,
319319
) -> i64 {
320320
unsafe {
321321
if let (Some(ref_me), Some(ref_pk)) = (me.as_ref(), pk.as_mut()) {
@@ -444,7 +444,7 @@ mod signer {
444444

445445
mod key_reg {
446446
use crate::c_api::{
447-
ClosedKeyRegPtr, KeyRegPtr, MerkleTreeCommitmentPtr, StmVerificationKeyPtr, NULLPOINTERERR,
447+
ClosedKeyRegPtr, KeyRegPtr, MerkleTreeCommitmentPtr, StmVerificationKeyPoPPtr, NULLPOINTERERR,
448448
};
449449
use crate::error::RegisterError;
450450
use crate::key_reg::KeyReg;
@@ -488,7 +488,7 @@ mod key_reg {
488488
pub extern "C" fn register_party(
489489
key_reg: KeyRegPtr,
490490
party_id: PartyId,
491-
party_key: StmVerificationKeyPtr,
491+
party_key: StmVerificationKeyPoPPtr,
492492
) -> i64 {
493493
unsafe {
494494
if let (Some(ref_key_reg), Some(party_key)) = (key_reg.as_mut(), party_key.as_ref()) {
@@ -511,7 +511,7 @@ mod key_reg {
511511
) -> i64 {
512512
unsafe {
513513
if let (Some(key_reg), Some(mk_tree)) = (key_reg.as_ref(), mk_tree.as_mut()) {
514-
*mk_tree = Box::into_raw(Box::new(key_reg.avk.to_commitment()));
514+
*mk_tree = Box::into_raw(Box::new(key_reg.merkle_tree.to_commitment()));
515515
return 0;
516516
}
517517
NULLPOINTERERR
@@ -582,12 +582,13 @@ mod clerk {
582582
}
583583
}
584584

585-
/// Try to verify a signature.
585+
/// Verify a signature.
586586
/// returns:
587587
/// * 0 if the signature is valid
588588
/// * -1 if the lottery win is false
589-
/// * -2 if the Merkle Tree is invalid
589+
/// * -2 if the Merkle Tree path is invalid
590590
/// * -3 if the MSP signature is invalid
591+
/// * -4 if the Index is out of bounds
591592
/// * NULLPOINTERERR if invalid pointers
592593
///
593594
#[no_mangle]
@@ -601,12 +602,14 @@ mod clerk {
601602
(me.as_ref(), msg.as_ref(), sig.as_ref())
602603
{
603604
let msg_str = CStr::from_ptr(msg);
604-
let out = ref_me.verify_sig(ref_sig, msg_str.to_bytes());
605+
let avk = StmAggrVerificationKey::from(&ref_me.closed_reg);
606+
let out = ref_sig.verify(&ref_me.params, &avk, msg_str.to_bytes());
605607
return match out {
606608
Ok(()) => 0,
607609
Err(VerificationFailure::LotteryLost) => -1,
608610
Err(VerificationFailure::InvalidMerkleTree(_)) => -2,
609611
Err(VerificationFailure::InvalidSignature(_)) => -3,
612+
Err(VerificationFailure::IndexBoundFailed(_,_)) => -4,
610613
};
611614
}
612615
NULLPOINTERERR

mithril-core/src/error.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ pub enum AggregationFailure {
8686
/// Error types for single signature verification
8787
#[derive(Debug, Clone, thiserror::Error)]
8888
pub enum VerificationFailure<D: Digest + FixedOutput> {
89+
/// The signature index is out of bounds
90+
#[error("Received index, {0}, is higher than what the security parameter allows, {1}.")]
91+
IndexBoundFailed(u64, u64),
8992
/// The lottery was actually lost for the signature
9093
#[error("Lottery for this epoch was lost.")]
9194
LotteryLost,
@@ -151,6 +154,20 @@ impl From<MerkleTreeError> for MultiSignatureError {
151154
}
152155
}
153156

157+
impl<D: Digest + Clone + FixedOutput> From<MultiSignatureError> for MithrilWitnessError<D> {
158+
fn from(_: MultiSignatureError) -> Self {
159+
// todo:
160+
Self::StakeInvalid
161+
}
162+
}
163+
164+
impl<D: Digest + Clone + FixedOutput> From<VerificationFailure<D>> for MithrilWitnessError<D> {
165+
fn from(_: VerificationFailure<D>) -> Self {
166+
// todo:
167+
Self::StakeInvalid
168+
}
169+
}
170+
154171
pub(crate) fn blst_err_to_atms(e: BLST_ERROR) -> Result<(), MultiSignatureError> {
155172
match e {
156173
BLST_ERROR::BLST_SUCCESS => Ok(()),

mithril-core/src/key_reg.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::error::RegisterError;
44
use digest::{Digest, FixedOutput};
55
use std::collections::{HashMap, HashSet};
66
use std::sync::Arc;
7+
use serde::{Deserialize, Serialize};
78

89
use super::multi_sig::VerificationKeyPoP;
910
use super::stm::{PartyId, Stake};
@@ -23,7 +24,11 @@ pub struct KeyReg {
2324

2425
/// Structure generated out of a closed registration. One can only get a global `avk` out of
2526
/// a closed key registration.
26-
#[derive(Clone, Debug, PartialEq, Eq)]
27+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
28+
#[serde(bound(
29+
serialize = "MerkleTree<D>: Serialize",
30+
deserialize = "MerkleTree<D>: Deserialize<'de>"
31+
))]
2732
pub struct ClosedKeyReg<D>
2833
where
2934
D: Digest + FixedOutput,
@@ -33,7 +38,7 @@ where
3338
/// Total stake of the registered parties.
3439
pub total_stake: Stake,
3540
/// Unique public key out of the key registration instance.
36-
pub avk: Arc<MerkleTree<D>>,
41+
pub merkle_tree: Arc<MerkleTree<D>>,
3742
}
3843

3944
/// Represents the status of a known participant in the protocol who is allowed
@@ -110,7 +115,7 @@ impl KeyReg {
110115
reg_parties.sort();
111116

112117
ClosedKeyReg {
113-
avk: Arc::new(MerkleTree::create(&reg_parties)),
118+
merkle_tree: Arc::new(MerkleTree::create(&reg_parties)),
114119
reg_parties,
115120
total_stake,
116121
}

0 commit comments

Comments
 (0)