diff --git a/ffi/go/mcl/mcl.go b/ffi/go/mcl/mcl.go index c4240ca..091c2f0 100644 --- a/ffi/go/mcl/mcl.go +++ b/ffi/go/mcl/mcl.go @@ -32,7 +32,7 @@ const IoSerializeHexStr = C.MCLBN_IO_SERIALIZE_HEX_STR // call this function before calling all the other operations // this function is not thread safe func Init(curve int) error { - err := C.mclBn_init(C.int(curve), C.MCLBN_FP_UNIT_SIZE) + err := C.mclBn_init(C.int(curve), C.MCLBN_COMPILED_TIME_VAR) if err != 0 { return fmt.Errorf("ERR mclBn_init curve=%d", curve) } diff --git a/include/mcl/bn.h b/include/mcl/bn.h index 8da9cfe..9c78f92 100644 --- a/include/mcl/bn.h +++ b/include/mcl/bn.h @@ -6,9 +6,16 @@ @license modified new BSD license http://opensource.org/licenses/BSD-3-Clause */ +/* + the order of an elliptic curve over Fp is Fr +*/ #ifndef MCLBN_FP_UNIT_SIZE #error "define MCLBN_FP_UNIT_SIZE 4(, 6 or 8)" #endif +#ifndef MCLBN_FR_UNIT_SIZE + #define MCLBN_FR_UNIT_SIZE MCLBN_FP_UNIT_SIZE +#endif +#define MCLBN_COMPILED_TIME_VAR ((MCLBN_FR_UNIT_SIZE) * 10 + (MCLBN_FP_UNIT_SIZE)) #include // for uint64_t, uint8_t #include // for size_t @@ -65,7 +72,7 @@ typedef struct mclBnGT mclBnGT; #else typedef struct { - uint64_t d[MCLBN_FP_UNIT_SIZE]; + uint64_t d[MCLBN_FR_UNIT_SIZE]; } mclBnFr; typedef struct { @@ -98,15 +105,17 @@ enum { /* init library @param curve [in] type of bn curve - @param maxUnitSize [in] MCLBN_FP_UNIT_SIZE - return 0 if success else -1 + @param compiledTimeVar [in] specify MCLBN_COMPILED_TIME_VAR, + which macro is used to make sure that the values + are the same when the library is built and used + @return 0 if success curve = BN254/BN_SNARK1 is allowed if maxUnitSize = 4 curve = BN381_1/BN381_2/BLS12_381 are allowed if maxUnitSize = 6 This parameter is used to detect a library compiled with different MCLBN_FP_UNIT_SIZE for safety. @note not threadsafe @note BN_init is used in libeay32 */ -MCLBN_DLL_API int mclBn_init(int curve, int maxUnitSize); +MCLBN_DLL_API int mclBn_init(int curve, int compiledTimeVar); /* diff --git a/include/mcl/she.h b/include/mcl/she.h index 2036719..60b399c 100644 --- a/include/mcl/she.h +++ b/include/mcl/she.h @@ -78,12 +78,14 @@ typedef struct { initialize this library call this once before using the other functions @param curve [in] enum value defined in mcl/bn.h - @param maxUnitSize [in] MCLBN_FP_UNIT_SIZE (fixed) - return 0 if success + @param compiledTimeVar [in] specify MCLBN_COMPILED_TIME_VAR, + which macro is used to make sure that the values + are the same when the library is built and used + @return 0 if success @note sheInit() is thread safe and serialized if it is called simultaneously but don't call it while using other functions. */ -MCLSHE_DLL_API int sheInit(int curve, int maxUnitSize); +MCLSHE_DLL_API int sheInit(int curve, int compiledTimeVar); // return written byte size if success else 0 MCLSHE_DLL_API mclSize sheSecretKeySerialize(void *buf, mclSize maxBufSize, const sheSecretKey *sec); diff --git a/readme.md b/readme.md index bea7e9d..96f0d55 100644 --- a/readme.md +++ b/readme.md @@ -10,6 +10,7 @@ mcl is a library for pairing-based cryptography. The current version supports the optimal Ate pairing over BN curves and BLS12-381 curves. # News +* 2nd argument of `mclBn_init` is changed from `maxUnitSize` to `compiledTimeVar`, which must be `MCLBN_COMPILED_TIME_VAR`. * break backward compatibility of mapToGi for BLS12. A map-to-function for BN is used. If `MCL_USE_OLD_MAPTO_FOR_BLS12` is defined, then the old function is used, but this will be removed in the future. diff --git a/src/bn_c_impl.hpp b/src/bn_c_impl.hpp index 7dc724a..812ff6c 100644 --- a/src/bn_c_impl.hpp +++ b/src/bn_c_impl.hpp @@ -45,9 +45,9 @@ extern "C" MCLBN_DLL_API void mclBnFree(void *p) } #endif -int mclBn_init(int curve, int maxUnitSize) +int mclBn_init(int curve, int compiledTimeVar) { - if (maxUnitSize != MCLBN_FP_UNIT_SIZE) { + if (compiledTimeVar != MCLBN_COMPILED_TIME_VAR) { return -10; } const mcl::CurveParam& cp = mcl::getCurveParam(curve); diff --git a/src/she_c_impl.hpp b/src/she_c_impl.hpp index d025f24..723765a 100644 --- a/src/she_c_impl.hpp +++ b/src/she_c_impl.hpp @@ -41,10 +41,10 @@ static const ZkpEq *cast(const sheZkpEq *p) { return reinterpret_cast(p); } static const ZkpBinEq *cast(const sheZkpBinEq *p) { return reinterpret_cast(p); } -int sheInit(int curve, int maxUnitSize) +int sheInit(int curve, int compiledTimeVar) try { - if (maxUnitSize != MCLBN_FP_UNIT_SIZE) { + if (compiledTimeVar != MCLBN_COMPILED_TIME_VAR) { return -2; } mcl::CurveParam cp; diff --git a/test/bn_c_test.hpp b/test/bn_c_test.hpp index 90bda27..9b5c4bb 100644 --- a/test/bn_c_test.hpp +++ b/test/bn_c_test.hpp @@ -26,13 +26,13 @@ CYBOZU_TEST_AUTO(init) #if MCLBN_FP_UNIT_SIZE == 4 printf("test BN254 %d\n", MCLBN_FP_UNIT_SIZE); - ret = mclBn_init(MCL_BN254, MCLBN_FP_UNIT_SIZE); + ret = mclBn_init(MCL_BN254, MCLBN_COMPILED_TIME_VAR); #elif MCLBN_FP_UNIT_SIZE == 6 printf("test BN381_1 %d\n", MCLBN_FP_UNIT_SIZE); - ret = mclBn_init(MCL_BN381_1, MCLBN_FP_UNIT_SIZE); + ret = mclBn_init(MCL_BN381_1, MCLBN_COMPILED_TIME_VAR); #elif MCLBN_FP_UNIT_SIZE == 8 printf("test BN462 %d\n", MCLBN_FP_UNIT_SIZE); - ret = mclBn_init(MCL_BN462, MCLBN_FP_UNIT_SIZE); + ret = mclBn_init(MCL_BN462, MCLBN_COMPILED_TIME_VAR); #else #error "bad MCLBN_FP_UNIT_SIZE" #endif @@ -508,7 +508,7 @@ CYBOZU_TEST_AUTO(serializeToHexStr) CYBOZU_TEST_AUTO(badG2) { int ret; - ret = mclBn_init(MCL_BN381_1, MCLBN_FP_UNIT_SIZE); + ret = mclBn_init(MCL_BN381_1, MCLBN_COMPILED_TIME_VAR); CYBOZU_TEST_EQUAL(ret, 0); const char *s = "1 18d3d8c085a5a5e7553c3a4eb628e88b8465bf4de2612e35a0a4eb018fb0c82e9698896031e62fd7633ffd824a859474 1dc6edfcf33e29575d4791faed8e7203832217423bf7f7fbf1f6b36625b12e7132c15fbc15562ce93362a322fb83dd0d 65836963b1f7b6959030ddfa15ab38ce056097e91dedffd996c1808624fa7e2644a77be606290aa555cda8481cfb3cb 1b77b708d3d4f65aeedf54b58393463a42f0dc5856baadb5ce608036baeca398c5d9e6b169473a8838098fd72fd28b50"; mclBnG2 Q;