Skip to content

Commit 2c6bbec

Browse files
committed
bindings/blst.{hpp,swg}: add SWIG bindings for EIP-2537 serialization.
1 parent 20bbcc4 commit 2c6bbec

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

bindings/blst.hpp

+18-18
Original file line numberDiff line numberDiff line change
@@ -204,16 +204,19 @@ class P1_Affine {
204204
}
205205
#endif
206206
P1_Affine(const byte *in, size_t len)
207-
{ if (len == 0 || len != (in[0]&0x80 ? 48 : 96))
207+
{ if (len == 0 || (len != (in[0]&0x80 ? 48 : 96) && len != 128))
208208
throw BLST_BAD_ENCODING;
209-
BLST_ERROR err = blst_p1_deserialize(&point, in);
209+
BLST_ERROR err = len == 128 ? blst_p1_deserialize_eip2537(&point, in)
210+
: blst_p1_deserialize(&point, in);
210211
if (err != BLST_SUCCESS)
211212
throw err;
212213
}
213214
P1_Affine(const P1& jacobian);
214215

215216
P1_Affine dup() const { return *this; }
216217
P1 to_jacobian() const;
218+
void serialize_eip2537(byte out[128]) const
219+
{ blst_p1_affine_serialize_eip2537(out, &point); }
217220
void serialize(byte out[96]) const
218221
{ blst_p1_affine_serialize(out, &point); }
219222
void compress(byte out[48]) const
@@ -264,18 +267,15 @@ class P1 {
264267
}
265268
#endif
266269
P1(const byte *in, size_t len)
267-
{ if (len == 0 || len != (in[0]&0x80 ? 48 : 96))
268-
throw BLST_BAD_ENCODING;
269-
blst_p1_affine a;
270-
BLST_ERROR err = blst_p1_deserialize(&a, in);
271-
if (err != BLST_SUCCESS)
272-
throw err;
273-
blst_p1_from_affine(&point, &a);
270+
{ P1_Affine affine{in, len};
271+
blst_p1_from_affine(&point, &affine.point);
274272
}
275273
P1(const P1_Affine& affine) { blst_p1_from_affine(&point, affine); }
276274

277275
P1 dup() const { return *this; }
278276
P1_Affine to_affine() const { return P1_Affine(*this); }
277+
void serialize_eip2537(byte out[128]) const
278+
{ blst_p1_serialize_eip2537(out, &point); }
279279
void serialize(byte out[96]) const { blst_p1_serialize(out, &point); }
280280
void compress(byte out[48]) const { blst_p1_compress(out, &point); }
281281
bool on_curve() const { return blst_p1_on_curve(&point); }
@@ -502,16 +502,19 @@ class P2_Affine {
502502
}
503503
#endif
504504
P2_Affine(const byte *in, size_t len)
505-
{ if (len == 0 || len != (in[0]&0x80 ? 96 : 192))
505+
{ if (len == 0 || (len != (in[0]&0x80 ? 96 : 192) && len != 256))
506506
throw BLST_BAD_ENCODING;
507-
BLST_ERROR err = blst_p2_deserialize(&point, in);
507+
BLST_ERROR err = len == 256 ? blst_p2_deserialize_eip2537(&point, in)
508+
: blst_p2_deserialize(&point, in);
508509
if (err != BLST_SUCCESS)
509510
throw err;
510511
}
511512
P2_Affine(const P2& jacobian);
512513

513514
P2_Affine dup() const { return *this; }
514515
P2 to_jacobian() const;
516+
void serialize_eip2537(byte out[256]) const
517+
{ blst_p2_affine_serialize_eip2537(out, &point); }
515518
void serialize(byte out[192]) const
516519
{ blst_p2_affine_serialize(out, &point); }
517520
void compress(byte out[96]) const
@@ -562,18 +565,15 @@ class P2 {
562565
}
563566
#endif
564567
P2(const byte *in, size_t len)
565-
{ if (len == 0 || len != (in[0]&0x80 ? 96 : 192))
566-
throw BLST_BAD_ENCODING;
567-
blst_p2_affine a;
568-
BLST_ERROR err = blst_p2_deserialize(&a, in);
569-
if (err != BLST_SUCCESS)
570-
throw err;
571-
blst_p2_from_affine(&point, &a);
568+
{ P2_Affine affine{in, len};
569+
blst_p2_from_affine(&point, &affine.point);
572570
}
573571
P2(const P2_Affine& affine) { blst_p2_from_affine(&point, affine); }
574572

575573
P2 dup() const { return *this; }
576574
P2_Affine to_affine() const { return P2_Affine(*this); }
575+
void serialize_eip2537(byte out[256]) const
576+
{ blst_p2_serialize(out, &point); }
577577
void serialize(byte out[192]) const { blst_p2_serialize(out, &point); }
578578
void compress(byte out[96]) const { blst_p2_compress(out, &point); }
579579
bool on_curve() const { return blst_p2_on_curve(&point); }

bindings/blst.swg

+4
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,10 @@ import java.nio.file.*;
689689
void blst_p2_serialize, void blst_p2_affine_serialize,
690690
void compress, void blst_p1_compress, void blst_p1_affine_compress,
691691
void blst_p2_compress, void blst_p2_affine_compress,
692+
void serialize_eip2537, void blst_p1_serialize_eip2537,
693+
void blst_p2_serialize_eip2537,
694+
void blst_p1_affine_serialize_eip2537,
695+
void blst_p2_affine_serialize_eip2537,
692696
void blst_sk_to_pk2_in_g1, void blst_sign_pk2_in_g1,
693697
void blst_sk_to_pk2_in_g2, void blst_sign_pk2_in_g2
694698
}

0 commit comments

Comments
 (0)