Skip to content

Commit fb9249b

Browse files
committed
Remove serialisation from signer and clerk
This is intentional, to force generation of signers or clerks to produce an instance from valid keys (and their PoP).
1 parent 22b4592 commit fb9249b

File tree

3 files changed

+19
-155
lines changed

3 files changed

+19
-155
lines changed

mithril-core/src/c_api.rs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -378,27 +378,8 @@ mod initializer {
378378
mod signer {
379379
use super::*;
380380

381-
/// Checks if a signer is eligible to sign. Returns 0 on success, -1 on failure, or -99 if
382-
/// pointers are invalid.
383-
#[no_mangle]
384-
pub extern "C" fn stm_signer_eligibility_check(
385-
me: StmSignerPtr,
386-
msg: *const c_char,
387-
index: Index,
388-
) -> i64 {
389-
unsafe {
390-
if let (Some(ref_me), Some(msg)) = (me.as_ref(), msg.as_ref()) {
391-
let msg_str = CStr::from_ptr(msg);
392-
if ref_me.eligibility_check(msg_str.to_bytes(), index) {
393-
return 0;
394-
}
395-
return -1;
396-
}
397-
NULLPOINTERERR
398-
}
399-
}
400-
401-
/// Try to sign a message. Sets *out to point to the signature if successful.
381+
/// Try to sign a message. Sets *out to point to the signature if successful. Returns 0 on success,
382+
/// -1 on failure, or -99 if pointers are invalid.
402383
#[no_mangle]
403384
pub extern "C" fn stm_signer_sign(
404385
me: StmSignerPtr,
@@ -411,10 +392,12 @@ mod signer {
411392
(me.as_ref(), out.as_mut(), msg.as_ref())
412393
{
413394
let msg_str = CStr::from_ptr(msg);
414-
if let Some(s) = ref_me.sign(msg_str.to_bytes(), index) {
395+
return if let Some(s) = ref_me.sign(msg_str.to_bytes(), index) {
415396
*ref_out = Box::into_raw(Box::new(s));
416-
}
417-
return 0;
397+
0
398+
} else {
399+
-1
400+
};
418401
}
419402
NULLPOINTERERR
420403
}

mithril-core/src/key_reg.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use crate::error::RegisterError;
44
use digest::{Digest, FixedOutput};
5-
use serde::{Deserialize, Serialize};
65
use std::collections::{HashMap, HashSet};
76
use std::sync::Arc;
87

@@ -24,11 +23,7 @@ pub struct KeyReg {
2423

2524
/// Structure generated out of a closed registration. One can only get a global `avk` out of
2625
/// a closed key registration.
27-
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
28-
#[serde(bound(
29-
serialize = "MerkleTree<D>: Serialize",
30-
deserialize = "MerkleTree<D>: Deserialize<'de>"
31-
))]
26+
#[derive(Clone, Debug, PartialEq, Eq)]
3227
pub struct ClosedKeyReg<D>
3328
where
3429
D: Digest + FixedOutput,

mithril-core/src/stm.rs

Lines changed: 11 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,7 @@ pub struct StmInitializer {
220220
/// Participant in the protocol can sign messages. This instance can only be generated out of
221221
/// an `StmInitializer` and a `ClosedKeyReg`. This ensures that a `MerkleTree` root is not
222222
/// computed before all participants have registered.
223-
#[derive(Debug, Clone, Serialize, Deserialize)]
224-
#[serde(bound(
225-
serialize = "ClosedKeyReg<D>: Serialize",
226-
deserialize = "ClosedKeyReg<D>: Deserialize<'de>"
227-
))]
223+
#[derive(Debug, Clone)]
228224
pub struct StmSigner<D>
229225
where
230226
D: Digest + FixedOutput,
@@ -241,11 +237,7 @@ where
241237
/// `StmClerk` can verify and aggregate `StmSig`s and verify `StmMultiSig`s. Clerks can only be
242238
/// generated with the registration closed. This avoids that a Merkle Tree is computed before
243239
/// all parties have registered.
244-
#[derive(Debug, Clone, Serialize, Deserialize)]
245-
#[serde(bound(
246-
serialize = "ClosedKeyReg<D>: Serialize",
247-
deserialize = "ClosedKeyReg<D>: Deserialize<'de>"
248-
))]
240+
#[derive(Debug, Clone)]
249241
pub struct StmClerk<D>
250242
where
251243
D: Clone + Digest + FixedOutput,
@@ -645,45 +637,22 @@ impl<D> StmSigner<D>
645637
where
646638
D: Clone + Digest + FixedOutput,
647639
{
648-
/////////////////////
649-
// Operation phase //
650-
/////////////////////
651-
/// Try to win the lottery for this message/index combo.
652-
pub fn eligibility_check(&self, msg: &[u8], index: Index) -> bool {
653-
// let msg' <- AVK || msg
654-
// sigma <- MSP.Sig(msk, msg')
655-
// ev <- MSP.Eval(msg', index, sigma)
656-
// return 1 if ev < phi(stake) else return 0
640+
/// If lottery is won for this message/index, signs it.
641+
pub fn sign(&self, msg: &[u8], index: Index) -> Option<StmSig<D>> {
657642
let msgp = self
658643
.closed_reg
659644
.merkle_tree
660645
.to_commitment()
661646
.concat_with_msg(msg);
662647
let sigma = self.sk.sign(&msgp);
663-
let ev = sigma.eval(&msgp, index);
664-
ev_lt_phi(
648+
649+
// Check if lottery is won
650+
if ev_lt_phi(
665651
self.params.phi_f,
666-
ev,
652+
sigma.eval(&msgp, index),
667653
self.stake,
668654
self.closed_reg.total_stake,
669-
)
670-
}
671-
672-
/// If lottery is won for this message/index, signs it.
673-
pub fn sign(&self, msg: &[u8], index: Index) -> Option<StmSig<D>> {
674-
if self.eligibility_check(msg, index) {
675-
// msg' <- AVK||msg
676-
// sigma <- MSP.Sig(msk,msg')
677-
// pi = (sigma, reg_i, i, p_i) where
678-
// p_i is the users path inside the merkle tree AVK
679-
// reg_i is (mvk_i, stake_i)
680-
// return pi
681-
let msgp = self
682-
.closed_reg
683-
.merkle_tree
684-
.to_commitment()
685-
.concat_with_msg(msg);
686-
let sigma = self.sk.sign(&msgp);
655+
) {
687656
let path = self
688657
.closed_reg
689658
.merkle_tree
@@ -847,64 +816,6 @@ where
847816
}
848817
}
849818

850-
/// Return a byte string
851-
///
852-
/// # Layout
853-
/// * party_id,
854-
/// * mt_index,
855-
/// * stake,
856-
/// * total_stake,
857-
/// * params,
858-
/// * sk,
859-
/// * pk,
860-
/// * avk,
861-
pub fn to_bytes(&self) -> Vec<u8> {
862-
let mut output = Vec::new();
863-
output.extend_from_slice(&self.party_id.to_be_bytes());
864-
output.extend_from_slice(&self.mt_index.to_be_bytes());
865-
output.extend_from_slice(&self.stake.to_be_bytes());
866-
output.extend_from_slice(&self.closed_reg.total_stake.to_be_bytes());
867-
output.extend_from_slice(&self.params.to_bytes());
868-
output.extend_from_slice(&self.sk.to_bytes());
869-
output.extend_from_slice(&self.vk.to_bytes());
870-
output.extend_from_slice(&self.closed_reg.merkle_tree.to_bytes());
871-
872-
output
873-
}
874-
875-
// /// Convert a byte string into an `StmSigner`
876-
// ///
877-
// /// # Panics
878-
// /// todo: we probably want this in all `from_bytes` fns
879-
// /// If the slice has not the expected size, the function fails.
880-
// pub fn from_bytes(bytes: &[u8]) -> Result<Self, MultiSignatureError> {
881-
// let mut u64_bytes = [0u8; 8];
882-
// u64_bytes.copy_from_slice(&bytes[..8]);
883-
// let party_id = u64::from_be_bytes(u64_bytes);
884-
// u64_bytes.copy_from_slice(&bytes[8..16]);
885-
// let mt_index = u64::from_be_bytes(u64_bytes);
886-
// u64_bytes.copy_from_slice(&bytes[16..24]);
887-
// let stake = u64::from_be_bytes(u64_bytes);
888-
// u64_bytes.copy_from_slice(&bytes[24..32]);
889-
// let total_stake = u64::from_be_bytes(u64_bytes);
890-
// let params = StmParameters::from_bytes(&bytes[32..])?;
891-
// let sk = SigningKey::from_bytes(&bytes[56..])?;
892-
// let pk = StmVerificationKey::from_bytes(&bytes[88..])?;
893-
//
894-
// let avk = Arc::new(MerkleTree::from_bytes(&bytes[280..])?);
895-
//
896-
// Ok(Self {
897-
// party_id,
898-
// mt_index,
899-
// stake,
900-
// params,
901-
// avk,
902-
// sk,
903-
// pk,
904-
// total_stake,
905-
// })
906-
// }
907-
908819
/// Compute the `StmAggrVerificationKey` related to the used registration
909820
pub fn compute_avk(&self) -> StmAggrVerificationKey<D> {
910821
StmAggrVerificationKey::from(&self.closed_reg)
@@ -1383,31 +1294,6 @@ mod tests {
13831294
assert!(bincode::deserialize::<StmInitializer>(&bytes).is_ok())
13841295
}
13851296

1386-
#[test]
1387-
fn test_signer_serialize_deserialize(msg in any::<[u8;16]>(), nparties in 1..12usize) {
1388-
let params = StmParameters { m: (nparties as u64), k: 1, phi_f: 1.0 };
1389-
let ps = setup_equal_parties(params, nparties);
1390-
for p in ps {
1391-
let bytes = bincode::serialize(&p).unwrap();
1392-
let signer = bincode::deserialize::<StmSigner<D>>(&bytes).unwrap();
1393-
assert!(signer.sign(&msg, 1).is_some())
1394-
}
1395-
}
1396-
1397-
#[test]
1398-
fn test_clerk_serialize_deserialize(msg in any::<[u8;16]>(), nparties in 1..12usize) {
1399-
let params = StmParameters { m: (nparties as u64), k: 1, phi_f: 1.0 };
1400-
let ps = setup_equal_parties(params, nparties);
1401-
for p in ps {
1402-
let sig = p.sign(&msg, 1).unwrap();
1403-
let clerk = StmClerk::from_signer(&p);
1404-
let bytes = bincode::serialize(&clerk).unwrap();
1405-
let clerk = bincode::deserialize::<StmClerk<D>>(&bytes).unwrap();
1406-
let avk = clerk.compute_avk();
1407-
assert!(sig.verify(&params, &avk, &msg).is_ok())
1408-
}
1409-
}
1410-
14111297
#[test]
14121298
fn test_sig_serialize_deserialize(msg in any::<[u8;16]>()) {
14131299
let nparties = 2;
@@ -1453,9 +1339,9 @@ mod tests {
14531339
}
14541340

14551341
/// Pick N between min and max, and then
1456-
/// generate a vector of N stakes summing to N*tstake,
1342+
/// generate a vector of N stakes summing to N * tstake,
14571343
/// plus a subset S of 0..N such that the sum of the stakes at indices
1458-
/// in S is astake*N
1344+
/// in S is astake * N
14591345
fn arb_parties_adversary_stake(
14601346
min: usize,
14611347
max: usize,

0 commit comments

Comments
 (0)