forked from supranational/blst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
supranational.blst.cs
1015 lines (899 loc) · 43.6 KB
/
supranational.blst.cs
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
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// DO NOT EDIT THIS FILE!!!
// The file is auto-generated by run.me
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
using System;
using System.Text;
using System.Numerics;
using System.Runtime.InteropServices;
using size_t = System.UIntPtr;
#if NET5_0_OR_GREATER
using System.Runtime.Loader;
using System.Reflection;
using System.IO;
#endif
namespace supranational { public static class blst {
#if NET5_0_OR_GREATER
private static readonly string dll;
static blst()
{
if (String.IsNullOrEmpty(dll)) {
var name = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "blst.dll"
: RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "libblst.dll.dylib"
: "libblst.dll.so";
var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var arch = RuntimeInformation.ProcessArchitecture switch {
Architecture.X64 => "x64",
Architecture.Arm64 => "arm64",
_ => "unsupported"
};
#if NET8_0_OR_GREATER
// RuntimeInformation.RuntimeIdentifier changed between .NET 7 and 8
// and only aligns to the nuget layout in 8+
var rid = RuntimeInformation.RuntimeIdentifier;
#else
// Mimic pre-8 RuntimeInformation.RuntimeIdentifier as
// "win-x64", "linux-x64", "linux-arm64", "osx-x64", etc.
var os = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win"
: RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "osx"
: RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD) ? "freebsd"
: "linux";
var rid = $"{os}-{arch}";
#endif
// first look for the file in the standard locations for a nuget installed native lib
dll = Path.Combine(dir, "runtimes", rid, "native", name);
if (!File.Exists(dll))
dll = Path.Combine(dir, arch, name); // try the original non-standard location
if (!File.Exists(dll))
dll = Path.Combine(Environment.CurrentDirectory, name);
if (File.Exists(dll)) {
AssemblyLoadContext.Default.ResolvingUnmanagedDll += (asm, needs) =>
(needs == "blst.dll" ? NativeLibrary.Load(dll) : IntPtr.Zero);
}
}
}
#endif
public enum ERROR {
SUCCESS = 0,
BAD_ENCODING,
POINT_NOT_ON_CURVE,
POINT_NOT_IN_GROUP,
AGGR_TYPE_MISMATCH,
VERIFY_FAIL,
PK_IS_INFINITY,
BAD_SCALAR,
}
public class Exception : ApplicationException {
private readonly ERROR code;
public Exception(ERROR err) { code = err; }
public override string Message
{ get
{ switch(code) {
case ERROR.BAD_ENCODING: return "bad encoding";
case ERROR.POINT_NOT_ON_CURVE: return "point not on curve";
case ERROR.POINT_NOT_IN_GROUP: return "point not in group";
case ERROR.AGGR_TYPE_MISMATCH: return "aggregate type mismatch";
case ERROR.VERIFY_FAIL: return "verify failure";
case ERROR.PK_IS_INFINITY: return "public key is infinity";
case ERROR.BAD_SCALAR: return "bad scalar";
default: return null;
}
}
}
}
public enum ByteOrder {
BigEndian,
LittleEndian,
}
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern
void blst_keygen([Out] byte[] key, [In] byte[] IKM, size_t IKM_len,
[In] byte[] info, size_t info_len);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern
void blst_keygen_v3([Out] byte[] key, [In] byte[] IKM, size_t IKM_len,
[In] byte[] info, size_t info_len);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern
void blst_keygen_v4_5([Out] byte[] key, [In] byte[] IKM, size_t IKM_len,
[In] byte[] salt, size_t salt_len,
[In] byte[] info, size_t info_len);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern
void blst_keygen_v5([Out] byte[] key, [In] byte[] IKM, size_t IKM_len,
[In] byte[] salt, size_t salt_len,
[In] byte[] info, size_t info_len);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_derive_master_eip2333([Out] byte[] key,
[In] byte[] IKM, size_t IKM_len);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_derive_child_eip2333([Out] byte[] key,
[In] byte[] master, uint child_index);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_scalar_from_bendian([Out] byte[] ret, [In] byte[] key);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_bendian_from_scalar([Out] byte[] ret, [In] byte[] key);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_sk_check([In] byte[] key);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_scalar_from_lendian([Out] byte[] key, [In] byte[] inp);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_lendian_from_scalar([Out] byte[] key, [In] byte[] inp);
public struct SecretKey {
internal byte[] key;
//public SecretKey() { key = new byte[32]; }
public SecretKey(byte[] IKM, string info)
{ key = new byte[32]; keygen(IKM, info); }
public SecretKey(byte[] inp, ByteOrder order=ByteOrder.BigEndian)
{ key = new byte[32];
switch(order) {
case ByteOrder.BigEndian: from_bendian(inp); break;
case ByteOrder.LittleEndian: from_lendian(inp); break;
}
}
public void keygen(byte[] IKM, string info="")
{ if (key == null) key = new byte[32];
byte[] info_bytes = Encoding.UTF8.GetBytes(info);
blst_keygen(key, IKM, (size_t)IKM.Length,
info_bytes, (size_t)info_bytes.Length);
}
public void keygen_v3(byte[] IKM, string info="")
{ if (key == null) key = new byte[32];
byte[] info_bytes = Encoding.UTF8.GetBytes(info);
blst_keygen_v3(key, IKM, (size_t)IKM.Length,
info_bytes, (size_t)info_bytes.Length);
}
public void keygen_v4_5(byte[] IKM, string salt, string info="")
{ if (key == null) key = new byte[32];
byte[] salt_bytes = Encoding.UTF8.GetBytes(salt);
byte[] info_bytes = Encoding.UTF8.GetBytes(info);
blst_keygen_v4_5(key, IKM, (size_t)IKM.Length,
salt_bytes, (size_t)salt_bytes.Length,
info_bytes, (size_t)info_bytes.Length);
}
public void keygen_v5(byte[] IKM, byte[] salt, string info="")
{ if (key == null) key = new byte[32];
byte[] info_bytes = Encoding.UTF8.GetBytes(info);
blst_keygen_v5(key, IKM, (size_t)IKM.Length,
salt, (size_t)salt.Length,
info_bytes, (size_t)info_bytes.Length);
}
public void keygen_v5(byte[] IKM, string salt, string info="")
{ keygen_v5(IKM, Encoding.UTF8.GetBytes(salt), info); }
public void derive_master_eip2333(byte[] IKM)
{ if (key == null) key = new byte[32];
blst_derive_master_eip2333(key, IKM, (size_t)IKM.Length);
}
public SecretKey(SecretKey master, uint child_index)
{ key = new byte[32];
blst_derive_child_eip2333(key, master.key, child_index);
}
public void from_bendian(byte[] inp)
{ if (inp.Length != 32)
throw new Exception(ERROR.BAD_ENCODING);
if (key == null) key = new byte[32];
blst_scalar_from_bendian(key, inp);
if (!blst_sk_check(key))
throw new Exception(ERROR.BAD_ENCODING);
}
public void from_lendian(byte[] inp)
{ if (inp.Length != 32)
throw new Exception(ERROR.BAD_ENCODING);
if (key == null) key = new byte[32];
blst_scalar_from_lendian(key, inp);
if (!blst_sk_check(key))
throw new Exception(ERROR.BAD_ENCODING);
}
public byte[] to_bendian()
{ byte[] ret = new byte[32];
blst_bendian_from_scalar(ret, key);
return ret;
}
public byte[] to_lendian()
{ byte[] ret = new byte[32];
blst_lendian_from_scalar(ret, key);
return ret;
}
}
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_scalar_from_be_bytes([Out] byte[] ret, [In] byte[] inp,
size_t inp_len);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_scalar_from_le_bytes([Out] byte[] ret, [In] byte[] inp,
size_t inp_len);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_sk_add_n_check([Out] byte[] ret, [In] byte[] a,
[In] byte[] b);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_sk_sub_n_check([Out] byte[] ret, [In] byte[] a,
[In] byte[] b);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_sk_mul_n_check([Out] byte[] ret, [In] byte[] a,
[In] byte[] b);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_sk_inverse([Out] byte[] ret, [In] byte[] a);
public struct Scalar {
internal byte[] val;
//public Scalar() { val = new byte[32]; }
public Scalar(byte[] inp, ByteOrder order=ByteOrder.BigEndian)
{ val = new byte[32];
switch(order) {
case ByteOrder.BigEndian: from_bendian(inp); break;
case ByteOrder.LittleEndian: from_lendian(inp); break;
}
}
private Scalar(bool _) { val = new byte[32]; }
private Scalar(Scalar orig) { val = (byte[])orig.val.Clone(); }
public Scalar dup() { return new Scalar(this); }
public void from_bendian(byte[] inp)
{ if (val == null) val = new byte[32];
blst_scalar_from_be_bytes(val, inp, (size_t)inp.Length);
}
public void from_lendian(byte[] inp)
{ if (val == null) val = new byte[32];
blst_scalar_from_le_bytes(val, inp, (size_t)inp.Length);
}
public byte[] to_bendian()
{ byte[] ret = new byte[32];
blst_bendian_from_scalar(ret, val);
return ret;
}
public byte[] to_lendian()
{ byte[] ret = new byte[32];
blst_lendian_from_scalar(ret, val);
return ret;
}
public Scalar add(SecretKey a)
{ if (!blst_sk_add_n_check(val, val, a.key))
throw new Exception(ERROR.BAD_SCALAR);
return this;
}
public Scalar add(Scalar a)
{ if (!blst_sk_add_n_check(val, val, a.val))
throw new Exception(ERROR.BAD_SCALAR);
return this;
}
public Scalar sub(Scalar a)
{ if (!blst_sk_sub_n_check(val, val, a.val))
throw new Exception(ERROR.BAD_SCALAR);
return this;
}
public Scalar mul(Scalar a)
{ if (!blst_sk_mul_n_check(val, val, a.val))
throw new Exception(ERROR.BAD_SCALAR);
return this;
}
public Scalar inverse()
{ blst_sk_inverse(val, val); return this; }
public static Scalar operator+(Scalar a, Scalar b)
{ return a.dup().add(b); }
public static Scalar operator-(Scalar a, Scalar b)
{ return a.dup().sub(b); }
public static Scalar operator*(Scalar a, Scalar b)
{ return a.dup().mul(b); }
public static Scalar operator/(Scalar a, Scalar b)
{ return b.dup().inverse().mul(a); }
}
private const int P1_COMPRESSED_SZ = 384/8;
private const int P2_COMPRESSED_SZ = 2*P1_COMPRESSED_SZ;
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern size_t blst_p1_affine_sizeof();
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern ERROR blst_p1_deserialize([Out] long[] ret, [In] byte[] inp);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p1_affine_serialize([Out] byte[] ret, [In] long[] inp);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p1_affine_compress([Out] byte[] ret, [In] long[] inp);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p1_to_affine([Out] long[] ret, [In] long[] inp);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_p1_affine_on_curve([In] long[] point);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_p1_affine_in_g1([In] long[] point);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_p1_affine_is_inf([In] long[] point);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_p1_affine_is_equal([In] long[] a, [In] long[] b);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr blst_p1_generator();
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern ERROR blst_core_verify_pk_in_g2([In] long[] pk, [In] long[] sig,
bool hash_or_encode,
[In] byte[] msg, size_t msg_len,
[In] byte[] dst, size_t dst_len,
[In] byte[] aug, size_t aug_len);
public struct P1_Affine {
internal readonly long[] point;
private static readonly int sz = (int)blst_p1_affine_sizeof()/sizeof(long);
//public P1_Affine() { point = new long[sz]; }
private P1_Affine(bool _) { point = new long[sz]; }
private P1_Affine(P1_Affine p) { point = (long[])p.point.Clone(); }
public P1_Affine(byte[] inp) : this(true)
{ int len = inp.Length;
if (len == 0 || len != ((inp[0]&0x80) == 0x80 ? P1_COMPRESSED_SZ
: 2*P1_COMPRESSED_SZ))
throw new Exception(ERROR.BAD_ENCODING);
ERROR err = blst_p1_deserialize(point, inp);
if (err != ERROR.SUCCESS)
throw new Exception(err);
}
public P1_Affine(P1 jacobian) : this(true)
{ blst_p1_to_affine(point, jacobian.point); }
public P1_Affine dup() { return new P1_Affine(this); }
public P1 to_jacobian() { return new P1(this); }
public byte[] serialize()
{ byte[] ret = new byte[2*P1_COMPRESSED_SZ];
blst_p1_affine_serialize(ret, point);
return ret;
}
public byte[] compress()
{ byte[] ret = new byte[P1_COMPRESSED_SZ];
blst_p1_affine_compress(ret, point);
return ret;
}
public bool on_curve() { return blst_p1_affine_on_curve(point); }
public bool in_group() { return blst_p1_affine_in_g1(point); }
public bool is_inf() { return blst_p1_affine_is_inf(point); }
public bool is_equal(P1_Affine p)
{ return blst_p1_affine_is_equal(point, p.point); }
ERROR core_verify(P2_Affine pk, bool hash_or_encode,
byte[] msg, string DST = "", byte[] aug = null)
{ byte[] dst = Encoding.UTF8.GetBytes(DST);
return blst_core_verify_pk_in_g2(pk.point, point,
hash_or_encode,
msg, (size_t)msg.Length,
dst, (size_t)dst.Length,
aug, (size_t)(aug!=null ? aug.Length : 0));
}
public static P1_Affine generator()
{ var ret = new P1_Affine(true);
Marshal.Copy(blst_p1_generator(), ret.point, 0, ret.point.Length);
return ret;
}
}
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern size_t blst_p1_sizeof();
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p1_serialize([Out] byte[] ret, [In] long[] inp);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p1_compress([Out] byte[] ret, [In] long[] inp);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p1_from_affine([Out] long[] ret, [In] long[] inp);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_p1_on_curve([In] long[] point);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_p1_in_g1([In] long[] point);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_p1_is_inf([In] long[] point);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_p1_is_equal([In] long[] a, [In] long[] b);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_sk_to_pk_in_g1([Out] long[] ret, [In] byte[] SK);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern
void blst_encode_to_g1([Out] long[] ret, [In] byte[] msg, size_t msg_len,
[In] byte[] dst, size_t dst_len,
[In] byte[] aug, size_t aug_len);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern
void blst_hash_to_g1([Out] long[] ret, [In] byte[] msg, size_t msg_len,
[In] byte[] dst, size_t dst_len,
[In] byte[] aug, size_t aug_len);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern
void blst_sign_pk_in_g2([Out] long[] ret, [In] long[] hash, [In] byte[] SK);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern
void blst_p1_mult([Out] long[] ret, [In] long[] a,
[In] byte[] scalar, size_t nbits);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p1_cneg([Out] long[] ret, bool cbit);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern
void blst_p1_add_or_double([Out] long[] ret, [In] long[] a, [In] long[] b);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern
void blst_p1_add_or_double_affine([Out] long[] ret, [In] long[] a,
[In] long[] b);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p1_double([Out] long[] ret, [In] long[] a);
public struct P1 {
internal long[] point;
private static readonly int sz = (int)blst_p1_sizeof()/sizeof(long);
//public P1() { point = new long[sz]; }
private P1(bool _) { point = new long[sz]; }
private P1(P1 p) { point = (long[])p.point.Clone(); }
private long[] self()
{ if (point==null) { point = new long[sz]; } return point; }
public P1(SecretKey sk) : this(true)
{ blst_sk_to_pk_in_g1(point, sk.key); }
public P1(byte[] inp) : this(true)
{ int len = inp.Length;
if (len == 0 || len != ((inp[0]&0x80) == 0x80 ? P1_COMPRESSED_SZ
: 2*P1_COMPRESSED_SZ))
throw new Exception(ERROR.BAD_ENCODING);
ERROR err = blst_p1_deserialize(point, inp);
if (err != ERROR.SUCCESS)
throw new Exception(err);
blst_p1_from_affine(point, point);
}
public P1(P1_Affine affine) : this(true)
{ blst_p1_from_affine(point, affine.point); }
public P1 dup() { return new P1(this); }
public P1_Affine to_affine() { return new P1_Affine(this); }
public byte[] serialize()
{ byte[] ret = new byte[2*P1_COMPRESSED_SZ];
blst_p1_serialize(ret, point);
return ret;
}
public byte[] compress()
{ byte[] ret = new byte[P1_COMPRESSED_SZ];
blst_p1_compress(ret, point);
return ret;
}
public bool on_curve() { return blst_p1_on_curve(point); }
public bool in_group() { return blst_p1_in_g1(point); }
public bool is_inf() { return blst_p1_is_inf(point); }
public bool is_equal(P1 p) { return blst_p1_is_equal(point, p.point); }
public P1 hash_to(byte[] msg, string DST="", byte[] aug=null)
{ byte[] dst = Encoding.UTF8.GetBytes(DST);
blst_hash_to_g1(self(), msg, (size_t)msg.Length,
dst, (size_t)dst.Length,
aug, (size_t)(aug!=null ? aug.Length : 0));
return this;
}
public P1 encode_to(byte[] msg, string DST="", byte[] aug=null)
{ byte[] dst = Encoding.UTF8.GetBytes(DST);
blst_encode_to_g1(self(), msg, (size_t)msg.Length,
dst, (size_t)dst.Length,
aug, (size_t)(aug!=null ? aug.Length : 0));
return this;
}
public P1 sign_with(SecretKey sk)
{ blst_sign_pk_in_g2(point, point, sk.key); return this; }
public P1 sign_with(Scalar scalar)
{ blst_sign_pk_in_g2(point, point, scalar.val); return this; }
public void aggregate(P1_Affine inp)
{ if (blst_p1_affine_in_g1(inp.point))
blst_p1_add_or_double_affine(point, point, inp.point);
else
throw new Exception(ERROR.POINT_NOT_IN_GROUP);
}
public P1 mult(byte[] scalar)
{ blst_p1_mult(point, point, scalar, (size_t)(scalar.Length*8));
return this;
}
public P1 mult(Scalar scalar)
{ blst_p1_mult(point, point, scalar.val, (size_t)255);
return this;
}
public P1 mult(BigInteger scalar)
{ byte[] val;
if (scalar.Sign < 0) {
val = BigInteger.Negate(scalar).ToByteArray();
blst_p1_cneg(point, true);
} else {
val = scalar.ToByteArray();
}
int len = val.Length;
if (val[len-1]==0) len--;
blst_p1_mult(point, point, val, (size_t)(len*8));
return this;
}
public P1 cneg(bool flag) { blst_p1_cneg(point, flag); return this; }
public P1 neg() { blst_p1_cneg(point, true); return this; }
public P1 add(P1 a)
{ blst_p1_add_or_double(point, point, a.point); return this; }
public P1 add(P1_Affine a)
{ blst_p1_add_or_double_affine(point, point, a.point); return this; }
public P1 dbl()
{ blst_p1_double(point, point); return this; }
public static P1 generator()
{ var ret = new P1(true);
Marshal.Copy(blst_p1_generator(), ret.point, 0, ret.point.Length);
return ret;
}
}
public static P1 G1() { return P1.generator(); }
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_aggregated_in_g1([Out] long[] fp12, [In] long[] p);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern ERROR blst_pairing_aggregate_pk_in_g1([In, Out] long[] fp12,
[In] long[] pk, [In] long[] sig,
[In] byte[] msg, size_t msg_len,
[In] byte[] aug, size_t aug_len);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern ERROR blst_pairing_mul_n_aggregate_pk_in_g1([In, Out] long[] fp12,
[In] long[] pk, [In] long[] sig,
[In] byte[] scalar, size_t nbits,
[In] byte[] msg, size_t msg_len,
[In] byte[] aug, size_t aug_len);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern size_t blst_p2_affine_sizeof();
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern ERROR blst_p2_deserialize([Out] long[] ret, [In] byte[] inp);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p2_affine_serialize([Out] byte[] ret, [In] long[] inp);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p2_affine_compress([Out] byte[] ret, [In] long[] inp);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p2_to_affine([Out] long[] ret, [In] long[] inp);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_p2_affine_on_curve([In] long[] point);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_p2_affine_in_g2([In] long[] point);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_p2_affine_is_inf([In] long[] point);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_p2_affine_is_equal([In] long[] a, [In] long[] b);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr blst_p2_generator();
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern ERROR blst_core_verify_pk_in_g1([In] long[] pk, [In] long[] sig,
bool hash_or_encode,
[In] byte[] msg, size_t msg_len,
[In] byte[] dst, size_t dst_len,
[In] byte[] aug, size_t aug_len);
public struct P2_Affine {
internal readonly long[] point;
private static readonly int sz = (int)blst_p2_affine_sizeof()/sizeof(long);
//public P2_Affine() { point = new long[sz]; }
private P2_Affine(bool _) { point = new long[sz]; }
private P2_Affine(P2_Affine p) { point = (long[])p.point.Clone(); }
public P2_Affine(byte[] inp) : this(true)
{ int len = inp.Length;
if (len == 0 || len != ((inp[0]&0x80) == 0x80 ? P2_COMPRESSED_SZ
: 2*P2_COMPRESSED_SZ))
throw new Exception(ERROR.BAD_ENCODING);
ERROR err = blst_p2_deserialize(point, inp);
if (err != ERROR.SUCCESS)
throw new Exception(err);
}
public P2_Affine(P2 jacobian) : this(true)
{ blst_p2_to_affine(point, jacobian.point); }
public P2_Affine dup() { return new P2_Affine(this); }
public P2 to_jacobian() { return new P2(this); }
public byte[] serialize()
{ byte[] ret = new byte[2*P2_COMPRESSED_SZ];
blst_p2_affine_serialize(ret, point);
return ret;
}
public byte[] compress()
{ byte[] ret = new byte[P2_COMPRESSED_SZ];
blst_p2_affine_compress(ret, point);
return ret;
}
public bool on_curve() { return blst_p2_affine_on_curve(point); }
public bool in_group() { return blst_p2_affine_in_g2(point); }
public bool is_inf() { return blst_p2_affine_is_inf(point); }
public bool is_equal(P2_Affine p)
{ return blst_p2_affine_is_equal(point, p.point); }
ERROR core_verify(P1_Affine pk, bool hash_or_encode,
byte[] msg, string DST = "", byte[] aug = null)
{ byte[] dst = Encoding.UTF8.GetBytes(DST);
return blst_core_verify_pk_in_g1(pk.point, point,
hash_or_encode,
msg, (size_t)msg.Length,
dst, (size_t)dst.Length,
aug, (size_t)(aug!=null ? aug.Length : 0));
}
public static P2_Affine generator()
{ var ret = new P2_Affine(true);
Marshal.Copy(blst_p2_generator(), ret.point, 0, ret.point.Length);
return ret;
}
}
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern size_t blst_p2_sizeof();
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p2_serialize([Out] byte[] ret, [In] long[] inp);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p2_compress([Out] byte[] ret, [In] long[] inp);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p2_from_affine([Out] long[] ret, [In] long[] inp);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_p2_on_curve([In] long[] point);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_p2_in_g2([In] long[] point);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_p2_is_inf([In] long[] point);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_p2_is_equal([In] long[] a, [In] long[] b);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_sk_to_pk_in_g2([Out] long[] ret, [In] byte[] SK);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern
void blst_encode_to_g2([Out] long[] ret, [In] byte[] msg, size_t msg_len,
[In] byte[] dst, size_t dst_len,
[In] byte[] aug, size_t aug_len);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern
void blst_hash_to_g2([Out] long[] ret, [In] byte[] msg, size_t msg_len,
[In] byte[] dst, size_t dst_len,
[In] byte[] aug, size_t aug_len);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern
void blst_sign_pk_in_g1([Out] long[] ret, [In] long[] hash, [In] byte[] SK);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern
void blst_p2_mult([Out] long[] ret, [In] long[] a,
[In] byte[] scalar, size_t nbits);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p2_cneg([Out] long[] ret, bool cbit);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern
void blst_p2_add_or_double([Out] long[] ret, [In] long[] a, [In] long[] b);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern
void blst_p2_add_or_double_affine([Out] long[] ret, [In] long[] a,
[In] long[] b);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p2_double([Out] long[] ret, [In] long[] a);
public struct P2 {
internal long[] point;
private static readonly int sz = (int)blst_p2_sizeof()/sizeof(long);
//public P2() { point = new long[sz]; }
private P2(bool _) { point = new long[sz]; }
private P2(P2 p) { point = (long[])p.point.Clone(); }
private long[] self()
{ if (point==null) { point = new long[sz]; } return point; }
public P2(SecretKey sk) : this(true)
{ blst_sk_to_pk_in_g2(point, sk.key); }
public P2(byte[] inp) : this(true)
{ int len = inp.Length;
if (len == 0 || len != ((inp[0]&0x80) == 0x80 ? P2_COMPRESSED_SZ
: 2*P2_COMPRESSED_SZ))
throw new Exception(ERROR.BAD_ENCODING);
ERROR err = blst_p2_deserialize(point, inp);
if (err != ERROR.SUCCESS)
throw new Exception(err);
blst_p2_from_affine(point, point);
}
public P2(P2_Affine affine) : this(true)
{ blst_p2_from_affine(point, affine.point); }
public P2 dup() { return new P2(this); }
public P2_Affine to_affine() { return new P2_Affine(this); }
public byte[] serialize()
{ byte[] ret = new byte[2*P2_COMPRESSED_SZ];
blst_p2_serialize(ret, point);
return ret;
}
public byte[] compress()
{ byte[] ret = new byte[P2_COMPRESSED_SZ];
blst_p2_compress(ret, point);
return ret;
}
public bool on_curve() { return blst_p2_on_curve(point); }
public bool in_group() { return blst_p2_in_g2(point); }
public bool is_inf() { return blst_p2_is_inf(point); }
public bool is_equal(P2 p) { return blst_p2_is_equal(point, p.point); }
public P2 hash_to(byte[] msg, string DST="", byte[] aug=null)
{ byte[] dst = Encoding.UTF8.GetBytes(DST);
blst_hash_to_g2(self(), msg, (size_t)msg.Length,
dst, (size_t)dst.Length,
aug, (size_t)(aug!=null ? aug.Length : 0));
return this;
}
public P2 encode_to(byte[] msg, string DST="", byte[] aug=null)
{ byte[] dst = Encoding.UTF8.GetBytes(DST);
blst_encode_to_g2(self(), msg, (size_t)msg.Length,
dst, (size_t)dst.Length,
aug, (size_t)(aug!=null ? aug.Length : 0));
return this;
}
public P2 sign_with(SecretKey sk)
{ blst_sign_pk_in_g1(point, point, sk.key); return this; }
public P2 sign_with(Scalar scalar)
{ blst_sign_pk_in_g1(point, point, scalar.val); return this; }
public void aggregate(P2_Affine inp)
{ if (blst_p2_affine_in_g2(inp.point))
blst_p2_add_or_double_affine(point, point, inp.point);
else
throw new Exception(ERROR.POINT_NOT_IN_GROUP);
}
public P2 mult(byte[] scalar)
{ blst_p2_mult(point, point, scalar, (size_t)(scalar.Length*8));
return this;
}
public P2 mult(Scalar scalar)
{ blst_p2_mult(point, point, scalar.val, (size_t)255);
return this;
}
public P2 mult(BigInteger scalar)
{ byte[] val;
if (scalar.Sign < 0) {
val = BigInteger.Negate(scalar).ToByteArray();
blst_p2_cneg(point, true);
} else {
val = scalar.ToByteArray();
}
int len = val.Length;
if (val[len-1]==0) len--;
blst_p2_mult(point, point, val, (size_t)(len*8));
return this;
}
public P2 cneg(bool flag) { blst_p2_cneg(point, flag); return this; }
public P2 neg() { blst_p2_cneg(point, true); return this; }
public P2 add(P2 a)
{ blst_p2_add_or_double(point, point, a.point); return this; }
public P2 add(P2_Affine a)
{ blst_p2_add_or_double_affine(point, point, a.point); return this; }
public P2 dbl()
{ blst_p2_double(point, point); return this; }
public static P2 generator()
{ var ret = new P2(true);
Marshal.Copy(blst_p2_generator(), ret.point, 0, ret.point.Length);
return ret;
}
}
public static P2 G2() { return P2.generator(); }
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_aggregated_in_g2([Out] long[] fp12, [In] long[] p);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern ERROR blst_pairing_aggregate_pk_in_g2([In, Out] long[] fp12,
[In] long[] pk, [In] long[] sig,
[In] byte[] msg, size_t msg_len,
[In] byte[] aug, size_t aug_len);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern ERROR blst_pairing_mul_n_aggregate_pk_in_g2([In, Out] long[] fp12,
[In] long[] pk, [In] long[] sig,
[In] byte[] scalar, size_t nbits,
[In] byte[] msg, size_t msg_len,
[In] byte[] aug, size_t aug_len);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern size_t blst_fp12_sizeof();
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_miller_loop([Out] long[] fp12, [In] long[] q,
[In] long[] p);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_fp12_is_one([In] long[] fp12);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_fp12_is_equal([In] long[] a, [In] long[] b);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_fp12_sqr([Out] long[] ret, [In] long[] a);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_fp12_mul([Out] long[] ret, [In] long[] a,
[In] long[] b);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_final_exp([Out] long[] ret, [In] long[] a);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_fp12_finalverify([In] long[] a, [In] long[] b);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr blst_fp12_one();
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_fp12_in_group([In] long[] a);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_bendian_from_fp12([Out] byte[] ret, [In] long[] a);
public struct PT {
internal readonly long[] fp12;
private static readonly int sz = (int)blst_fp12_sizeof()/sizeof(long);
internal PT(bool _) { fp12 = new long[sz]; }
private PT(PT orig) { fp12 = (long[])orig.fp12.Clone(); }
public PT(P1_Affine p) : this(true)
{ blst_aggregated_in_g1(fp12, p.point); }
public PT(P1 p) : this(true)
{ blst_aggregated_in_g1(fp12, (new P1_Affine(p)).point); }
public PT(P2_Affine q) : this(true)
{ blst_aggregated_in_g2(fp12, q.point); }
public PT(P2 q) : this(true)
{ blst_aggregated_in_g2(fp12, (new P2_Affine(q)).point); }
public PT(P2_Affine q, P1_Affine p) : this(true)
{ blst_miller_loop(fp12, q.point, p.point); }
public PT(P1_Affine p, P2_Affine q) : this(q, p) {}
public PT(P2 q, P1 p) : this(true)
{ blst_miller_loop(fp12, (new P2_Affine(q)).point,
(new P1_Affine(p)).point);
}
public PT(P1 p, P2 q) : this(q, p) {}
public PT dup() { return new PT(this); }
public bool is_one() { return blst_fp12_is_one(fp12); }
public bool is_equal(PT p)
{ return blst_fp12_is_equal(fp12, p.fp12); }
public PT sqr() { blst_fp12_sqr(fp12, fp12); return this; }
public PT mul(PT p) { blst_fp12_mul(fp12, fp12, p.fp12); return this; }
public PT final_exp() { blst_final_exp(fp12, fp12); return this; }
public bool in_group() { return blst_fp12_in_group(fp12); }
public byte[] to_bendian()
{ byte[] ret = new byte[12*P1_COMPRESSED_SZ];
blst_bendian_from_fp12(ret, fp12);
return ret;
}
public static bool finalverify(PT gt1, PT gt2)
{ return blst_fp12_finalverify(gt1.fp12, gt2.fp12); }
public static PT one()
{ var ret = new PT(true);
Marshal.Copy(blst_fp12_one(), ret.fp12, 0, ret.fp12.Length);
return ret;
}
}
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern size_t blst_pairing_sizeof();
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern
void blst_pairing_init([In, Out] long[] ctx, bool hash_or_encode,
[In] ref long dst, size_t dst_len);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_pairing_commit([In, Out] long[] ctx);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern ERROR blst_pairing_merge([In, Out] long[] ctx, [In] long[] ctx1);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern bool blst_pairing_finalverify([In] long[] ctx, [In] long[] sig);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern
void blst_pairing_raw_aggregate([In, Out] long[] ctx, [In] long[] q,
[In] long[] p);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr blst_pairing_as_fp12([In] long[] ctx);
public struct Pairing {
private readonly long[] ctx;
private static readonly int sz = (int)blst_pairing_sizeof()/sizeof(long);
public Pairing(bool hash_or_encode=false, string DST="")
{
byte[] dst = Encoding.UTF8.GetBytes(DST);
int dst_len = dst.Length;
int add_len = dst_len!=0 ? (dst_len+sizeof(long)-1)/sizeof(long) : 1;
Array.Resize(ref dst, add_len*sizeof(long));
ctx = new long[sz+add_len];
for (int i=0; i<add_len; i++)
ctx[sz+i] = BitConverter.ToInt64(dst, i*sizeof(long));
GCHandle h = GCHandle.Alloc(ctx, GCHandleType.Pinned);
blst_pairing_init(ctx, hash_or_encode, ref ctx[sz], (size_t)dst_len);
h.Free();
}
public ERROR aggregate(P1_Affine pk, Nullable<P2_Affine> sig,
byte[] msg, byte[] aug=null)
{ return blst_pairing_aggregate_pk_in_g1(ctx, pk.point,
sig.HasValue ? sig.Value.point : null,
msg, (size_t)msg.Length,
aug, (size_t)(aug!=null ? aug.Length : 0));
}
public ERROR aggregate(P2_Affine pk, Nullable<P1_Affine> sig,
byte[] msg, byte[] aug=null)
{ return blst_pairing_aggregate_pk_in_g2(ctx, pk.point,
sig.HasValue ? sig.Value.point : null,
msg, (size_t)msg.Length,
aug, (size_t)(aug!=null ? aug.Length : 0));
}
public ERROR mul_n_aggregate(P2_Affine pk, P1_Affine sig,
byte[] scalar, int nbits,
byte[] msg, byte[] aug=null)
{ return blst_pairing_mul_n_aggregate_pk_in_g2(ctx, pk.point, sig.point,
scalar, (size_t)nbits,
msg, (size_t)msg.Length,
aug, (size_t)(aug!=null ? aug.Length : 0));
}
public ERROR mul_n_aggregate(P1_Affine pk, P2_Affine sig,
byte[] scalar, int nbits,
byte[] msg, byte[] aug=null)
{ return blst_pairing_mul_n_aggregate_pk_in_g1(ctx, pk.point, sig.point,
scalar, (size_t)nbits,
msg, (size_t)msg.Length,
aug, (size_t)(aug!=null ? aug.Length : 0));
}
public void commit() { blst_pairing_commit(ctx); }
public void merge(Pairing a)
{ var err = blst_pairing_merge(ctx, a.ctx);
if (err != ERROR.SUCCESS)
throw new Exception(err);
}
public bool finalverify(PT sig=new PT())
{ return blst_pairing_finalverify(ctx, sig.fp12); }
public void raw_aggregate(P2_Affine q, P1_Affine p)
{ blst_pairing_raw_aggregate(ctx, q.point, p.point); }
public void raw_aggregate(P1_Affine p, P2_Affine q)
{ raw_aggregate(q, p); }