From 525946569532251877ad55dbd203ea99d20171ef Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Mon, 30 Sep 2019 12:45:58 +0900 Subject: [PATCH] add mclBnF{r,p}_isNegative --- include/mcl/bn.h | 5 +++++ include/mcl/impl/bn_c_impl.hpp | 8 ++++++++ test/bn_c_test.hpp | 25 +++++++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/include/mcl/bn.h b/include/mcl/bn.h index 0a5c3b4..e381c49 100644 --- a/include/mcl/bn.h +++ b/include/mcl/bn.h @@ -286,6 +286,11 @@ MCLBN_DLL_API int mclBnFp2_isEqual(const mclBnFp2 *x, const mclBnFp2 *y); MCLBN_DLL_API int mclBnFp2_isZero(const mclBnFp2 *x); MCLBN_DLL_API int mclBnFp2_isOne(const mclBnFp2 *x); +// return 1 if half <= x < r, where half = (r + 1) / 2 else 0 +MCLBN_DLL_API int mclBnFr_isNegative(const mclBnFr *x); +// return 1 if half <= x < p, where half = (p + 1) / 2 else 0 +MCLBN_DLL_API int mclBnFp_isNegative(const mclBnFp *x); + #ifndef MCL_DONT_USE_CSRPNG // return 0 if success MCLBN_DLL_API int mclBnFr_setByCSPRNG(mclBnFr *x); diff --git a/include/mcl/impl/bn_c_impl.hpp b/include/mcl/impl/bn_c_impl.hpp index 18280e6..f9c3f13 100644 --- a/include/mcl/impl/bn_c_impl.hpp +++ b/include/mcl/impl/bn_c_impl.hpp @@ -175,6 +175,10 @@ int mclBnFr_isOne(const mclBnFr *x) { return cast(x)->isOne(); } +int mclBnFr_isNegative(const mclBnFr *x) +{ + return cast(x)->isNegative(); +} #ifndef MCL_DONT_USE_CSRPNG int mclBnFr_setByCSPRNG(mclBnFr *x) @@ -727,6 +731,10 @@ int mclBnFp_isOne(const mclBnFp *x) { return cast(x)->isOne(); } +int mclBnFp_isNegative(const mclBnFp *x) +{ + return cast(x)->isNegative(); +} int mclBnFp_setHashOf(mclBnFp *x, const void *buf, mclSize bufSize) { diff --git a/test/bn_c_test.hpp b/test/bn_c_test.hpp index e1f3ff0..45095b7 100644 --- a/test/bn_c_test.hpp +++ b/test/bn_c_test.hpp @@ -327,6 +327,31 @@ CYBOZU_TEST_AUTO(GT_inv) CYBOZU_TEST_ASSERT(mclBnGT_isOne(&e4)); } +CYBOZU_TEST_AUTO(Fr_isNegative) +{ + mclBnFr a, half, one; + mclBnFr_setInt(&half, 2); + mclBnFr_inv(&half, &half); // half = (r + 1) / 2 + mclBnFr_setInt(&one, 1); + mclBnFr_sub(&a, &half, &one); + CYBOZU_TEST_ASSERT(!mclBnFr_isNegative(&a)); + mclBnFr_add(&a, &a, &one); + CYBOZU_TEST_ASSERT(mclBnFr_isNegative(&a)); +} + +CYBOZU_TEST_AUTO(Fp_isNegative) +{ + mclBnFp a, half, one; + mclBnFp_setInt(&half, 2); + mclBnFp_inv(&half, &half); // half = (p + 1) / 2 + mclBnFp_setInt(&one, 1); + mclBnFp_sub(&a, &half, &one); + CYBOZU_TEST_ASSERT(!mclBnFp_isNegative(&a)); + mclBnFp_add(&a, &a, &one); + CYBOZU_TEST_ASSERT(mclBnFp_isNegative(&a)); +} + + CYBOZU_TEST_AUTO(pairing) { mclBnFr a, b, ab;