From bce3026b6d499b049e03c44094437cdf8bee92ee Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Mon, 8 Feb 2016 23:30:58 +0900 Subject: [PATCH] test of addNC/subNC --- include/mcl/fp.hpp | 8 ++++++++ src/fp.cpp | 7 ++++--- test/mont_fp_test.cpp | 10 ++++++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index 09f5909..9387355 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -129,6 +129,14 @@ public: { Gmp::getStr(pstr, op_.mp); } + /* + binary patter of p + @note the value of p is zero + */ + static inline const FpT& getP() + { + return *reinterpret_cast(op_.p); + } static inline bool isOdd(const FpT& x) { fp::Block b; diff --git a/src/fp.cpp b/src/fp.cpp index 08b7b53..1fc761f 100644 --- a/src/fp.cpp +++ b/src/fp.cpp @@ -122,12 +122,12 @@ struct OpeFunc { } Gmp::getArray(z, N, mz); } - // z = x + y + // z = x + y without carry static inline void fp_addNCC(Unit *z, const Unit *x, const Unit *y) { - Unit ret[N + 2]; // not N + 1 + Unit ret[N + 1]; mpz_t mz, mx, my; - set_zero(mz, ret, N + 2); + set_zero(mz, ret, N + 1); set_mpz_t(mx, x); set_mpz_t(my, y); mpz_add(mz, mx, my); @@ -140,6 +140,7 @@ struct OpeFunc { set_zero(mz, ret, N + 1); set_mpz_t(mx, x); set_mpz_t(my, y); + assert(mx >= my); mpz_sub(mz, mx, my); Gmp::getArray(z, N, mz); } diff --git a/test/mont_fp_test.cpp b/test/mont_fp_test.cpp index 7336314..e7927f6 100644 --- a/test/mont_fp_test.cpp +++ b/test/mont_fp_test.cpp @@ -459,9 +459,15 @@ struct Test { { Fp x(5), y(3), z; Fp::addNC(z, x, y); + if (Fp::compareRaw(z, Fp::getP()) >= 0) { + Fp::subNC(z, z, Fp::getP()); + } CYBOZU_TEST_EQUAL(z, Fp(8)); -// Fp::subNC(z, x, y); -// CYBOZU_TEST_EQUAL(z, Fp(2)); + if (Fp::compareRaw(x, y) < 0) { + Fp::addNC(x, x, Fp::getP()); + } + Fp::subNC(x, x, y); + CYBOZU_TEST_EQUAL(x, Fp(2)); } } void power()