add test of edge of Fp::mul

dev
MITSUNARI Shigeo 9 years ago
parent 99b2447e0e
commit 5dfe956eb6
  1. 9
      include/mcl/bn.hpp
  2. 40
      test/fp_tower_test.cpp

@ -149,8 +149,8 @@ struct ParamT {
half = Fp(1) / Fp(2);
Fp2 xi(cp.xi_a, 1);
b_invxi = Fp2(b) / xi;
G1::setParam(Fp(0), Fp(b));
G2::setParam(Fp2(0), b_invxi);
G1::setParam(0, b);
G2::setParam(0, b_invxi);
power(gammar[0], xi, (p - 1) / 6);
for (size_t i = 1; i < gammarN; i++) {
@ -224,17 +224,12 @@ struct Naive {
f = 1;
const int c = (int)mcl::gmp::getBitSize(r);
for (int i = c - 2; i >= 0; i--) {
printf("i=%d\n", i);
Fp12::sqr(f, f);
evalLine(t, T, T, P);
PUT(t);
f *= t;
G2::dbl(T, T);
PUT(T);
if (mcl::gmp::testBit(r, i)) {
evalLine(t, T, Q, P);
puts("in");
PUT(t);
f *= t;
T += Q;
}

@ -36,8 +36,8 @@ void testFp2()
y.a = 3;
y.b = 4;
/*
x = 1 + 2u
y = 3 + 4u
x = 1 + 2i
y = 3 + 4i
*/
add(z, x, y);
CYBOZU_TEST_EQUAL(z, Fp2(4, 6));
@ -45,18 +45,18 @@ void testFp2()
CYBOZU_TEST_EQUAL(z, Fp2(-2, -2));
mul(z, x, y);
/*
(1 + 2u)(3 + 4u) = (3 - 8) + (4 + 6)u = -5 + 10u
(1 + 2i)(3 + 4i) = (3 - 8) + (4 + 6)i = -5 + 10i
*/
CYBOZU_TEST_EQUAL(z, Fp2(-5, 10));
neg(z, z);
CYBOZU_TEST_EQUAL(z, Fp2(5, -10));
/*
xi = 9 + u
(1 - 2u)(9 + u) = (9 + 2) + (1 - 18)u = 11 - 17u
xi = xi_a + i
(1 - 2i)(xi_a + i) = (xi_a + 2) + (1 - 2 xi_a)i
*/
z = Fp2(1, -2);
Fp2::mulXi(z, z);
CYBOZU_TEST_EQUAL(z, Fp2(11, -17));
CYBOZU_TEST_EQUAL(z, Fp2(Fp2::getXi_a() + 2, Fp2::getXi_a() * (-2) + 1));
z = x * x;
sqr(y, x);
CYBOZU_TEST_EQUAL(z, y);
@ -92,6 +92,24 @@ void testFp2()
}
CYBOZU_TEST_EQUAL(z, y);
}
{
mpz_class t = Fp::getOp().mp;
t /= 2;
Fp x;
x.setMpz(t);
CYBOZU_TEST_EQUAL(x * 2, Fp(-1));
t += 1;
x.setMpz(t);
CYBOZU_TEST_EQUAL(x * 2, 1);
}
{
Fp2 a(1, 1);
Fp2 b(1, -1);
Fp2 c(Fp2(2) / a);
CYBOZU_TEST_EQUAL(c, b);
CYBOZU_TEST_EQUAL(a * b, Fp2(2));
CYBOZU_TEST_EQUAL(a * c, Fp2(2));
}
y = x;
inv(y, x);
y *= x;
@ -322,7 +340,7 @@ void test(const char *p, mcl::fp::Mode mode)
{
Fp::setModulo(p, 0, mode);
printf("mode=%s\n", mcl::fp::ModeToStr(mode));
const int xi_a = 9;
const int xi_a = 1;
Fp2::init(xi_a);
if (Fp::getBitSize() > 256) {
printf("not support p=%s\n", p);
@ -370,13 +388,13 @@ void testAll()
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
const char *p = tbl[i];
printf("prime=%s %d\n", p, (int)(strlen(p) - 2) * 4);
test(tbl[i], mcl::fp::FP_GMP);
test(p, mcl::fp::FP_GMP);
#ifdef MCL_USE_LLVM
test(tbl[i], mcl::fp::FP_LLVM);
test(tbl[i], mcl::fp::FP_LLVM_MONT);
test(p, mcl::fp::FP_LLVM);
test(p, mcl::fp::FP_LLVM_MONT);
#endif
#ifdef MCL_USE_XBYAK
test(tbl[i], mcl::fp::FP_XBYAK);
test(p, mcl::fp::FP_XBYAK);
#endif
}
}

Loading…
Cancel
Save