diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2017-05-30 21:31:10 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2017-05-30 21:31:10 +0800 |
commit | 22f72d79138e1bde2c2c5bc5069dbcfcff94cbfb (patch) | |
tree | 4576a04092a40fd6b820b5e0b5b5e7cb1a9fdaaa | |
parent | 44901d00cf23954b568554af9417f292187049a2 (diff) | |
download | dexon-mcl-22f72d79138e1bde2c2c5bc5069dbcfcff94cbfb.tar.gz dexon-mcl-22f72d79138e1bde2c2c5bc5069dbcfcff94cbfb.tar.zst dexon-mcl-22f72d79138e1bde2c2c5bc5069dbcfcff94cbfb.zip |
rename isSame to isEqual
-rw-r--r-- | ffi/cs/bn256.cs | 16 | ||||
-rw-r--r-- | ffi/python/pairing.py | 2 | ||||
-rw-r--r-- | include/mcl/bn_if.h | 30 | ||||
-rw-r--r-- | sample/pairing_c.c | 7 | ||||
-rw-r--r-- | src/bn256.cpp | 24 | ||||
-rw-r--r-- | test/bn256_test.cpp | 49 |
6 files changed, 72 insertions, 56 deletions
diff --git a/ffi/cs/bn256.cs b/ffi/cs/bn256.cs index 7a01627..6203712 100644 --- a/ffi/cs/bn256.cs +++ b/ffi/cs/bn256.cs @@ -17,7 +17,7 @@ namespace mcl { [DllImport("bn256.dll")]
public static extern int BN256_Fr_isValid(ref Fr x);
[DllImport("bn256.dll")]
- public static extern int BN256_Fr_isSame(ref Fr x, ref Fr y);
+ public static extern int BN256_Fr_isEqual(ref Fr x, ref Fr y);
[DllImport("bn256.dll")]
public static extern int BN256_Fr_isZero(ref Fr x);
[DllImport("bn256.dll")]
@@ -50,7 +50,7 @@ namespace mcl { [DllImport("bn256.dll")]
public static extern int BN256_G1_isValid(ref G1 x);
[DllImport("bn256.dll")]
- public static extern int BN256_G1_isSame(ref G1 x, ref G1 y);
+ public static extern int BN256_G1_isEqual(ref G1 x, ref G1 y);
[DllImport("bn256.dll")]
public static extern int BN256_G1_isZero(ref G1 x);
[DllImport("bn256.dll")]
@@ -75,7 +75,7 @@ namespace mcl { [DllImport("bn256.dll")]
public static extern int BN256_G2_isValid(ref G2 x);
[DllImport("bn256.dll")]
- public static extern int BN256_G2_isSame(ref G2 x, ref G2 y);
+ public static extern int BN256_G2_isEqual(ref G2 x, ref G2 y);
[DllImport("bn256.dll")]
public static extern int BN256_G2_isZero(ref G2 x);
[DllImport("bn256.dll")]
@@ -98,7 +98,7 @@ namespace mcl { [DllImport("bn256.dll")]
public static extern int BN256_GT_setStr(ref GT x, [In][MarshalAs(UnmanagedType.LPStr)] string s);
[DllImport("bn256.dll")]
- public static extern int BN256_GT_isSame(ref GT x, ref GT y);
+ public static extern int BN256_GT_isEqual(ref GT x, ref GT y);
[DllImport("bn256.dll")]
public static extern int BN256_GT_isZero(ref GT x);
[DllImport("bn256.dll")]
@@ -150,7 +150,7 @@ namespace mcl { }
public bool Equals(Fr rhs)
{
- return BN256_Fr_isSame(ref this, ref rhs) == 1;
+ return BN256_Fr_isEqual(ref this, ref rhs) == 1;
}
public bool IsZero()
{
@@ -250,7 +250,7 @@ namespace mcl { }
public bool Equals(G1 rhs)
{
- return BN256_G1_isSame(ref this, ref rhs) == 1;
+ return BN256_G1_isEqual(ref this, ref rhs) == 1;
}
public bool IsZero()
{
@@ -311,7 +311,7 @@ namespace mcl { }
public bool Equals(G2 rhs)
{
- return BN256_G2_isSame(ref this, ref rhs) == 1;
+ return BN256_G2_isEqual(ref this, ref rhs) == 1;
}
public bool IsZero()
{
@@ -370,7 +370,7 @@ namespace mcl { }
public bool Equals(GT rhs)
{
- return BN256_GT_isSame(ref this, ref rhs) == 1;
+ return BN256_GT_isEqual(ref this, ref rhs) == 1;
}
public bool IsZero()
{
diff --git a/ffi/python/pairing.py b/ffi/python/pairing.py index 9ca30b7..88b7291 100644 --- a/ffi/python/pairing.py +++ b/ffi/python/pairing.py @@ -30,7 +30,7 @@ class Fr(Structure): def isOne(self, rhs): return g_lib.BN256_Fr_isOne(self.v) != 0 def __eq__(self, rhs): - return g_lib.BN256_Fr_isSame(self.v, rhs.v) != 0 + return g_lib.BN256_Fr_isEqual(self.v, rhs.v) != 0 def __ne__(self, rhs): return not(P == Q) def __add__(self, rhs): diff --git a/include/mcl/bn_if.h b/include/mcl/bn_if.h index f032d25..5777d34 100644 --- a/include/mcl/bn_if.h +++ b/include/mcl/bn_if.h @@ -18,20 +18,20 @@ extern "C" { #ifdef BN_DEFINE_STRUCT typedef struct { - uint64_t d[4]; -} BN_Fr; // sizeof(BN_Fr) = 32 + uint64_t d[BN_MAX_FP_UNIT_SIZE]; +} BN_Fr; typedef struct { - uint64_t d[4 * 3]; -} BN_G1; // sizeof(BN_G1) = 96 + uint64_t d[BN_MAX_FP_UNIT_SIZE * 3]; +} BN_G1; typedef struct { - uint64_t d[4 * 2 * 3]; -} BN_G2; // sizeof(BN_G2) == 192 + uint64_t d[BN_MAX_FP_UNIT_SIZE * 2 * 3]; +} BN_G2; typedef struct { - uint64_t d[4 * 12]; -} BN_GT; // sizeof(BN_GT) == 384 + uint64_t d[BN_MAX_FP_UNIT_SIZE * 12]; +} BN_GT; #else @@ -48,7 +48,11 @@ typedef struct BN_GT BN_GT; #else #define BN_DLL_API __declspec(dllimport) #ifndef MCL_NO_AUTOLINK - #pragma comment(lib, "bn256.lib") + #if BN_MAX_FP_UNIT_SIZE == 4 + #pragma comment(lib, "bn_if256.lib") + #else + #pragma comment(lib, "bn_if384.lib") + #endif #endif #endif #else @@ -79,7 +83,7 @@ BN_DLL_API int BN_Fr_setStr(BN_Fr *x, const char *s); // return 1 if true and 0 otherwise BN_DLL_API int BN_Fr_isValid(const BN_Fr *x); -BN_DLL_API int BN_Fr_isSame(const BN_Fr *x, const BN_Fr *y); +BN_DLL_API int BN_Fr_isEqual(const BN_Fr *x, const BN_Fr *y); BN_DLL_API int BN_Fr_isZero(const BN_Fr *x); BN_DLL_API int BN_Fr_isOne(const BN_Fr *x); @@ -109,7 +113,7 @@ BN_DLL_API int BN_G1_setStr(BN_G1 *x, const char *s); // return 1 if true and 0 otherwise BN_DLL_API int BN_G1_isValid(const BN_G1 *x); -BN_DLL_API int BN_G1_isSame(const BN_G1 *x, const BN_G1 *y); +BN_DLL_API int BN_G1_isEqual(const BN_G1 *x, const BN_G1 *y); BN_DLL_API int BN_G1_isZero(const BN_G1 *x); BN_DLL_API int BN_G1_hashAndMapTo(BN_G1 *x, const char *s); @@ -134,7 +138,7 @@ BN_DLL_API int BN_G2_setStr(BN_G2 *x, const char *s); // return 1 if true and 0 otherwise BN_DLL_API int BN_G2_isValid(const BN_G2 *x); -BN_DLL_API int BN_G2_isSame(const BN_G2 *x, const BN_G2 *y); +BN_DLL_API int BN_G2_isEqual(const BN_G2 *x, const BN_G2 *y); BN_DLL_API int BN_G2_isZero(const BN_G2 *x); BN_DLL_API int BN_G2_hashAndMapTo(BN_G2 *x, const char *s); @@ -158,7 +162,7 @@ BN_DLL_API void BN_GT_copy(BN_GT *y, const BN_GT *x); BN_DLL_API int BN_GT_setStr(BN_GT *x, const char *s); // return 1 if true and 0 otherwise -BN_DLL_API int BN_GT_isSame(const BN_GT *x, const BN_GT *y); +BN_DLL_API int BN_GT_isEqual(const BN_GT *x, const BN_GT *y); BN_DLL_API int BN_GT_isZero(const BN_GT *x); BN_DLL_API int BN_GT_isOne(const BN_GT *x); diff --git a/sample/pairing_c.c b/sample/pairing_c.c index 95a8c35..863b756 100644 --- a/sample/pairing_c.c +++ b/sample/pairing_c.c @@ -1,5 +1,6 @@ #define BN256_DEFINE_STRUCT -#include <mcl/bn256.h> +#define BN_MAX_FP_UNIT_SIZE 4 +#include <mcl/bn_if.h> #include <stdio.h> int g_err = 0; @@ -36,11 +37,11 @@ int main() printf("e = %s\n", buf); BN256_GT_pow(&e1, &e, &a); BN256_pairing(&e2, &aP, &Q); - ASSERT(BN256_GT_isSame(&e1, &e2)); + ASSERT(BN256_GT_isEqual(&e1, &e2)); BN256_GT_pow(&e1, &e, &b); BN256_pairing(&e2, &P, &bQ); - ASSERT(BN256_GT_isSame(&e1, &e2)); + ASSERT(BN256_GT_isEqual(&e1, &e2)); ASSERT(BN256_setErrFile("") == 0); if (g_err) { printf("err %d\n", g_err); diff --git a/src/bn256.cpp b/src/bn256.cpp index c8f12cf..9a32ac5 100644 --- a/src/bn256.cpp +++ b/src/bn256.cpp @@ -1,8 +1,8 @@ -#include <mcl/bn256.hpp> #define BN_DLL_EXPORT #define BN_DEFINE_STRUCT -#include <mcl/bn256.h> -#if CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11 +#define BN_MAX_FP_UNIT_SIZE 4 +#include <mcl/bn_if.h> +#if 0 // #if CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11 #include <random> static std::random_device g_rg; #else @@ -10,7 +10,13 @@ static std::random_device g_rg; static cybozu::RandomGenerator g_rg; #endif +#if BN_MAX_FP_UNIT_SIZE == 4 +#include <mcl/bn256.hpp> using namespace mcl::bn256; +#else +#include <mcl/bn384.hpp> +using namespace mcl::bn384; +#endif static FILE *g_fp = NULL; @@ -61,7 +67,11 @@ int BN_setErrFile(const char *name) int BN_init(void) try { +#if BN_MAX_FP_UNIT_SIZE == 4 bn256init(); +#else + bn384init(); +#endif return 0; } catch (std::exception& e) { if (g_fp) fprintf(g_fp, "%s\n", e.what()); @@ -102,7 +112,7 @@ int BN_Fr_isValid(const BN_Fr *x) { return cast(x)->isValid(); } -int BN_Fr_isSame(const BN_Fr *x, const BN_Fr *y) +int BN_Fr_isEqual(const BN_Fr *x, const BN_Fr *y) { return *cast(x) == *cast(y); } @@ -195,7 +205,7 @@ int BN_G1_isValid(const BN_G1 *x) { return cast(x)->isValid(); } -int BN_G1_isSame(const BN_G1 *x, const BN_G1 *y) +int BN_G1_isEqual(const BN_G1 *x, const BN_G1 *y) { return *cast(x) == *cast(y); } @@ -281,7 +291,7 @@ int BN_G2_isValid(const BN_G2 *x) { return cast(x)->isValid(); } -int BN_G2_isSame(const BN_G2 *x, const BN_G2 *y) +int BN_G2_isEqual(const BN_G2 *x, const BN_G2 *y) { return *cast(x) == *cast(y); } @@ -364,7 +374,7 @@ int BN_GT_setStr(BN_GT *x, const char *str) } // return 1 if true -int BN_GT_isSame(const BN_GT *x, const BN_GT *y) +int BN_GT_isEqual(const BN_GT *x, const BN_GT *y) { return *cast(x) == *cast(y); } diff --git a/test/bn256_test.cpp b/test/bn256_test.cpp index d5c7276..1907e0d 100644 --- a/test/bn256_test.cpp +++ b/test/bn256_test.cpp @@ -2,7 +2,8 @@ #include <mcl/bn256.hpp> #define BN_DEFINE_STRUCT -#include <mcl/bn256.h> +#define BN_MAX_FP_UNIT_SIZE 4 +#include <mcl/bn_if.h> #include <iostream> @@ -50,16 +51,16 @@ CYBOZU_TEST_AUTO(Fr) CYBOZU_TEST_ASSERT(BN_Fr_isOne(&x)); BN_Fr_setInt(&y, -1); - CYBOZU_TEST_ASSERT(!BN_Fr_isSame(&x, &y)); + CYBOZU_TEST_ASSERT(!BN_Fr_isEqual(&x, &y)); BN_Fr_copy(&y, &x); - CYBOZU_TEST_ASSERT(BN_Fr_isSame(&x, &y)); + CYBOZU_TEST_ASSERT(BN_Fr_isEqual(&x, &y)); BN_Fr_setHashOf(&x, ""); BN_Fr_setHashOf(&y, "abc"); - CYBOZU_TEST_ASSERT(!BN_Fr_isSame(&x, &y)); + CYBOZU_TEST_ASSERT(!BN_Fr_isEqual(&x, &y)); BN_Fr_setHashOf(&x, "abc"); - CYBOZU_TEST_ASSERT(BN_Fr_isSame(&x, &y)); + CYBOZU_TEST_ASSERT(BN_Fr_isEqual(&x, &y)); char buf[1024]; BN_Fr_setInt(&x, 12345678); @@ -99,7 +100,7 @@ CYBOZU_TEST_AUTO(Fr) BN_Fr_neg(&x, &x); CYBOZU_TEST_ASSERT(!BN_Fr_getStr(buf, sizeof(buf), &x)); CYBOZU_TEST_ASSERT(!BN_Fr_setStr(&y, buf)); - CYBOZU_TEST_ASSERT(BN_Fr_isSame(&x, &y)); + CYBOZU_TEST_ASSERT(BN_Fr_isEqual(&x, &y)); } CYBOZU_TEST_AUTO(G1) @@ -117,7 +118,7 @@ CYBOZU_TEST_AUTO(G1) char buf[1024]; CYBOZU_TEST_ASSERT(!BN_G1_getStr(buf, sizeof(buf), &y)); CYBOZU_TEST_ASSERT(!BN_G1_setStr(&x, buf)); - CYBOZU_TEST_ASSERT(BN_G1_isSame(&x, &y)); + CYBOZU_TEST_ASSERT(BN_G1_isEqual(&x, &y)); CYBOZU_TEST_ASSERT(!BN_G1_setStr(&x, "1 -1 -1")); // "1 <x> <y>" CYBOZU_TEST_ASSERT(!BN_G1_isZero(&x)); @@ -126,23 +127,23 @@ CYBOZU_TEST_AUTO(G1) CYBOZU_TEST_ASSERT(!BN_G1_setStr(&x, "1 -1 -1")); // "1 <x> <y>" BN_G1_neg(&x, &x); - CYBOZU_TEST_ASSERT(BN_G1_isSame(&x, &y)); + CYBOZU_TEST_ASSERT(BN_G1_isEqual(&x, &y)); CYBOZU_TEST_ASSERT(!BN_G1_hashAndMapTo(&y, "abc")); BN_G1_dbl(&x, &y); // x = 2y BN_G1_add(&z, &y, &y); - CYBOZU_TEST_ASSERT(BN_G1_isSame(&x, &z)); + CYBOZU_TEST_ASSERT(BN_G1_isEqual(&x, &z)); BN_G1_add(&z, &z, &y); // z = 3y BN_Fr n; BN_Fr_setInt(&n, 3); BN_G1_mul(&x, &y, &n); // x = 3y - CYBOZU_TEST_ASSERT(BN_G1_isSame(&x, &z)); + CYBOZU_TEST_ASSERT(BN_G1_isEqual(&x, &z)); BN_G1_sub(&x, &x, &y); // x = 2y BN_Fr_setInt(&n, 2); BN_G1_mul(&z, &y, &n); // z = 2y - CYBOZU_TEST_ASSERT(BN_G1_isSame(&x, &z)); + CYBOZU_TEST_ASSERT(BN_G1_isEqual(&x, &z)); } CYBOZU_TEST_AUTO(G2) @@ -161,7 +162,7 @@ CYBOZU_TEST_AUTO(G2) char buf[1024]; CYBOZU_TEST_ASSERT(!BN_G2_getStr(buf, sizeof(buf), &x)); CYBOZU_TEST_ASSERT(!BN_G2_setStr(&y, buf)); - CYBOZU_TEST_ASSERT(BN_G2_isSame(&x, &y)); + CYBOZU_TEST_ASSERT(BN_G2_isEqual(&x, &y)); BN_G2_neg(&x, &x); BN_G2_add(&x, &x, &y); @@ -169,17 +170,17 @@ CYBOZU_TEST_AUTO(G2) BN_G2_dbl(&x, &y); // x = 2y BN_G2_add(&z, &y, &y); - CYBOZU_TEST_ASSERT(BN_G2_isSame(&x, &z)); + CYBOZU_TEST_ASSERT(BN_G2_isEqual(&x, &z)); BN_G2_add(&z, &z, &y); // z = 3y BN_Fr n; BN_Fr_setInt(&n, 3); BN_G2_mul(&x, &y, &n); // x = 3y - CYBOZU_TEST_ASSERT(BN_G2_isSame(&x, &z)); + CYBOZU_TEST_ASSERT(BN_G2_isEqual(&x, &z)); BN_G2_sub(&x, &x, &y); // x = 2y BN_Fr_setInt(&n, 2); BN_G2_mul(&z, &y, &n); // z = 2y - CYBOZU_TEST_ASSERT(BN_G2_isSame(&x, &z)); + CYBOZU_TEST_ASSERT(BN_G2_isEqual(&x, &z)); } CYBOZU_TEST_AUTO(GT) @@ -197,14 +198,14 @@ CYBOZU_TEST_AUTO(GT) CYBOZU_TEST_EQUAL(buf, "1 2 3 4 5 6 7 8 9 10 11 12"); BN_GT_copy(&y, &x); - CYBOZU_TEST_ASSERT(BN_GT_isSame(&x, &y)); + CYBOZU_TEST_ASSERT(BN_GT_isEqual(&x, &y)); CYBOZU_TEST_ASSERT(!BN_GT_setStr(&z, "-1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12")); CYBOZU_TEST_ASSERT(!BN_GT_getStr(buf, sizeof(buf), &z)); CYBOZU_TEST_ASSERT(!BN_GT_setStr(&y, buf)); BN_GT_neg(&z, &y); - CYBOZU_TEST_ASSERT(BN_GT_isSame(&x, &z)); + CYBOZU_TEST_ASSERT(BN_GT_isEqual(&x, &z)); BN_GT_add(&y, &x, &y); CYBOZU_TEST_ASSERT(BN_GT_isZero(&y)); @@ -216,14 +217,14 @@ CYBOZU_TEST_AUTO(GT) BN_GT_div(&z, &z, &y); CYBOZU_TEST_ASSERT(!BN_GT_getStr(buf, sizeof(buf), &x)); - CYBOZU_TEST_ASSERT(BN_GT_isSame(&x, &z)); + CYBOZU_TEST_ASSERT(BN_GT_isEqual(&x, &z)); BN_Fr n; BN_Fr_setInt(&n, 3); BN_GT_pow(&z, &x, &n); BN_GT_mul(&y, &x, &x); BN_GT_mul(&y, &y, &x); - CYBOZU_TEST_ASSERT(BN_GT_isSame(&y, &z)); + CYBOZU_TEST_ASSERT(BN_GT_isEqual(&y, &z)); } CYBOZU_TEST_AUTO(pairing) @@ -245,11 +246,11 @@ CYBOZU_TEST_AUTO(pairing) BN_pairing(&e, &P, &Q); BN_GT_pow(&e1, &e, &a); BN_pairing(&e2, &aP, &Q); - CYBOZU_TEST_ASSERT(BN_GT_isSame(&e1, &e2)); + CYBOZU_TEST_ASSERT(BN_GT_isEqual(&e1, &e2)); BN_GT_pow(&e1, &e, &b); BN_pairing(&e2, &P, &bQ); - CYBOZU_TEST_ASSERT(BN_GT_isSame(&e1, &e2)); + CYBOZU_TEST_ASSERT(BN_GT_isEqual(&e1, &e2)); } CYBOZU_TEST_AUTO(precomputed) @@ -272,18 +273,18 @@ CYBOZU_TEST_AUTO(precomputed) BN_pairing(&e1, &P1, &Q1); BN_precomputedMillerLoop(&f1, &P1, Q1buf.data()); BN_GT_finalExp(&f1, &f1); - CYBOZU_TEST_ASSERT(BN_GT_isSame(&e1, &f1)); + CYBOZU_TEST_ASSERT(BN_GT_isEqual(&e1, &f1)); BN_pairing(&e2, &P2, &Q2); BN_precomputedMillerLoop(&f2, &P2, Q2buf.data()); BN_GT_finalExp(&f2, &f2); - CYBOZU_TEST_ASSERT(BN_GT_isSame(&e2, &f2)); + CYBOZU_TEST_ASSERT(BN_GT_isEqual(&e2, &f2)); BN_precomputedMillerLoop2(&f3, &P1, Q1buf.data(), &P2, Q2buf.data()); BN_GT_finalExp(&f3, &f3); BN_GT_mul(&e1, &e1, &e2); - CYBOZU_TEST_ASSERT(BN_GT_isSame(&e1, &f3)); + CYBOZU_TEST_ASSERT(BN_GT_isEqual(&e1, &f3)); } CYBOZU_TEST_AUTO(end) |