add mclBnFp_mapToG1 and mclBnFp2_mapToG2

pull/2/head
MITSUNARI Shigeo 6 years ago
parent 6ef8f3db38
commit 5b4abee36f
  1. 26
      include/mcl/bn.h
  2. 72
      include/mcl/impl/bn_c_impl.hpp
  3. 1
      readme.md
  4. 68
      test/bn_c_test.hpp

@ -68,6 +68,8 @@ typedef struct mclBnFr mclBnFr;
typedef struct mclBnG1 mclBnG1; typedef struct mclBnG1 mclBnG1;
typedef struct mclBnG2 mclBnG2; typedef struct mclBnG2 mclBnG2;
typedef struct mclBnGT mclBnGT; typedef struct mclBnGT mclBnGT;
typedef struct mclBnFp mclBnFp;
typedef struct mclBnFp2 mclBnFp2;
#else #else
@ -87,6 +89,15 @@ typedef struct {
uint64_t d[MCLBN_FP_UNIT_SIZE * 12]; uint64_t d[MCLBN_FP_UNIT_SIZE * 12];
} mclBnGT; } mclBnGT;
typedef struct {
uint64_t d[MCLBN_FP_UNIT_SIZE];
} mclBnFp;
typedef struct {
mclBnFp a;
mclBnFp b;
} mclBnFp2;
#endif #endif
#include <mcl/curve_type.h> #include <mcl/curve_type.h>
@ -168,6 +179,8 @@ MCLBN_DLL_API mclSize mclBnFr_deserialize(mclBnFr *x, const void *buf, mclSize b
MCLBN_DLL_API mclSize mclBnG1_deserialize(mclBnG1 *x, const void *buf, mclSize bufSize); MCLBN_DLL_API mclSize mclBnG1_deserialize(mclBnG1 *x, const void *buf, mclSize bufSize);
MCLBN_DLL_API mclSize mclBnG2_deserialize(mclBnG2 *x, const void *buf, mclSize bufSize); MCLBN_DLL_API mclSize mclBnG2_deserialize(mclBnG2 *x, const void *buf, mclSize bufSize);
MCLBN_DLL_API mclSize mclBnGT_deserialize(mclBnGT *x, const void *buf, mclSize bufSize); MCLBN_DLL_API mclSize mclBnGT_deserialize(mclBnGT *x, const void *buf, mclSize bufSize);
MCLBN_DLL_API mclSize mclBnFp_deserialize(mclBnFp *x, const void *buf, mclSize bufSize);
MCLBN_DLL_API mclSize mclBnFp2_deserialize(mclBnFp2 *x, const void *buf, mclSize bufSize);
/* /*
serialize serialize
@ -177,6 +190,8 @@ MCLBN_DLL_API mclSize mclBnFr_serialize(void *buf, mclSize maxBufSize, const mcl
MCLBN_DLL_API mclSize mclBnG1_serialize(void *buf, mclSize maxBufSize, const mclBnG1 *x); MCLBN_DLL_API mclSize mclBnG1_serialize(void *buf, mclSize maxBufSize, const mclBnG1 *x);
MCLBN_DLL_API mclSize mclBnG2_serialize(void *buf, mclSize maxBufSize, const mclBnG2 *x); MCLBN_DLL_API mclSize mclBnG2_serialize(void *buf, mclSize maxBufSize, const mclBnG2 *x);
MCLBN_DLL_API mclSize mclBnGT_serialize(void *buf, mclSize maxBufSize, const mclBnGT *x); MCLBN_DLL_API mclSize mclBnGT_serialize(void *buf, mclSize maxBufSize, const mclBnGT *x);
MCLBN_DLL_API mclSize mclBnFp_serialize(void *buf, mclSize maxBufSize, const mclBnFp *x);
MCLBN_DLL_API mclSize mclBnFp2_serialize(void *buf, mclSize maxBufSize, const mclBnFp2 *x);
/* /*
set string set string
@ -202,6 +217,8 @@ MCLBN_DLL_API mclSize mclBnGT_getStr(char *buf, mclSize maxBufSize, const mclBnG
// set zero // set zero
MCLBN_DLL_API void mclBnFr_clear(mclBnFr *x); MCLBN_DLL_API void mclBnFr_clear(mclBnFr *x);
MCLBN_DLL_API void mclBnFp_clear(mclBnFp *x);
MCLBN_DLL_API void mclBnFp2_clear(mclBnFp2 *x);
// set x to y // set x to y
MCLBN_DLL_API void mclBnFr_setInt(mclBnFr *y, mclInt x); MCLBN_DLL_API void mclBnFr_setInt(mclBnFr *y, mclInt x);
@ -209,6 +226,7 @@ MCLBN_DLL_API void mclBnFr_setInt32(mclBnFr *y, int x);
// mask buf with (1 << (bitLen(r) - 1)) - 1 if buf >= r // mask buf with (1 << (bitLen(r) - 1)) - 1 if buf >= r
MCLBN_DLL_API int mclBnFr_setLittleEndian(mclBnFr *x, const void *buf, mclSize bufSize); MCLBN_DLL_API int mclBnFr_setLittleEndian(mclBnFr *x, const void *buf, mclSize bufSize);
MCLBN_DLL_API int mclBnFp_setLittleEndian(mclBnFp *x, const void *buf, mclSize bufSize);
// return 1 if true and 0 otherwise // return 1 if true and 0 otherwise
MCLBN_DLL_API int mclBnFr_isValid(const mclBnFr *x); MCLBN_DLL_API int mclBnFr_isValid(const mclBnFr *x);
@ -216,6 +234,9 @@ MCLBN_DLL_API int mclBnFr_isEqual(const mclBnFr *x, const mclBnFr *y);
MCLBN_DLL_API int mclBnFr_isZero(const mclBnFr *x); MCLBN_DLL_API int mclBnFr_isZero(const mclBnFr *x);
MCLBN_DLL_API int mclBnFr_isOne(const mclBnFr *x); MCLBN_DLL_API int mclBnFr_isOne(const mclBnFr *x);
MCLBN_DLL_API int mclBnFp_isEqual(const mclBnFp *x, const mclBnFp *y);
MCLBN_DLL_API int mclBnFp2_isEqual(const mclBnFp2 *x, const mclBnFp2 *y);
#ifndef MCL_DONT_USE_CSRPNG #ifndef MCL_DONT_USE_CSRPNG
// return 0 if success // return 0 if success
MCLBN_DLL_API int mclBnFr_setByCSPRNG(mclBnFr *x); MCLBN_DLL_API int mclBnFr_setByCSPRNG(mclBnFr *x);
@ -234,7 +255,12 @@ MCLBN_DLL_API void mclBn_setRandFunc(void *self, unsigned int (*readFunc)(void *
// hash(s) and set x // hash(s) and set x
// return 0 if success // return 0 if success
MCLBN_DLL_API int mclBnFr_setHashOf(mclBnFr *x, const void *buf, mclSize bufSize); MCLBN_DLL_API int mclBnFr_setHashOf(mclBnFr *x, const void *buf, mclSize bufSize);
MCLBN_DLL_API int mclBnFp_setHashOf(mclBnFp *x, const void *buf, mclSize bufSize);
// map x to y
// return 0 if success else -1
MCLBN_DLL_API int mclBnFp_mapToG1(mclBnG1 *y, const mclBnFp *x);
MCLBN_DLL_API int mclBnFp2_mapToG2(mclBnG2 *y, const mclBnFp2 *x);
MCLBN_DLL_API void mclBnFr_neg(mclBnFr *y, const mclBnFr *x); MCLBN_DLL_API void mclBnFr_neg(mclBnFr *y, const mclBnFr *x);
MCLBN_DLL_API void mclBnFr_inv(mclBnFr *y, const mclBnFr *x); MCLBN_DLL_API void mclBnFr_inv(mclBnFr *y, const mclBnFr *x);

@ -34,6 +34,12 @@ static const Fp12 *cast(const mclBnGT *p) { return reinterpret_cast<const Fp12*>
static Fp6 *cast(uint64_t *p) { return reinterpret_cast<Fp6*>(p); } static Fp6 *cast(uint64_t *p) { return reinterpret_cast<Fp6*>(p); }
static const Fp6 *cast(const uint64_t *p) { return reinterpret_cast<const Fp6*>(p); } static const Fp6 *cast(const uint64_t *p) { return reinterpret_cast<const Fp6*>(p); }
static Fp2 *cast(mclBnFp2 *p) { return reinterpret_cast<Fp2*>(p); }
static const Fp2 *cast(const mclBnFp2 *p) { return reinterpret_cast<const Fp2*>(p); }
static Fp *cast(mclBnFp *p) { return reinterpret_cast<Fp*>(p); }
static const Fp *cast(const mclBnFp *p) { return reinterpret_cast<const Fp*>(p); }
template<class T> template<class T>
int setStr(T *x, const char *buf, mclSize bufSize, int ioMode) int setStr(T *x, const char *buf, mclSize bufSize, int ioMode)
{ {
@ -525,3 +531,69 @@ void mclBn_verifyOrderG2(int doVerify)
verifyOrderG2(doVerify != 0); verifyOrderG2(doVerify != 0);
} }
mclSize mclBnFp_deserialize(mclBnFp *x, const void *buf, mclSize bufSize)
{
return (mclSize)cast(x)->deserialize(buf, bufSize);
}
mclSize mclBnFp_serialize(void *buf, mclSize maxBufSize, const mclBnFp *x)
{
return (mclSize)cast(x)->serialize(buf, maxBufSize);
}
void mclBnFp_clear(mclBnFp *x)
{
cast(x)->clear();
}
int mclBnFp_setLittleEndian(mclBnFp *x, const void *buf, mclSize bufSize)
{
cast(x)->setArrayMask((const char *)buf, bufSize);
return 0;
}
int mclBnFp_isEqual(const mclBnFp *x, const mclBnFp *y)
{
return *cast(x) == *cast(y);
}
int mclBnFp_setHashOf(mclBnFp *x, const void *buf, mclSize bufSize)
{
cast(x)->setHashOf(buf, bufSize);
return 0;
}
int mclBnFp_mapToG1(mclBnG1 *y, const mclBnFp *x)
{
bool b;
mapToG1(&b, *cast(y), *cast(x));
return b ? 0 : -1;
}
mclSize mclBnFp2_deserialize(mclBnFp2 *x, const void *buf, mclSize bufSize)
{
return (mclSize)cast(x)->deserialize(buf, bufSize);
}
mclSize mclBnFp2_serialize(void *buf, mclSize maxBufSize, const mclBnFp2 *x)
{
return (mclSize)cast(x)->serialize(buf, maxBufSize);
}
void mclBnFp2_clear(mclBnFp2 *x)
{
cast(x)->clear();
}
int mclBnFp2_isEqual(const mclBnFp2 *x, const mclBnFp2 *y)
{
return *cast(x) == *cast(y);
}
int mclBnFp2_mapToG2(mclBnG2 *y, const mclBnFp2 *x)
{
bool b;
mapToG2(&b, *cast(y), *cast(x));
return b ? 0 : -1;
}

@ -405,6 +405,7 @@ Y. Sakemi, Y. Nogami, K. Okeya, Y. Morikawa, CANS 2008.
# History # History
* 2019/Jan/31 add mclBnFp_mapToG1, mclBnFp2_mapToG2
* 2019/Jan/31 fix crash on x64-CPU without AVX * 2019/Jan/31 fix crash on x64-CPU without AVX
# Author # Author

@ -560,3 +560,71 @@ CYBOZU_TEST_AUTO(setRandFunc)
} }
} }
} }
CYBOZU_TEST_AUTO(Fp)
{
mclBnFp x1, x2;
char buf[1024];
int ret = mclBnFp_setHashOf(&x1, "abc", 3);
CYBOZU_TEST_ASSERT(ret == 0);
mclSize n = mclBnFp_serialize(buf, sizeof(buf), &x1);
CYBOZU_TEST_ASSERT(n > 0);
n = mclBnFp_deserialize(&x2, buf, n);
CYBOZU_TEST_ASSERT(n > 0);
CYBOZU_TEST_ASSERT(mclBnFp_isEqual(&x1, &x2));
for (size_t i = 0; i < n; i++) {
buf[i] = char(i);
}
ret = mclBnFp_setLittleEndian(&x1, buf, n);
CYBOZU_TEST_ASSERT(ret == 0);
memset(buf, 0, sizeof(buf));
n = mclBnFp_serialize(buf, sizeof(buf), &x1);
CYBOZU_TEST_ASSERT(n > 0);
for (size_t i = 0; i < n - 1; i++) {
CYBOZU_TEST_EQUAL(buf[i], char(i));
}
mclBnFp_clear(&x1);
memset(&x2, 0, sizeof(x2));
CYBOZU_TEST_ASSERT(mclBnFp_isEqual(&x1, &x2));
}
CYBOZU_TEST_AUTO(Fp2)
{
mclBnFp2 x1, x2;
char buf[1024];
int ret = mclBnFp_setHashOf(&x1.a, "abc", 3);
CYBOZU_TEST_ASSERT(ret == 0);
ret = mclBnFp_setHashOf(&x1.b, "xyz", 3);
CYBOZU_TEST_ASSERT(ret == 0);
mclSize n = mclBnFp2_serialize(buf, sizeof(buf), &x1);
CYBOZU_TEST_ASSERT(n > 0);
n = mclBnFp2_deserialize(&x2, buf, n);
CYBOZU_TEST_ASSERT(n > 0);
CYBOZU_TEST_ASSERT(mclBnFp2_isEqual(&x1, &x2));
mclBnFp2_clear(&x1);
memset(&x2, 0, sizeof(x2));
CYBOZU_TEST_ASSERT(mclBnFp2_isEqual(&x1, &x2));
}
CYBOZU_TEST_AUTO(mapToG1)
{
mclBnFp x;
mclBnG1 P1, P2;
mclBnFp_setHashOf(&x, "abc", 3);
int ret = mclBnFp_mapToG1(&P1, &x);
CYBOZU_TEST_ASSERT(ret == 0);
mclBnG1_hashAndMapTo(&P2, "abc", 3);
CYBOZU_TEST_ASSERT(mclBnG1_isEqual(&P1, &P2));
}
CYBOZU_TEST_AUTO(mapToG2)
{
mclBnFp2 x;
mclBnG2 P1, P2;
mclBnFp_setHashOf(&x.a, "abc", 3);
mclBnFp_clear(&x.b);
int ret = mclBnFp2_mapToG2(&P1, &x);
CYBOZU_TEST_ASSERT(ret == 0);
mclBnG2_hashAndMapTo(&P2, "abc", 3);
CYBOZU_TEST_ASSERT(mclBnG2_isEqual(&P1, &P2));
}

Loading…
Cancel
Save