This repository has been archived by the owner on Mar 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 142
/
mod.rs
892 lines (787 loc) · 27 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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
use crate::errors::prelude::*;
use crate::pair::{GroupOrderElement, Pair, PointG1, PointG2};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
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.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct Generator {
point: PointG2,
bytes: Vec<u8>,
}
impl Generator {
/// Creates and returns random generator point that satisfies BLS algorithm requirements.
///
/// # Example
///
/// ```
/// use ursa::bls::Generator;
/// Generator::new().unwrap();
/// ```
pub fn new() -> UrsaCryptoResult<Generator> {
let point = PointG2::new()?;
Ok(Generator {
point,
bytes: point.to_bytes()?,
})
}
/// Returns BLS generator point bytes representation.
///
/// # Example
///
/// ```
/// use ursa::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 ursa::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]) -> UrsaCryptoResult<Generator> {
Ok(Generator {
point: PointG2::from_bytes(bytes)?,
bytes: bytes.to_vec(),
})
}
}
/// BLS sign key.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
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 ursa::bls::SignKey;
/// SignKey::new(None).unwrap();
/// ```
pub fn new(seed: Option<&[u8]>) -> UrsaCryptoResult<SignKey> {
let group_order_element = match seed {
Some(seed) => GroupOrderElement::new_from_seed(seed)?,
_ => GroupOrderElement::new()?,
};
Ok(SignKey {
group_order_element,
bytes: group_order_element.to_bytes()?,
})
}
/// Returns BLS sign key bytes representation.
///
/// # Example
///
/// ```
/// use ursa::bls::SignKey;
/// let sign_key = SignKey::new(None).unwrap();
/// let bytes = sign_key.as_bytes();
/// ```
pub fn as_bytes(&self) -> &[u8] {
self.bytes.as_slice()
}
/// Creates and returns BLS sign key from bytes representation.
///
/// # Example
///
/// ```
/// use ursa::bls::SignKey;
/// let sign_key = SignKey::new(None).unwrap();
/// let bytes = sign_key.as_bytes();
/// let sign_key = SignKey::from_bytes(bytes).unwrap();
/// ```
pub fn from_bytes(bytes: &[u8]) -> UrsaCryptoResult<SignKey> {
Ok(SignKey {
group_order_element: GroupOrderElement::from_bytes(bytes)?,
bytes: bytes.to_vec(),
})
}
}
/// BLS verification key.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct VerKey {
point: PointG2,
bytes: Vec<u8>,
}
impl VerKey {
/// Creates and returns BLS ver key that corresponds to sign key.
///
/// # Example
///
/// ```
/// use ursa::bls::{Generator, SignKey, 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) -> UrsaCryptoResult<VerKey> {
let point = gen.point.mul(&sign_key.group_order_element)?;
Ok(VerKey {
point,
bytes: point.to_bytes()?,
})
}
/// Returns BLS verification key to bytes representation.
///
/// # Example
///
/// ```
/// use ursa::bls::{Generator, SignKey, VerKey};
/// let gen = Generator::new().unwrap();
/// let sign_key = SignKey::new(None).unwrap();
/// let ver_key = VerKey::new(&gen, &sign_key).unwrap();
/// let bytes = ver_key.as_bytes();
/// ```
pub fn as_bytes(&self) -> &[u8] {
self.bytes.as_slice()
}
/// Creates and returns BLS verification key from bytes representation.
///
/// # Example
///
/// ```
/// use ursa::bls::{Generator, SignKey, VerKey};
/// let gen = Generator::new().unwrap();
/// let sign_key = SignKey::new(None).unwrap();
/// let ver_key = VerKey::new(&gen, &sign_key).unwrap();
/// let bytes = ver_key.as_bytes();
/// let ver_key2 = VerKey::from_bytes(bytes).unwrap();
/// ```
pub fn from_bytes(bytes: &[u8]) -> UrsaCryptoResult<VerKey> {
let point = PointG2::from_bytes(bytes)?;
Ok(VerKey {
point,
bytes: bytes.to_vec(),
})
}
}
/// Proof of possession for BLS verification key.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
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 ursa::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) -> UrsaCryptoResult<ProofOfPossession> {
let point = Bls::_gen_signature(&ver_key.bytes, sign_key, Keccak256::default())?;
Ok(ProofOfPossession {
point,
bytes: point.to_bytes()?,
})
}
/// Returns BLS proof of possession to bytes representation.
///
/// # Example
///
/// ```
/// use ursa::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();
/// let signature = ProofOfPossession::new(&ver_key, &sign_key).unwrap();
/// let bytes = signature.as_bytes();
/// ```
pub fn as_bytes(&self) -> &[u8] {
self.bytes.as_slice()
}
/// Creates and returns BLS proof of possession from bytes representation.
///
/// # Example
///
/// ```
/// use ursa::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();
/// let signature = ProofOfPossession::new(&ver_key, &sign_key).unwrap();
/// let bytes = signature.as_bytes();
/// let signature2 = ProofOfPossession::from_bytes(bytes).unwrap();
/// ```
pub fn from_bytes(bytes: &[u8]) -> UrsaCryptoResult<ProofOfPossession> {
let point = PointG1::from_bytes(bytes)?;
Ok(ProofOfPossession {
point,
bytes: bytes.to_vec(),
})
}
}
/// BLS signature.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct Signature {
point: PointG1,
bytes: Vec<u8>,
}
impl Signature {
/// Returns BLS signature to bytes representation.
///
/// # Example
///
/// ```
/// use ursa::bls::{SignKey, Signature, Bls};
/// let sign_key = SignKey::new(None).unwrap();
/// let message = vec![1, 2, 3, 4, 5];
/// let signature = Bls::sign(&message, &sign_key).unwrap();
/// let bytes = signature.as_bytes();
/// ```
pub fn as_bytes(&self) -> &[u8] {
self.bytes.as_slice()
}
/// Creates and returns BLS signature from bytes representation.
///
/// # Example
///
/// ```
/// use ursa::bls::{SignKey, Signature, Bls};
/// let sign_key = SignKey::new(None).unwrap();
/// let message = vec![1, 2, 3, 4, 5];
/// let signature = Bls::sign(&message, &sign_key).unwrap();
/// let bytes = signature.as_bytes();
/// let signature2 = Signature::from_bytes(bytes).unwrap();
/// ```
pub fn from_bytes(bytes: &[u8]) -> UrsaCryptoResult<Signature> {
let point = PointG1::from_bytes(bytes)?;
Ok(Signature {
point,
bytes: bytes.to_vec(),
})
}
}
/// BLS multi signature.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
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 ursa::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]) -> UrsaCryptoResult<MultiSignature> {
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
///
/// ```
/// use ursa::bls::{SignKey, Bls, MultiSignature};
/// 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];
/// let multi_signature = MultiSignature::new(&signatures).unwrap();
/// let bytes = multi_signature.as_bytes();
/// ```
pub fn as_bytes(&self) -> &[u8] {
self.bytes.as_slice()
}
/// Creates and returns BLS multi signature from bytes representation.
///
/// # Example
///
/// ```
/// use ursa::bls::{SignKey, Bls, MultiSignature};
/// 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];
/// let multi_signature = MultiSignature::new(&signatures).unwrap();
/// let bytes = multi_signature.as_bytes();
/// let multi_signature2 = MultiSignature::from_bytes(bytes).unwrap();
/// ```
pub fn from_bytes(bytes: &[u8]) -> UrsaCryptoResult<MultiSignature> {
let point = PointG1::from_bytes(bytes)?;
Ok(MultiSignature {
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 ursa::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) -> UrsaCryptoResult<Signature> {
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 ursa::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,
) -> UrsaCryptoResult<bool> {
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 ursa::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,
) -> UrsaCryptoResult<bool> {
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 ursa::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,
) -> UrsaCryptoResult<bool> {
// 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) -> UrsaCryptoResult<PointG1>
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,
) -> UrsaCryptoResult<bool>
where
T: Digest,
{
let h = Bls::_hash(message, hasher)?;
// e(&signature, &gen.point) == e(&h, &ver_key) => 1 == e(&h, &ver_key)*e(&signature, &gen.point)^-1 == e(&h, &ver_key)*e(&signature, -&gen.point)
Pair::pair2(signature, &gen.point.neg()?, &h, ver_key)?.is_unity()
}
fn _hash<T>(message: &[u8], mut hasher: T) -> UrsaCryptoResult<PointG1>
where
T: Digest,
{
hasher.update(message);
PointG1::from_hash(hasher.finalize().as_slice())
}
}
#[cfg(test)]
mod tests {
use super::*;
// Test Generator
#[test]
fn generator_new_works() {
Generator::new().unwrap();
}
// Test SignKey
#[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 sign_key_as_bytes_works() {
let sign_key = SignKey::new(None).unwrap();
let bytes = sign_key.as_bytes();
assert!(!bytes.is_empty());
}
#[test]
fn sign_key_from_bytes_works() {
let sign_key = SignKey::new(None).unwrap();
let bytes = sign_key.as_bytes();
SignKey::from_bytes(bytes).unwrap();
}
// Test VerKey
#[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 ver_key_as_bytes_works() {
let gen = Generator::new().unwrap();
let sign_key = SignKey::new(None).unwrap();
let ver_key = VerKey::new(&gen, &sign_key).unwrap();
let bytes = ver_key.as_bytes();
assert!(bytes.len() == ver_key.bytes.len());
}
#[test]
fn ver_key_from_bytes_works() {
let gen = Generator::new().unwrap();
let sign_key = SignKey::new(None).unwrap();
let ver_key = VerKey::new(&gen, &sign_key).unwrap();
let bytes = ver_key.as_bytes();
let ver_key2 = VerKey::from_bytes(bytes).unwrap();
assert!(ver_key2.point == ver_key.point);
}
// Test ProofOfPossession
#[test]
fn proof_of_possession_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 proof_of_possession_as_bytes_works() {
let gen = Generator::new().unwrap();
let sign_key = SignKey::new(None).unwrap();
let ver_key = VerKey::new(&gen, &sign_key).unwrap();
let signature = ProofOfPossession::new(&ver_key, &sign_key).unwrap();
let bytes = signature.as_bytes();
assert!(bytes.len() == signature.bytes.len());
}
#[test]
fn proof_of_possession_from_bytes_works() {
let gen = Generator::new().unwrap();
let sign_key = SignKey::new(None).unwrap();
let ver_key = VerKey::new(&gen, &sign_key).unwrap();
let signature = ProofOfPossession::new(&ver_key, &sign_key).unwrap();
let bytes = signature.as_bytes();
let signature2 = ProofOfPossession::from_bytes(bytes).unwrap();
assert!(signature2.point == signature.point);
}
// Test Bls
#[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 signature_as_bytes_works() {
let sign_key = SignKey::new(None).unwrap();
let message = vec![1, 2, 3, 4, 5];
let signature = Bls::sign(&message, &sign_key).unwrap();
let bytes = signature.as_bytes();
assert!(bytes.len() == signature.bytes.len());
}
#[test]
fn signature_from_bytes_works() {
let sign_key = SignKey::new(None).unwrap();
let message = vec![1, 2, 3, 4, 5];
let signature = Bls::sign(&message, &sign_key).unwrap();
let bytes = signature.as_bytes();
let signature2 = Signature::from_bytes(bytes).unwrap();
assert!(signature2.point == signature.point);
}
#[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 multi_signature_as_bytes_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];
let multi_signature = MultiSignature::new(&signatures).unwrap();
let bytes = multi_signature.as_bytes();
assert!(bytes.len() == multi_signature.bytes.len());
}
#[test]
fn multi_signature_from_bytes_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];
let multi_signature = MultiSignature::new(&signatures).unwrap();
let bytes = multi_signature.as_bytes();
let multi_signature2 = MultiSignature::from_bytes(bytes).unwrap();
assert!(multi_signature2.point == multi_signature.point);
}
#[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)
}
}