This repository has been archived by the owner on Jun 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
mod.rs
701 lines (613 loc) · 20 KB
/
mod.rs
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
use errors::IndyCryptoError;
use pair::{GroupOrderElement, PointG2, PointG1, Pair};
use sha2::{Sha256, Digest};
use sha3::Keccak256;
/// BLS generator point.
/// BLS algorithm requires choosing of generator point that must be known to all parties.
/// The most of BLS methods require generator to be provided.
#[derive(Debug, Serialize, Deserialize)]
pub struct Generator {
point: PointG2,
bytes: Vec<u8>
}
impl Generator {
/// Creates and returns random generator point that satisfies BLS algorithm requirements.
///
/// # Example
///
/// ```
/// use indy_crypto::bls::Generator;
/// Generator::new().unwrap();
/// ```
pub fn new() -> Result<Generator, IndyCryptoError> {
let point = PointG2::new()?;
Ok(Generator {
point: point,
bytes: point.to_bytes()?
})
}
/// Returns BLS generator point bytes representation.
///
/// # Example
///
/// ```
/// use indy_crypto::bls::*;
/// let gen = Generator::new().unwrap();
/// let gen_bytes = gen.as_bytes();
/// assert!(gen_bytes.len() > 0);
/// ```
pub fn as_bytes(&self) -> &[u8] {
self.bytes.as_slice()
}
/// Creates and returns generator point from bytes representation.
///
/// # Example
///
/// ```
/// use indy_crypto::bls::Generator;
/// let gen = Generator::new().unwrap();
/// let gen_bytes = gen.as_bytes();
/// Generator::from_bytes(gen_bytes).unwrap();
/// ```
pub fn from_bytes(bytes: &[u8]) -> Result<Generator, IndyCryptoError> {
Ok(
Generator {
point: PointG2::from_bytes(bytes)?,
bytes: bytes.to_vec()
}
)
}
}
/// BLS sign key.
#[derive(Debug, Serialize, Deserialize)]
pub struct SignKey {
group_order_element: GroupOrderElement,
bytes: Vec<u8>
}
impl SignKey {
/// Creates and returns random (or seeded from seed) BLS sign key algorithm requirements.
///
/// # Example
///
/// ```
/// use indy_crypto::bls::SignKey;
/// SignKey::new(None).unwrap();
/// ```
pub fn new(seed: Option<&[u8]>) -> Result<SignKey, IndyCryptoError> {
let group_order_element = match seed {
Some(seed) => GroupOrderElement::new_from_seed(seed)?,
_ => GroupOrderElement::new()?
};
Ok(SignKey {
group_order_element: group_order_element,
bytes: group_order_element.to_bytes()?
})
}
/// Returns BLS sign key bytes representation.
///
/// # Example
///
/// ```
/// //TODO: Provide an example!
/// ```
pub fn as_bytes(&self) -> &[u8] {
self.bytes.as_slice()
}
/// Creates and returns BLS sign key from bytes representation.
///
/// # Example
///
/// ```
/// //TODO: Provide an example!
/// ```
pub fn from_bytes(bytes: &[u8]) -> Result<SignKey, IndyCryptoError> {
Ok(
SignKey {
group_order_element: GroupOrderElement::from_bytes(bytes)?,
bytes: bytes.to_vec()
}
)
}
}
/// BLS verification key.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VerKey {
point: PointG2,
bytes: Vec<u8>
}
impl VerKey {
/// Creates and returns BLS ver key that corresponds to sign key.
///
/// # Example
///
/// ```
/// use indy_crypto::bls::Generator;
/// use indy_crypto::bls::SignKey;
/// use indy_crypto::bls::VerKey;
/// let gen = Generator::new().unwrap();
/// let sign_key = SignKey::new(None).unwrap();
/// VerKey::new(&gen, &sign_key).unwrap();
/// ```
pub fn new(gen: &Generator, sign_key: &SignKey) -> Result<VerKey, IndyCryptoError> {
let point = gen.point.mul(&sign_key.group_order_element)?;
Ok(VerKey {
point: point,
bytes: point.to_bytes()?
})
}
/// Returns BLS verification key to bytes representation.
///
/// # Example
///
/// ```
/// //TODO: Provide an example!
/// ```
pub fn as_bytes(&self) -> &[u8] {
self.bytes.as_slice()
}
/// Creates and returns BLS verification key from bytes representation.
///
/// # Example
///
/// ```
/// //TODO: Provide an example!
/// ```
pub fn from_bytes(bytes: &[u8]) -> Result<VerKey, IndyCryptoError> {
let point = PointG2::from_bytes(bytes)?;
Ok(
VerKey {
point,
bytes: bytes.to_vec()
}
)
}
}
/// Proof of possession for BLS verification key.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProofOfPossession {
point: PointG1,
bytes: Vec<u8>
}
impl ProofOfPossession {
/// Creates and returns BLS proof of possession that corresponds to ver key.
///
/// # Arguments
///
/// * `ver_key` - Ver key
/// * `sign_key` - Sign key
///
/// # Example
///
/// ```
/// use indy_crypto::bls::{Generator, SignKey, VerKey, ProofOfPossession};
/// let gen = Generator::new().unwrap();
/// let sign_key = SignKey::new(None).unwrap();
/// let ver_key = VerKey::new(&gen, &sign_key).unwrap();
/// ProofOfPossession::new(&ver_key, &sign_key).unwrap();
/// ```
pub fn new(ver_key: &VerKey, sign_key: &SignKey) -> Result<ProofOfPossession, IndyCryptoError> {
let point = Bls::_gen_signature(&ver_key.bytes, sign_key, Keccak256::default())?;
Ok(ProofOfPossession {
point: point,
bytes: point.to_bytes()?
})
}
/// Returns BLS proof of possession to bytes representation.
///
/// # Example
///
/// ```
/// //TODO: Provide an example!
/// ```
pub fn as_bytes(&self) -> &[u8] {
self.bytes.as_slice()
}
/// Creates and returns BLS proof of possession from bytes representation.
///
/// # Example
///
/// ```
/// //TODO: Provide an example!
/// ```
pub fn from_bytes(bytes: &[u8]) -> Result<ProofOfPossession, IndyCryptoError> {
let point = PointG1::from_bytes(bytes)?;
Ok(ProofOfPossession {
point,
bytes: bytes.to_vec()
})
}
}
/// BLS signature.
#[derive(Debug, Serialize, Deserialize)]
pub struct Signature {
point: PointG1,
bytes: Vec<u8>,
}
impl Signature {
/// Returns BLS signature to bytes representation.
///
/// # Example
///
/// ```
/// //TODO: Provide an example!
/// ```
pub fn as_bytes(&self) -> &[u8] {
self.bytes.as_slice()
}
/// Creates and returns BLS signature from bytes representation.
///
/// # Example
///
/// ```
/// //TODO: Provide an example!
/// ```
pub fn from_bytes(bytes: &[u8]) -> Result<Signature, IndyCryptoError> {
let point = PointG1::from_bytes(bytes)?;
Ok(
Signature {
point,
bytes: bytes.to_vec()
}
)
}
}
/// BLS multi signature.
#[derive(Debug, Serialize, Deserialize)]
pub struct MultiSignature {
point: PointG1,
bytes: Vec<u8>,
}
impl MultiSignature {
/// Creates and returns multi signature for provided list of signatures.
///
/// # Arguments
///
/// * `signatures` - List of signatures
///
/// # Example
///
/// ```
/// use indy_crypto::bls::*;
/// let sign_key1 = SignKey::new(None).unwrap();
/// let sign_key2 = SignKey::new(None).unwrap();
///
/// let message = vec![1, 2, 3, 4, 5];
///
/// let signature1 = Bls::sign(&message, &sign_key1).unwrap();
/// let signature2 = Bls::sign(&message, &sign_key2).unwrap();
///
/// let signatures = vec![
/// &signature1,
/// &signature2
/// ];
///
/// MultiSignature::new(&signatures).unwrap();
/// ```
pub fn new(signatures: &[&Signature]) -> Result<MultiSignature, IndyCryptoError> {
let mut point = PointG1::new_inf()?;
for signature in signatures {
point = point.add(&signature.point)?;
}
Ok(MultiSignature {
point,
bytes: point.to_bytes()?
})
}
/// Returns BLS multi signature bytes representation.
///
/// # Example
///
/// ```
/// //TODO: Provide an example!
/// ```
pub fn as_bytes(&self) -> &[u8] {
self.bytes.as_slice()
}
/// Creates and returns BLS multi signature from bytes representation.
///
/// # Example
///
/// ```
/// //TODO: Provide an example!
/// ```
pub fn from_bytes(bytes: &[u8]) -> Result<MultiSignature, IndyCryptoError> {
let point = PointG1::from_bytes(bytes)?;
Ok(
MultiSignature {
point: point,
bytes: bytes.to_vec()
}
)
}
}
pub struct Bls {}
impl Bls {
/// Signs the message and returns signature.
///
/// # Arguments
///
/// * `message` - Message to sign
/// * `sign_key` - Sign key
///
/// # Example
///
/// ```
/// use indy_crypto::bls::*;
/// let message = vec![1, 2, 3, 4, 5];
/// let sign_key = SignKey::new(None).unwrap();
/// Bls::sign(&message, &sign_key).unwrap();
/// ```
pub fn sign(message: &[u8], sign_key: &SignKey) -> Result<Signature, IndyCryptoError> {
let point = Bls::_gen_signature(message, sign_key, Sha256::default())?;
Ok(Signature {
point,
bytes: point.to_bytes()?
})
}
/// Verifies the message signature and returns true - if signature valid or false otherwise.
///
/// # Arguments
///
/// * `signature` - Signature to verify
/// * `message` - Message to verify
/// * `ver_key` - Verification key
/// * `gen` - Generator point
///
/// # Example
///
/// ```
/// use indy_crypto::bls::*;
/// let gen = Generator::new().unwrap();
/// let sign_key = SignKey::new(None).unwrap();
/// let ver_key = VerKey::new(&gen, &sign_key).unwrap();
/// let message = vec![1, 2, 3, 4, 5];
/// let signature = Bls::sign(&message, &sign_key).unwrap();
///
/// let valid = Bls::verify(&signature, &message, &ver_key, &gen).unwrap();
/// assert!(valid);
/// ```
pub fn verify(signature: &Signature, message: &[u8], ver_key: &VerKey, gen: &Generator) -> Result<bool, IndyCryptoError> {
Bls::_verify_signature(&signature.point, message, &ver_key.point, gen, Sha256::default())
}
/// Verifies the proof of possession and returns true - if valid or false otherwise.
///
/// # Arguments
///
/// * `pop` - Proof of possession
/// * `ver_key` - Verification key
/// * `gen` - Generator point
///
/// # Example
///
/// ```
/// use indy_crypto::bls::*;
/// let gen = Generator::new().unwrap();
/// let sign_key = SignKey::new(None).unwrap();
/// let ver_key = VerKey::new(&gen, &sign_key).unwrap();
/// let pop = ProofOfPossession::new(&ver_key, &sign_key).unwrap();
///
/// let valid = Bls::verify_proof_of_posession(&pop, &ver_key, &gen).unwrap();
/// assert!(valid);
/// ```
pub fn verify_proof_of_posession(pop: &ProofOfPossession, ver_key: &VerKey, gen: &Generator) -> Result<bool, IndyCryptoError> {
Bls::_verify_signature(&pop.point, &ver_key.bytes, &ver_key.point, gen, Keccak256::default())
}
/// Verifies the message multi signature and returns true - if signature valid or false otherwise.
///
/// # Arguments
///
/// * `multi_sig` - Multi signature to verify
/// * `message` - Message to verify
/// * `ver_keys` - List of verification keys
/// * `gen` - Generator point
///
/// # Example
///
/// ```
/// use indy_crypto::bls::*;
/// let gen = Generator::new().unwrap();
///
/// let sign_key1 = SignKey::new(None).unwrap();
/// let ver_key1 = VerKey::new(&gen, &sign_key1).unwrap();
/// let sign_key2 = SignKey::new(None).unwrap();
/// let ver_key2 = VerKey::new(&gen, &sign_key2).unwrap();
///
/// let message = vec![1, 2, 3, 4, 5];
///
/// let signature1 = Bls::sign(&message, &sign_key1).unwrap();
/// let signature2 = Bls::sign(&message, &sign_key2).unwrap();
///
/// let signatures = vec![
/// &signature1,
/// &signature2
/// ];
///
/// let multi_sig = MultiSignature::new(&signatures).unwrap();
///
/// let ver_keys = vec![
/// &ver_key1, &ver_key2
/// ];
///
/// let valid = Bls::verify_multi_sig(&multi_sig, &message, &ver_keys, &gen).unwrap();
/// assert!(valid)
/// ```
pub fn verify_multi_sig(multi_sig: &MultiSignature, message: &[u8], ver_keys: &[&VerKey], gen: &Generator) -> Result<bool, IndyCryptoError> {
// Since each signer (identified by a Verkey) has signed the same message, the public keys
// can be added together to form the aggregated verkey
let mut aggregated_verkey = PointG2::new_inf()?;
for ver_key in ver_keys {
aggregated_verkey = aggregated_verkey.add(&ver_key.point)?;
}
// TODO: Add a new method that takes a message and an aggregated verkey and expose using
// the C API. Verifiers can thus cache the aggregated verkey and avoid several EC point additions.
// The code below should be moved to such method.
Bls::_verify_signature(&multi_sig.point, message, &aggregated_verkey, gen, Sha256::default())
}
fn _gen_signature<T>(message: &[u8], sign_key: &SignKey, hasher: T) -> Result<PointG1, IndyCryptoError> where T: Digest {
Bls::_hash(message, hasher)?.mul(&sign_key.group_order_element)
}
pub fn _verify_signature<T>(signature: &PointG1, message: &[u8], ver_key: &PointG2, gen: &Generator, hasher: T) -> Result<bool, IndyCryptoError> where T: Digest {
let h = Bls::_hash(message, hasher)?;
Ok(Pair::pair(&signature, &gen.point)?.eq(&Pair::pair(&h, &ver_key)?))
}
fn _hash<T>(message: &[u8], mut hasher: T) -> Result<PointG1, IndyCryptoError> where T: Digest {
hasher.input(message);
Ok(PointG1::from_hash(hasher.result().as_slice())?)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn generator_new_works() {
Generator::new().unwrap();
}
#[test]
fn sign_key_new_works() {
SignKey::new(None).unwrap();
}
#[test]
fn sign_key_new_works_for_seed() {
let seed = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 21, 2, 3, 4, 5, 6, 7, 8, 9, 10, 31, 32];
SignKey::new(Some(&seed)).unwrap();
}
#[test]
fn ver_key_new_works() {
let gen = Generator::new().unwrap();
let sign_key = SignKey::new(None).unwrap();
VerKey::new(&gen, &sign_key).unwrap();
}
#[test]
fn pop_new_works() {
let gen = Generator::new().unwrap();
let sign_key = SignKey::new(None).unwrap();
let ver_key = VerKey::new(&gen, &sign_key).unwrap();
ProofOfPossession::new(&ver_key, &sign_key).unwrap();
}
#[test]
fn bls_sign_works() {
let sign_key = SignKey::new(None).unwrap();
let message = vec![1, 2, 3, 4, 5];
Bls::sign(&message, &sign_key).unwrap();
}
#[test]
fn multi_signature_new_works() {
let message = vec![1, 2, 3, 4, 5];
let sign_key1 = SignKey::new(None).unwrap();
let sign_key2 = SignKey::new(None).unwrap();
let signature1 = Bls::sign(&message, &sign_key1).unwrap();
let signature2 = Bls::sign(&message, &sign_key2).unwrap();
let signatures = vec![
&signature1,
&signature2
];
MultiSignature::new(&signatures).unwrap();
}
#[test]
fn verify_works() {
let message = vec![1, 2, 3, 4, 5];
let gen = Generator::new().unwrap();
let sign_key = SignKey::new(None).unwrap();
let ver_key = VerKey::new(&gen, &sign_key).unwrap();
let signature = Bls::sign(&message, &sign_key).unwrap();
let valid = Bls::verify(&signature, &message, &ver_key, &gen).unwrap();
assert!(valid)
}
#[test]
fn verify_pop_works() {
let gen = Generator::new().unwrap();
let sign_key = SignKey::new(None).unwrap();
let ver_key = VerKey::new(&gen, &sign_key).unwrap();
let pop = ProofOfPossession::new(&ver_key, &sign_key).unwrap();
let valid = Bls::verify_proof_of_posession(&pop, &ver_key, &gen).unwrap();
assert!(valid)
}
#[test]
fn verify_works_for_invalid_message() {
let message = vec![1, 2, 3, 4, 5];
let message_invalid = vec![1, 2, 3, 4, 5, 6];
let gen = Generator::new().unwrap();
let sign_key = SignKey::new(None).unwrap();
let ver_key = VerKey::new(&gen, &sign_key).unwrap();
let signature = Bls::sign(&message, &sign_key).unwrap();
let valid = Bls::verify(&signature, &message_invalid, &ver_key, &gen).unwrap();
assert!(!valid)
}
#[test]
fn verify_works_for_invalid_signature() {
let message = vec![1, 2, 3, 4, 5];
let gen = Generator::new().unwrap();
let sign_key = SignKey::new(None).unwrap();
let ver_key = VerKey::new(&gen, &SignKey::new(None).unwrap()).unwrap();
let signature_invalid = Bls::sign(&message, &sign_key).unwrap();
let valid = Bls::verify(&signature_invalid, &message, &ver_key, &gen).unwrap();
assert!(!valid)
}
#[test]
fn verify_multi_sig_works() {
let message = vec![1, 2, 3, 4, 5];
let gen = Generator::new().unwrap();
let sign_key1 = SignKey::new(None).unwrap();
let ver_key1 = VerKey::new(&gen, &sign_key1).unwrap();
let sign_key2 = SignKey::new(None).unwrap();
let ver_key2 = VerKey::new(&gen, &sign_key2).unwrap();
let ver_keys = vec![
&ver_key1,
&ver_key2
];
let signature1 = Bls::sign(&message, &sign_key1).unwrap();
let signature2 = Bls::sign(&message, &sign_key2).unwrap();
let signatures = vec![
&signature1,
&signature2
];
let multi_signature = MultiSignature::new(&signatures).unwrap();
let valid = Bls::verify_multi_sig(&multi_signature, &message, &ver_keys, &gen).unwrap();
assert!(valid)
}
#[test]
fn verify_multi_sig_works_for_invalid_message() {
let message = vec![1, 2, 3, 4, 5];
let message_invalid = vec![1, 2, 3, 4, 5, 6];
let gen = Generator::new().unwrap();
let sign_key1 = SignKey::new(None).unwrap();
let ver_key1 = VerKey::new(&gen, &sign_key1).unwrap();
let sign_key2 = SignKey::new(None).unwrap();
let ver_key2 = VerKey::new(&gen, &sign_key2).unwrap();
let ver_keys = vec![
&ver_key1,
&ver_key2
];
let signature1 = Bls::sign(&message, &sign_key1).unwrap();
let signature2 = Bls::sign(&message, &sign_key2).unwrap();
let signatures = vec![
&signature1,
&signature2
];
let multi_signature = MultiSignature::new(&signatures).unwrap();
let valid = Bls::verify_multi_sig(&multi_signature, &message_invalid, &ver_keys, &gen).unwrap();
assert!(!valid)
}
#[test]
fn verify_multi_sig_works_for_invalid_signature() {
let message = vec![1, 2, 3, 4, 5];
let gen = Generator::new().unwrap();
let sign_key1 = SignKey::new(None).unwrap();
let ver_key1 = VerKey::new(&gen, &sign_key1).unwrap();
let sign_key2 = SignKey::new(None).unwrap();
let ver_key2 = VerKey::new(&gen, &SignKey::new(None).unwrap()).unwrap();
let ver_keys = vec![
&ver_key1,
&ver_key2
];
let signature1 = Bls::sign(&message, &sign_key1).unwrap();
let signature2 = Bls::sign(&message, &sign_key2).unwrap();
let signatures = vec![
&signature1,
&signature2
];
let multi_signature_invalid = MultiSignature::new(&signatures).unwrap();
let valid = Bls::verify_multi_sig(&multi_signature_invalid, &message, &ver_keys, &gen).unwrap();
assert!(!valid)
}
}