1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
use crate::traits::*;
use bincode::{deserialize, serialize};
use core::convert::TryFrom;
use crypto::hash::HashValue;
use crypto_derive::{SilentDebug, SilentDisplay};
use derive_deref::Deref;
use failure::prelude::*;
use pairing::{
bls12_381::{Fr, FrRepr},
PrimeField,
};
use rand::Rng;
use serde::{Deserialize, Serialize};
use threshold_crypto;
type ThresholdBLSPrivateKey =
threshold_crypto::serde_impl::SerdeSecret<threshold_crypto::SecretKey>;
#[derive(Serialize, Deserialize, Deref, SilentDisplay, SilentDebug)]
pub struct BLS12381PrivateKey(ThresholdBLSPrivateKey);
#[derive(Clone, Hash, Serialize, Deserialize, Deref, Debug, PartialEq, Eq)]
pub struct BLS12381PublicKey(threshold_crypto::PublicKey);
#[derive(Clone, Hash, Serialize, Deserialize, Deref, Debug, PartialEq, Eq)]
pub struct BLS12381Signature(threshold_crypto::Signature);
impl BLS12381PublicKey {
pub fn to_bytes(&self) -> [u8; threshold_crypto::PK_SIZE] {
self.0.to_bytes()
}
}
impl BLS12381Signature {
pub fn to_bytes(&self) -> [u8; threshold_crypto::SIG_SIZE] {
self.0.to_bytes()
}
}
impl BLS12381PrivateKey {
#[allow(dead_code)]
fn from_bytes_unchecked(
mut fr_repr: [u64; 4usize],
) -> std::result::Result<BLS12381PrivateKey, CryptoMaterialError> {
fr_repr[3] &= 0x1FFF_FFFF_FFFF_FFFF;
let mut fr = Fr::from_repr(FrRepr(fr_repr)).unwrap();
Ok(BLS12381PrivateKey(
threshold_crypto::serde_impl::SerdeSecret(threshold_crypto::SecretKey::from_mut(
&mut fr,
)),
))
}
}
impl PrivateKey for BLS12381PrivateKey {
type PublicKeyMaterial = BLS12381PublicKey;
}
impl SigningKey for BLS12381PrivateKey {
type VerifyingKeyMaterial = BLS12381PublicKey;
type SignatureMaterial = BLS12381Signature;
fn sign_message(&self, message: &HashValue) -> BLS12381Signature {
let secret_key: &ThresholdBLSPrivateKey = self;
let sig = secret_key.sign(message.as_ref());
BLS12381Signature(sig)
}
}
impl Uniform for BLS12381PrivateKey {
fn generate_for_testing<R>(rng: &mut R) -> Self
where
R: ::rand::SeedableRng + ::rand::RngCore + ::rand::CryptoRng,
{
let mut fr_repr: [u64; 4usize] = rng.gen();
fr_repr[3] &= 0x1FFF_FFFF_FFFF_FFFF;
let mut fr = Fr::from_repr(FrRepr(fr_repr)).unwrap();
BLS12381PrivateKey(threshold_crypto::serde_impl::SerdeSecret(
threshold_crypto::SecretKey::from_mut(&mut fr),
))
}
}
impl std::cmp::PartialEq<Self> for BLS12381PrivateKey {
fn eq(&self, other: &Self) -> bool {
serialize(self).unwrap() == serialize(other).unwrap()
}
}
impl std::cmp::Eq for BLS12381PrivateKey {}
impl TryFrom<&[u8]> for BLS12381PrivateKey {
type Error = CryptoMaterialError;
fn try_from(bytes: &[u8]) -> std::result::Result<BLS12381PrivateKey, CryptoMaterialError> {
let key_res = deserialize::<BLS12381PrivateKey>(bytes);
key_res.or(Err(CryptoMaterialError::DeserializationError))
}
}
impl ValidKey for BLS12381PrivateKey {
fn to_bytes(&self) -> Vec<u8> {
unimplemented!("ask ladi!")
}
}
impl Genesis for BLS12381PrivateKey {
fn genesis() -> Self {
let mut buf = [0u8; threshold_crypto::PK_SIZE];
buf[threshold_crypto::PK_SIZE - 1] = 1;
Self::try_from(buf.as_ref()).unwrap()
}
}
impl From<&BLS12381PrivateKey> for BLS12381PublicKey {
fn from(secret_key: &BLS12381PrivateKey) -> Self {
let secret: &ThresholdBLSPrivateKey = secret_key;
let public: threshold_crypto::PublicKey = secret.public_key();
BLS12381PublicKey(public)
}
}
impl PublicKey for BLS12381PublicKey {
type PrivateKeyMaterial = BLS12381PrivateKey;
fn length() -> usize {
threshold_crypto::PK_SIZE
}
}
impl VerifyingKey for BLS12381PublicKey {
type SigningKeyMaterial = BLS12381PrivateKey;
type SignatureMaterial = BLS12381Signature;
}
impl std::fmt::Display for BLS12381PublicKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", hex::encode(&self.to_bytes()[..]))
}
}
impl TryFrom<&[u8]> for BLS12381PublicKey {
type Error = CryptoMaterialError;
fn try_from(bytes: &[u8]) -> std::result::Result<BLS12381PublicKey, CryptoMaterialError> {
let key_res = deserialize::<BLS12381PublicKey>(bytes);
key_res.or(Err(CryptoMaterialError::DeserializationError))
}
}
impl ValidKey for BLS12381PublicKey {
fn to_bytes(&self) -> Vec<u8> {
self.0.to_bytes().to_vec()
}
}
impl Signature for BLS12381Signature {
type VerifyingKeyMaterial = BLS12381PublicKey;
type SigningKeyMaterial = BLS12381PrivateKey;
fn verify(&self, message: &HashValue, public_key: &BLS12381PublicKey) -> Result<()> {
self.verify_arbitrary_msg(message.as_ref(), public_key)
}
fn verify_arbitrary_msg(&self, message: &[u8], public_key: &BLS12381PublicKey) -> Result<()> {
if public_key.verify(self, message.as_ref()) {
Ok(())
} else {
bail!("The provided signature is not valid on this PublicKey and Message")
}
}
fn to_bytes(&self) -> Vec<u8> {
self.0.to_bytes().to_vec()
}
}
impl TryFrom<&[u8]> for BLS12381Signature {
type Error = CryptoMaterialError;
fn try_from(bytes: &[u8]) -> std::result::Result<BLS12381Signature, CryptoMaterialError> {
let l = bytes.len();
if l > threshold_crypto::SIG_SIZE {
return Err(CryptoMaterialError::WrongLengthError);
}
let mut tmp = [0u8; threshold_crypto::SIG_SIZE];
tmp[..l].copy_from_slice(&bytes[..l]);
let sig = threshold_crypto::Signature::from_bytes(&tmp)
.map_err(|_err| CryptoMaterialError::ValidationError)?;
Ok(BLS12381Signature(sig))
}
}