cggmp21_keygen/security_level.rs
1//! Security level of CGGMP DKG protocol
2//!
3//! Security level is defined as set of parameters in the CGGMP paper. Higher security level gives more
4//! security but makes protocol execution slower.
5//!
6//! We provide a predefined default [SecurityLevel128].
7//!
8//! You can define your own security level using macro [define_security_level]. Be sure that you properly
9//! analyzed the CGGMP paper and you understand implications. Inconsistent security level may cause unexpected
10//! unverbose runtime error or reduced security of the protocol.
11
12/// Security level of the DKG protocol
13///
14/// You should not implement this trait manually. Use [define_security_level] macro instead.
15pub trait SecurityLevel: Clone + Sync + Send + 'static {
16 /// $\kappa$ bits of security
17 const SECURITY_BITS: u32;
18 /// $\kappa/8$ bytes of security
19 const SECURITY_BYTES: usize;
20
21 /// Static array of $\kappa/8$ bytes
22 type Rid: AsRef<[u8]>
23 + AsMut<[u8]>
24 + Default
25 + Clone
26 + hex::FromHex<Error = hex::FromHexError>
27 + Send
28 + Sync
29 + Unpin
30 + 'static;
31}
32
33/// Internal module that's powers `define_security_level` macro
34#[doc(hidden)]
35pub mod _internal {
36 use hex::FromHex;
37
38 #[derive(Clone)]
39 pub struct Rid<const N: usize>([u8; N]);
40
41 impl<const N: usize> AsRef<[u8]> for Rid<N> {
42 fn as_ref(&self) -> &[u8] {
43 &self.0
44 }
45 }
46
47 impl<const N: usize> AsMut<[u8]> for Rid<N> {
48 fn as_mut(&mut self) -> &mut [u8] {
49 &mut self.0
50 }
51 }
52
53 impl<const N: usize> Default for Rid<N> {
54 fn default() -> Self {
55 Self([0u8; N])
56 }
57 }
58
59 impl<const N: usize> FromHex for Rid<N>
60 where
61 [u8; N]: FromHex,
62 {
63 type Error = <[u8; N] as FromHex>::Error;
64 fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
65 FromHex::from_hex(hex).map(Self)
66 }
67 }
68}
69
70/// Defines security level of CGGMP21 DKG protocol
71///
72/// ## Example
73///
74/// This code defines security level corresponding to $\kappa=1024$ (note: choice of parameters is random,
75/// it does not correspond to meaningful security level):
76/// ```rust
77/// use cggmp21_keygen::security_level::define_security_level;
78///
79/// #[derive(Clone)]
80/// pub struct MyLevel;
81/// define_security_level!(MyLevel{
82/// security_bits = 1024,
83/// });
84/// ```
85#[macro_export]
86macro_rules! define_security_level {
87 ($struct_name:ident {
88 security_bits = $k:expr$(,)?
89 }) => {
90 impl $crate::security_level::SecurityLevel for $struct_name {
91 const SECURITY_BITS: u32 = $k;
92 const SECURITY_BYTES: usize = $k / 8;
93 type Rid = $crate::security_level::_internal::Rid<{ $k / 8 }>;
94 }
95 };
96}
97
98#[doc(inline)]
99pub use define_security_level;
100
101/// 128-bits security level
102///
103/// This security level is intended to provide 128 bits of security for the protocol when run with up to 128 participants.
104#[derive(Clone)]
105pub struct SecurityLevel128;
106define_security_level!(SecurityLevel128{
107 security_bits = 384,
108});