From 4400b61efbe0ec8f2e7f4abedcd7c48bf39da7af Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Mon, 29 May 2017 08:36:39 +0900 Subject: [PATCH] change spec of copyAndMask --- include/mcl/fp.hpp | 2 +- src/fp.cpp | 5 ++++- test/fp_test.cpp | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index 764191e..de63e3e 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -226,7 +226,7 @@ public: toMont(); } /* - mask inBuf with (1 << (bitLen - 1)) - 1 + mask inBuf with (1 << (bitLen - 1)) - 1 if x >= p */ template void setArrayMask(const S *inBuf, size_t n) diff --git a/src/fp.cpp b/src/fp.cpp index 7de679c..65fdc85 100644 --- a/src/fp.cpp +++ b/src/fp.cpp @@ -607,7 +607,10 @@ void copyAndMask(Unit *y, const void *x, size_t xByteSize, const Op& op, bool do if (isGreaterOrEqualArray(y, op.p, op.N)) throw cybozu::Exception("fp:copyAndMask:large x"); return; } - maskArray(y, op.N, op.bitSize - 1); + maskArray(y, op.N, op.bitSize); + if (isGreaterOrEqualArray(y, op.p, op.N)) { + maskArray(y, op.N, op.bitSize - 1); + } assert(isLessArray(y, op.p, op.N)); } diff --git a/test/fp_test.cpp b/test/fp_test.cpp index a601517..5c6a05c 100644 --- a/test/fp_test.cpp +++ b/test/fp_test.cpp @@ -545,7 +545,7 @@ void setArrayMaskTest2(mcl::fp::Mode mode) } tbl[] = { { { 0x234567a4, 0x00000001, 0x00100000}, 1, "0x234567a4" }, { { 0x234567a4, 0x00000001, 0x00100000}, 2, "0x1234567a4" }, - { { 0x234567a4, 0x00000001, 0x00100000}, 3, "0x00000000000001234567a4" }, + { { 0x234567a4, 0x00000001, 0x00100000}, 3, "0x10000000000001234567a4" }, { { 0x234567a5, 0xfffffff1, 0xffffffff}, 3, "0x0ffffffffffff1234567a5" }, }; Fp x;