From c68217b9a75d5c6a713b7adb8db8d304ef50411b Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Mon, 30 Sep 2019 14:20:24 +0900 Subject: [PATCH] add mclBnF{p,r}_isOdd --- include/mcl/bn.h | 10 ++++++---- include/mcl/impl/bn_c_impl.hpp | 8 ++++++++ test/bn_c_test.hpp | 21 +++++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/include/mcl/bn.h b/include/mcl/bn.h index b910025..6e5d1ff 100644 --- a/include/mcl/bn.h +++ b/include/mcl/bn.h @@ -279,20 +279,22 @@ MCLBN_DLL_API int mclBnFr_isValid(const mclBnFr *x); 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 mclBnFr_isOdd(const mclBnFr *x); +// return 1 if half <= x < r, where half = (r + 1) / 2 else 0 +MCLBN_DLL_API int mclBnFr_isNegative(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 mclBnFp_isOdd(const mclBnFp *x); +// return 1 if half <= x < p, where half = (p + 1) / 2 else 0 +MCLBN_DLL_API int mclBnFp_isNegative(const mclBnFp *x); 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 diff --git a/include/mcl/impl/bn_c_impl.hpp b/include/mcl/impl/bn_c_impl.hpp index f9c3f13..51112a7 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_isOdd(const mclBnFr *x) +{ + return cast(x)->isOdd(); +} int mclBnFr_isNegative(const mclBnFr *x) { return cast(x)->isNegative(); @@ -731,6 +735,10 @@ int mclBnFp_isOne(const mclBnFp *x) { return cast(x)->isOne(); } +int mclBnFp_isOdd(const mclBnFp *x) +{ + return cast(x)->isOdd(); +} int mclBnFp_isNegative(const mclBnFp *x) { return cast(x)->isNegative(); diff --git a/test/bn_c_test.hpp b/test/bn_c_test.hpp index 45095b7..9c1818b 100644 --- a/test/bn_c_test.hpp +++ b/test/bn_c_test.hpp @@ -351,6 +351,27 @@ CYBOZU_TEST_AUTO(Fp_isNegative) CYBOZU_TEST_ASSERT(mclBnFp_isNegative(&a)); } +CYBOZU_TEST_AUTO(Fr_isOdd) +{ + mclBnFr x, one; + mclBnFr_clear(&x); + mclBnFr_setInt(&one, 1); + for (size_t i = 0; i < 100; i++) { + CYBOZU_TEST_EQUAL(mclBnFr_isOdd(&x), i & 1); + mclBnFr_add(&x, &x, &one); + } +} + +CYBOZU_TEST_AUTO(Fp_isOdd) +{ + mclBnFp x, one; + mclBnFp_clear(&x); + mclBnFp_setInt(&one, 1); + for (size_t i = 0; i < 100; i++) { + CYBOZU_TEST_EQUAL(mclBnFp_isOdd(&x), i & 1); + mclBnFp_add(&x, &x, &one); + } +} CYBOZU_TEST_AUTO(pairing) {