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
module Public_key_hash = struct
include
Blake2B.Make
(Base58)
(struct
let name = "Bls12_381.Public_key_hash"
let title = "A Bls12_381 public key hash"
let b58check_prefix = Base58.Prefix.bls12_381_public_key_hash
let size = Some 20
end)
module Logging = struct
let tag = Tag.def ~doc:title name pp
end
end
let () = Base58.check_encoded_prefix Public_key_hash.b58check_encoding "tz4" 36
module Public_key = struct
open Bls12_381_signature.MinPk
type t = Bls12_381_signature.MinPk.pk
let name = "Bls12_381.Public_key"
let title = "A Bls12_381 public key"
let to_bytes = pk_to_bytes
let to_string s = Bytes.to_string (to_bytes s)
let of_bytes_opt = pk_of_bytes_opt
let of_string_opt s = of_bytes_opt (Bytes.of_string s)
let of_bytes_without_validation = of_bytes_opt
let size _pk = Bls12_381_signature.MinPk.pk_size_in_bytes
type Base58.data += Data of t
let b58check_encoding =
Base58.register_encoding
~prefix:Base58.Prefix.bls12_381_public_key
~length:(size ())
~to_raw:to_string
~of_raw:of_string_opt
~wrap:(fun x -> Data x)
let () = Base58.check_encoded_prefix b58check_encoding "BLpk" 76
let hash v = Public_key_hash.hash_bytes [to_bytes v]
include Compare.Make (struct
type nonrec t = t
let compare a b =
Bytes.compare
(Bls12_381_signature.MinPk.pk_to_bytes a)
(Bls12_381_signature.MinPk.pk_to_bytes b)
end)
include Helpers.MakeRaw (struct
type nonrec t = t
let name = name
let of_bytes_opt = of_bytes_opt
let of_string_opt = of_string_opt
let to_string = to_string
end)
include Helpers.MakeB58 (struct
type nonrec t = t
let name = name
let b58check_encoding = b58check_encoding
end)
include Helpers.MakeEncoder (struct
type nonrec t = t
let name = name
let title = title
let raw_encoding =
let open Data_encoding in
conv to_bytes of_bytes_exn (Fixed.bytes (size ()))
let of_b58check = of_b58check
let of_b58check_opt = of_b58check_opt
let of_b58check_exn = of_b58check_exn
let to_b58check = to_b58check
let to_short_b58check = to_short_b58check
end)
let pp ppf t = Format.fprintf ppf "%s" (to_b58check t)
end
module Secret_key = struct
type t = Bls12_381_signature.sk
let name = "Bls12_381.Secret_key"
let title = "A Bls12_381 secret key"
include Compare.Make (struct
type nonrec t = t
let compare a b =
let a = Bls12_381_signature.sk_to_bytes a
and b = Bls12_381_signature.sk_to_bytes b in
Bytes.compare a b
end)
let size = Bls12_381_signature.sk_size_in_bytes
let to_bytes = Bls12_381_signature.sk_to_bytes
let to_string s = Bytes.to_string (to_bytes s)
let of_bytes_opt = Bls12_381_signature.sk_of_bytes_opt
let of_string_opt s = of_bytes_opt (Bytes.of_string s)
let to_public_key = Bls12_381_signature.MinPk.derive_pk
type Base58.data += Data of t
let b58check_encoding =
Base58.register_encoding
~prefix:Base58.Prefix.bls12_381_secret_key
~length:size
~to_raw:to_string
~of_raw:of_string_opt
~wrap:(fun sk -> Data sk)
let of_b58check_opt s =
match Base58.simple_decode b58check_encoding s with
| Some x -> Some x
| None -> Format.kasprintf Stdlib.failwith "Unexpected data (%s)" name
let of_b58check_exn s =
match of_b58check_opt s with
| Some x -> x
| None -> Format.kasprintf Stdlib.failwith "Unexpected data (%s)" name
let of_b58check s =
match of_b58check_opt s with
| Some x -> Ok x
| None ->
Error_monad.error_with
"Failed to read a b58check_encoding data (%s): %S"
name
s
let to_b58check s = Base58.simple_encode b58check_encoding s
let to_short_b58check s =
String.sub
(to_b58check s)
0
(10 + String.length (Base58.prefix b58check_encoding))
let () = Base58.check_encoded_prefix b58check_encoding "BLsk" 54
include Helpers.MakeRaw (struct
type nonrec t = t
let name = name
let of_bytes_opt = of_bytes_opt
let of_string_opt = of_string_opt
let to_string = to_string
end)
include Helpers.MakeEncoder (struct
type nonrec t = t
let name = name
let title = title
let raw_encoding =
let open Data_encoding in
conv to_bytes of_bytes_exn (Fixed.bytes size)
let of_b58check = of_b58check
let of_b58check_opt = of_b58check_opt
let of_b58check_exn = of_b58check_exn
let to_b58check = to_b58check
let to_short_b58check = to_short_b58check
end)
let pp ppf t = Format.fprintf ppf "%s" (to_b58check t)
end
type t = Bls12_381_signature.MinPk.signature
type watermark = Bytes.t
let name = "Bls12_381_signature"
let title = "A Bls12_381 signature"
let size = Bls12_381_signature.MinPk.signature_size_in_bytes
let to_bytes = Bls12_381_signature.MinPk.signature_to_bytes
let of_bytes_opt s =
if Bytes.length s = size then
Bls12_381_signature.MinPk.signature_of_bytes_opt s
else None
let to_string s = Bytes.to_string (to_bytes s)
let of_string_opt s = of_bytes_opt (Bytes.of_string s)
type Base58.data += Data of t
let b58check_encoding =
Base58.register_encoding
~prefix:Base58.Prefix.bls12_381_signature
~length:size
~to_raw:to_string
~of_raw:of_string_opt
~wrap:(fun x -> Data x)
let () = Base58.check_encoded_prefix b58check_encoding "BLsig" 142
include Helpers.MakeB58 (struct
type nonrec t = t
let name = name
let b58check_encoding = b58check_encoding
end)
include Helpers.MakeRaw (struct
type nonrec t = t
let name = name
let of_bytes_opt = of_bytes_opt
let of_string_opt = of_string_opt
let to_string = to_string
end)
include Compare.Make (struct
type nonrec t = t
let compare a b =
let a = to_bytes a and b = to_bytes b in
Bytes.compare a b
end)
include Helpers.MakeEncoder (struct
type nonrec t = t
let name = name
let title = title
let raw_encoding =
Data_encoding.conv to_bytes of_bytes_exn (Data_encoding.Fixed.bytes size)
let of_b58check = of_b58check
let of_b58check_opt = of_b58check_opt
let of_b58check_exn = of_b58check_exn
let to_b58check = to_b58check
let to_short_b58check = to_short_b58check
end)
let pp ppf t = Format.fprintf ppf "%s" (to_b58check t)
let zero =
Bls12_381_signature.MinPk.signature_of_bytes_exn
@@ Bytes.of_string
"\192\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
let sign ?watermark sk msg =
let msg =
match watermark with None -> msg | Some prefix -> Bytes.cat prefix msg
in
Bls12_381_signature.MinPk.Aug.sign sk msg
let check ?watermark pk signature msg =
let msg =
match watermark with None -> msg | Some prefix -> Bytes.cat prefix msg
in
Bls12_381_signature.MinPk.Aug.verify pk msg signature
let generate_key ?seed () =
let seed =
match seed with
| Some seed -> seed
| None ->
Hacl.Rand.gen 32
in
let sk = Bls12_381_signature.generate_sk seed in
let pk = Bls12_381_signature.MinPk.derive_pk sk in
let pkh = Public_key.hash pk in
(pkh, pk, sk)
let deterministic_nonce sk msg =
let key = Secret_key.to_bytes sk in
Hacl.Hash.SHA256.HMAC.digest ~key ~msg
let deterministic_nonce_hash sk msg =
Blake2B.to_bytes (Blake2B.hash_bytes [deterministic_nonce sk msg])
let aggregate_check pk_msg_list signature =
let pk_msg_list =
List.map
(fun (pk, watermark, msg) ->
let msg =
match watermark with
| None -> msg
| Some prefix -> Bytes.cat prefix msg
in
(pk, msg))
pk_msg_list
in
Bls12_381_signature.MinPk.Aug.aggregate_verify pk_msg_list signature
let aggregate_signature_opt = Bls12_381_signature.MinPk.aggregate_signature_opt
module Primitive = struct
include Bls12_381
let pairing_check = Bls12_381.Pairing.pairing_check
end