diff --git a/ffi/java/MclTest.java b/ffi/java/MclTest.java index d8ed0fb..ba8b4c8 100644 --- a/ffi/java/MclTest.java +++ b/ffi/java/MclTest.java @@ -11,11 +11,13 @@ public class MclTest { System.out.println("libName : " + libName); System.loadLibrary(lib); } + public static int errN = 0; public static void assertEquals(String msg, String x, String y) { if (x.equals(y)) { System.out.println("OK : " + msg); } else { System.out.println("NG : " + msg + ", x = " + x + ", y = " + y); + errN++; } } public static void assertBool(String msg, boolean b) { @@ -23,6 +25,7 @@ public class MclTest { System.out.println("OK : " + msg); } else { System.out.println("NG : " + msg); + errN++; } } public static void testCurve(int curveType, String name) { @@ -43,14 +46,12 @@ public class MclTest { assertEquals("x == 18", (new Fr("12", 16)).toString(), "18"); assertEquals("x == ff", (new Fr("255")).toString(16), "ff"); -/* { byte[] b = x.serialize(); Fr t = new Fr(); t.deserialize(b); - assertEquals("serialize", x, t); + assertBool("serialize", x.equals(t)); } -*/ G1 P = new G1(); System.out.println("P=" + P); Mcl.hashAndMapToG1(P, "test".getBytes()); @@ -60,6 +61,12 @@ public class MclTest { System.out.println("P=" + P); Mcl.neg(P, P); System.out.println("P=" + P); + { + byte[] b = P.serialize(); + G1 t = new G1(); + t.deserialize(b); + assertBool("serialize", P.equals(t)); + } G2 Q = new G2(); Mcl.hashAndMapToG2(Q, "abc".getBytes()); @@ -72,6 +79,12 @@ public class MclTest { P1.setStr(s); assertBool("P == P1", P1.equals(P)); } + { + byte[] b = Q.serialize(); + G2 t = new G2(); + t.deserialize(b); + assertBool("serialize", Q.equals(t)); + } GT e = new GT(); Mcl.pairing(e, P, Q); @@ -84,13 +97,23 @@ public class MclTest { Mcl.pairing(e1, P, cQ); Mcl.pow(e2, e, c); // e2 = e^c assertBool("e1 == e2", e1.equals(e2)); - + { + byte[] b = e1.serialize(); + GT t = new GT(); + t.deserialize(b); + assertBool("serialize", e1.equals(t)); + } G1 cP = new G1(P); Mcl.mul(cP, P, c); // cP = P * c Mcl.pairing(e1, cP, Q); assertBool("e1 == e2", e1.equals(e2)); BLSsignature(Q); + if (errN == 0) { + System.out.println("all test passed"); + } else { + System.out.println("ERR=" + errN); + } } catch (RuntimeException e) { System.out.println("unknown exception :" + e); } diff --git a/ffi/java/com/herumi/mcl/Fp.java b/ffi/java/com/herumi/mcl/Fp.java index 93b9732..aa4d1e8 100644 --- a/ffi/java/com/herumi/mcl/Fp.java +++ b/ffi/java/com/herumi/mcl/Fp.java @@ -91,4 +91,6 @@ public class Fp { MclJNI.Fp_deserialize(swigCPtr, this, cbuf); } + public byte[] serialize() { return MclJNI.Fp_serialize(swigCPtr, this); } + } diff --git a/ffi/java/com/herumi/mcl/Fr.java b/ffi/java/com/herumi/mcl/Fr.java index a06a97a..2127f9d 100644 --- a/ffi/java/com/herumi/mcl/Fr.java +++ b/ffi/java/com/herumi/mcl/Fr.java @@ -91,4 +91,6 @@ public class Fr { MclJNI.Fr_deserialize(swigCPtr, this, cbuf); } + public byte[] serialize() { return MclJNI.Fr_serialize(swigCPtr, this); } + } diff --git a/ffi/java/com/herumi/mcl/G1.java b/ffi/java/com/herumi/mcl/G1.java index 146d904..ec640b6 100644 --- a/ffi/java/com/herumi/mcl/G1.java +++ b/ffi/java/com/herumi/mcl/G1.java @@ -79,4 +79,6 @@ public class G1 { MclJNI.G1_deserialize(swigCPtr, this, cbuf); } + public byte[] serialize() { return MclJNI.G1_serialize(swigCPtr, this); } + } diff --git a/ffi/java/com/herumi/mcl/G2.java b/ffi/java/com/herumi/mcl/G2.java index 5aded6d..2480dd3 100644 --- a/ffi/java/com/herumi/mcl/G2.java +++ b/ffi/java/com/herumi/mcl/G2.java @@ -79,4 +79,6 @@ public class G2 { MclJNI.G2_deserialize(swigCPtr, this, cbuf); } + public byte[] serialize() { return MclJNI.G2_serialize(swigCPtr, this); } + } diff --git a/ffi/java/com/herumi/mcl/GT.java b/ffi/java/com/herumi/mcl/GT.java index 187af70..fdc590d 100644 --- a/ffi/java/com/herumi/mcl/GT.java +++ b/ffi/java/com/herumi/mcl/GT.java @@ -67,4 +67,10 @@ public class GT { return MclJNI.GT_toString__SWIG_1(swigCPtr, this); } + public void deserialize(byte[] cbuf) { + MclJNI.GT_deserialize(swigCPtr, this, cbuf); + } + + public byte[] serialize() { return MclJNI.GT_serialize(swigCPtr, this); } + } diff --git a/ffi/java/com/herumi/mcl/MclJNI.java b/ffi/java/com/herumi/mcl/MclJNI.java index 7181f50..9e7c008 100644 --- a/ffi/java/com/herumi/mcl/MclJNI.java +++ b/ffi/java/com/herumi/mcl/MclJNI.java @@ -32,6 +32,7 @@ public class MclJNI { public final static native String Fr_toString__SWIG_0(long jarg1, Fr jarg1_, int jarg2); public final static native String Fr_toString__SWIG_1(long jarg1, Fr jarg1_); public final static native void Fr_deserialize(long jarg1, Fr jarg1_, byte[] jarg2); + public final static native byte[] Fr_serialize(long jarg1, Fr jarg1_); public final static native void delete_Fr(long jarg1); public final static native void neg__SWIG_1(long jarg1, Fp jarg1_, long jarg2, Fp jarg2_); public final static native void add__SWIG_1(long jarg1, Fp jarg1_, long jarg2, Fp jarg2_, long jarg3, Fp jarg3_); @@ -55,6 +56,7 @@ public class MclJNI { public final static native String Fp_toString__SWIG_0(long jarg1, Fp jarg1_, int jarg2); public final static native String Fp_toString__SWIG_1(long jarg1, Fp jarg1_); public final static native void Fp_deserialize(long jarg1, Fp jarg1_, byte[] jarg2); + public final static native byte[] Fp_serialize(long jarg1, Fp jarg1_); public final static native void delete_Fp(long jarg1); public final static native void neg__SWIG_2(long jarg1, G1 jarg1_, long jarg2, G1 jarg2_); public final static native void dbl__SWIG_0(long jarg1, G1 jarg1_, long jarg2, G1 jarg2_); @@ -73,6 +75,7 @@ public class MclJNI { public final static native String G1_toString__SWIG_0(long jarg1, G1 jarg1_, int jarg2); public final static native String G1_toString__SWIG_1(long jarg1, G1 jarg1_); public final static native void G1_deserialize(long jarg1, G1 jarg1_, byte[] jarg2); + public final static native byte[] G1_serialize(long jarg1, G1 jarg1_); public final static native void delete_G1(long jarg1); public final static native void neg__SWIG_3(long jarg1, G2 jarg1_, long jarg2, G2 jarg2_); public final static native void dbl__SWIG_1(long jarg1, G2 jarg1_, long jarg2, G2 jarg2_); @@ -90,6 +93,7 @@ public class MclJNI { public final static native String G2_toString__SWIG_0(long jarg1, G2 jarg1_, int jarg2); public final static native String G2_toString__SWIG_1(long jarg1, G2 jarg1_); public final static native void G2_deserialize(long jarg1, G2 jarg1_, byte[] jarg2); + public final static native byte[] G2_serialize(long jarg1, G2 jarg1_); public final static native void delete_G2(long jarg1); public final static native void mul__SWIG_6(long jarg1, GT jarg1_, long jarg2, GT jarg2_, long jarg3, GT jarg3_); public final static native long new_GT__SWIG_0(); @@ -100,5 +104,7 @@ public class MclJNI { public final static native void GT_setStr__SWIG_1(long jarg1, GT jarg1_, String jarg2); public final static native String GT_toString__SWIG_0(long jarg1, GT jarg1_, int jarg2); public final static native String GT_toString__SWIG_1(long jarg1, GT jarg1_); + public final static native void GT_deserialize(long jarg1, GT jarg1_, byte[] jarg2); + public final static native byte[] GT_serialize(long jarg1, GT jarg1_); public final static native void delete_GT(long jarg1); } diff --git a/ffi/java/mcl.i b/ffi/java/mcl.i index 6649ca7..1b1c1cd 100644 --- a/ffi/java/mcl.i +++ b/ffi/java/mcl.i @@ -11,18 +11,19 @@ %} -%include "mcl_impl.hpp" - -%javaconst(1); -#define BN254 0 -#define BLS12_381 5 - %typemap(jtype) void serialize "byte[]" %typemap(jstype) void serialize "byte[]" %typemap(jni) void serialize "jbyteArray" %typemap(javaout) void serialize { return $jnicall; } -%typemap(in, numinputs=0) std::string& out (std::string temp) "$1=&temp;" +%typemap(in, numinputs=0) std::string& out (std::string buf) "$1=&buf;" %typemap(argout) std::string& out { $result = JCALL1(NewByteArray, jenv, $1->size()); JCALL4(SetByteArrayRegion, jenv, $result, 0, $1->size(), (const jbyte*)$1->c_str()); } + +%include "mcl_impl.hpp" + +%javaconst(1); +#define BN254 0 +#define BLS12_381 5 + diff --git a/ffi/java/mcl_impl.hpp b/ffi/java/mcl_impl.hpp index 8edb656..76f4ff9 100644 --- a/ffi/java/mcl_impl.hpp +++ b/ffi/java/mcl_impl.hpp @@ -15,6 +15,23 @@ void SystemInit(int curveType) throw(std::exception) mcl::bn::initPairing(cp); } +template +void deserializeT(T& x, const char *cbuf, size_t bufSize) +{ + if (x.deserialize(cbuf, bufSize) == 0) { + throw std::runtime_error("deserialize"); + } +} + +template +void serializeT(std::string& out, const T& x) +{ + out.resize(48 * 12); + size_t n = x.serialize(&out[0], out.size()); + if (n == 0) throw std::runtime_error("serializeT"); + out.resize(n); +} + class G1; class G2; class GT; @@ -63,9 +80,11 @@ public: } void deserialize(const char *cbuf, size_t bufSize) throw(std::exception) { - if (self_.deserialize(cbuf, bufSize) == 0) { - throw std::runtime_error("deserialize"); - } + deserializeT(self_, cbuf, bufSize); + } + void serialize(std::string& out) const throw(std::exception) + { + serializeT(out, self_); } }; @@ -136,9 +155,11 @@ public: } void deserialize(const char *cbuf, size_t bufSize) throw(std::exception) { - if (self_.deserialize(cbuf, bufSize) == 0) { - throw std::runtime_error("deserialize"); - } + deserializeT(self_, cbuf, bufSize); + } + void serialize(std::string& out) const throw(std::exception) + { + serializeT(out, self_); } }; @@ -207,9 +228,11 @@ public: } void deserialize(const char *cbuf, size_t bufSize) throw(std::exception) { - if (self_.deserialize(cbuf, bufSize) == 0) { - throw std::runtime_error("deserialize"); - } + deserializeT(self_, cbuf, bufSize); + } + void serialize(std::string& out) const throw(std::exception) + { + serializeT(out, self_); } }; @@ -275,9 +298,11 @@ public: } void deserialize(const char *cbuf, size_t bufSize) throw(std::exception) { - if (self_.deserialize(cbuf, bufSize) == 0) { - throw std::runtime_error("deserialize"); - } + deserializeT(self_, cbuf, bufSize); + } + void serialize(std::string& out) const throw(std::exception) + { + serializeT(out, self_); } }; @@ -326,6 +351,14 @@ public: { return self_.getStr(base); } + void deserialize(const char *cbuf, size_t bufSize) throw(std::exception) + { + deserializeT(self_, cbuf, bufSize); + } + void serialize(std::string& out) const throw(std::exception) + { + serializeT(out, self_); + } }; void mul(GT& z, const GT& x, const GT& y) diff --git a/ffi/java/mcl_wrap.cxx b/ffi/java/mcl_wrap.cxx index f9a36a7..4999bca 100644 --- a/ffi/java/mcl_wrap.cxx +++ b/ffi/java/mcl_wrap.cxx @@ -788,6 +788,33 @@ SWIGEXPORT void JNICALL Java_com_herumi_mcl_MclJNI_Fr_1deserialize(JNIEnv *jenv, } +SWIGEXPORT jbyteArray JNICALL Java_com_herumi_mcl_MclJNI_Fr_1serialize(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) { + jbyteArray jresult = 0 ; + Fr *arg1 = (Fr *) 0 ; + std::string *arg2 = 0 ; + std::string buf2 ; + + (void)jenv; + (void)jcls; + arg2=&buf2; + (void)jarg1_; + arg1 = *(Fr **)&jarg1; + try { + ((Fr const *)arg1)->serialize(*arg2); + } + catch(std::exception &_e) { + SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, (&_e)->what()); + return 0; + } + + { + jresult = jenv->NewByteArray(arg2->size()); + jenv->SetByteArrayRegion(jresult, 0, arg2->size(), (const jbyte*)arg2->c_str()); + } + return jresult; +} + + SWIGEXPORT void JNICALL Java_com_herumi_mcl_MclJNI_delete_1Fr(JNIEnv *jenv, jclass jcls, jlong jarg1) { Fr *arg1 = (Fr *) 0 ; @@ -1322,6 +1349,33 @@ SWIGEXPORT void JNICALL Java_com_herumi_mcl_MclJNI_Fp_1deserialize(JNIEnv *jenv, } +SWIGEXPORT jbyteArray JNICALL Java_com_herumi_mcl_MclJNI_Fp_1serialize(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) { + jbyteArray jresult = 0 ; + Fp *arg1 = (Fp *) 0 ; + std::string *arg2 = 0 ; + std::string buf2 ; + + (void)jenv; + (void)jcls; + arg2=&buf2; + (void)jarg1_; + arg1 = *(Fp **)&jarg1; + try { + ((Fp const *)arg1)->serialize(*arg2); + } + catch(std::exception &_e) { + SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, (&_e)->what()); + return 0; + } + + { + jresult = jenv->NewByteArray(arg2->size()); + jenv->SetByteArrayRegion(jresult, 0, arg2->size(), (const jbyte*)arg2->c_str()); + } + return jresult; +} + + SWIGEXPORT void JNICALL Java_com_herumi_mcl_MclJNI_delete_1Fp(JNIEnv *jenv, jclass jcls, jlong jarg1) { Fp *arg1 = (Fp *) 0 ; @@ -1766,6 +1820,33 @@ SWIGEXPORT void JNICALL Java_com_herumi_mcl_MclJNI_G1_1deserialize(JNIEnv *jenv, } +SWIGEXPORT jbyteArray JNICALL Java_com_herumi_mcl_MclJNI_G1_1serialize(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) { + jbyteArray jresult = 0 ; + G1 *arg1 = (G1 *) 0 ; + std::string *arg2 = 0 ; + std::string buf2 ; + + (void)jenv; + (void)jcls; + arg2=&buf2; + (void)jarg1_; + arg1 = *(G1 **)&jarg1; + try { + ((G1 const *)arg1)->serialize(*arg2); + } + catch(std::exception &_e) { + SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, (&_e)->what()); + return 0; + } + + { + jresult = jenv->NewByteArray(arg2->size()); + jenv->SetByteArrayRegion(jresult, 0, arg2->size(), (const jbyte*)arg2->c_str()); + } + return jresult; +} + + SWIGEXPORT void JNICALL Java_com_herumi_mcl_MclJNI_delete_1G1(JNIEnv *jenv, jclass jcls, jlong jarg1) { G1 *arg1 = (G1 *) 0 ; @@ -2209,6 +2290,33 @@ SWIGEXPORT void JNICALL Java_com_herumi_mcl_MclJNI_G2_1deserialize(JNIEnv *jenv, } +SWIGEXPORT jbyteArray JNICALL Java_com_herumi_mcl_MclJNI_G2_1serialize(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) { + jbyteArray jresult = 0 ; + G2 *arg1 = (G2 *) 0 ; + std::string *arg2 = 0 ; + std::string buf2 ; + + (void)jenv; + (void)jcls; + arg2=&buf2; + (void)jarg1_; + arg1 = *(G2 **)&jarg1; + try { + ((G2 const *)arg1)->serialize(*arg2); + } + catch(std::exception &_e) { + SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, (&_e)->what()); + return 0; + } + + { + jresult = jenv->NewByteArray(arg2->size()); + jenv->SetByteArrayRegion(jresult, 0, arg2->size(), (const jbyte*)arg2->c_str()); + } + return jresult; +} + + SWIGEXPORT void JNICALL Java_com_herumi_mcl_MclJNI_delete_1G2(JNIEnv *jenv, jclass jcls, jlong jarg1) { G2 *arg1 = (G2 *) 0 ; @@ -2416,6 +2524,66 @@ SWIGEXPORT jstring JNICALL Java_com_herumi_mcl_MclJNI_GT_1toString_1_1SWIG_11(JN } +SWIGEXPORT void JNICALL Java_com_herumi_mcl_MclJNI_GT_1deserialize(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jbyteArray jarg2) { + GT *arg1 = (GT *) 0 ; + char *arg2 = (char *) 0 ; + size_t arg3 ; + + (void)jenv; + (void)jcls; + (void)jarg1_; + arg1 = *(GT **)&jarg1; + { + if (jarg2) { + arg2 = (char *) jenv->GetByteArrayElements(jarg2, 0); + arg3 = (size_t) jenv->GetArrayLength(jarg2); + } else { + arg2 = 0; + arg3 = 0; + } + } + try { + (arg1)->deserialize((char const *)arg2,arg3); + } + catch(std::exception &_e) { + SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, (&_e)->what()); + return ; + } + + { + if (jarg2) jenv->ReleaseByteArrayElements(jarg2, (jbyte *)arg2, 0); + } + +} + + +SWIGEXPORT jbyteArray JNICALL Java_com_herumi_mcl_MclJNI_GT_1serialize(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) { + jbyteArray jresult = 0 ; + GT *arg1 = (GT *) 0 ; + std::string *arg2 = 0 ; + std::string buf2 ; + + (void)jenv; + (void)jcls; + arg2=&buf2; + (void)jarg1_; + arg1 = *(GT **)&jarg1; + try { + ((GT const *)arg1)->serialize(*arg2); + } + catch(std::exception &_e) { + SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, (&_e)->what()); + return 0; + } + + { + jresult = jenv->NewByteArray(arg2->size()); + jenv->SetByteArrayRegion(jresult, 0, arg2->size(), (const jbyte*)arg2->c_str()); + } + return jresult; +} + + SWIGEXPORT void JNICALL Java_com_herumi_mcl_MclJNI_delete_1GT(JNIEnv *jenv, jclass jcls, jlong jarg1) { GT *arg1 = (GT *) 0 ; diff --git a/ffi/java/run-mcl.bat b/ffi/java/run-mcl.bat index 903876e..f3c39ca 100644 --- a/ffi/java/run-mcl.bat +++ b/ffi/java/run-mcl.bat @@ -1,9 +1,9 @@ @echo off -echo [[compile Bn256Test.java]] -%JAVA_DIR%\bin\javac Bn256Test.java +echo [[compile MclTest.java]] +%JAVA_DIR%\bin\javac MclTest.java -echo [[run Bn256Test]] +echo [[run MclTest]] set TOP_DIR=..\.. pushd %TOP_DIR%\bin -%JAVA_DIR%\bin\java -classpath ../ffi/java Bn256Test %1 %2 %3 %4 %5 %6 +%JAVA_DIR%\bin\java -classpath ../ffi/java MclTest %1 %2 %3 %4 %5 %6 popd