Skip to content

Commit bad4030

Browse files
committed
basic signer is feature
1 parent 136c5f1 commit bad4030

File tree

3 files changed

+121
-109
lines changed

3 files changed

+121
-109
lines changed

mithril-stm/src/protocol/participant/initializer.rs

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ use rand_core::{CryptoRng, RngCore};
55
use serde::{Deserialize, Serialize};
66

77
use super::Signer;
8+
#[cfg(feature = "basic_verifier")]
9+
use crate::RegisteredParty;
810
use crate::{
9-
ClosedKeyRegistration, Parameters, RegisterError, RegisteredParty, Stake, StmResult,
11+
ClosedKeyRegistration, Parameters, RegisterError, Stake, StmResult,
1012
signature_scheme::{BlsSigningKey, BlsVerificationKeyProofOfPossession},
1113
};
1214

@@ -120,10 +122,49 @@ impl Initializer {
120122
Self::create_signer(self, closed_reg)
121123
}
122124

125+
/// Convert to bytes
126+
/// # Layout
127+
/// * Stake (u64)
128+
/// * Params
129+
/// * Secret Key
130+
/// * Public key (including PoP)
131+
pub fn to_bytes(&self) -> [u8; 256] {
132+
let mut out = [0u8; 256];
133+
out[..8].copy_from_slice(&self.stake.to_be_bytes());
134+
out[8..32].copy_from_slice(&self.params.to_bytes());
135+
out[32..64].copy_from_slice(&self.sk.to_bytes());
136+
out[64..].copy_from_slice(&self.pk.to_bytes());
137+
out
138+
}
139+
140+
/// Convert a slice of bytes to an `Initializer`
141+
/// # Error
142+
/// The function fails if the given string of bytes is not of required size.
143+
pub fn from_bytes(bytes: &[u8]) -> StmResult<Initializer> {
144+
let mut u64_bytes = [0u8; 8];
145+
u64_bytes.copy_from_slice(bytes.get(..8).ok_or(RegisterError::SerializationError)?);
146+
let stake = u64::from_be_bytes(u64_bytes);
147+
let params =
148+
Parameters::from_bytes(bytes.get(8..32).ok_or(RegisterError::SerializationError)?)?;
149+
let sk =
150+
BlsSigningKey::from_bytes(bytes.get(32..).ok_or(RegisterError::SerializationError)?)?;
151+
let pk = VerificationKeyProofOfPossession::from_bytes(
152+
bytes.get(64..).ok_or(RegisterError::SerializationError)?,
153+
)?;
154+
155+
Ok(Self {
156+
stake,
157+
params,
158+
sk,
159+
pk,
160+
})
161+
}
162+
123163
/// Creates a new basic signer that does not include closed registration.
124164
/// Takes `eligible_parties` as a parameter and determines the signer's index in the parties.
125165
/// `eligible_parties` is verified and trusted which is only run by a full-node
126166
/// that has already verified the parties.
167+
#[cfg(feature = "basic_verifier")]
127168
pub fn create_basic_signer<D: Digest + Clone + FixedOutput>(
128169
self,
129170
eligible_parties: &[RegisteredParty],
@@ -155,50 +196,13 @@ impl Initializer {
155196
/// `eligible_parties` is verified and trusted which is only run by a full-node
156197
/// that has already verified the parties.
157198
#[deprecated(since = "0.5.0", note = "Use `create_basic_signer` instead")]
199+
#[cfg(feature = "basic_verifier")]
158200
pub fn new_core_signer<D: Digest + Clone + FixedOutput>(
159201
self,
160202
eligible_parties: &[RegisteredParty],
161203
) -> Option<Signer<D>> {
162204
Self::create_basic_signer(self, eligible_parties)
163205
}
164-
165-
/// Convert to bytes
166-
/// # Layout
167-
/// * Stake (u64)
168-
/// * Params
169-
/// * Secret Key
170-
/// * Public key (including PoP)
171-
pub fn to_bytes(&self) -> [u8; 256] {
172-
let mut out = [0u8; 256];
173-
out[..8].copy_from_slice(&self.stake.to_be_bytes());
174-
out[8..32].copy_from_slice(&self.params.to_bytes());
175-
out[32..64].copy_from_slice(&self.sk.to_bytes());
176-
out[64..].copy_from_slice(&self.pk.to_bytes());
177-
out
178-
}
179-
180-
/// Convert a slice of bytes to an `Initializer`
181-
/// # Error
182-
/// The function fails if the given string of bytes is not of required size.
183-
pub fn from_bytes(bytes: &[u8]) -> StmResult<Initializer> {
184-
let mut u64_bytes = [0u8; 8];
185-
u64_bytes.copy_from_slice(bytes.get(..8).ok_or(RegisterError::SerializationError)?);
186-
let stake = u64::from_be_bytes(u64_bytes);
187-
let params =
188-
Parameters::from_bytes(bytes.get(8..32).ok_or(RegisterError::SerializationError)?)?;
189-
let sk =
190-
BlsSigningKey::from_bytes(bytes.get(32..).ok_or(RegisterError::SerializationError)?)?;
191-
let pk = VerificationKeyProofOfPossession::from_bytes(
192-
bytes.get(64..).ok_or(RegisterError::SerializationError)?,
193-
)?;
194-
195-
Ok(Self {
196-
stake,
197-
params,
198-
sk,
199-
pk,
200-
})
201-
}
202206
}
203207

204208
impl PartialEq for Initializer {

mithril-stm/src/protocol/participant/signer.rs

Lines changed: 71 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct Signer<D: Digest> {
2222
params: Parameters,
2323
sk: BlsSigningKey,
2424
vk: VerificationKey,
25-
closed_reg: ClosedKeyRegistration<D>,
25+
closed_reg: Option<ClosedKeyRegistration<D>>,
2626
}
2727

2828
impl<D: Clone + Digest + FixedOutput> Signer<D> {
@@ -41,7 +41,7 @@ impl<D: Clone + Digest + FixedOutput> Signer<D> {
4141
params,
4242
sk,
4343
vk,
44-
closed_reg,
44+
closed_reg: Some(closed_reg),
4545
}
4646
}
4747

@@ -58,59 +58,27 @@ impl<D: Clone + Digest + FixedOutput> Signer<D> {
5858
Self::set_signer(signer_index, stake, params, sk, vk, closed_reg)
5959
}
6060

61-
/// Create a basic signer (no registration data) for given input
62-
pub(crate) fn set_basic_signer(
63-
signer_index: u64,
64-
stake: Stake,
65-
params: Parameters,
66-
sk: BlsSigningKey,
67-
vk: VerificationKey,
68-
) -> Signer<D> {
69-
Self {
70-
signer_index,
71-
stake,
72-
params,
73-
sk,
74-
vk,
75-
closed_reg: todo!(),
76-
}
77-
}
78-
79-
/// Create a core signer (no registration data) for given input
80-
#[deprecated(since = "0.5.0", note = "Use `set_basic_signer` instead")]
81-
pub fn set_core_signer(
82-
signer_index: u64,
83-
stake: Stake,
84-
params: Parameters,
85-
sk: BlsSigningKey,
86-
vk: VerificationKey,
87-
) -> Signer<D> {
88-
Self::set_basic_signer(signer_index, stake, params, sk, vk)
89-
}
90-
9161
/// This function produces a signature following the description of Section 2.4.
9262
/// Once the signature is produced, this function checks whether any index in `[0,..,self.params.m]`
9363
/// wins the lottery by evaluating the dense mapping.
9464
/// It records all the winning indexes in `Self.indexes`.
9565
/// If it wins at least one lottery, it stores the signer's merkle tree index. The proof of membership
9666
/// will be handled by the aggregator.
9767
pub fn sign(&self, msg: &[u8]) -> Option<SingleSignature> {
98-
// let closed_reg = self.closed_reg.as_ref().expect("Closed registration not found! Cannot produce SingleSignatures. Use core_sign to produce core signatures (not valid for an StmCertificate).");
99-
let msgp = self
100-
.closed_reg
68+
let closed_reg = self.closed_reg.as_ref().expect("Closed registration not found! Cannot produce SingleSignatures. Use core_sign to produce core signatures (not valid for an StmCertificate).");
69+
let msgp = closed_reg
10170
.merkle_tree
10271
.to_merkle_tree_batch_commitment()
10372
.concatenate_with_message(msg);
10473
let sigma = self.sk.sign(&msgp);
10574

106-
let indexes = self.check_lottery(&msgp, &sigma, self.closed_reg.total_stake);
75+
let indexes = self.check_lottery(&msgp, &sigma, closed_reg.total_stake);
10776

108-
// let signature = self.basic_sign(&msgp, closed_reg.total_stake)?;
10977
if !indexes.is_empty() {
11078
Some(SingleSignature {
11179
sigma,
112-
signer_index: self.signer_index,
11380
indexes,
81+
signer_index: self.signer_index,
11482
})
11583
} else {
11684
None
@@ -133,36 +101,6 @@ impl<D: Clone + Digest + FixedOutput> Signer<D> {
133101
self.stake
134102
}
135103

136-
/// A basic signature generated without closed key registration.
137-
/// The basic signature can be verified by basic verifier.
138-
/// Once the signature is produced, this function checks whether any index in `[0,..,self.params.m]`
139-
/// wins the lottery by evaluating the dense mapping.
140-
/// It records all the winning indexes in `Self.indexes`.
141-
pub fn basic_sign(&self, msg: &[u8], total_stake: Stake) -> Option<SingleSignature> {
142-
let sigma = self.sk.sign(msg);
143-
144-
let indexes = self.check_lottery(msg, &sigma, total_stake);
145-
if !indexes.is_empty() {
146-
Some(SingleSignature {
147-
sigma,
148-
indexes,
149-
signer_index: self.signer_index,
150-
})
151-
} else {
152-
None
153-
}
154-
}
155-
156-
/// A basic signature generated without closed key registration.
157-
/// The basic signature can be verified by basic verifier.
158-
/// Once the signature is produced, this function checks whether any index in `[0,..,self.params.m]`
159-
/// wins the lottery by evaluating the dense mapping.
160-
/// It records all the winning indexes in `Self.indexes`.
161-
#[deprecated(since = "0.5.0", note = "Use `basic_sign` instead")]
162-
pub fn core_sign(&self, msg: &[u8], total_stake: Stake) -> Option<SingleSignature> {
163-
Self::basic_sign(self, msg, total_stake)
164-
}
165-
166104
/// Collects and returns the winning indices.
167105
pub fn check_lottery(&self, msg: &[u8], sigma: &BlsSignature, total_stake: Stake) -> Vec<u64> {
168106
let mut indexes = Vec::new();
@@ -192,12 +130,76 @@ impl<D: Clone + Digest + FixedOutput> Signer<D> {
192130

193131
/// Get closed key registration
194132
pub(crate) fn get_closed_key_registration(&self) -> Option<ClosedKeyRegistration<D>> {
195-
Some(self.closed_reg.clone())
133+
self.closed_reg.clone()
196134
}
197135

198136
/// Get closed key registration
199137
#[deprecated(since = "0.5.0", note = "Use `get_closed_key_registration` instead")]
200138
pub fn get_closed_reg(&self) -> Option<ClosedKeyRegistration<D>> {
201139
Self::get_closed_key_registration(self)
202140
}
141+
142+
/// Create a basic signer (no registration data) for given input
143+
#[cfg(feature = "basic_verifier")]
144+
pub(crate) fn set_basic_signer(
145+
signer_index: u64,
146+
stake: Stake,
147+
params: Parameters,
148+
sk: BlsSigningKey,
149+
vk: VerificationKey,
150+
) -> Signer<D> {
151+
Self {
152+
signer_index,
153+
stake,
154+
params,
155+
sk,
156+
vk,
157+
closed_reg: None,
158+
}
159+
}
160+
161+
/// Create a core signer (no registration data) for given input
162+
#[deprecated(since = "0.5.0", note = "Use `set_basic_signer` instead")]
163+
#[cfg(feature = "basic_verifier")]
164+
pub fn set_core_signer(
165+
signer_index: u64,
166+
stake: Stake,
167+
params: Parameters,
168+
sk: BlsSigningKey,
169+
vk: VerificationKey,
170+
) -> Signer<D> {
171+
Self::set_basic_signer(signer_index, stake, params, sk, vk)
172+
}
173+
174+
/// A basic signature generated without closed key registration.
175+
/// The basic signature can be verified by basic verifier.
176+
/// Once the signature is produced, this function checks whether any index in `[0,..,self.params.m]`
177+
/// wins the lottery by evaluating the dense mapping.
178+
/// It records all the winning indexes in `Self.indexes`.
179+
#[cfg(feature = "basic_verifier")]
180+
pub fn basic_sign(&self, msg: &[u8], total_stake: Stake) -> Option<SingleSignature> {
181+
let sigma = self.sk.sign(msg);
182+
183+
let indexes = self.check_lottery(msg, &sigma, total_stake);
184+
if !indexes.is_empty() {
185+
Some(SingleSignature {
186+
sigma,
187+
indexes,
188+
signer_index: self.signer_index,
189+
})
190+
} else {
191+
None
192+
}
193+
}
194+
195+
/// A basic signature generated without closed key registration.
196+
/// The basic signature can be verified by basic verifier.
197+
/// Once the signature is produced, this function checks whether any index in `[0,..,self.params.m]`
198+
/// wins the lottery by evaluating the dense mapping.
199+
/// It records all the winning indexes in `Self.indexes`.
200+
#[deprecated(since = "0.5.0", note = "Use `basic_sign` instead")]
201+
#[cfg(feature = "basic_verifier")]
202+
pub fn core_sign(&self, msg: &[u8], total_stake: Stake) -> Option<SingleSignature> {
203+
Self::basic_sign(self, msg, total_stake)
204+
}
203205
}

mithril-stm/src/protocol/single_signature/signature.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ impl SingleSignature {
3535
msg: &[u8],
3636
) -> StmResult<()> {
3737
let msgp = avk.get_merkle_tree_batch_commitment().concatenate_with_message(msg);
38-
self.basic_verify(params, pk, stake, &msgp, &avk.get_total_stake())
38+
self.sigma.verify(&msgp, pk).with_context(|| {
39+
format!(
40+
"Single signature verification failed for signer index {}.",
41+
self.signer_index
42+
)
43+
})?;
44+
self.check_indices(params, stake, &msgp, &avk.get_total_stake())
3945
.with_context(|| {
4046
format!(
4147
"Single signature verification failed for signer index {}.",

0 commit comments

Comments
 (0)