diff --git a/include/mcl/bn.h b/include/mcl/bn.h index b2211b7..905733d 100644 --- a/include/mcl/bn.h +++ b/include/mcl/bn.h @@ -317,9 +317,11 @@ MCLBN_DLL_API mclSize mclBnFp_getLittleEndian(void *buf, mclSize maxBufSize, con // set (buf mod r) to x // return 0 if bufSize <= (byte size of Fr * 2) else -1 MCLBN_DLL_API int mclBnFr_setLittleEndianMod(mclBnFr *x, const void *buf, mclSize bufSize); +MCLBN_DLL_API int mclBnFr_setBigEndianMod(mclBnFr *x, const void *buf, mclSize bufSize); // set (buf mod p) to x // return 0 if bufSize <= (byte size of Fp * 2) else -1 MCLBN_DLL_API int mclBnFp_setLittleEndianMod(mclBnFp *x, const void *buf, mclSize bufSize); +MCLBN_DLL_API int mclBnFp_setBigEndianMod(mclBnFp *x, const void *buf, mclSize bufSize); // return 1 if true and 0 otherwise MCLBN_DLL_API int mclBnFr_isValid(const mclBnFr *x); diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index 5de0dfb..96110a8 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -408,6 +408,31 @@ public: } return n; } + /* + set (little endian % p) + error if bufSize > 64 + */ + void setLittleEndianMod(bool *pb, const void *buf, size_t bufSize) + { + setArray(pb, (const char *)buf, bufSize, mcl::fp::Mod); + } + /* + set (big endian % p) + error if bufSize > 64 + */ + void setBigEndianMod(bool *pb, const void *buf, size_t bufSize) + { + if (bufSize > 64) { + *pb = false; + return; + } + const uint8_t *p = (const uint8_t*)buf; + uint8_t swapBuf[64]; + for (size_t i = 0; i < bufSize; i++) { + swapBuf[bufSize - 1 - i] = p[i]; + } + setArray(pb, swapBuf, bufSize, mcl::fp::Mod); + } void setByCSPRNG(bool *pb, fp::RandGen rg = fp::RandGen()) { if (rg.isZero()) rg = fp::RandGen::get(); @@ -416,6 +441,18 @@ public: setArrayMask(v_, op_.N); } #ifndef CYBOZU_DONT_USE_EXCEPTION + void setLittleEndianMod(const void *buf, size_t bufSize) + { + bool b; + setLittleEndianMod(&b, buf, bufSize, mcl::fp::Mod); + if (!b) throw cybozu::Exception("setLittleEndianMod"); + } + void setBigEndianMod(const void *buf, size_t bufSize) + { + bool b; + setBigEndianMod(&b, buf, bufSize, mcl::fp::Mod); + if (!b) throw cybozu::Exception("setBigEndianMod"); + } void setByCSPRNG(fp::RandGen rg = fp::RandGen()) { bool b; diff --git a/include/mcl/impl/bn_c_impl.hpp b/include/mcl/impl/bn_c_impl.hpp index f31380b..d601861 100644 --- a/include/mcl/impl/bn_c_impl.hpp +++ b/include/mcl/impl/bn_c_impl.hpp @@ -748,7 +748,14 @@ int mclBnFp_setLittleEndian(mclBnFp *x, const void *buf, mclSize bufSize) int mclBnFp_setLittleEndianMod(mclBnFp *x, const void *buf, mclSize bufSize) { bool b; - cast(x)->setArray(&b, (const char *)buf, bufSize, mcl::fp::Mod); + cast(x)->setLittleEndianMod(&b, buf, bufSize); + return b ? 0 : -1; +} + +int mclBnFp_setBigEndianMod(mclBnFp *x, const void *buf, mclSize bufSize) +{ + bool b; + cast(x)->setBigEndianMod(&b, buf, bufSize); return b ? 0 : -1; } diff --git a/include/mcl/mapto_wb19.hpp b/include/mcl/mapto_wb19.hpp index 45e31c8..6b353bc 100644 --- a/include/mcl/mapto_wb19.hpp +++ b/include/mcl/mapto_wb19.hpp @@ -30,9 +30,8 @@ inline void hashToFp2old(Fp2& out, const void *msg, size_t msgSize, uint8_t ctr, info_pfx[4] = char(i + 1); uint8_t t[64]; fp::hkdf_expand(t, msg_prime, info_pfx); - fp::local::byteSwap(t, 64); bool b; - out.getFp0()[i].setArrayMod(&b, t, 64); + out.getFp0()[i].setBigEndianMod(&b, t, 64); assert(b); (void)b; } } @@ -450,10 +449,8 @@ struct MapToG2_WB19 { } Fp *x = out[0].getFp0(); for (size_t i = 0; i < 4; i++) { - uint8_t *p = &md[64 * i]; - fp::local::byteSwap(p, 64); bool b; - x[i].setArrayMod(&b, p, 64); + x[i].setBigEndianMod(&b, &md[64 * i], 64); assert(b); (void)b; } }