Skip to content

Commit 14da1a1

Browse files
committed
bindings/c#: add EIP-2537 serialization.
1 parent 2df8627 commit 14da1a1

File tree

2 files changed

+91
-19
lines changed

2 files changed

+91
-19
lines changed

bindings/c#/run.me

+31-7
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,13 @@ static extern ERROR blst_core_verify_pk_in_g2([In] long[] pk, [In] long[] sig,
348348
[In] byte[] dst, size_t dst_len,
349349
[In] byte[] aug, size_t aug_len);
350350
351+
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
352+
static extern ERROR blst_p1_deserialize_eip2537([Out] long[] ret,
353+
[In] byte[] inp);
354+
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
355+
static extern void blst_p1_affine_serialize_eip2537([Out] byte[] ret,
356+
[In] long[] inp);
357+
351358
public struct P1_Affine {
352359
internal readonly long[] point;
353360
@@ -359,10 +366,12 @@ public struct P1_Affine {
359366
360367
public P1_Affine(byte[] inp) : this(true)
361368
{ int len = inp.Length;
362-
if (len == 0 || len != ((inp[0]&0x80) == 0x80 ? P1_COMPRESSED_SZ
363-
: 2*P1_COMPRESSED_SZ))
369+
if (len == 0 || (len != ((inp[0]&0x80) == 0x80 ? P1_COMPRESSED_SZ
370+
: 2*P1_COMPRESSED_SZ) &&
371+
len != 128*1))
364372
throw new Exception(ERROR.BAD_ENCODING);
365-
ERROR err = blst_p1_deserialize(point, inp);
373+
ERROR err = len == 128*1 ? blst_p1_deserialize_eip2537(point, inp)
374+
: blst_p1_deserialize(point, inp);
366375
if (err != ERROR.SUCCESS)
367376
throw new Exception(err);
368377
}
@@ -371,6 +380,11 @@ public struct P1_Affine {
371380
372381
public P1_Affine dup() { return new P1_Affine(this); }
373382
public P1 to_jacobian() { return new P1(this); }
383+
public byte[] serialize_eip2537()
384+
{ byte[] ret = new byte[128*1];
385+
blst_p1_affine_serialize_eip2537(ret, point);
386+
return ret;
387+
}
374388
public byte[] serialize()
375389
{ byte[] ret = new byte[2*P1_COMPRESSED_SZ];
376390
blst_p1_affine_serialize(ret, point);
@@ -455,6 +469,9 @@ void blst_p1_add_or_double_affine([Out] long[] ret, [In] long[] a,
455469
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
456470
static extern void blst_p1_double([Out] long[] ret, [In] long[] a);
457471
472+
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
473+
static extern void blst_p1_serialize_eip2537([Out] byte[] ret, [In] long[] inp);
474+
458475
public struct P1 {
459476
internal long[] point;
460477
@@ -470,10 +487,12 @@ public struct P1 {
470487
{ blst_sk_to_pk_in_g1(point, sk.key); }
471488
public P1(byte[] inp) : this(true)
472489
{ int len = inp.Length;
473-
if (len == 0 || len != ((inp[0]&0x80) == 0x80 ? P1_COMPRESSED_SZ
474-
: 2*P1_COMPRESSED_SZ))
490+
if (len == 0 || (len != ((inp[0]&0x80) == 0x80 ? P1_COMPRESSED_SZ
491+
: 2*P1_COMPRESSED_SZ) &&
492+
len != 128*1))
475493
throw new Exception(ERROR.BAD_ENCODING);
476-
ERROR err = blst_p1_deserialize(point, inp);
494+
ERROR err = len == 128*1 ? blst_p1_deserialize_eip2537(point, inp)
495+
: blst_p1_deserialize(point, inp);
477496
if (err != ERROR.SUCCESS)
478497
throw new Exception(err);
479498
blst_p1_from_affine(point, point);
@@ -483,6 +502,11 @@ public struct P1 {
483502
484503
public P1 dup() { return new P1(this); }
485504
public P1_Affine to_affine() { return new P1_Affine(this); }
505+
public byte[] serialize_eip2537()
506+
{ byte[] ret = new byte[128*1];
507+
blst_p1_serialize_eip2537(ret, point);
508+
return ret;
509+
}
486510
public byte[] serialize()
487511
{ byte[] ret = new byte[2*P1_COMPRESSED_SZ];
488512
blst_p1_serialize(ret, point);
@@ -789,7 +813,7 @@ if newer([here[-1], fname]):
789813
print("\n\n", file=fd)
790814
print(top, file=fd)
791815
print(middle, file=fd)
792-
print(re.sub(r'((?<!f)[pgPG])([12])', xchg_1vs2, middle), file=fd)
816+
print(re.sub(r'((?<![if])[pgPG\*])([12])', xchg_1vs2, middle), file=fd)
793817
print(bottom, file=fd)
794818
fd.close()
795819

bindings/c#/supranational.blst.cs

+60-12
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,13 @@ static extern ERROR blst_core_verify_pk_in_g2([In] long[] pk, [In] long[] sig,
344344
[In] byte[] dst, size_t dst_len,
345345
[In] byte[] aug, size_t aug_len);
346346

347+
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
348+
static extern ERROR blst_p1_deserialize_eip2537([Out] long[] ret,
349+
[In] byte[] inp);
350+
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
351+
static extern void blst_p1_affine_serialize_eip2537([Out] byte[] ret,
352+
[In] long[] inp);
353+
347354
public struct P1_Affine {
348355
internal readonly long[] point;
349356

@@ -355,10 +362,12 @@ public struct P1_Affine {
355362

356363
public P1_Affine(byte[] inp) : this(true)
357364
{ int len = inp.Length;
358-
if (len == 0 || len != ((inp[0]&0x80) == 0x80 ? P1_COMPRESSED_SZ
359-
: 2*P1_COMPRESSED_SZ))
365+
if (len == 0 || (len != ((inp[0]&0x80) == 0x80 ? P1_COMPRESSED_SZ
366+
: 2*P1_COMPRESSED_SZ) &&
367+
len != 128*1))
360368
throw new Exception(ERROR.BAD_ENCODING);
361-
ERROR err = blst_p1_deserialize(point, inp);
369+
ERROR err = len == 128*1 ? blst_p1_deserialize_eip2537(point, inp)
370+
: blst_p1_deserialize(point, inp);
362371
if (err != ERROR.SUCCESS)
363372
throw new Exception(err);
364373
}
@@ -367,6 +376,11 @@ public P1_Affine(P1 jacobian) : this(true)
367376

368377
public P1_Affine dup() { return new P1_Affine(this); }
369378
public P1 to_jacobian() { return new P1(this); }
379+
public byte[] serialize_eip2537()
380+
{ byte[] ret = new byte[128*1];
381+
blst_p1_affine_serialize_eip2537(ret, point);
382+
return ret;
383+
}
370384
public byte[] serialize()
371385
{ byte[] ret = new byte[2*P1_COMPRESSED_SZ];
372386
blst_p1_affine_serialize(ret, point);
@@ -451,6 +465,9 @@ void blst_p1_add_or_double_affine([Out] long[] ret, [In] long[] a,
451465
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
452466
static extern void blst_p1_double([Out] long[] ret, [In] long[] a);
453467

468+
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
469+
static extern void blst_p1_serialize_eip2537([Out] byte[] ret, [In] long[] inp);
470+
454471
public struct P1 {
455472
internal long[] point;
456473

@@ -466,10 +483,12 @@ public P1(SecretKey sk) : this(true)
466483
{ blst_sk_to_pk_in_g1(point, sk.key); }
467484
public P1(byte[] inp) : this(true)
468485
{ int len = inp.Length;
469-
if (len == 0 || len != ((inp[0]&0x80) == 0x80 ? P1_COMPRESSED_SZ
470-
: 2*P1_COMPRESSED_SZ))
486+
if (len == 0 || (len != ((inp[0]&0x80) == 0x80 ? P1_COMPRESSED_SZ
487+
: 2*P1_COMPRESSED_SZ) &&
488+
len != 128*1))
471489
throw new Exception(ERROR.BAD_ENCODING);
472-
ERROR err = blst_p1_deserialize(point, inp);
490+
ERROR err = len == 128*1 ? blst_p1_deserialize_eip2537(point, inp)
491+
: blst_p1_deserialize(point, inp);
473492
if (err != ERROR.SUCCESS)
474493
throw new Exception(err);
475494
blst_p1_from_affine(point, point);
@@ -479,6 +498,11 @@ public P1(P1_Affine affine) : this(true)
479498

480499
public P1 dup() { return new P1(this); }
481500
public P1_Affine to_affine() { return new P1_Affine(this); }
501+
public byte[] serialize_eip2537()
502+
{ byte[] ret = new byte[128*1];
503+
blst_p1_serialize_eip2537(ret, point);
504+
return ret;
505+
}
482506
public byte[] serialize()
483507
{ byte[] ret = new byte[2*P1_COMPRESSED_SZ];
484508
blst_p1_serialize(ret, point);
@@ -607,6 +631,13 @@ static extern ERROR blst_core_verify_pk_in_g1([In] long[] pk, [In] long[] sig,
607631
[In] byte[] dst, size_t dst_len,
608632
[In] byte[] aug, size_t aug_len);
609633

634+
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
635+
static extern ERROR blst_p2_deserialize_eip2537([Out] long[] ret,
636+
[In] byte[] inp);
637+
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
638+
static extern void blst_p2_affine_serialize_eip2537([Out] byte[] ret,
639+
[In] long[] inp);
640+
610641
public struct P2_Affine {
611642
internal readonly long[] point;
612643

@@ -618,10 +649,12 @@ public struct P2_Affine {
618649

619650
public P2_Affine(byte[] inp) : this(true)
620651
{ int len = inp.Length;
621-
if (len == 0 || len != ((inp[0]&0x80) == 0x80 ? P2_COMPRESSED_SZ
622-
: 2*P2_COMPRESSED_SZ))
652+
if (len == 0 || (len != ((inp[0]&0x80) == 0x80 ? P2_COMPRESSED_SZ
653+
: 2*P2_COMPRESSED_SZ) &&
654+
len != 128*2))
623655
throw new Exception(ERROR.BAD_ENCODING);
624-
ERROR err = blst_p2_deserialize(point, inp);
656+
ERROR err = len == 128*2 ? blst_p2_deserialize_eip2537(point, inp)
657+
: blst_p2_deserialize(point, inp);
625658
if (err != ERROR.SUCCESS)
626659
throw new Exception(err);
627660
}
@@ -630,6 +663,11 @@ public P2_Affine(P2 jacobian) : this(true)
630663

631664
public P2_Affine dup() { return new P2_Affine(this); }
632665
public P2 to_jacobian() { return new P2(this); }
666+
public byte[] serialize_eip2537()
667+
{ byte[] ret = new byte[128*2];
668+
blst_p2_affine_serialize_eip2537(ret, point);
669+
return ret;
670+
}
633671
public byte[] serialize()
634672
{ byte[] ret = new byte[2*P2_COMPRESSED_SZ];
635673
blst_p2_affine_serialize(ret, point);
@@ -714,6 +752,9 @@ void blst_p2_add_or_double_affine([Out] long[] ret, [In] long[] a,
714752
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
715753
static extern void blst_p2_double([Out] long[] ret, [In] long[] a);
716754

755+
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
756+
static extern void blst_p2_serialize_eip2537([Out] byte[] ret, [In] long[] inp);
757+
717758
public struct P2 {
718759
internal long[] point;
719760

@@ -729,10 +770,12 @@ public P2(SecretKey sk) : this(true)
729770
{ blst_sk_to_pk_in_g2(point, sk.key); }
730771
public P2(byte[] inp) : this(true)
731772
{ int len = inp.Length;
732-
if (len == 0 || len != ((inp[0]&0x80) == 0x80 ? P2_COMPRESSED_SZ
733-
: 2*P2_COMPRESSED_SZ))
773+
if (len == 0 || (len != ((inp[0]&0x80) == 0x80 ? P2_COMPRESSED_SZ
774+
: 2*P2_COMPRESSED_SZ) &&
775+
len != 128*2))
734776
throw new Exception(ERROR.BAD_ENCODING);
735-
ERROR err = blst_p2_deserialize(point, inp);
777+
ERROR err = len == 128*2 ? blst_p2_deserialize_eip2537(point, inp)
778+
: blst_p2_deserialize(point, inp);
736779
if (err != ERROR.SUCCESS)
737780
throw new Exception(err);
738781
blst_p2_from_affine(point, point);
@@ -742,6 +785,11 @@ public P2(P2_Affine affine) : this(true)
742785

743786
public P2 dup() { return new P2(this); }
744787
public P2_Affine to_affine() { return new P2_Affine(this); }
788+
public byte[] serialize_eip2537()
789+
{ byte[] ret = new byte[128*2];
790+
blst_p2_serialize_eip2537(ret, point);
791+
return ret;
792+
}
745793
public byte[] serialize()
746794
{ byte[] ret = new byte[2*P2_COMPRESSED_SZ];
747795
blst_p2_serialize(ret, point);

0 commit comments

Comments
 (0)