@@ -26,27 +26,103 @@ use pairing_plus::{bls12_381::{Fr, G1, G2, Bls12}, serdes::SerDes, hash_to_field
26
26
use rand:: { thread_rng, RngCore } ;
27
27
use std:: collections:: { BTreeMap , BTreeSet } ;
28
28
29
+ // This shows how the generators are created with nothing up my sleeve values
30
+ // const PREHASH: &'static [u8] = b"To be, or not to be- that is the question:
31
+ // Whether 'tis nobler in the mind to suffer
32
+ // The slings and arrows of outrageous fortune
33
+ // Or to take arms against a sea of troubles,
34
+ // And by opposing end them. To die- to sleep-
35
+ // No more; and by a sleep to say we end
36
+ // The heartache, and the thousand natural shocks
37
+ // That flesh is heir to. 'Tis a consummation
38
+ // Devoutly to be wish'd. To die- to sleep.
39
+ // To sleep- perchance to dream: ay, there's the rub!
40
+ // For in that sleep of death what dreams may come
41
+ // When we have shuffled off this mortal coil,
42
+ // Must give us pause. There's the respect
43
+ // That makes calamity of so long life.
44
+ // For who would bear the whips and scorns of time,
45
+ // Th' oppressor's wrong, the proud man's contumely,
46
+ // The pangs of despis'd love, the law's delay,
47
+ // The insolence of office, and the spurns
48
+ // That patient merit of th' unworthy takes,
49
+ // When he himself might his quietus make
50
+ // With a bare bodkin? Who would these fardels bear,
51
+ // To grunt and sweat under a weary life,
52
+ // But that the dread of something after death-
53
+ // The undiscover'd country, from whose bourn
54
+ // No traveller returns- puzzles the will,
55
+ // And makes us rather bear those ills we have
56
+ // Than fly to others that we know not of?
57
+ // Thus conscience does make cowards of us all,
58
+ // And thus the native hue of resolution
59
+ // Is sicklied o'er with the pale cast of thought,
60
+ // And enterprises of great pith and moment
61
+ // With this regard their currents turn awry
62
+ // And lose the name of action.- Soft you now!
63
+ // The fair Ophelia!- Nymph, in thy orisons
64
+ // Be all my sins rememb'red.";
65
+ // const DST_G1: &'static [u8] = b"BLS12381G1_XMD:BLAKE2B_SSWU_RO_BLS_SIGNATURES:1_0_0";
66
+ // const DST_G2: &'static [u8] = b"BLS12381G2_XMD:BLAKE2B_SSWU_RO_BLS_SIGNATURES:1_0_0";
67
+ //
68
+ // fn main() {
69
+ // let g1 = <G1 as HashToCurve<ExpandMsgXmd<blake2::Blake2b>>>::hash_to_curve(PREHASH, DST_G1);
70
+ // let g2 = <G2 as HashToCurve<ExpandMsgXmd<blake2::Blake2b>>>::hash_to_curve(PREHASH, DST_G2);
71
+ //
72
+ // let mut g1_bytes = Vec::new();
73
+ // let mut g2_bytes = Vec::new();
74
+ //
75
+ // g1.serialize(&mut g1_bytes, true).unwrap();
76
+ // g2.serialize(&mut g2_bytes, true).unwrap();
77
+ //
78
+ // println!("g1 = {}", hex::encode(g1_bytes.as_slice()));
79
+ // println!("g2 = {}", hex::encode(g2_bytes.as_slice()));
80
+ // }
81
+ // g1 = b9c9058e8a44b87014f98be4e1818db718f8b2d5101fc89e6983625f321f14b84d7cf6e155004987a215ee426df173c9
82
+ // g2 = a963de2adfb1163cf4bed24d708ce47432742d2080b2573ebe2e19a8698f60c541cec000fcb19783e9be73341356df5f1191cddec7c476d7742bcc421afc5d505e63373c627ea01fda04f0e40159d25bdd12f45a010d8580a78f6a7d262272f3
83
+
84
+ const BLINDING_G1 : & ' static [ u8 ] = & [ 185 , 201 , 5 , 142 , 138 , 68 , 184 , 112 , 20 , 249 , 139 , 228 , 225 , 129 , 141 , 183 , 24 , 248 , 178 , 213 , 16 , 31 , 200 , 158 , 105 , 131 , 98 , 95 , 50 , 31 , 20 , 184 , 77 , 124 , 246 , 225 , 85 , 0 , 73 , 135 , 162 , 21 , 238 , 66 , 109 , 241 , 115 , 201 ] ;
85
+ const BLINDING_G2 : & ' static [ u8 ] = & [ 169 , 99 , 222 , 42 , 223 , 177 , 22 , 60 , 244 , 190 , 210 , 77 , 112 , 140 , 228 , 116 , 50 , 116 , 45 , 32 , 128 , 178 , 87 , 62 , 190 , 46 , 25 , 168 , 105 , 143 , 96 , 197 , 65 , 206 , 192 , 0 , 252 , 177 , 151 , 131 , 233 , 190 , 115 , 52 , 19 , 86 , 223 , 95 , 17 , 145 , 205 , 222 , 199 , 196 , 118 , 215 , 116 , 43 , 204 , 66 , 26 , 252 , 93 , 80 , 94 , 99 , 55 , 60 , 98 , 126 , 160 , 31 , 218 , 4 , 240 , 228 , 1 , 89 , 210 , 91 , 221 , 18 , 244 , 90 , 1 , 13 , 133 , 128 , 167 , 143 , 106 , 125 , 38 , 34 , 114 , 243 ] ;
86
+
87
+ /// Generate a blinded BLS key pair where secret key `x` and blinding factor `r` in Fp
88
+ /// and public key `w` = `g2` ^ `x` * `blinding_g2` ^ `r`
89
+ /// `seed`: `ArrayBuffer` [opt]
90
+ /// `return` Object { publicKey: `ArrayBuffer`, secretKey: `ArrayBuffer`, blindingFactor: `ArrayBuffer` }
91
+ fn bls_generate_blinded_g2_key ( cx : FunctionContext ) -> JsResult < JsObject > {
92
+ bls_generate_keypair :: < G2 > ( cx, Some ( BLINDING_G2 ) )
93
+ }
94
+
95
+ /// Generate a blinded BLS key pair where secret key `x` and blinding factor `r` in Fp
96
+ /// and public key `w` = `g1` ^ `x` * `blinding_g1` ^ `r`
97
+ /// `seed`: `ArrayBuffer` [opt]
98
+ /// `return` Object { publicKey: `ArrayBuffer`, secretKey: `ArrayBuffer`, blindingFactor: `ArrayBuffer` }
99
+ fn bls_generate_blinded_g1_key ( cx : FunctionContext ) -> JsResult < JsObject > {
100
+ bls_generate_keypair :: < G1 > ( cx, Some ( BLINDING_G1 ) )
101
+ }
102
+
29
103
/// Generate a BLS key pair where secret key `x` in Fp
30
104
/// and public key `w` = `g2` ^ `x`
31
105
/// `seed`: `ArrayBuffer` [opt]
32
106
/// `return`: Object { publicKey: `ArrayBuffer`, secretKey: `ArrayBuffer` }
33
107
fn bls_generate_g2_key ( cx : FunctionContext ) -> JsResult < JsObject > {
34
- bls_generate_keypair :: < G2 > ( cx)
108
+ bls_generate_keypair :: < G2 > ( cx, None )
35
109
}
36
110
37
111
/// Generate a BLS key pair where secret key `x` in Fp
38
112
/// and public key `w` = `g1` ^ `x`
39
113
/// `seed`: `ArrayBuffer` [opt]
40
114
/// `return`: Object { publicKey: `ArrayBuffer`, secretKey: `ArrayBuffer` }
41
115
fn bls_generate_g1_key ( cx : FunctionContext ) -> JsResult < JsObject > {
42
- bls_generate_keypair :: < G1 > ( cx)
116
+ bls_generate_keypair :: < G1 > ( cx, None )
43
117
}
44
118
45
- fn bls_generate_keypair < G : CurveProjective < Engine = Bls12 , Scalar = Fr > + SerDes > ( mut cx : FunctionContext ) -> JsResult < JsObject > {
119
+ fn bls_generate_keypair < ' a , ' b , G : CurveProjective < Engine = Bls12 , Scalar = Fr > + SerDes > ( mut cx : FunctionContext < ' a > , blinded : Option < & ' b [ u8 ] > ) -> JsResult < ' a , JsObject > {
120
+ let mut passed_seed = false ;
46
121
let seed = match cx. argument_opt ( 0 ) {
47
122
Some ( arg) => {
48
123
let arg: Handle < JsArrayBuffer > = arg. downcast :: < JsArrayBuffer > ( ) . or_throw ( & mut cx) ?;
49
124
let seed_data = cx. borrow ( & arg, |data| data. as_slice :: < u8 > ( ) ) ;
125
+ passed_seed = true ;
50
126
seed_data. to_vec ( )
51
127
} ,
52
128
None => {
@@ -61,6 +137,28 @@ fn bls_generate_keypair<G: CurveProjective<Engine = Bls12, Scalar = Fr> + SerDes
61
137
let mut pk = G :: one ( ) ;
62
138
pk. mul_assign ( sk) ;
63
139
140
+ let r =
141
+ match blinded {
142
+ Some ( g) => {
143
+ let mut data = g. to_vec ( ) ;
144
+ let mut gg = g. clone ( ) ;
145
+ if passed_seed {
146
+ data. extend_from_slice ( seed. as_slice ( ) ) ;
147
+ } else {
148
+ let mut rng = thread_rng ( ) ;
149
+ let mut blinding_factor = vec ! [ 0u8 , 32 ] ;
150
+ rng. fill_bytes ( blinding_factor. as_mut_slice ( ) ) ;
151
+ data. extend_from_slice ( blinding_factor. as_slice ( ) ) ;
152
+ }
153
+ let mut blinding_g = G :: deserialize ( & mut gg, true ) . unwrap ( ) ;
154
+ let r = gen_sk ( data. as_slice ( ) ) ;
155
+ blinding_g. mul_assign ( r) ;
156
+ pk. add_assign ( & blinding_g) ;
157
+ Some ( r)
158
+ } ,
159
+ None => None
160
+ } ;
161
+
64
162
let mut sk_bytes = Vec :: new ( ) ;
65
163
let mut pk_bytes = Vec :: new ( ) ;
66
164
sk. serialize ( & mut sk_bytes, true ) . unwrap ( ) ;
@@ -71,6 +169,12 @@ fn bls_generate_keypair<G: CurveProjective<Engine = Bls12, Scalar = Fr> + SerDes
71
169
let result = JsObject :: new ( & mut cx) ;
72
170
result. set ( & mut cx, "publicKey" , pk_array) ?;
73
171
result. set ( & mut cx, "secretKey" , sk_array) ?;
172
+ if let Some ( rr) = r {
173
+ let mut r_bytes = Vec :: new ( ) ;
174
+ rr. serialize ( & mut r_bytes, true ) . unwrap ( ) ;
175
+ let r_array = slice_to_js_array_buffer ! ( & r_bytes[ ..] , cx) ;
176
+ result. set ( & mut cx, "blindingFactor" , r_array) ?;
177
+ }
74
178
75
179
Ok ( result)
76
180
}
@@ -834,6 +938,8 @@ fn bitvector_to_revealed(data: &[u8]) -> BTreeSet<usize> {
834
938
}
835
939
836
940
register_module ! ( mut m, {
941
+ m. export_function( "bls_generate_blinded_g2_key" , bls_generate_blinded_g2_key) ?;
942
+ m. export_function( "bls_generate_blinded_g1_key" , bls_generate_blinded_g1_key) ?;
837
943
m. export_function( "bls_generate_g2_key" , bls_generate_g2_key) ?;
838
944
m. export_function( "bls_generate_g1_key" , bls_generate_g1_key) ?;
839
945
m. export_function( "bls_secret_key_to_bbs_key" , bls_secret_key_to_bbs_key) ?;
0 commit comments