@@ -44,9 +44,7 @@ use crate::poly::{Commitment, Poly};
44
44
use crate :: secret:: clear_fr;
45
45
use crate :: util:: sha3_256;
46
46
47
- pub use blstrs:: {
48
- Bls12 as PEngine , G1Affine as G1 , G1Projective , G2Affine as G2 , G2Projective , Scalar as Fr ,
49
- } ;
47
+ pub use blstrs:: { Bls12 as PEngine , G1Affine , G1Projective , G2Affine , G2Projective , Scalar as Fr } ;
50
48
51
49
/// The size of a secret key's representation in bytes.
52
50
pub const SK_SIZE : usize = 32 ;
@@ -62,7 +60,7 @@ pub const DST: &[u8; 43] = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_";
62
60
63
61
/// A public key.
64
62
#[ derive( Deserialize , Serialize , Copy , Clone , PartialEq , Eq ) ]
65
- pub struct PublicKey ( #[ serde( with = "serde_impl::affine" ) ] G1 ) ;
63
+ pub struct PublicKey ( #[ serde( with = "serde_impl::affine" ) ] G1Affine ) ;
66
64
67
65
impl Hash for PublicKey {
68
66
fn hash < H : Hasher > ( & self , state : & mut H ) {
@@ -90,15 +88,15 @@ impl Ord for PublicKey {
90
88
}
91
89
92
90
/// Utility to convert blsttc to blst types
93
- impl From < PublicKey > for G1 {
91
+ impl From < PublicKey > for G1Affine {
94
92
fn from ( item : PublicKey ) -> Self {
95
93
item. 0
96
94
}
97
95
}
98
96
99
97
/// Utility to convert blsttc to blst types
100
- impl From < G1 > for PublicKey {
101
- fn from ( item : G1 ) -> Self {
98
+ impl From < G1Affine > for PublicKey {
99
+ fn from ( item : G1Affine ) -> Self {
102
100
PublicKey ( item)
103
101
}
104
102
}
@@ -118,8 +116,8 @@ impl From<G1Projective> for PublicKey {
118
116
}
119
117
120
118
/// Utility to compare between blsttc and blst types
121
- impl std:: cmp:: PartialEq < G1 > for PublicKey {
122
- fn eq ( & self , other : & G1 ) -> bool {
119
+ impl std:: cmp:: PartialEq < G1Affine > for PublicKey {
120
+ fn eq ( & self , other : & G1Affine ) -> bool {
123
121
& self . 0 == other
124
122
}
125
123
}
@@ -138,9 +136,10 @@ impl PublicKey {
138
136
}
139
137
140
138
/// Returns `true` if the signature matches the element of `G2`.
141
- pub fn verify_g2 < H : Into < G2 > > ( & self , sig : & Signature , hash : H ) -> bool {
139
+ pub fn verify_g2 < H : Into < G2Affine > > ( & self , sig : & Signature , hash : H ) -> bool {
142
140
!self . is_zero ( )
143
- && PEngine :: pairing ( & self . 0 , & hash. into ( ) ) == PEngine :: pairing ( & G1 :: generator ( ) , & sig. 0 )
141
+ && PEngine :: pairing ( & self . 0 , & hash. into ( ) )
142
+ == PEngine :: pairing ( & G1Affine :: generator ( ) , & sig. 0 )
144
143
}
145
144
146
145
/// Returns `true` if the signature matches the message.
@@ -159,7 +158,7 @@ impl PublicKey {
159
158
/// Encrypts the message.
160
159
pub fn encrypt_with_rng < R : RngCore , M : AsRef < [ u8 ] > > ( & self , rng : & mut R , msg : M ) -> Ciphertext {
161
160
let r: Fr = Fr :: random ( rng) ;
162
- let u = G1 :: generator ( ) . mul ( r) . to_affine ( ) ;
161
+ let u = G1Affine :: generator ( ) . mul ( r) . to_affine ( ) ;
163
162
let v: Vec < u8 > = {
164
163
let g = self . 0 . mul ( r) ;
165
164
xor_with_hash ( g. to_affine ( ) , msg. as_ref ( ) )
@@ -213,7 +212,7 @@ impl fmt::Debug for PublicKeyShare {
213
212
214
213
impl PublicKeyShare {
215
214
/// Returns `true` if the signature matches the element of `G2`.
216
- pub fn verify_g2 < H : Into < G2 > > ( & self , sig : & SignatureShare , hash : H ) -> bool {
215
+ pub fn verify_g2 < H : Into < G2Affine > > ( & self , sig : & SignatureShare , hash : H ) -> bool {
217
216
self . 0 . verify_g2 ( & sig. 0 , hash)
218
217
}
219
218
@@ -248,7 +247,7 @@ impl PublicKeyShare {
248
247
/// A signature.
249
248
// Note: Random signatures can be generated for testing.
250
249
#[ derive( Deserialize , Serialize , Clone , PartialEq , Eq ) ]
251
- pub struct Signature ( #[ serde( with = "serde_impl::affine" ) ] G2 ) ;
250
+ pub struct Signature ( #[ serde( with = "serde_impl::affine" ) ] G2Affine ) ;
252
251
253
252
impl PartialOrd for Signature {
254
253
fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
@@ -421,11 +420,11 @@ impl SecretKey {
421
420
422
421
/// Returns the matching public key.
423
422
pub fn public_key ( & self ) -> PublicKey {
424
- PublicKey ( ( G1 :: generator ( ) * self . 0 ) . to_affine ( ) )
423
+ PublicKey ( ( G1Affine :: generator ( ) * self . 0 ) . to_affine ( ) )
425
424
}
426
425
427
426
/// Signs the given element of `G2`.
428
- pub fn sign_g2 < H : Into < G2 > > ( & self , hash : H ) -> Signature {
427
+ pub fn sign_g2 < H : Into < G2Affine > > ( & self , hash : H ) -> Signature {
429
428
Signature ( hash. into ( ) . mul ( self . 0 ) . to_affine ( ) )
430
429
}
431
430
@@ -523,7 +522,7 @@ impl SecretKeyShare {
523
522
}
524
523
525
524
/// Signs the given element of `G2`.
526
- pub fn sign_g2 < H : Into < G2 > > ( & self , hash : H ) -> SignatureShare {
525
+ pub fn sign_g2 < H : Into < G2Affine > > ( & self , hash : H ) -> SignatureShare {
527
526
SignatureShare ( self . 0 . sign_g2 ( hash) )
528
527
}
529
528
@@ -571,9 +570,9 @@ impl SecretKeyShare {
571
570
/// An encrypted message.
572
571
#[ derive( Deserialize , Serialize , Debug , Clone , PartialEq , Eq ) ]
573
572
pub struct Ciphertext (
574
- #[ serde( with = "serde_impl::affine" ) ] G1 ,
573
+ #[ serde( with = "serde_impl::affine" ) ] G1Affine ,
575
574
Vec < u8 > ,
576
- #[ serde( with = "serde_impl::affine" ) ] G2 ,
575
+ #[ serde( with = "serde_impl::affine" ) ] G2Affine ,
577
576
) ;
578
577
579
578
impl Hash for Ciphertext {
@@ -605,7 +604,7 @@ impl Ciphertext {
605
604
pub fn verify ( & self ) -> bool {
606
605
let Ciphertext ( ref u, ref v, ref w) = * self ;
607
606
let hash = hash_g1_g2 ( * u, v) ;
608
- PEngine :: pairing ( & G1 :: generator ( ) , w) == PEngine :: pairing ( u, & hash)
607
+ PEngine :: pairing ( & G1Affine :: generator ( ) , w) == PEngine :: pairing ( u, & hash)
609
608
}
610
609
611
610
/// Returns byte representation of Ciphertext
@@ -640,7 +639,7 @@ impl Ciphertext {
640
639
641
640
/// A decryption share. A threshold of decryption shares can be used to decrypt a message.
642
641
#[ derive( Clone , Deserialize , Serialize , PartialEq , Eq ) ]
643
- pub struct DecryptionShare ( #[ serde( with = "serde_impl::affine" ) ] G1 ) ;
642
+ pub struct DecryptionShare ( #[ serde( with = "serde_impl::affine" ) ] G1Affine ) ;
644
643
645
644
impl Distribution < DecryptionShare > for Standard {
646
645
fn sample < R : Rng + ?Sized > ( & self , rng : & mut R ) -> DecryptionShare {
@@ -785,7 +784,7 @@ impl PublicKeySet {
785
784
/// Derives a child public key set for a given index.
786
785
pub fn derive_child ( & self , index : & [ u8 ] ) -> Self {
787
786
let index_fr = derivation_index_into_fr ( index) ;
788
- let child_coeffs: Vec < G1 > = self
787
+ let child_coeffs: Vec < G1Affine > = self
789
788
. commit
790
789
. coeff
791
790
. iter ( )
@@ -907,12 +906,12 @@ impl SecretKeySet {
907
906
}
908
907
909
908
/// Returns a hash of the given message in `G2`.
910
- pub fn hash_g2 < M : AsRef < [ u8 ] > > ( msg : M ) -> G2 {
909
+ pub fn hash_g2 < M : AsRef < [ u8 ] > > ( msg : M ) -> G2Affine {
911
910
G2Projective :: hash_to_curve ( msg. as_ref ( ) , DST , & [ ] ) . to_affine ( )
912
911
}
913
912
914
913
/// Returns a hash of the group element and message, in the second group.
915
- fn hash_g1_g2 < M : AsRef < [ u8 ] > > ( g1 : G1 , msg : M ) -> G2 {
914
+ fn hash_g1_g2 < M : AsRef < [ u8 ] > > ( g1 : G1Affine , msg : M ) -> G2Affine {
916
915
// If the message is large, hash it, otherwise copy it.
917
916
// TODO: Benchmark and optimize the threshold.
918
917
let mut msg = if msg. as_ref ( ) . len ( ) > 64 {
@@ -925,7 +924,7 @@ fn hash_g1_g2<M: AsRef<[u8]>>(g1: G1, msg: M) -> G2 {
925
924
}
926
925
927
926
/// Returns the bitwise xor of `bytes` with a sequence of pseudorandom bytes determined by `g1`.
928
- fn xor_with_hash ( g1 : G1 , bytes : & [ u8 ] ) -> Vec < u8 > {
927
+ fn xor_with_hash ( g1 : G1Affine , bytes : & [ u8 ] ) -> Vec < u8 > {
929
928
let digest = sha3_256 ( & g1. to_compressed ( ) ) ;
930
929
let rng = ChaChaRng :: from_seed ( digest) ;
931
930
let xor = |( a, b) : ( u8 , & u8 ) | a ^ b;
@@ -1905,19 +1904,19 @@ mod tests {
1905
1904
assert ! ( !pk. is_zero( ) ) ;
1906
1905
1907
1906
// Rogue 0 pubkey
1908
- let rogue_public_key = PublicKey ( G1 :: identity ( ) ) ;
1907
+ let rogue_public_key = PublicKey ( G1Affine :: identity ( ) ) ;
1909
1908
assert ! ( rogue_public_key. is_zero( ) ) ;
1910
1909
1911
1910
// Rogue 0 sig
1912
- let rogue_sig = Signature ( G2 :: identity ( ) ) ;
1911
+ let rogue_sig = Signature ( G2Affine :: identity ( ) ) ;
1913
1912
1914
1913
// just any hash will do
1915
1914
let hash = hash_g2 ( b"anything" ) ;
1916
1915
1917
1916
// check that the attack works without the 0 check
1918
1917
assert_eq ! (
1919
1918
PEngine :: pairing( & rogue_public_key. 0 , & hash) ,
1920
- PEngine :: pairing( & G1 :: generator( ) , & rogue_sig. 0 )
1919
+ PEngine :: pairing( & G1Affine :: generator( ) , & rogue_sig. 0 )
1921
1920
) ;
1922
1921
1923
1922
// check that verify_g2 is protected against this attack
0 commit comments