Skip to content

Commit

Permalink
bindings/c#: add EIP-2537 serialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
dot-asm committed Jun 13, 2024
1 parent 2df8627 commit 14da1a1
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 19 deletions.
38 changes: 31 additions & 7 deletions bindings/c#/run.me
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,13 @@ static extern ERROR blst_core_verify_pk_in_g2([In] long[] pk, [In] long[] sig,
[In] byte[] dst, size_t dst_len,
[In] byte[] aug, size_t aug_len);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern ERROR blst_p1_deserialize_eip2537([Out] long[] ret,
[In] byte[] inp);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p1_affine_serialize_eip2537([Out] byte[] ret,
[In] long[] inp);
public struct P1_Affine {
internal readonly long[] point;
Expand All @@ -359,10 +366,12 @@ public struct P1_Affine {
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))
if (len == 0 || (len != ((inp[0]&0x80) == 0x80 ? P1_COMPRESSED_SZ
: 2*P1_COMPRESSED_SZ) &&
len != 128*1))
throw new Exception(ERROR.BAD_ENCODING);
ERROR err = blst_p1_deserialize(point, inp);
ERROR err = len == 128*1 ? blst_p1_deserialize_eip2537(point, inp)
: blst_p1_deserialize(point, inp);
if (err != ERROR.SUCCESS)
throw new Exception(err);
}
Expand All @@ -371,6 +380,11 @@ public struct P1_Affine {
public P1_Affine dup() { return new P1_Affine(this); }
public P1 to_jacobian() { return new P1(this); }
public byte[] serialize_eip2537()
{ byte[] ret = new byte[128*1];
blst_p1_affine_serialize_eip2537(ret, point);
return ret;
}
public byte[] serialize()
{ byte[] ret = new byte[2*P1_COMPRESSED_SZ];
blst_p1_affine_serialize(ret, point);
Expand Down Expand Up @@ -455,6 +469,9 @@ void blst_p1_add_or_double_affine([Out] long[] ret, [In] long[] a,
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p1_double([Out] long[] ret, [In] long[] a);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p1_serialize_eip2537([Out] byte[] ret, [In] long[] inp);
public struct P1 {
internal long[] point;
Expand All @@ -470,10 +487,12 @@ public struct P1 {
{ 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))
if (len == 0 || (len != ((inp[0]&0x80) == 0x80 ? P1_COMPRESSED_SZ
: 2*P1_COMPRESSED_SZ) &&
len != 128*1))
throw new Exception(ERROR.BAD_ENCODING);
ERROR err = blst_p1_deserialize(point, inp);
ERROR err = len == 128*1 ? blst_p1_deserialize_eip2537(point, inp)
: blst_p1_deserialize(point, inp);
if (err != ERROR.SUCCESS)
throw new Exception(err);
blst_p1_from_affine(point, point);
Expand All @@ -483,6 +502,11 @@ public struct P1 {
public P1 dup() { return new P1(this); }
public P1_Affine to_affine() { return new P1_Affine(this); }
public byte[] serialize_eip2537()
{ byte[] ret = new byte[128*1];
blst_p1_serialize_eip2537(ret, point);
return ret;
}
public byte[] serialize()
{ byte[] ret = new byte[2*P1_COMPRESSED_SZ];
blst_p1_serialize(ret, point);
Expand Down Expand Up @@ -789,7 +813,7 @@ if newer([here[-1], fname]):
print("\n\n", file=fd)
print(top, file=fd)
print(middle, file=fd)
print(re.sub(r'((?<!f)[pgPG])([12])', xchg_1vs2, middle), file=fd)
print(re.sub(r'((?<![if])[pgPG\*])([12])', xchg_1vs2, middle), file=fd)
print(bottom, file=fd)
fd.close()

Expand Down
72 changes: 60 additions & 12 deletions bindings/c#/supranational.blst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ static extern ERROR blst_core_verify_pk_in_g2([In] long[] pk, [In] long[] sig,
[In] byte[] dst, size_t dst_len,
[In] byte[] aug, size_t aug_len);

[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern ERROR blst_p1_deserialize_eip2537([Out] long[] ret,
[In] byte[] inp);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p1_affine_serialize_eip2537([Out] byte[] ret,
[In] long[] inp);

public struct P1_Affine {
internal readonly long[] point;

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

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))
if (len == 0 || (len != ((inp[0]&0x80) == 0x80 ? P1_COMPRESSED_SZ
: 2*P1_COMPRESSED_SZ) &&
len != 128*1))
throw new Exception(ERROR.BAD_ENCODING);
ERROR err = blst_p1_deserialize(point, inp);
ERROR err = len == 128*1 ? blst_p1_deserialize_eip2537(point, inp)
: blst_p1_deserialize(point, inp);
if (err != ERROR.SUCCESS)
throw new Exception(err);
}
Expand All @@ -367,6 +376,11 @@ public P1_Affine(P1 jacobian) : this(true)

public P1_Affine dup() { return new P1_Affine(this); }
public P1 to_jacobian() { return new P1(this); }
public byte[] serialize_eip2537()
{ byte[] ret = new byte[128*1];
blst_p1_affine_serialize_eip2537(ret, point);
return ret;
}
public byte[] serialize()
{ byte[] ret = new byte[2*P1_COMPRESSED_SZ];
blst_p1_affine_serialize(ret, point);
Expand Down Expand Up @@ -451,6 +465,9 @@ void blst_p1_add_or_double_affine([Out] long[] ret, [In] long[] a,
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p1_double([Out] long[] ret, [In] long[] a);

[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p1_serialize_eip2537([Out] byte[] ret, [In] long[] inp);

public struct P1 {
internal long[] point;

Expand All @@ -466,10 +483,12 @@ 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))
if (len == 0 || (len != ((inp[0]&0x80) == 0x80 ? P1_COMPRESSED_SZ
: 2*P1_COMPRESSED_SZ) &&
len != 128*1))
throw new Exception(ERROR.BAD_ENCODING);
ERROR err = blst_p1_deserialize(point, inp);
ERROR err = len == 128*1 ? blst_p1_deserialize_eip2537(point, inp)
: blst_p1_deserialize(point, inp);
if (err != ERROR.SUCCESS)
throw new Exception(err);
blst_p1_from_affine(point, point);
Expand All @@ -479,6 +498,11 @@ public P1(P1_Affine affine) : this(true)

public P1 dup() { return new P1(this); }
public P1_Affine to_affine() { return new P1_Affine(this); }
public byte[] serialize_eip2537()
{ byte[] ret = new byte[128*1];
blst_p1_serialize_eip2537(ret, point);
return ret;
}
public byte[] serialize()
{ byte[] ret = new byte[2*P1_COMPRESSED_SZ];
blst_p1_serialize(ret, point);
Expand Down Expand Up @@ -607,6 +631,13 @@ static extern ERROR blst_core_verify_pk_in_g1([In] long[] pk, [In] long[] sig,
[In] byte[] dst, size_t dst_len,
[In] byte[] aug, size_t aug_len);

[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern ERROR blst_p2_deserialize_eip2537([Out] long[] ret,
[In] byte[] inp);
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p2_affine_serialize_eip2537([Out] byte[] ret,
[In] long[] inp);

public struct P2_Affine {
internal readonly long[] point;

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

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))
if (len == 0 || (len != ((inp[0]&0x80) == 0x80 ? P2_COMPRESSED_SZ
: 2*P2_COMPRESSED_SZ) &&
len != 128*2))
throw new Exception(ERROR.BAD_ENCODING);
ERROR err = blst_p2_deserialize(point, inp);
ERROR err = len == 128*2 ? blst_p2_deserialize_eip2537(point, inp)
: blst_p2_deserialize(point, inp);
if (err != ERROR.SUCCESS)
throw new Exception(err);
}
Expand All @@ -630,6 +663,11 @@ public P2_Affine(P2 jacobian) : this(true)

public P2_Affine dup() { return new P2_Affine(this); }
public P2 to_jacobian() { return new P2(this); }
public byte[] serialize_eip2537()
{ byte[] ret = new byte[128*2];
blst_p2_affine_serialize_eip2537(ret, point);
return ret;
}
public byte[] serialize()
{ byte[] ret = new byte[2*P2_COMPRESSED_SZ];
blst_p2_affine_serialize(ret, point);
Expand Down Expand Up @@ -714,6 +752,9 @@ void blst_p2_add_or_double_affine([Out] long[] ret, [In] long[] a,
[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p2_double([Out] long[] ret, [In] long[] a);

[DllImport("blst.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void blst_p2_serialize_eip2537([Out] byte[] ret, [In] long[] inp);

public struct P2 {
internal long[] point;

Expand All @@ -729,10 +770,12 @@ 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))
if (len == 0 || (len != ((inp[0]&0x80) == 0x80 ? P2_COMPRESSED_SZ
: 2*P2_COMPRESSED_SZ) &&
len != 128*2))
throw new Exception(ERROR.BAD_ENCODING);
ERROR err = blst_p2_deserialize(point, inp);
ERROR err = len == 128*2 ? blst_p2_deserialize_eip2537(point, inp)
: blst_p2_deserialize(point, inp);
if (err != ERROR.SUCCESS)
throw new Exception(err);
blst_p2_from_affine(point, point);
Expand All @@ -742,6 +785,11 @@ public P2(P2_Affine affine) : this(true)

public P2 dup() { return new P2(this); }
public P2_Affine to_affine() { return new P2_Affine(this); }
public byte[] serialize_eip2537()
{ byte[] ret = new byte[128*2];
blst_p2_serialize_eip2537(ret, point);
return ret;
}
public byte[] serialize()
{ byte[] ret = new byte[2*P2_COMPRESSED_SZ];
blst_p2_serialize(ret, point);
Expand Down

0 comments on commit 14da1a1

Please sign in to comment.