From 04b13fb039ddb74a098c5c46cbde606d29f74cbe Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Mon, 3 May 2021 14:51:54 +0900 Subject: [PATCH] refactor setArrayMask and requires unsigned --- include/mcl/fp.hpp | 48 ++++++++++++++++++++-------------- include/mcl/impl/bn_c_impl.hpp | 6 ++--- test/fp_test.cpp | 6 ++--- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index 88515d4..4e45814 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -339,24 +339,44 @@ public: cybozu::write(pb, os, buf + sizeof(buf) - len, len); } /* - set array x as little endian + treat x as little endian + if x >= p then error */ template - void setArray_(bool *pb, const S *x, size_t n, mcl::fp::MaskMode mode = fp::NoMask) - { - *pb = fp::copyAndMask(v_, x, sizeof(S) * n, op_, mode); - toMont(); - } - template void setArray(bool *pb, const S *x, size_t n) { - if (!fp::convertArrayAsLE(v_, op_.N, x, n) || fp::isGreaterOrEqualArray(v_, op_.p, op_.N)) { + if (!fp::convertArrayAsLE(v_, op_.N, x, n)) { + *pb = false; + return; + } + if (fp::isGreaterOrEqualArray(v_, op_.p, op_.N)) { *pb = false; return; } *pb = true; toMont(); } + /* + treat x as little endian + x &= (1 << bitLen) = 1 + x &= (1 << (bitLen - 1)) - 1 if x >= p + */ + template + void setArrayMask(const S *x, size_t n) + { + const size_t dstByte = sizeof(fp::Unit) * op_.N; + if (sizeof(S) * n > dstByte) { + n = dstByte / sizeof(S); + } + bool b = fp::convertArrayAsLE(v_, op_.N, x, n); + assert(b); + (void)b; + fp::maskArray(v_, op_.N, op_.bitSize); + if (fp::isGreaterOrEqualArray(v_, op_.p, op_.N)) { + fp::maskArray(v_, op_.N, op_.bitSize - 1); + } + toMont(); + } /* set (x as little endian) % p error if size of x >= sizeof(Fp) * 2 @@ -380,16 +400,6 @@ public: if (!*pb) return; toMont(); } - - /* - mask x with (1 << (bitLen - 1)) - 1 if x >= p - */ - template - void setArrayMask(const S *x, size_t n) - { - fp::copyAndMask(v_, x, sizeof(S) * n, op_, fp::SmallMask); - toMont(); - } void getBlock(fp::Block& b) const { b.n = op_.N; @@ -488,7 +498,7 @@ public: */ void setHashOf(const void *msg, size_t msgSize) { - char buf[MCL_MAX_HASH_BIT_SIZE / 8]; + uint8_t buf[MCL_MAX_HASH_BIT_SIZE / 8]; uint32_t size = op_.hash(buf, static_cast(sizeof(buf)), msg, static_cast(msgSize)); setArrayMask(buf, size); } diff --git a/include/mcl/impl/bn_c_impl.hpp b/include/mcl/impl/bn_c_impl.hpp index b63c9b8..4f930e2 100644 --- a/include/mcl/impl/bn_c_impl.hpp +++ b/include/mcl/impl/bn_c_impl.hpp @@ -159,7 +159,7 @@ int mclBnFr_setStr(mclBnFr *x, const char *buf, mclSize bufSize, int ioMode) } int mclBnFr_setLittleEndian(mclBnFr *x, const void *buf, mclSize bufSize) { - cast(x)->setArrayMask((const char *)buf, bufSize); + cast(x)->setArrayMask((const uint8_t *)buf, bufSize); return 0; } int mclBnFr_setBigEndianMod(mclBnFr *x, const void *buf, mclSize bufSize) @@ -176,7 +176,7 @@ mclSize mclBnFr_getLittleEndian(void *buf, mclSize maxBufSize, const mclBnFr *x) int mclBnFr_setLittleEndianMod(mclBnFr *x, const void *buf, mclSize bufSize) { bool b; - cast(x)->setArrayMod(&b, (const char *)buf, bufSize); + cast(x)->setArrayMod(&b, (const uint8_t *)buf, bufSize); return b ? 0 : -1; } mclSize mclBnFr_deserialize(mclBnFr *x, const void *buf, mclSize bufSize) @@ -729,7 +729,7 @@ void mclBnFp_clear(mclBnFp *x) int mclBnFp_setLittleEndian(mclBnFp *x, const void *buf, mclSize bufSize) { - cast(x)->setArrayMask((const char *)buf, bufSize); + cast(x)->setArrayMask((const uint8_t *)buf, bufSize); return 0; } diff --git a/test/fp_test.cpp b/test/fp_test.cpp index c863a66..496c8a0 100644 --- a/test/fp_test.cpp +++ b/test/fp_test.cpp @@ -547,11 +547,11 @@ void setArrayTest2(mcl::fp::Mode mode) void setArrayMaskTest1() { - char b1[] = { 0x56, 0x34, 0x12 }; + uint8_t b1[] = { 0x56, 0x34, 0x12 }; Fp x; x.setArrayMask(b1, 3); CYBOZU_TEST_EQUAL(x, 0x123456); - int b2[] = { 0x12, 0x34 }; + uint32_t b2[] = { 0x12, 0x34 }; x.setArrayMask(b2, 2); CYBOZU_TEST_EQUAL(x, Fp("0x3400000012")); } @@ -811,7 +811,7 @@ void setHashOfTest() digest = cybozu::Sha512().digest(msgTbl[i]); } Fp x, y; - x.setArrayMask(digest.c_str(), digest.size()); + x.setArrayMask((const uint8_t*)digest.c_str(), digest.size()); y.setHashOf(msgTbl[i]); CYBOZU_TEST_EQUAL(x, y); }