From 2c6bbec1570162a2804ae79add0d16bc9a7d1fba Mon Sep 17 00:00:00 2001 From: Andy Polyakov Date: Thu, 13 Jun 2024 10:34:42 +0200 Subject: [PATCH] bindings/blst.{hpp,swg}: add SWIG bindings for EIP-2537 serialization. --- bindings/blst.hpp | 36 ++++++++++++++++++------------------ bindings/blst.swg | 4 ++++ 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/bindings/blst.hpp b/bindings/blst.hpp index 127755be..8534ff7e 100644 --- a/bindings/blst.hpp +++ b/bindings/blst.hpp @@ -204,9 +204,10 @@ 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; } @@ -214,6 +215,8 @@ class P1_Affine { 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 @@ -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); } @@ -502,9 +502,10 @@ 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; } @@ -512,6 +513,8 @@ class P2_Affine { 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 @@ -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); } diff --git a/bindings/blst.swg b/bindings/blst.swg index 4cb9c30d..34eb27c4 100644 --- a/bindings/blst.swg +++ b/bindings/blst.swg @@ -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 }