add mclBnFp_* like as mclBnFp_*

update-fork
MITSUNARI Shigeo 5 years ago
parent 55a1de171b
commit 21dedae745
  1. 14
      include/mcl/bn.h
  2. 56
      include/mcl/impl/bn_c_impl.hpp
  3. 99
      test/bn_c_test.hpp

@ -238,6 +238,8 @@ MCLBN_DLL_API void mclBnFp2_clear(mclBnFp2 *x);
// set x to y
MCLBN_DLL_API void mclBnFr_setInt(mclBnFr *y, mclInt x);
MCLBN_DLL_API void mclBnFr_setInt32(mclBnFr *y, int x);
MCLBN_DLL_API void mclBnFp_setInt(mclBnFp *y, mclInt x);
MCLBN_DLL_API void mclBnFp_setInt32(mclBnFp *y, int x);
// x = buf & (1 << bitLen(r)) - 1
// if (x >= r) x &= (1 << (bitLen(r) - 1)) - 1
@ -266,12 +268,16 @@ 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_isOne(const mclBnFr *x);
MCLBN_DLL_API int mclBnFp_isValid(const mclBnFp *x);
MCLBN_DLL_API int mclBnFp_isEqual(const mclBnFp *x, const mclBnFp *y);
MCLBN_DLL_API int mclBnFp_isZero(const mclBnFp *x);
MCLBN_DLL_API int mclBnFp_isOne(const mclBnFp *x);
MCLBN_DLL_API int mclBnFp2_isEqual(const mclBnFp2 *x, const mclBnFp2 *y);
#ifndef MCL_DONT_USE_CSRPNG
// return 0 if success
MCLBN_DLL_API int mclBnFr_setByCSPRNG(mclBnFr *x);
MCLBN_DLL_API int mclBnFp_setByCSPRNG(mclBnFp *x);
/*
set user-defined random function for setByCSPRNG
@ -302,6 +308,14 @@ MCLBN_DLL_API void mclBnFr_sub(mclBnFr *z, const mclBnFr *x, const mclBnFr *y);
MCLBN_DLL_API void mclBnFr_mul(mclBnFr *z, const mclBnFr *x, const mclBnFr *y);
MCLBN_DLL_API void mclBnFr_div(mclBnFr *z, const mclBnFr *x, const mclBnFr *y);
MCLBN_DLL_API void mclBnFp_neg(mclBnFp *y, const mclBnFp *x);
MCLBN_DLL_API void mclBnFp_inv(mclBnFp *y, const mclBnFp *x);
MCLBN_DLL_API void mclBnFp_sqr(mclBnFp *y, const mclBnFp *x);
MCLBN_DLL_API void mclBnFp_add(mclBnFp *z, const mclBnFp *x, const mclBnFp *y);
MCLBN_DLL_API void mclBnFp_sub(mclBnFp *z, const mclBnFp *x, const mclBnFp *y);
MCLBN_DLL_API void mclBnFp_mul(mclBnFp *z, const mclBnFp *x, const mclBnFp *y);
MCLBN_DLL_API void mclBnFp_div(mclBnFp *z, const mclBnFp *x, const mclBnFp *y);
////////////////////////////////////////////////
// set zero
MCLBN_DLL_API void mclBnG1_clear(mclBnG1 *x);

@ -183,6 +183,12 @@ int mclBnFr_setByCSPRNG(mclBnFr *x)
cast(x)->setByCSPRNG(&b);
return b ? 0 : -1;
}
int mclBnFp_setByCSPRNG(mclBnFp *x)
{
bool b;
cast(x)->setByCSPRNG(&b);
return b ? 0 : -1;
}
void mclBn_setRandFunc(void *self, unsigned int (*readFunc)(void *self, void *buf, unsigned int bufSize))
{
mcl::fp::RandGen::setRandFunc(self, readFunc);
@ -234,6 +240,35 @@ void mclBnFr_div(mclBnFr *z, const mclBnFr *x, const mclBnFr *y)
Fr::div(*cast(z),*cast(x), *cast(y));
}
void mclBnFp_neg(mclBnFp *y, const mclBnFp *x)
{
Fp::neg(*cast(y), *cast(x));
}
void mclBnFp_inv(mclBnFp *y, const mclBnFp *x)
{
Fp::inv(*cast(y), *cast(x));
}
void mclBnFp_sqr(mclBnFp *y, const mclBnFp *x)
{
Fp::sqr(*cast(y), *cast(x));
}
void mclBnFp_add(mclBnFp *z, const mclBnFp *x, const mclBnFp *y)
{
Fp::add(*cast(z),*cast(x), *cast(y));
}
void mclBnFp_sub(mclBnFp *z, const mclBnFp *x, const mclBnFp *y)
{
Fp::sub(*cast(z),*cast(x), *cast(y));
}
void mclBnFp_mul(mclBnFp *z, const mclBnFp *x, const mclBnFp *y)
{
Fp::mul(*cast(z),*cast(x), *cast(y));
}
void mclBnFp_div(mclBnFp *z, const mclBnFp *x, const mclBnFp *y)
{
Fp::div(*cast(z),*cast(x), *cast(y));
}
////////////////////////////////////////////////
// set zero
void mclBnG1_clear(mclBnG1 *x)
@ -564,6 +599,15 @@ void mclBn_verifyOrderG2(int doVerify)
verifyOrderG2(doVerify != 0);
}
void mclBnFp_setInt(mclBnFp *y, mclInt x)
{
*cast(y) = x;
}
void mclBnFp_setInt32(mclBnFp *y, int x)
{
*cast(y) = x;
}
mclSize mclBnFp_getStr(char *buf, mclSize maxBufSize, const mclBnFp *x, int ioMode)
{
return cast(x)->getStr(buf, maxBufSize, ioMode);
@ -604,10 +648,22 @@ mclSize mclBnFp_getLittleEndian(void *buf, mclSize maxBufSize, const mclBnFp *x)
{
return cast(x)->getLittleEndian(buf, maxBufSize);
}
int mclBnFp_isValid(const mclBnFp *x)
{
return cast(x)->isValid();
}
int mclBnFp_isEqual(const mclBnFp *x, const mclBnFp *y)
{
return *cast(x) == *cast(y);
}
int mclBnFp_isZero(const mclBnFp *x)
{
return cast(x)->isZero();
}
int mclBnFp_isOne(const mclBnFp *x)
{
return cast(x)->isOne();
}
int mclBnFp_setHashOf(mclBnFp *x, const void *buf, mclSize bufSize)
{

@ -568,6 +568,94 @@ CYBOZU_TEST_AUTO(setRandFunc)
}
}
CYBOZU_TEST_AUTO(Fp_1)
{
mclBnFp x, y;
memset(&x, 0xff, sizeof(x));
CYBOZU_TEST_ASSERT(!mclBnFp_isValid(&x));
CYBOZU_TEST_ASSERT(!mclBnFp_isZero(&x));
mclBnFp_clear(&x);
CYBOZU_TEST_ASSERT(mclBnFp_isZero(&x));
mclBnFp_setInt(&x, 1);
CYBOZU_TEST_ASSERT(mclBnFp_isOne(&x));
mclBnFp_setInt(&y, -1);
CYBOZU_TEST_ASSERT(!mclBnFp_isEqual(&x, &y));
y = x;
CYBOZU_TEST_ASSERT(mclBnFp_isEqual(&x, &y));
mclBnFp_setHashOf(&x, "", 0);
mclBnFp_setHashOf(&y, "abc", 3);
CYBOZU_TEST_ASSERT(!mclBnFp_isEqual(&x, &y));
mclBnFp_setHashOf(&x, "abc", 3);
CYBOZU_TEST_ASSERT(mclBnFp_isEqual(&x, &y));
char buf[1024];
mclBnFp_setInt(&x, 12345678);
size_t size;
size = mclBnFp_getStr(buf, sizeof(buf), &x, 10);
CYBOZU_TEST_EQUAL(size, 8);
CYBOZU_TEST_EQUAL(buf, "12345678");
mclBnFp_setInt(&x, -7654321);
mclBnFp_neg(&x, &x);
size = mclBnFp_getStr(buf, sizeof(buf), &x, 10);
CYBOZU_TEST_EQUAL(size, 7);
CYBOZU_TEST_EQUAL(buf, "7654321");
mclBnFp_setInt(&y, 123 - 7654321);
mclBnFp_add(&x, &x, &y);
size = mclBnFp_getStr(buf, sizeof(buf), &x, 10);
CYBOZU_TEST_EQUAL(size, 3);
CYBOZU_TEST_EQUAL(buf, "123");
mclBnFp_setInt(&y, 100);
mclBnFp_sub(&x, &x, &y);
size = mclBnFp_getStr(buf, sizeof(buf), &x, 10);
CYBOZU_TEST_EQUAL(size, 2);
CYBOZU_TEST_EQUAL(buf, "23");
mclBnFp_mul(&x, &x, &y);
size = mclBnFp_getStr(buf, sizeof(buf), &x, 10);
CYBOZU_TEST_EQUAL(size, 4);
CYBOZU_TEST_EQUAL(buf, "2300");
mclBnFp_div(&x, &x, &y);
size = mclBnFp_getStr(buf, sizeof(buf), &x, 10);
CYBOZU_TEST_EQUAL(size, 2);
CYBOZU_TEST_EQUAL(buf, "23");
mclBnFp_mul(&x, &y, &y);
mclBnFp_sqr(&y, &y);
CYBOZU_TEST_ASSERT(mclBnFp_isEqual(&x, &y));
const char *s = "12345678901234567";
CYBOZU_TEST_ASSERT(!mclBnFp_setStr(&x, s, strlen(s), 10));
s = "20000000000000000";
CYBOZU_TEST_ASSERT(!mclBnFp_setStr(&y, s, strlen(s), 10));
mclBnFp_add(&x, &x, &y);
size = mclBnFp_getStr(buf, sizeof(buf), &x, 10);
CYBOZU_TEST_EQUAL(size, 17);
CYBOZU_TEST_EQUAL(buf, "32345678901234567");
mclBnFp_setInt(&x, 1);
mclBnFp_neg(&x, &x);
size = mclBnFp_getStr(buf, sizeof(buf), &x, 10);
CYBOZU_TEST_ASSERT(size > 0);
CYBOZU_TEST_EQUAL(size, strlen(buf));
CYBOZU_TEST_ASSERT(!mclBnFp_setStr(&y, buf, size, 10));
CYBOZU_TEST_ASSERT(mclBnFp_isEqual(&x, &y));
for (int i = 0; i < 10; i++) {
mclBnFp_setByCSPRNG(&x);
mclBnFp_getStr(buf, sizeof(buf), &x, 16);
printf("%s\n", buf);
}
}
CYBOZU_TEST_AUTO(Fp)
{
mclBnFp x1, x2;
@ -593,6 +681,17 @@ CYBOZU_TEST_AUTO(Fp)
mclBnFp_clear(&x1);
memset(&x2, 0, sizeof(x2));
CYBOZU_TEST_ASSERT(mclBnFp_isEqual(&x1, &x2));
mclBnFp_clear(&x1);
CYBOZU_TEST_ASSERT(mclBnFp_isZero(&x1));
mclBnFp_setInt(&x1, 1);
CYBOZU_TEST_ASSERT(mclBnFp_isOne(&x1));
mclBnFp_setInt(&x1, -1);
CYBOZU_TEST_ASSERT(!mclBnFp_isOne(&x1));
mclBnFp_neg(&x1, &x1);
CYBOZU_TEST_ASSERT(mclBnFp_isOne(&x1));
}
CYBOZU_TEST_AUTO(mod)

Loading…
Cancel
Save