diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2016-03-18 16:21:20 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2016-03-18 16:21:26 +0800 |
commit | 9b32ca0a1f3b990ed63d8f78a4a5cff65b57d611 (patch) | |
tree | 6bbfd5683e9120f242de26234be09255787711db | |
parent | a3199e913f9e16b8223072838038d4340e92aa12 (diff) | |
download | tangerine-mcl-9b32ca0a1f3b990ed63d8f78a4a5cff65b57d611.tar.gz tangerine-mcl-9b32ca0a1f3b990ed63d8f78a4a5cff65b57d611.tar.zst tangerine-mcl-9b32ca0a1f3b990ed63d8f78a4a5cff65b57d611.zip |
add java api
-rw-r--r-- | java/Makefile | 45 | ||||
-rw-r--r-- | java/MclTest.java | 141 | ||||
-rw-r--r-- | java/com/herumi/mcl/CipherText.java | 66 | ||||
-rw-r--r-- | java/com/herumi/mcl/Mcl.java | 38 | ||||
-rw-r--r-- | java/com/herumi/mcl/MclJNI.java | 51 | ||||
-rw-r--r-- | java/com/herumi/mcl/PrivateKey.java | 86 | ||||
-rw-r--r-- | java/com/herumi/mcl/PublicKey.java | 82 | ||||
-rw-r--r-- | java/com/herumi/mcl/SWIGTYPE_p_bool.java | 26 | ||||
-rw-r--r-- | java/mcl_if.hpp | 147 | ||||
-rw-r--r-- | java/mcl_if.i | 27 | ||||
-rw-r--r-- | test/elgamal_test.cpp | 7 |
11 files changed, 712 insertions, 4 deletions
diff --git a/java/Makefile b/java/Makefile new file mode 100644 index 0000000..7de1a2b --- /dev/null +++ b/java/Makefile @@ -0,0 +1,45 @@ +include ../common.mk +ifeq ($(UNAME_S),Darwin) + JAVA_INC=-I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/ + LIB_SUF=dylib +else + JAVA_INC=-I/usr/lib/jvm/default-java/include +#JAVA_INC=-I/usr/lib/jvm/java-7-openjdk-amd64/include + LIB_SUF=so + CFLAGS+=-z noexecstack + LIB+=-lrt +endif +CFLAGS+= -shared -fPIC $(JAVA_INC) + +PACKAGE_NAME=com.herumi.mcl +PACKAGE_DIR=$(subst .,/,$(PACKAGE_NAME)) + +TARGET=../bin/mcl_if_wrap.$(LIB_SUF) +JAVA_EXE=cd ../bin && LD_LIBRARY_PATH=./:$(LD_LIBRARY_PATH) java -classpath ../java +all: $(TARGET) + +mcl_if_wrap.cxx: mcl_if.i mcl_if.hpp + swig -java -package $(PACKAGE_NAME) -outdir $(PACKAGE_DIR) -c++ -Wall mcl_if.i + +$(TARGET): mcl_if_wrap.cxx + $(PRE)$(CXX) $< -o $@ $(CFLAGS) $(LDFLAGS) ../src/fp.cpp ../src/x64.s + + +%.class: %.java + javac $< + +MclTest.class: MclTest.java $(TARGET) + +jar: + jar cvf mcl.jar com + +test: MclTest.class $(TARGET) + $(JAVA_EXE) MclTest + $(JAVA_EXE) MclTest -e NIST_P192 + $(JAVA_EXE) MclTest -e NIST_P256 -h sha256 + $(JAVA_EXE) MclTest -e NIST_P384 -h sha384 + $(JAVA_EXE) MclTest -e NIST_P521 -h sha512 + +clean: + rm -rf *.class $(TARGET) $(PACKAGE_DIR)/*.class + diff --git a/java/MclTest.java b/java/MclTest.java new file mode 100644 index 0000000..816311c --- /dev/null +++ b/java/MclTest.java @@ -0,0 +1,141 @@ +import java.io.*; +import com.herumi.mcl.*; + +/* + MclTest [ecParam] + ecParam = secp192k1, NIST_P224, ... + hashParam = hash224, hash384, ... +*/ +public class MclTest { + static { + System.loadLibrary("mcl_if_wrap"); + } + public static void assertEquals(String msg, int x, int y) { + if (x == y) { + System.out.println("OK : " + msg); + } else { + System.out.println("NG : " + msg + ", x = " + x + ", y = " + y); + } + } + public static void assertBool(String msg, boolean b) { + if (b) { + System.out.println("OK : " + msg); + } else { + System.out.println("NG : " + msg); + } + } + public static void main(String argv[]) { + try { + String ecStr = "secp192k1"; + String hashStr = "sha224"; + for (int i = 0; i < argv.length; i++) { + if (argv[i].equals("-e") && i < argv.length - 1) { + ecStr = argv[i + 1]; + i++; + } else + if (argv[i].equals("-h") && i < argv.length - 1) { + hashStr = argv[i + 1]; + i++; + } + } + String param = ecStr + " " + hashStr; + System.out.println("param=" + param); + Mcl.SystemInit(param); + + String prvStr = ""; + String pubStr = ""; + { + PrivateKey prv = new PrivateKey(); + prv.init(); + prvStr = prv.toStr(); + PublicKey pub = prv.getPublicKey(); + pubStr = pub.toStr(); + } + int m = 1234; + CipherText c = new CipherText(); + PublicKey pub = new PublicKey(); + + pub.fromStr(pubStr); + + pub.enc(c, m); + + PrivateKey prv = new PrivateKey(); + prv.fromStr(prvStr); + prv.setCache(0, 60000); + + int dec = prv.dec(c); + // verify dec(enc(m)) == m + assertEquals("dec(enc(m)) == m", m, dec); + + // verify toStr, fromStr + { + String cStr = c.toStr(); + CipherText c2 = new CipherText(); + c2.fromStr(cStr); + int dec2 = prv.dec(c2); + assertEquals("fromStr(toStr(CipherText) == CipherText", dec, dec2); + } + + // verify dec(enc(str)) == str + pub.enc(c, "1234"); + dec = prv.dec(c); + assertEquals("dec(enc(str)) == str", m, dec); + + // verify dec(mul(enc(m), 3)) == m * 3 + c.mul(3); + m *= 3; + dec = prv.dec(c); + assertEquals("mul(int)", m, dec); + + // verify dec(mul(enc(m), "10")) == m * 10 + c.mul("10"); + m *= 10; + dec = prv.dec(c); + assertEquals("mul(str)", m, dec); + + // convert str + { + String s = c.toStr(); + CipherText c2 = new CipherText(); + c2.fromStr(s); + dec = prv.dec(c); + assertEquals("fromStr", m, dec); + } + // rerandomize + pub.rerandomize(c); + dec = prv.dec(c); + assertEquals("rerandomize", m, dec); + int m2 = 12345; + // verify dec(add(enc(m), m2)) == m + m2 + pub.add(c, m2); + m += m2; + dec = prv.dec(c); + assertEquals("pub.add(int)", m, dec); + + pub.add(c, "993"); + m += 993; + dec = prv.dec(c); + assertEquals("pub.add(str)", m, dec); + + // string test + String m3 = "-2000000"; + String m4 = "2001234"; + CipherText c2 = new CipherText(); + SWIGTYPE_p_bool b = Mcl.new_p_bool(); + pub.enc(c, m3); + dec = prv.dec(c, b); + assertBool("expect dec fail", !Mcl.p_bool_value(b)); + pub.enc(c2, m4); + dec = prv.dec(c2, b); + assertBool("expect dec fail", !Mcl.p_bool_value(b)); + c.add(c2); // m3 + m4 + + dec = prv.dec(c, b); + assertEquals("int add", 1234, dec); + assertBool("expect dec success", Mcl.p_bool_value(b)); + Mcl.delete_p_bool(b); + } catch (RuntimeException e) { + System.out.println("unknown exception :" + e); + } + } +} diff --git a/java/com/herumi/mcl/CipherText.java b/java/com/herumi/mcl/CipherText.java new file mode 100644 index 0000000..42dd805 --- /dev/null +++ b/java/com/herumi/mcl/CipherText.java @@ -0,0 +1,66 @@ +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 2.0.11 + * + * Do not make changes to this file unless you know what you are doing--modify + * the SWIG interface file instead. + * ----------------------------------------------------------------------------- */ + +package com.herumi.mcl; + +public class CipherText { + private long swigCPtr; + protected boolean swigCMemOwn; + + protected CipherText(long cPtr, boolean cMemoryOwn) { + swigCMemOwn = cMemoryOwn; + swigCPtr = cPtr; + } + + protected static long getCPtr(CipherText obj) { + return (obj == null) ? 0 : obj.swigCPtr; + } + + protected void finalize() { + delete(); + } + + public synchronized void delete() { + if (swigCPtr != 0) { + if (swigCMemOwn) { + swigCMemOwn = false; + MclJNI.delete_CipherText(swigCPtr); + } + swigCPtr = 0; + } + } + + public String toStr() { + return MclJNI.CipherText_toStr(swigCPtr, this); + } + + public String toString() { + return MclJNI.CipherText_toString(swigCPtr, this); + } + + public void fromStr(String str) { + MclJNI.CipherText_fromStr(swigCPtr, this, str); + } + + public void add(CipherText c) { + MclJNI.CipherText_add(swigCPtr, this, CipherText.getCPtr(c), c); + } + + public void mul(int m) { + MclJNI.CipherText_mul__SWIG_0(swigCPtr, this, m); + } + + public void mul(String str) { + MclJNI.CipherText_mul__SWIG_1(swigCPtr, this, str); + } + + public CipherText() { + this(MclJNI.new_CipherText(), true); + } + +} diff --git a/java/com/herumi/mcl/Mcl.java b/java/com/herumi/mcl/Mcl.java new file mode 100644 index 0000000..20f7845 --- /dev/null +++ b/java/com/herumi/mcl/Mcl.java @@ -0,0 +1,38 @@ +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 2.0.11 + * + * Do not make changes to this file unless you know what you are doing--modify + * the SWIG interface file instead. + * ----------------------------------------------------------------------------- */ + +package com.herumi.mcl; + +public class Mcl { + public static SWIGTYPE_p_bool new_p_bool() { + long cPtr = MclJNI.new_p_bool(); + return (cPtr == 0) ? null : new SWIGTYPE_p_bool(cPtr, false); + } + + public static SWIGTYPE_p_bool copy_p_bool(boolean value) { + long cPtr = MclJNI.copy_p_bool(value); + return (cPtr == 0) ? null : new SWIGTYPE_p_bool(cPtr, false); + } + + public static void delete_p_bool(SWIGTYPE_p_bool obj) { + MclJNI.delete_p_bool(SWIGTYPE_p_bool.getCPtr(obj)); + } + + public static void p_bool_assign(SWIGTYPE_p_bool obj, boolean value) { + MclJNI.p_bool_assign(SWIGTYPE_p_bool.getCPtr(obj), value); + } + + public static boolean p_bool_value(SWIGTYPE_p_bool obj) { + return MclJNI.p_bool_value(SWIGTYPE_p_bool.getCPtr(obj)); + } + + public static void SystemInit(String param) { + MclJNI.SystemInit(param); + } + +} diff --git a/java/com/herumi/mcl/MclJNI.java b/java/com/herumi/mcl/MclJNI.java new file mode 100644 index 0000000..10105c2 --- /dev/null +++ b/java/com/herumi/mcl/MclJNI.java @@ -0,0 +1,51 @@ +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 2.0.11 + * + * Do not make changes to this file unless you know what you are doing--modify + * the SWIG interface file instead. + * ----------------------------------------------------------------------------- */ + +package com.herumi.mcl; + +public class MclJNI { + public final static native long new_p_bool(); + public final static native long copy_p_bool(boolean jarg1); + public final static native void delete_p_bool(long jarg1); + public final static native void p_bool_assign(long jarg1, boolean jarg2); + public final static native boolean p_bool_value(long jarg1); + public final static native void SystemInit(String jarg1); + public final static native String CipherText_toStr(long jarg1, CipherText jarg1_); + public final static native String CipherText_toString(long jarg1, CipherText jarg1_); + public final static native void CipherText_fromStr(long jarg1, CipherText jarg1_, String jarg2); + public final static native void CipherText_add(long jarg1, CipherText jarg1_, long jarg2, CipherText jarg2_); + public final static native void CipherText_mul__SWIG_0(long jarg1, CipherText jarg1_, int jarg2); + public final static native void CipherText_mul__SWIG_1(long jarg1, CipherText jarg1_, String jarg2); + public final static native long new_CipherText(); + public final static native void delete_CipherText(long jarg1); + public final static native String PublicKey_toStr(long jarg1, PublicKey jarg1_); + public final static native String PublicKey_toString(long jarg1, PublicKey jarg1_); + public final static native void PublicKey_fromStr(long jarg1, PublicKey jarg1_, String jarg2); + public final static native void PublicKey_save(long jarg1, PublicKey jarg1_, String jarg2); + public final static native void PublicKey_load(long jarg1, PublicKey jarg1_, String jarg2); + public final static native void PublicKey_enc__SWIG_0(long jarg1, PublicKey jarg1_, long jarg2, CipherText jarg2_, int jarg3); + public final static native void PublicKey_enc__SWIG_1(long jarg1, PublicKey jarg1_, long jarg2, CipherText jarg2_, String jarg3); + public final static native void PublicKey_rerandomize(long jarg1, PublicKey jarg1_, long jarg2, CipherText jarg2_); + public final static native void PublicKey_add__SWIG_0(long jarg1, PublicKey jarg1_, long jarg2, CipherText jarg2_, int jarg3); + public final static native void PublicKey_add__SWIG_1(long jarg1, PublicKey jarg1_, long jarg2, CipherText jarg2_, String jarg3); + public final static native long new_PublicKey(); + public final static native void delete_PublicKey(long jarg1); + public final static native String PrivateKey_toStr(long jarg1, PrivateKey jarg1_); + public final static native String PrivateKey_toString(long jarg1, PrivateKey jarg1_); + public final static native void PrivateKey_fromStr(long jarg1, PrivateKey jarg1_, String jarg2); + public final static native void PrivateKey_save(long jarg1, PrivateKey jarg1_, String jarg2); + public final static native void PrivateKey_load(long jarg1, PrivateKey jarg1_, String jarg2); + public final static native void PrivateKey_init(long jarg1, PrivateKey jarg1_); + public final static native long PrivateKey_getPublicKey(long jarg1, PrivateKey jarg1_); + public final static native int PrivateKey_dec__SWIG_0(long jarg1, PrivateKey jarg1_, long jarg2, CipherText jarg2_, long jarg3); + public final static native int PrivateKey_dec__SWIG_1(long jarg1, PrivateKey jarg1_, long jarg2, CipherText jarg2_); + public final static native void PrivateKey_setCache(long jarg1, PrivateKey jarg1_, int jarg2, int jarg3); + public final static native void PrivateKey_clearCache(long jarg1, PrivateKey jarg1_); + public final static native long new_PrivateKey(); + public final static native void delete_PrivateKey(long jarg1); +} diff --git a/java/com/herumi/mcl/PrivateKey.java b/java/com/herumi/mcl/PrivateKey.java new file mode 100644 index 0000000..a3c7698 --- /dev/null +++ b/java/com/herumi/mcl/PrivateKey.java @@ -0,0 +1,86 @@ +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 2.0.11 + * + * Do not make changes to this file unless you know what you are doing--modify + * the SWIG interface file instead. + * ----------------------------------------------------------------------------- */ + +package com.herumi.mcl; + +public class PrivateKey { + private long swigCPtr; + protected boolean swigCMemOwn; + + protected PrivateKey(long cPtr, boolean cMemoryOwn) { + swigCMemOwn = cMemoryOwn; + swigCPtr = cPtr; + } + + protected static long getCPtr(PrivateKey obj) { + return (obj == null) ? 0 : obj.swigCPtr; + } + + protected void finalize() { + delete(); + } + + public synchronized void delete() { + if (swigCPtr != 0) { + if (swigCMemOwn) { + swigCMemOwn = false; + MclJNI.delete_PrivateKey(swigCPtr); + } + swigCPtr = 0; + } + } + + public String toStr() { + return MclJNI.PrivateKey_toStr(swigCPtr, this); + } + + public String toString() { + return MclJNI.PrivateKey_toString(swigCPtr, this); + } + + public void fromStr(String str) { + MclJNI.PrivateKey_fromStr(swigCPtr, this, str); + } + + public void save(String fileName) { + MclJNI.PrivateKey_save(swigCPtr, this, fileName); + } + + public void load(String fileName) { + MclJNI.PrivateKey_load(swigCPtr, this, fileName); + } + + public void init() { + MclJNI.PrivateKey_init(swigCPtr, this); + } + + public PublicKey getPublicKey() { + return new PublicKey(MclJNI.PrivateKey_getPublicKey(swigCPtr, this), true); + } + + public int dec(CipherText c, SWIGTYPE_p_bool b) { + return MclJNI.PrivateKey_dec__SWIG_0(swigCPtr, this, CipherText.getCPtr(c), c, SWIGTYPE_p_bool.getCPtr(b)); + } + + public int dec(CipherText c) { + return MclJNI.PrivateKey_dec__SWIG_1(swigCPtr, this, CipherText.getCPtr(c), c); + } + + public void setCache(int rangeMin, int rangeMax) { + MclJNI.PrivateKey_setCache(swigCPtr, this, rangeMin, rangeMax); + } + + public void clearCache() { + MclJNI.PrivateKey_clearCache(swigCPtr, this); + } + + public PrivateKey() { + this(MclJNI.new_PrivateKey(), true); + } + +} diff --git a/java/com/herumi/mcl/PublicKey.java b/java/com/herumi/mcl/PublicKey.java new file mode 100644 index 0000000..c4f2753 --- /dev/null +++ b/java/com/herumi/mcl/PublicKey.java @@ -0,0 +1,82 @@ +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 2.0.11 + * + * Do not make changes to this file unless you know what you are doing--modify + * the SWIG interface file instead. + * ----------------------------------------------------------------------------- */ + +package com.herumi.mcl; + +public class PublicKey { + private long swigCPtr; + protected boolean swigCMemOwn; + + protected PublicKey(long cPtr, boolean cMemoryOwn) { + swigCMemOwn = cMemoryOwn; + swigCPtr = cPtr; + } + + protected static long getCPtr(PublicKey obj) { + return (obj == null) ? 0 : obj.swigCPtr; + } + + protected void finalize() { + delete(); + } + + public synchronized void delete() { + if (swigCPtr != 0) { + if (swigCMemOwn) { + swigCMemOwn = false; + MclJNI.delete_PublicKey(swigCPtr); + } + swigCPtr = 0; + } + } + + public String toStr() { + return MclJNI.PublicKey_toStr(swigCPtr, this); + } + + public String toString() { + return MclJNI.PublicKey_toString(swigCPtr, this); + } + + public void fromStr(String str) { + MclJNI.PublicKey_fromStr(swigCPtr, this, str); + } + + public void save(String fileName) { + MclJNI.PublicKey_save(swigCPtr, this, fileName); + } + + public void load(String fileName) { + MclJNI.PublicKey_load(swigCPtr, this, fileName); + } + + public void enc(CipherText c, int m) { + MclJNI.PublicKey_enc__SWIG_0(swigCPtr, this, CipherText.getCPtr(c), c, m); + } + + public void enc(CipherText c, String str) { + MclJNI.PublicKey_enc__SWIG_1(swigCPtr, this, CipherText.getCPtr(c), c, str); + } + + public void rerandomize(CipherText c) { + MclJNI.PublicKey_rerandomize(swigCPtr, this, CipherText.getCPtr(c), c); + } + + public void add(CipherText c, int m) { + MclJNI.PublicKey_add__SWIG_0(swigCPtr, this, CipherText.getCPtr(c), c, m); + } + + public void add(CipherText c, String str) { + MclJNI.PublicKey_add__SWIG_1(swigCPtr, this, CipherText.getCPtr(c), c, str); + } + + public PublicKey() { + this(MclJNI.new_PublicKey(), true); + } + +} diff --git a/java/com/herumi/mcl/SWIGTYPE_p_bool.java b/java/com/herumi/mcl/SWIGTYPE_p_bool.java new file mode 100644 index 0000000..f03b9bb --- /dev/null +++ b/java/com/herumi/mcl/SWIGTYPE_p_bool.java @@ -0,0 +1,26 @@ +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 2.0.11 + * + * Do not make changes to this file unless you know what you are doing--modify + * the SWIG interface file instead. + * ----------------------------------------------------------------------------- */ + +package com.herumi.mcl; + +public class SWIGTYPE_p_bool { + private long swigCPtr; + + protected SWIGTYPE_p_bool(long cPtr, boolean futureUse) { + swigCPtr = cPtr; + } + + protected SWIGTYPE_p_bool() { + swigCPtr = 0; + } + + protected static long getCPtr(SWIGTYPE_p_bool obj) { + return (obj == null) ? 0 : obj.swigCPtr; + } +} + diff --git a/java/mcl_if.hpp b/java/mcl_if.hpp new file mode 100644 index 0000000..54ed4bb --- /dev/null +++ b/java/mcl_if.hpp @@ -0,0 +1,147 @@ +#pragma once +//#define MCL_MAX_OP_BIT_SIZE 521 +#include <iostream> +#include <fstream> +#include <cybozu/random_generator.hpp> +#include <cybozu/crypto.hpp> +#include <mcl/fp.hpp> +#include <mcl/ecparam.hpp> +#include <mcl/elgamal.hpp> + +typedef mcl::FpT<mcl::FpTag> Fp; +typedef mcl::FpT<mcl::ZnTag> Zn; +typedef mcl::EcT<Fp> Ec; +typedef mcl::ElgamalT<Ec, Zn> Elgamal; + +/* + init system + @param param [in] string such as "ecParamName hashName" + @note NOT thread safe because setting global parameters of elliptic curve + ex1) "secp192k1 sha256" // 192bit security + sha256 + ex2) "secp160k1 sha1" // 160bit security + sha1 + hashName : sha1 sha224 sha256 sha384 sha512 +*/ +void SystemInit(const std::string& param) throw(std::exception) +{ + std::istringstream iss(param); + std::string ecParamStr; + std::string hashNameStr; + if (iss >> ecParamStr >> hashNameStr) { + Param& p = Param::getParam(); + p.ecParam = mcl::getEcParam(ecParamStr); + Zn::setModulo(p.ecParam->n); + Fp::setModulo(p.ecParam->p); + Ec::setParam(p.ecParam->a, p.ecParam->b); + p.hashName = cybozu::crypto::Hash::getName(hashNameStr); + return; + } + throw cybozu::Exception("SystemInit:bad param") << param; +} + +class CipherText { + Elgamal::CipherText self_; + friend class PublicKey; + friend class PrivateKey; +public: + std::string toStr() const throw(std::exception) { return self_.toStr(); } + std::string toString() const throw(std::exception) { return toStr(); } + void fromStr(const std::string& str) throw(std::exception) { self_.fromStr(str); } + + void add(const CipherText& c) throw(std::exception) { self_.add(c.self_); } + void mul(int m) throw(std::exception) + { + self_.mul(m); + } + void mul(const std::string& str) throw(std::exception) + { + Zn zn(str); + self_.mul(zn); + } +}; + +class PublicKey { + Elgamal::PublicKey self_; + friend class PrivateKey; +public: + std::string toStr() const throw(std::exception) { return self_.toStr(); } + std::string toString() const throw(std::exception) { return toStr(); } + void fromStr(const std::string& str) throw(std::exception) { self_.fromStr(str); } + + void save(const std::string& fileName) const throw(std::exception) + { + std::ofstream ofs(fileName.c_str(), std::ios::binary); + if (!(ofs << self_)) throw cybozu::Exception("PublicKey:save") << fileName; + } + void load(const std::string& fileName) throw(std::exception) + { + std::ifstream ifs(fileName.c_str(), std::ios::binary); + if (!(ifs >> self_)) throw cybozu::Exception("PublicKey:load") << fileName; + } + void enc(CipherText& c, int m) const throw(std::exception) + { + self_.enc(c.self_, m, Param::getParam().rg); + } + void enc(CipherText& c, const std::string& str) const throw(std::exception) + { + Zn zn(str); + self_.enc(c.self_, zn, Param::getParam().rg); + } + void rerandomize(CipherText& c) const throw(std::exception) + { + self_.rerandomize(c.self_, Param::getParam().rg); + } + void add(CipherText& c, int m) const throw(std::exception) + { + self_.add(c.self_, m); + } + void add(CipherText& c, const std::string& str) const throw(std::exception) + { + Zn zn(str); + self_.add(c.self_, zn); + } +}; + +class PrivateKey { + Elgamal::PrivateKey self_; +public: + std::string toStr() const throw(std::exception) { return self_.toStr(); } + std::string toString() const throw(std::exception) { return toStr(); } + void fromStr(const std::string& str) throw(std::exception) { self_.fromStr(str); } + + void save(const std::string& fileName) const throw(std::exception) + { + std::ofstream ofs(fileName.c_str(), std::ios::binary); + if (!(ofs << self_)) throw cybozu::Exception("PrivateKey:save") << fileName; + } + void load(const std::string& fileName) throw(std::exception) + { + std::ifstream ifs(fileName.c_str(), std::ios::binary); + if (!(ifs >> self_)) throw cybozu::Exception("PrivateKey:load") << fileName; + } + void init() throw(std::exception) + { + Param& p = Param::getParam(); + const Fp x0(p.ecParam->gx); + const Fp y0(p.ecParam->gy); + Ec P(x0, y0); + self_.init(P, Zn::getBitSize(), p.rg); + } + PublicKey getPublicKey() const throw(std::exception) + { + PublicKey ret; + ret.self_ = self_.getPublicKey(); + return ret; + } + int dec(const CipherText& c, bool *b = 0) const throw(std::exception) + { + return self_.dec(c.self_, b); + } + void setCache(int rangeMin, int rangeMax) throw(std::exception) + { + self_.setCache(rangeMin, rangeMax); + } + void clearCache() throw(std::exception) + { + self_.clearCache(); + } +}; diff --git a/java/mcl_if.i b/java/mcl_if.i new file mode 100644 index 0000000..a6ec6b7 --- /dev/null +++ b/java/mcl_if.i @@ -0,0 +1,27 @@ +%module Mcl + +%include "std_string.i" +%include "std_except.i" + + +%{ +#include <cybozu/random_generator.hpp> +#include <cybozu/crypto.hpp> +#include <mcl/ecparam.hpp> +struct Param { +const mcl::EcParam *ecParam; +cybozu::RandomGenerator rg; +cybozu::crypto::Hash::Name hashName; +static inline Param& getParam() +{ + static Param p; + return p; +} +}; + +#include "mcl_if.hpp" +%} +%include cpointer.i +%pointer_functions(bool, p_bool); + +%include "mcl_if.hpp" diff --git a/test/elgamal_test.cpp b/test/elgamal_test.cpp index 36b702f..0d8ccf8 100644 --- a/test/elgamal_test.cpp +++ b/test/elgamal_test.cpp @@ -1,10 +1,9 @@ #include <cybozu/test.hpp> -#include <mcl/fp.hpp> -#include <mcl/gmp_util.hpp> -#include <mcl/elgamal.hpp> #include <cybozu/random_generator.hpp> -#include <mcl/ecparam.hpp> #include <cybozu/crypto.hpp> +#include <mcl/fp.hpp> +#include <mcl/ecparam.hpp> +#include <mcl/elgamal.hpp> struct TagZn; typedef mcl::FpT<> Fp; |