From d30c66387d3bfc5d560aaf5c3220ed5c369bd497 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Tue, 7 Mar 2017 09:50:24 +0900 Subject: [PATCH] rename shared library from _if to _dy --- Makefile | 16 +- common.mk | 3 + ffi/cs/bn256.cs | 116 ++++++------ ffi/python/pairing.py | 2 +- include/mcl/{bn256_if.h => bn256.h} | 2 +- java/Makefile | 2 - mklib.bat | 8 +- readme.md | 8 +- sample/{pairing_if.c => pairing_c.c} | 2 +- src/{bn256_if.cpp => bn256.cpp} | 2 +- test/bn256_if_test.cpp | 259 --------------------------- 11 files changed, 81 insertions(+), 339 deletions(-) rename include/mcl/{bn256_if.h => bn256.h} (99%) rename sample/{pairing_if.c => pairing_c.c} (97%) rename src/{bn256_if.cpp => bn256.cpp} (99%) delete mode 100644 test/bn256_if_test.cpp diff --git a/Makefile b/Makefile index 1255fca..bf25d27 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ LIB_DIR=lib OBJ_DIR=obj EXE_DIR=bin SRC_SRC=fp.cpp -TEST_SRC=fp_test.cpp ec_test.cpp fp_util_test.cpp window_method_test.cpp elgamal_test.cpp fp_tower_test.cpp gmp_test.cpp bn_test.cpp bn256_if_test.cpp +TEST_SRC=fp_test.cpp ec_test.cpp fp_util_test.cpp window_method_test.cpp elgamal_test.cpp fp_tower_test.cpp gmp_test.cpp bn_test.cpp bn256_test.cpp ifeq ($(CPU),x86-64) MCL_USE_XBYAK?=1 TEST_SRC+=mont_fp_test.cpp sq_test.cpp @@ -14,7 +14,7 @@ ifeq ($(CPU),x86-64) TEST_SRC+=fp_generator_test.cpp endif endif -SAMPLE_SRC=bench.cpp ecdh.cpp random.cpp rawbench.cpp vote.cpp pairing.cpp large.cpp tri-dh.cpp bls_sig.cpp pairing_if.c +SAMPLE_SRC=bench.cpp ecdh.cpp random.cpp rawbench.cpp vote.cpp pairing.cpp large.cpp tri-dh.cpp bls_sig.cpp pairing_c.c ifneq ($(MCL_MAX_BIT_SIZE),) CFLAGS+=-DMCL_MAX_BIT_SIZE=$(MCL_MAX_BIT_SIZE) @@ -24,9 +24,9 @@ ifeq ($(MCL_USE_XBYAK),0) endif ################################################################## MCL_LIB=$(LIB_DIR)/libmcl.a -MCL_SLIB=$(LIB_DIR)/libmcls.so -BN256_LIB=$(LIB_DIR)/libbn256_if.a -BN256_SLIB=$(LIB_DIR)/libbn256_if.so +MCL_SLIB=$(LIB_DIR)/libmcl_dy.$(LIB_SUF) +BN256_LIB=$(LIB_DIR)/libbn256.a +BN256_SLIB=$(LIB_DIR)/libbn256_dy.$(LIB_SUF) all: $(MCL_LIB) $(MCL_SLIB) $(BN256_LIB) $(BN256_SLIB) #LLVM_VER=-3.8 @@ -43,7 +43,7 @@ ifneq ($(CPU),) endif ASM_OBJ=$(OBJ_DIR)/$(CPU).o LIB_OBJ=$(OBJ_DIR)/fp.o -BN256_OBJ=$(OBJ_DIR)/bn256_if.o +BN256_OBJ=$(OBJ_DIR)/bn256.o FUNC_LIST=src/func.list MCL_USE_LLVM?=1 ifeq ($(MCL_USE_LLVM),1) @@ -141,10 +141,10 @@ $(OBJ_DIR)/%.o: %.c $(EXE_DIR)/%.exe: $(OBJ_DIR)/%.o $(MCL_LIB) $(PRE)$(CXX) $< -o $@ $(MCL_LIB) $(LDFLAGS) -$(EXE_DIR)/bn256_if_test.exe: $(OBJ_DIR)/bn256_if_test.o $(BN256_LIB) +$(EXE_DIR)/bn256_test.exe: $(OBJ_DIR)/bn256_test.o $(BN256_LIB) $(PRE)$(CXX) $< -o $@ $(BN256_LIB) $(LDFLAGS) -$(EXE_DIR)/pairing_if.exe: $(OBJ_DIR)/pairing_if.o $(BN256_LIB) +$(EXE_DIR)/pairing_c.exe: $(OBJ_DIR)/pairing_c.o $(BN256_LIB) $(PRE)$(CC) $< -o $@ $(BN256_LIB) $(LDFLAGS) -lstdc++ SAMPLE_EXE=$(addprefix $(EXE_DIR)/,$(addsuffix .exe,$(basename $(SAMPLE_SRC)))) diff --git a/common.mk b/common.mk index 245c3e0..0b66caa 100644 --- a/common.mk +++ b/common.mk @@ -6,8 +6,11 @@ endif ifeq ($(UNAME_S),Darwin) OS=mac ARCH=x86_64 + LIB_SUF=dylib CFLAGS+=-I/usr/local/opt/openssl/include LDFLAGS+=-L/usr/local/opt/openssl/lib +else + LIB_SUF=so endif ARCH?=$(shell arch) DO_IT=0 diff --git a/ffi/cs/bn256.cs b/ffi/cs/bn256.cs index cf87e72..adf999b 100644 --- a/ffi/cs/bn256.cs +++ b/ffi/cs/bn256.cs @@ -4,127 +4,127 @@ using System.Runtime.InteropServices; namespace mcl { class BN256 { - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_setErrFile([In][MarshalAs(UnmanagedType.LPStr)] string name); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_init(); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_Fr_clear([Out] Fr x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_Fr_setInt([Out] Fr y, int x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_Fr_setStr([Out] Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string s); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_Fr_isValid([In] Fr x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_Fr_isSame([In] Fr x, [In] Fr y); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_Fr_isZero([In] Fr x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_Fr_isOne([In] Fr x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_Fr_setRand([Out] Fr x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_Fr_setMsg([Out] Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string s); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_Fr_getStr([Out]StringBuilder buf, long maxBufSize, [In] Fr x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_Fr_neg([Out] Fr y, [In] Fr x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_Fr_inv([Out] Fr y, [In] Fr x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_Fr_add([Out] Fr z, [In] Fr x, [In] Fr y); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_Fr_sub([Out] Fr z, [In] Fr x, [In] Fr y); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_Fr_mul([Out] Fr z, [In] Fr x, [In] Fr y); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_Fr_div([Out] Fr z, [In] Fr x, [In] Fr y); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_G1_clear([Out] G1 x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_G1_setStr([Out] G1 x, [In][MarshalAs(UnmanagedType.LPStr)] string s); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_G1_isValid([In] G1 x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_G1_isSame([In] G1 x, [In] G1 y); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_G1_isZero([In] G1 x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_G1_hashAndMapTo([Out] G1 x, [In][MarshalAs(UnmanagedType.LPStr)] string s); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_G1_getStr([Out]StringBuilder buf, long maxBufSize, [In] G1 x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_G1_neg([Out] G1 y, [In] G1 x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_G1_dbl([Out] G1 y, [In] G1 x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_G1_add([Out] G1 z, [In] G1 x, [In] G1 y); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_G1_sub([Out] G1 z, [In] G1 x, [In] G1 y); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_G1_mul([Out] G1 z, [In] G1 x, [In] Fr y); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_G2_clear([Out] G2 x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_G2_setStr([Out] G2 x, [In][MarshalAs(UnmanagedType.LPStr)] string s); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_G2_isValid([In] G2 x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_G2_isSame([In] G2 x, [In] G2 y); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_G2_isZero([In] G2 x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_G2_hashAndMapTo([Out] G2 x, [In][MarshalAs(UnmanagedType.LPStr)] string s); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_G2_getStr([Out]StringBuilder buf, long maxBufSize, [In] G2 x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_G2_neg([Out] G2 y, [In] G2 x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_G2_dbl([Out] G2 y, [In] G2 x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_G2_add([Out] G2 z, [In] G2 x, [In] G2 y); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_G2_sub([Out] G2 z, [In] G2 x, [In] G2 y); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_G2_mul([Out] G2 z, [In] G2 x, [In] Fr y); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_GT_clear([Out] GT x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_GT_setStr([Out] GT x, [In][MarshalAs(UnmanagedType.LPStr)] string s); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_GT_isSame([In] GT x, [In] GT y); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_GT_isZero([In] GT x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_GT_isOne([In] GT x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern int BN256_GT_getStr([Out]StringBuilder buf, long maxBufSize, [In] GT x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_GT_neg([Out] GT y, [In] GT x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_GT_inv([Out] GT y, [In] GT x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_GT_add([Out] GT z, [In] GT x, [In] GT y); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_GT_sub([Out] GT z, [In] GT x, [In] GT y); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_GT_mul([Out] GT z, [In] GT x, [In] GT y); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_GT_div([Out] GT z, [In] GT x, [In] GT y); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_GT_finalExp([Out] GT y, [In] GT x); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_GT_pow([Out] GT z, [In] GT x, [In] Fr y); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_pairing([Out] GT z, [In] G1 x, [In] G2 y); - [DllImport("bn256_if.dll")] + [DllImport("bn256.dll")] public static extern void BN256_millerLoop([Out] GT z, [In] G1 x, [In] G2 y); [StructLayout(LayoutKind.Sequential)] diff --git a/ffi/python/pairing.py b/ffi/python/pairing.py index ffdbf4f..9ca30b7 100644 --- a/ffi/python/pairing.py +++ b/ffi/python/pairing.py @@ -5,7 +5,7 @@ g_lib = None def BN256_init(): global g_lib - g_lib = cdll.LoadLibrary("../../bin/bn256_if.dll") + g_lib = cdll.LoadLibrary("../../bin/bn256.dll") ret = g_lib.BN256_init() if ret: print "ERR BN256_init" diff --git a/include/mcl/bn256_if.h b/include/mcl/bn256.h similarity index 99% rename from include/mcl/bn256_if.h rename to include/mcl/bn256.h index 6f1c5a2..cb68d8c 100644 --- a/include/mcl/bn256_if.h +++ b/include/mcl/bn256.h @@ -46,7 +46,7 @@ typedef struct BN256_GT BN256_GT; #else #define BN256_DLL_API __declspec(dllimport) #ifndef MCL_NO_AUTOLINK - #pragma comment(lib, "bn256_if.lib") + #pragma comment(lib, "bn256.lib") #endif #endif #else diff --git a/java/Makefile b/java/Makefile index 26759c5..f7b01f3 100644 --- a/java/Makefile +++ b/java/Makefile @@ -1,11 +1,9 @@ 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 LDFLAGS+=-lrt endif diff --git a/mklib.bat b/mklib.bat index fa2f603..4e6ca7b 100644 --- a/mklib.bat +++ b/mklib.bat @@ -8,7 +8,7 @@ cl /c %CFLAGS% src\fp.cpp /Foobj\fp.obj echo lib /nologo /OUT:lib\mcl.lib /nodefaultlib obj\fp.obj lib /nologo /OUT:lib\mcl.lib /nodefaultlib obj\fp.obj -echo cl /c %CFLAGS% src\bn256_if.cpp /Foobj\bn256_if.obj -cl /c %CFLAGS% src\bn256_if.cpp /Foobj\bn256_if.obj -echo link /nologo /DLL /OUT:bin\bn256_if.dll obj\bn256_if.obj obj\fp.obj %LDFLAGS% /implib:lib\bn256_if.lib -link /nologo /DLL /OUT:bin\bn256_if.dll obj\bn256_if.obj obj\fp.obj %LDFLAGS% /implib:lib\bn256_if.lib +echo cl /c %CFLAGS% src\bn256.cpp /Foobj\bn256.obj +cl /c %CFLAGS% src\bn256.cpp /Foobj\bn256.obj +echo link /nologo /DLL /OUT:bin\bn256.dll obj\bn256.obj obj\fp.obj %LDFLAGS% /implib:lib\bn256.lib +link /nologo /DLL /OUT:bin\bn256.dll obj\bn256.obj obj\fp.obj %LDFLAGS% /implib:lib\bn256.lib diff --git a/readme.md b/readme.md index a48fbee..13a4da9 100644 --- a/readme.md +++ b/readme.md @@ -93,10 +93,10 @@ finalExp 546.259Kclk # Libraries -* libmcl.a ; static library of mcl for C++ -* libmcls.so ; shared library of mcl for C++ -* libbn256_if.a ; static library for C. include `mcl/bn256_if.h` -* libbn256_ifs.so ; shared library for C. +* libmcl.a ; static C++ library of mcl +* libmcl_dy.so ; shared C++ library of mcl +* libbn256.a ; static C library for `mcl/bn256f.h` +* libbn256_dy.so ; shared C library # How to initialize pairing library Call `mcl::bn256::bn256init` before calling any operations. diff --git a/sample/pairing_if.c b/sample/pairing_c.c similarity index 97% rename from sample/pairing_if.c rename to sample/pairing_c.c index 3e34e9c..95a8c35 100644 --- a/sample/pairing_if.c +++ b/sample/pairing_c.c @@ -1,5 +1,5 @@ #define BN256_DEFINE_STRUCT -#include +#include #include int g_err = 0; diff --git a/src/bn256_if.cpp b/src/bn256.cpp similarity index 99% rename from src/bn256_if.cpp rename to src/bn256.cpp index aa6114a..dbccca7 100644 --- a/src/bn256_if.cpp +++ b/src/bn256.cpp @@ -1,7 +1,7 @@ #include #define BN256_DLL_EXPORT #define BN256_DEFINE_STRUCT -#include +#include #if CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11 #include static std::random_device g_rg; diff --git a/test/bn256_if_test.cpp b/test/bn256_if_test.cpp deleted file mode 100644 index e767c03..0000000 --- a/test/bn256_if_test.cpp +++ /dev/null @@ -1,259 +0,0 @@ -#include -#include - -#define BN256_DEFINE_STRUCT -#include - -#include - -template -std::ostream dump(std::ostream& os, const uint64_t (&x)[N]) -{ - for (size_t i = 0; i < N; i++) { - char buf[64]; - CYBOZU_SNPRINTF(buf, sizeof(buf), "%016llx", (long long)x[i]); - os << buf; - } - return os; -} - -using namespace mcl::bn256; - -CYBOZU_TEST_AUTO(init) -{ - int ret; - CYBOZU_TEST_EQUAL(sizeof(BN256_Fr), sizeof(Fr)); - CYBOZU_TEST_EQUAL(sizeof(BN256_G1), sizeof(G1)); - CYBOZU_TEST_EQUAL(sizeof(BN256_G2), sizeof(G2)); - CYBOZU_TEST_EQUAL(sizeof(BN256_GT), sizeof(Fp12)); - - ret = BN256_setErrFile("stderr"); - CYBOZU_TEST_EQUAL(ret, 0); - - ret = BN256_init(); - CYBOZU_TEST_EQUAL(ret, 0); -} - -CYBOZU_TEST_AUTO(Fr) -{ - BN256_Fr x, y; - memset(&x, 0xff, sizeof(x)); - CYBOZU_TEST_ASSERT(!BN256_Fr_isValid(&x)); - memset(&x, 1, sizeof(x)); - CYBOZU_TEST_ASSERT(BN256_Fr_isValid(&x)); - CYBOZU_TEST_ASSERT(!BN256_Fr_isZero(&x)); - - BN256_Fr_clear(&x); - CYBOZU_TEST_ASSERT(BN256_Fr_isZero(&x)); - - BN256_Fr_setInt(&x, 1); - CYBOZU_TEST_ASSERT(BN256_Fr_isOne(&x)); - - BN256_Fr_setInt(&y, -1); - CYBOZU_TEST_ASSERT(!BN256_Fr_isSame(&x, &y)); - - BN256_Fr_copy(&y, &x); - CYBOZU_TEST_ASSERT(BN256_Fr_isSame(&x, &y)); - - BN256_Fr_setMsg(&x, ""); - BN256_Fr_setMsg(&y, "abc"); - CYBOZU_TEST_ASSERT(!BN256_Fr_isSame(&x, &y)); - BN256_Fr_setMsg(&x, "abc"); - CYBOZU_TEST_ASSERT(BN256_Fr_isSame(&x, &y)); - - char buf[1024]; - BN256_Fr_setInt(&x, 12345678); - CYBOZU_TEST_ASSERT(!BN256_Fr_getStr(buf, sizeof(buf), &x)); - CYBOZU_TEST_EQUAL(buf, "12345678"); - - BN256_Fr_setInt(&x, -7654321); - BN256_Fr_neg(&x, &x); - CYBOZU_TEST_ASSERT(!BN256_Fr_getStr(buf, sizeof(buf), &x)); - CYBOZU_TEST_EQUAL(buf, "7654321"); - - BN256_Fr_setInt(&y, 123 - 7654321); - BN256_Fr_add(&x, &x, &y); - CYBOZU_TEST_ASSERT(!BN256_Fr_getStr(buf, sizeof(buf), &x)); - CYBOZU_TEST_EQUAL(buf, "123"); - - BN256_Fr_setInt(&y, 100); - BN256_Fr_sub(&x, &x, &y); - CYBOZU_TEST_ASSERT(!BN256_Fr_getStr(buf, sizeof(buf), &x)); - CYBOZU_TEST_EQUAL(buf, "23"); - - BN256_Fr_mul(&x, &x, &y); - CYBOZU_TEST_ASSERT(!BN256_Fr_getStr(buf, sizeof(buf), &x)); - CYBOZU_TEST_EQUAL(buf, "2300"); - - BN256_Fr_div(&x, &x, &y); - CYBOZU_TEST_ASSERT(!BN256_Fr_getStr(buf, sizeof(buf), &x)); - CYBOZU_TEST_EQUAL(buf, "23"); - - CYBOZU_TEST_ASSERT(!BN256_Fr_setStr(&x, "12345678901234567")); - CYBOZU_TEST_ASSERT(!BN256_Fr_setStr(&y, "20000000000000000")); - BN256_Fr_add(&x, &x, &y); - CYBOZU_TEST_ASSERT(!BN256_Fr_getStr(buf, sizeof(buf), &x)); - CYBOZU_TEST_EQUAL(buf, "32345678901234567"); - - BN256_Fr_setInt(&x, 1); - BN256_Fr_neg(&x, &x); - CYBOZU_TEST_ASSERT(!BN256_Fr_getStr(buf, sizeof(buf), &x)); - CYBOZU_TEST_ASSERT(!BN256_Fr_setStr(&y, buf)); - CYBOZU_TEST_ASSERT(BN256_Fr_isSame(&x, &y)); -} - -CYBOZU_TEST_AUTO(G1) -{ - BN256_G1 x, y, z; - memset(&x, 0xff, sizeof(x)); - CYBOZU_TEST_ASSERT(!BN256_G1_isValid(&x)); - BN256_G1_clear(&x); - CYBOZU_TEST_ASSERT(BN256_G1_isValid(&x)); - BN256_G1_setStr(&y, "0"); - CYBOZU_TEST_ASSERT(BN256_G1_isZero(&x)); - CYBOZU_TEST_ASSERT(BN256_G1_isZero(&y)); - CYBOZU_TEST_ASSERT(!BN256_G1_setStr(&y, "1 -1 1")); // "1 " - CYBOZU_TEST_ASSERT(!BN256_G1_isZero(&y)); - char buf[1024]; - CYBOZU_TEST_ASSERT(!BN256_G1_getStr(buf, sizeof(buf), &y)); - CYBOZU_TEST_ASSERT(!BN256_G1_setStr(&x, buf)); - CYBOZU_TEST_ASSERT(BN256_G1_isSame(&x, &y)); - - CYBOZU_TEST_ASSERT(!BN256_G1_setStr(&x, "1 -1 -1")); // "1 " - CYBOZU_TEST_ASSERT(!BN256_G1_isZero(&x)); - BN256_G1_add(&x, &x, &y); - CYBOZU_TEST_ASSERT(BN256_G1_isZero(&x)); - - CYBOZU_TEST_ASSERT(!BN256_G1_setStr(&x, "1 -1 -1")); // "1 " - BN256_G1_neg(&x, &x); - CYBOZU_TEST_ASSERT(BN256_G1_isSame(&x, &y)); - - CYBOZU_TEST_ASSERT(!BN256_G1_hashAndMapTo(&y, "abc")); - - BN256_G1_dbl(&x, &y); // x = 2y - BN256_G1_add(&z, &y, &y); - CYBOZU_TEST_ASSERT(BN256_G1_isSame(&x, &z)); - BN256_G1_add(&z, &z, &y); // z = 3y - BN256_Fr n; - BN256_Fr_setInt(&n, 3); - BN256_G1_mul(&x, &y, &n); // x = 3y - CYBOZU_TEST_ASSERT(BN256_G1_isSame(&x, &z)); - BN256_G1_sub(&x, &x, &y); // x = 2y - - BN256_Fr_setInt(&n, 2); - BN256_G1_mul(&z, &y, &n); // z = 2y - CYBOZU_TEST_ASSERT(BN256_G1_isSame(&x, &z)); -} - -CYBOZU_TEST_AUTO(G2) -{ - BN256_G2 x, y, z; - memset(&x, 0xff, sizeof(x)); - CYBOZU_TEST_ASSERT(!BN256_G2_isValid(&x)); - BN256_G2_clear(&x); - CYBOZU_TEST_ASSERT(BN256_G2_isValid(&x)); - BN256_G2_setStr(&y, "0"); - CYBOZU_TEST_ASSERT(BN256_G2_isZero(&x)); - CYBOZU_TEST_ASSERT(BN256_G2_isZero(&y)); - - CYBOZU_TEST_ASSERT(!BN256_G2_hashAndMapTo(&x, "abc")); - - char buf[1024]; - CYBOZU_TEST_ASSERT(!BN256_G2_getStr(buf, sizeof(buf), &x)); - CYBOZU_TEST_ASSERT(!BN256_G2_setStr(&y, buf)); - CYBOZU_TEST_ASSERT(BN256_G2_isSame(&x, &y)); - - BN256_G2_neg(&x, &x); - BN256_G2_add(&x, &x, &y); - CYBOZU_TEST_ASSERT(BN256_G2_isZero(&x)); - - BN256_G2_dbl(&x, &y); // x = 2y - BN256_G2_add(&z, &y, &y); - CYBOZU_TEST_ASSERT(BN256_G2_isSame(&x, &z)); - BN256_G2_add(&z, &z, &y); // z = 3y - BN256_Fr n; - BN256_Fr_setInt(&n, 3); - BN256_G2_mul(&x, &y, &n); // x = 3y - CYBOZU_TEST_ASSERT(BN256_G2_isSame(&x, &z)); - BN256_G2_sub(&x, &x, &y); // x = 2y - - BN256_Fr_setInt(&n, 2); - BN256_G2_mul(&z, &y, &n); // z = 2y - CYBOZU_TEST_ASSERT(BN256_G2_isSame(&x, &z)); -} - -CYBOZU_TEST_AUTO(GT) -{ - BN256_GT x, y, z; - memset(&x, 1, sizeof(x)); - CYBOZU_TEST_ASSERT(!BN256_GT_isZero(&x)); - - BN256_GT_clear(&x); - CYBOZU_TEST_ASSERT(BN256_GT_isZero(&x)); - - char buf[1024]; - CYBOZU_TEST_ASSERT(!BN256_GT_setStr(&x, "1 2 3 4 5 6 7 8 9 10 11 12")); - CYBOZU_TEST_ASSERT(!BN256_GT_getStr(buf, sizeof(buf), &x)); - CYBOZU_TEST_EQUAL(buf, "1 2 3 4 5 6 7 8 9 10 11 12"); - - BN256_GT_copy(&y, &x); - CYBOZU_TEST_ASSERT(BN256_GT_isSame(&x, &y)); - - CYBOZU_TEST_ASSERT(!BN256_GT_setStr(&z, "-1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12")); - CYBOZU_TEST_ASSERT(!BN256_GT_getStr(buf, sizeof(buf), &z)); - CYBOZU_TEST_ASSERT(!BN256_GT_setStr(&y, buf)); - - BN256_GT_neg(&z, &y); - CYBOZU_TEST_ASSERT(BN256_GT_isSame(&x, &z)); - - BN256_GT_add(&y, &x, &y); - CYBOZU_TEST_ASSERT(BN256_GT_isZero(&y)); - - CYBOZU_TEST_ASSERT(!BN256_GT_setStr(&y, "2 0 0 0 0 0 0 0 0 0 0 0")); - BN256_GT_mul(&z, &x, &y); - CYBOZU_TEST_ASSERT(!BN256_GT_getStr(buf, sizeof(buf), &z)); - CYBOZU_TEST_EQUAL(buf, "2 4 6 8 10 12 14 16 18 20 22 24"); - - BN256_GT_div(&z, &z, &y); - CYBOZU_TEST_ASSERT(!BN256_GT_getStr(buf, sizeof(buf), &x)); - CYBOZU_TEST_ASSERT(BN256_GT_isSame(&x, &z)); - - BN256_Fr n; - BN256_Fr_setInt(&n, 3); - BN256_GT_pow(&z, &x, &n); - BN256_GT_mul(&y, &x, &x); - BN256_GT_mul(&y, &y, &x); - CYBOZU_TEST_ASSERT(BN256_GT_isSame(&y, &z)); -} - -CYBOZU_TEST_AUTO(pairing) -{ - BN256_Fr a, b, ab; - BN256_Fr_setInt(&a, 123); - BN256_Fr_setInt(&b, 456); - BN256_Fr_mul(&ab, &a, &b); - BN256_G1 P, aP; - BN256_G2 Q, bQ; - BN256_GT e, e1, e2; - - CYBOZU_TEST_ASSERT(!BN256_G1_setStr(&P, "1 -1 1")); // "1 " - CYBOZU_TEST_ASSERT(!BN256_G2_hashAndMapTo(&Q, "1")); - - BN256_G1_mul(&aP, &P, &a); - BN256_G2_mul(&bQ, &Q, &b); - - BN256_pairing(&e, &P, &Q); - BN256_GT_pow(&e1, &e, &a); - BN256_pairing(&e2, &aP, &Q); - CYBOZU_TEST_ASSERT(BN256_GT_isSame(&e1, &e2)); - - BN256_GT_pow(&e1, &e, &b); - BN256_pairing(&e2, &P, &bQ); - CYBOZU_TEST_ASSERT(BN256_GT_isSame(&e1, &e2)); -} - -CYBOZU_TEST_AUTO(end) -{ - int ret = BN256_setErrFile(""); - CYBOZU_TEST_EQUAL(ret, 0); -} \ No newline at end of file