Skip to content

Commit 6aa1fe7

Browse files
committed
bindings/blst.{hpp,swg}: make Java bindings work with swig 4.4.
1 parent b79e2bc commit 6aa1fe7

File tree

3 files changed

+122
-7
lines changed

3 files changed

+122
-7
lines changed

bindings/blst.hpp

Lines changed: 105 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ static const app__string_view None;
4343
# pragma GCC diagnostic pop
4444
#endif
4545

46+
#ifdef __BLST_BYTES_T__
47+
struct bytes_t {
48+
const byte* ptr;
49+
size_t len;
50+
51+
bytes_t() = default;
52+
bytes_t(const byte* p, size_t l) : ptr{p}, len{l} {}
53+
};
54+
#endif
55+
4656
class P1_Affine;
4757
class P1;
4858
class P2_Affine;
@@ -223,15 +233,21 @@ class P1_Affine {
223233
bool is_inf() const { return blst_p1_affine_is_inf(&point); }
224234
bool is_equal(const P1_Affine& p) const
225235
{ return blst_p1_affine_is_equal(&point, &p.point); }
236+
#ifdef __BLST_BYTES_T__
237+
BLST_ERROR core_verify(const P2_Affine& pk, bool hash_or_encode,
238+
bytes_t msg, const std::string& DST = "",
239+
bytes_t aug = {nullptr, 0}) const;
240+
#else
226241
BLST_ERROR core_verify(const P2_Affine& pk, bool hash_or_encode,
227242
const byte* msg, size_t msg_len,
228243
const std::string& DST = "",
229244
const byte* aug = nullptr, size_t aug_len = 0) const;
230-
#if __cplusplus >= 201703L
245+
# if __cplusplus >= 201703L
231246
BLST_ERROR core_verify(const P2_Affine& pk, bool hash_or_encode,
232247
const app__string_view msg,
233248
const std::string& DST = "",
234249
const app__string_view aug = None) const;
250+
# endif
235251
#endif
236252
static P1_Affine generator()
237253
{ return P1_Affine(blst_p1_affine_generator()); }
@@ -293,6 +309,20 @@ class P1 {
293309
{ blst_sign_pk_in_g2(&point, &point, &sk.key); return this; }
294310
P1* sign_with(const Scalar& scalar)
295311
{ blst_sign_pk_in_g2(&point, &point, scalar); return this; }
312+
#ifdef __BLST_BYTES_T__
313+
P1* hash_to(bytes_t msg, const std::string& DST = "",
314+
bytes_t aug = {nullptr, 0})
315+
{ blst_hash_to_g1(&point, msg.ptr, msg.len, C_bytes(DST.data()), DST.size(),
316+
aug.ptr, aug.len);
317+
return this;
318+
}
319+
P1* encode_to(bytes_t msg, const std::string& DST = "",
320+
bytes_t aug = {nullptr, 0})
321+
{ blst_encode_to_g1(&point, msg.ptr, msg.len, C_bytes(DST.data()), DST.size(),
322+
aug.ptr, aug.len);
323+
return this;
324+
}
325+
#else
296326
P1* hash_to(const byte* msg, size_t msg_len,
297327
const std::string& DST = "",
298328
const byte* aug = nullptr, size_t aug_len = 0)
@@ -307,7 +337,7 @@ class P1 {
307337
aug, aug_len);
308338
return this;
309339
}
310-
#if __cplusplus >= 201703L
340+
# if __cplusplus >= 201703L
311341
P1* hash_to(const app__string_view msg, const std::string& DST = "",
312342
const app__string_view aug = None)
313343
{ return hash_to(C_bytes(msg.data()), msg.size(), DST,
@@ -318,6 +348,7 @@ class P1 {
318348
{ return encode_to(C_bytes(msg.data()), msg.size(), DST,
319349
C_bytes(aug.data()), aug.size());
320350
}
351+
# endif
321352
#endif
322353
P1* mult(const byte* scalar, size_t nbits)
323354
{ blst_p1_mult(&point, &point, scalar, nbits); return this; }
@@ -521,15 +552,21 @@ class P2_Affine {
521552
bool is_inf() const { return blst_p2_affine_is_inf(&point); }
522553
bool is_equal(const P2_Affine& p) const
523554
{ return blst_p2_affine_is_equal(&point, &p.point); }
555+
#ifdef __BLST_BYTES_T__
556+
BLST_ERROR core_verify(const P1_Affine& pk, bool hash_or_encode,
557+
bytes_t msg, const std::string& DST = "",
558+
bytes_t aug = {nullptr, 0}) const;
559+
#else
524560
BLST_ERROR core_verify(const P1_Affine& pk, bool hash_or_encode,
525561
const byte* msg, size_t msg_len,
526562
const std::string& DST = "",
527563
const byte* aug = nullptr, size_t aug_len = 0) const;
528-
#if __cplusplus >= 201703L
564+
# if __cplusplus >= 201703L
529565
BLST_ERROR core_verify(const P1_Affine& pk, bool hash_or_encode,
530566
const app__string_view msg,
531567
const std::string& DST = "",
532568
const app__string_view aug = None) const;
569+
# endif
533570
#endif
534571
static P2_Affine generator()
535572
{ return P2_Affine(blst_p2_affine_generator()); }
@@ -591,6 +628,21 @@ class P2 {
591628
{ blst_sign_pk_in_g1(&point, &point, &sk.key); return this; }
592629
P2* sign_with(const Scalar& scalar)
593630
{ blst_sign_pk_in_g1(&point, &point, scalar); return this; }
631+
#ifdef __BLST_BYTES_T__
632+
P2* hash_to(const byte* msg, size_t msg_len,
633+
const std::string& DST = "",
634+
bytes_t aug = {nullptr, 0})
635+
{ blst_hash_to_g2(&point, msg, msg_len, C_bytes(DST.data()), DST.size(),
636+
aug.ptr, aug.len);
637+
return this;
638+
}
639+
P2* encode_to(bytes_t msg, const std::string& DST = "",
640+
bytes_t aug = {nullptr, 0})
641+
{ blst_encode_to_g2(&point, msg.ptr, msg.len, C_bytes(DST.data()), DST.size(),
642+
aug.ptr, aug.len);
643+
return this;
644+
}
645+
#else
594646
P2* hash_to(const byte* msg, size_t msg_len,
595647
const std::string& DST = "",
596648
const byte* aug = nullptr, size_t aug_len = 0)
@@ -605,7 +657,7 @@ class P2 {
605657
aug, aug_len);
606658
return this;
607659
}
608-
#if __cplusplus >= 201703L
660+
# if __cplusplus >= 201703L
609661
P2* hash_to(const app__string_view msg, const std::string& DST = "",
610662
const app__string_view aug = None)
611663
{ return hash_to(C_bytes(msg.data()), msg.size(), DST,
@@ -616,6 +668,7 @@ class P2 {
616668
{ return encode_to(C_bytes(msg.data()), msg.size(), DST,
617669
C_bytes(aug.data()), aug.size());
618670
}
671+
# endif
619672
#endif
620673
P2* mult(const byte* scalar, size_t nbits)
621674
{ blst_p2_mult(&point, &point, scalar, nbits); return this; }
@@ -796,6 +849,26 @@ inline P2 P2_Affine::to_jacobian() const { P2 ret(*this); return ret; }
796849
inline P1 G1() { return P1::generator(); }
797850
inline P2 G2() { return P2::generator(); }
798851

852+
#ifdef __BLST_BYTES_T__
853+
inline BLST_ERROR P1_Affine::core_verify(const P2_Affine& pk,
854+
bool hash_or_encode,
855+
bytes_t msg, const std::string& DST,
856+
bytes_t aug) const
857+
{ return blst_core_verify_pk_in_g2(pk, &point, hash_or_encode,
858+
msg.ptr, msg.len,
859+
C_bytes(DST.data()), DST.size(),
860+
aug.ptr, aug.len);
861+
}
862+
inline BLST_ERROR P2_Affine::core_verify(const P1_Affine& pk,
863+
bool hash_or_encode,
864+
bytes_t msg, const std::string& DST,
865+
bytes_t aug) const
866+
{ return blst_core_verify_pk_in_g1(pk, &point, hash_or_encode,
867+
msg.ptr, msg.len,
868+
C_bytes(DST.data()), DST.size(),
869+
aug.ptr, aug.len);
870+
}
871+
#else
799872
inline BLST_ERROR P1_Affine::core_verify(const P2_Affine& pk,
800873
bool hash_or_encode,
801874
const byte* msg, size_t msg_len,
@@ -816,7 +889,7 @@ inline BLST_ERROR P2_Affine::core_verify(const P1_Affine& pk,
816889
C_bytes(DST.data()), DST.size(),
817890
aug, aug_len);
818891
}
819-
#if __cplusplus >= 201703L
892+
# if __cplusplus >= 201703L
820893
inline BLST_ERROR P1_Affine::core_verify(const P2_Affine& pk,
821894
bool hash_or_encode,
822895
const app__string_view msg,
@@ -833,6 +906,7 @@ inline BLST_ERROR P2_Affine::core_verify(const P1_Affine& pk,
833906
{ return core_verify(pk, hash_or_encode, C_bytes(msg.data()), msg.size(), DST,
834907
C_bytes(aug.data()), aug.size());
835908
}
909+
# endif
836910
#endif
837911

838912
class PT {
@@ -904,6 +978,30 @@ class Pairing {
904978
~Pairing() { delete[] blst_pairing_get_dst(*this); }
905979
#endif
906980

981+
#ifdef __BLST_BYTES_T__
982+
BLST_ERROR aggregate(const P1_Affine* pk, const P2_Affine* sig,
983+
bytes_t msg, bytes_t aug = {nullptr, 0})
984+
{ return blst_pairing_aggregate_pk_in_g1(*this, *pk, *sig,
985+
msg.ptr, msg.len, aug.ptr, aug.len);
986+
}
987+
BLST_ERROR aggregate(const P2_Affine* pk, const P1_Affine* sig,
988+
bytes_t msg, bytes_t aug = {nullptr, 0})
989+
{ return blst_pairing_aggregate_pk_in_g2(*this, *pk, *sig,
990+
msg.ptr, msg.len, aug.ptr, aug.len);
991+
}
992+
BLST_ERROR mul_n_aggregate(const P1_Affine* pk, const P2_Affine* sig,
993+
const byte* scalar, size_t nbits,
994+
bytes_t msg, bytes_t aug = {nullptr, 0})
995+
{ return blst_pairing_mul_n_aggregate_pk_in_g1(*this, *pk, *sig,
996+
scalar, nbits, msg.ptr, msg.len, aug.ptr, aug.len);
997+
}
998+
BLST_ERROR mul_n_aggregate(const P2_Affine* pk, const P1_Affine* sig,
999+
const byte* scalar, size_t nbits,
1000+
bytes_t msg, bytes_t aug = {nullptr, 0})
1001+
{ return blst_pairing_mul_n_aggregate_pk_in_g2(*this, *pk, *sig,
1002+
scalar, nbits, msg.ptr, msg.len, aug.ptr, aug.len);
1003+
}
1004+
#else
9071005
BLST_ERROR aggregate(const P1_Affine* pk, const P2_Affine* sig,
9081006
const byte* msg, size_t msg_len,
9091007
const byte* aug = nullptr, size_t aug_len = 0)
@@ -930,7 +1028,7 @@ class Pairing {
9301028
{ return blst_pairing_mul_n_aggregate_pk_in_g2(*this, *pk, *sig,
9311029
scalar, nbits, msg, msg_len, aug, aug_len);
9321030
}
933-
#if __cplusplus >= 201703L
1031+
# if __cplusplus >= 201703L
9341032
BLST_ERROR aggregate(const P1_Affine* pk, const P2_Affine* sig,
9351033
const app__string_view msg,
9361034
const app__string_view aug = None)
@@ -959,6 +1057,7 @@ class Pairing {
9591057
C_bytes(msg.data()), msg.size(),
9601058
C_bytes(aug.data()), aug.size());
9611059
}
1060+
# endif
9621061
#endif
9631062
void commit()
9641063
{ blst_pairing_commit(*this); }

bindings/blst.swg

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,19 @@ import java.nio.file.*;
548548
%typemap(javain) (const byte *STRING, size_t LENGTH) "$javainput"
549549
%typemap(freearg)(const byte *STRING, size_t LENGTH) ""
550550

551+
%typemap(jni) blst::bytes_t "jbyteArray"
552+
%typemap(jtype) blst::bytes_t "byte[]"
553+
%typemap(jstype) blst::bytes_t "byte[]"
554+
%typemap(javain) blst::bytes_t "$javainput"
555+
%typemap(freearg)blst::bytes_t ""
556+
%typemap(in) blst::bytes_t %{
557+
$1.ptr = (const byte*)JCALL(GetByteArrayElements, $input, 0);
558+
$1.len = JCALL(GetArrayLength, $input);
559+
%}
560+
%typemap(argout) blst::bytes_t %{
561+
JCALL(ReleaseByteArrayElements, $input, (jbyte *)$1.ptr, JNI_ABORT);
562+
%}
563+
551564
#elif defined(SWIGJAVASCRIPT) && defined(SWIG_JAVASCRIPT_V8)
552565

553566
%header %{
@@ -731,6 +744,8 @@ import java.nio.file.*;
731744
%ignore nullptr;
732745
%ignore None;
733746
%ignore C_bytes;
747+
%ignore bytes_t;
748+
%feature("novaluewrapper") bytes_t;
734749
%catches(BLST_ERROR) P1(const byte* in, size_t len);
735750
%catches(BLST_ERROR) P1_Affine(const byte* in, size_t len);
736751
%catches(BLST_ERROR) aggregate(const P1_Affine& in);

bindings/java/build.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ if [ ! -f blst_wrap.cpp -o "$TOP"/blst.swg -nt blst_wrap.cpp \
4545
-o "$TOP"/blst.h -nt blst_wrap.cpp \
4646
-o ! -f $PKG/blst.java ]; then
4747
(set -x; swig -c++ -java -package $JAVA_PACKAGE -outdir $PKG \
48-
-o blst_wrap.cpp "$TOP"/blst.swg)
48+
-D__BLST_BYTES_T__ -o blst_wrap.cpp "$TOP"/blst.swg)
4949
fi
5050

5151
if [ ! -f $PKG/blst.class -o $PKG/blst.java -nt $PKG/blst.class ]; then
@@ -77,6 +77,7 @@ if [ ! -f $SO_NAME -o blst_wrap.cpp -nt $SO_NAME \
7777
awk '{ if($2=="__cplusplus" && $3<"2011") print "-std=c++11"; }'`
7878
(set -x; ${CXX} ${STD} -shared -o $SO_NAME -fPIC -fvisibility=hidden \
7979
-I"$JAVA_HOME"/include -I"$JNI_MD" -I"$TOP" \
80+
-D__BLST_BYTES_T__ \
8081
-O -Wall -Wno-unused-function blst_wrap.cpp \
8182
$LIBBLST_A ${LDFLAGS})
8283
fi

0 commit comments

Comments
 (0)