Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EIP-2537 serialization. #223

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ jobs:
registry: https://index.docker.io/v1/
image: emscripten/emsdk
options: --volume ${{ github.workspace }}:/blst --network=none
run: git clone -q /blst /tmp/blst && /tmp/blst/bindings/emscripten/run.me -O2
run: set -x && git config --global safe.directory \* && git clone -q /blst /tmp/blst && /tmp/blst/bindings/emscripten/run.me -O2

- name: C#
run: |
Expand Down
36 changes: 18 additions & 18 deletions bindings/blst.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,19 @@ class P1_Affine {
}
#endif
P1_Affine(const byte *in, size_t len)
{ if (len == 0 || len != (in[0]&0x80 ? 48 : 96))
{ if (len == 0 || (len != (in[0]&0x80 ? 48 : 96) && len != 128))
throw BLST_BAD_ENCODING;
BLST_ERROR err = blst_p1_deserialize(&point, in);
BLST_ERROR err = len == 128 ? blst_p1_deserialize_eip2537(&point, in)
: blst_p1_deserialize(&point, in);
if (err != BLST_SUCCESS)
throw err;
}
P1_Affine(const P1& jacobian);

P1_Affine dup() const { return *this; }
P1 to_jacobian() const;
void serialize_eip2537(byte out[128]) const
{ blst_p1_affine_serialize_eip2537(out, &point); }
void serialize(byte out[96]) const
{ blst_p1_affine_serialize(out, &point); }
void compress(byte out[48]) const
Expand Down Expand Up @@ -264,18 +267,15 @@ class P1 {
}
#endif
P1(const byte *in, size_t len)
{ if (len == 0 || len != (in[0]&0x80 ? 48 : 96))
throw BLST_BAD_ENCODING;
blst_p1_affine a;
BLST_ERROR err = blst_p1_deserialize(&a, in);
if (err != BLST_SUCCESS)
throw err;
blst_p1_from_affine(&point, &a);
{ P1_Affine affine{in, len};
blst_p1_from_affine(&point, &affine.point);
}
P1(const P1_Affine& affine) { blst_p1_from_affine(&point, affine); }

P1 dup() const { return *this; }
P1_Affine to_affine() const { return P1_Affine(*this); }
void serialize_eip2537(byte out[128]) const
{ blst_p1_serialize_eip2537(out, &point); }
void serialize(byte out[96]) const { blst_p1_serialize(out, &point); }
void compress(byte out[48]) const { blst_p1_compress(out, &point); }
bool on_curve() const { return blst_p1_on_curve(&point); }
Expand Down Expand Up @@ -502,16 +502,19 @@ class P2_Affine {
}
#endif
P2_Affine(const byte *in, size_t len)
{ if (len == 0 || len != (in[0]&0x80 ? 96 : 192))
{ if (len == 0 || (len != (in[0]&0x80 ? 96 : 192) && len != 256))
throw BLST_BAD_ENCODING;
BLST_ERROR err = blst_p2_deserialize(&point, in);
BLST_ERROR err = len == 256 ? blst_p2_deserialize_eip2537(&point, in)
: blst_p2_deserialize(&point, in);
if (err != BLST_SUCCESS)
throw err;
}
P2_Affine(const P2& jacobian);

P2_Affine dup() const { return *this; }
P2 to_jacobian() const;
void serialize_eip2537(byte out[256]) const
{ blst_p2_affine_serialize_eip2537(out, &point); }
void serialize(byte out[192]) const
{ blst_p2_affine_serialize(out, &point); }
void compress(byte out[96]) const
Expand Down Expand Up @@ -562,18 +565,15 @@ class P2 {
}
#endif
P2(const byte *in, size_t len)
{ if (len == 0 || len != (in[0]&0x80 ? 96 : 192))
throw BLST_BAD_ENCODING;
blst_p2_affine a;
BLST_ERROR err = blst_p2_deserialize(&a, in);
if (err != BLST_SUCCESS)
throw err;
blst_p2_from_affine(&point, &a);
{ P2_Affine affine{in, len};
blst_p2_from_affine(&point, &affine.point);
}
P2(const P2_Affine& affine) { blst_p2_from_affine(&point, affine); }

P2 dup() const { return *this; }
P2_Affine to_affine() const { return P2_Affine(*this); }
void serialize_eip2537(byte out[256]) const
{ blst_p2_serialize(out, &point); }
void serialize(byte out[192]) const { blst_p2_serialize(out, &point); }
void compress(byte out[96]) const { blst_p2_compress(out, &point); }
bool on_curve() const { return blst_p2_on_curve(&point); }
Expand Down
4 changes: 4 additions & 0 deletions bindings/blst.swg
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,10 @@ import java.nio.file.*;
void blst_p2_serialize, void blst_p2_affine_serialize,
void compress, void blst_p1_compress, void blst_p1_affine_compress,
void blst_p2_compress, void blst_p2_affine_compress,
void serialize_eip2537, void blst_p1_serialize_eip2537,
void blst_p2_serialize_eip2537,
void blst_p1_affine_serialize_eip2537,
void blst_p2_affine_serialize_eip2537,
void blst_sk_to_pk2_in_g1, void blst_sign_pk2_in_g1,
void blst_sk_to_pk2_in_g2, void blst_sign_pk2_in_g2
}
Expand Down
8 changes: 8 additions & 0 deletions bindings/blst_aux.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ size_t blst_p2_sizeof(void);
size_t blst_p2_affine_sizeof(void);
size_t blst_fp12_sizeof(void);

void blst_p1_affine_serialize_eip2537(byte out[128], const blst_p1_affine *in);
void blst_p1_serialize_eip2537(byte out[128], const blst_p1 *in);
BLST_ERROR blst_p1_deserialize_eip2537(blst_p1_affine *out, const byte in[128]);

void blst_p2_affine_serialize_eip2537(byte out[256], const blst_p2_affine *in);
void blst_p2_serialize_eip2537(byte out[256], const blst_p2 *in);
BLST_ERROR blst_p2_deserialize_eip2537(blst_p2_affine *out, const byte in[256]);

/*
* Single-shot SHA-256 hash function.
*/
Expand Down
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
Loading