From f83f1ccb72677f86c321f3997bf6f2939443e438 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Mon, 3 May 2021 17:33:52 +0900 Subject: [PATCH] set{Little/Big}EndianMod takes const uint8_t* --- include/mcl/fp.hpp | 45 ++++++++++++++++++---------------- include/mcl/impl/bn_c_impl.hpp | 6 ++--- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index 406b8b7..5308436 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -71,11 +71,10 @@ void expand_message_xmd(uint8_t out[], size_t outSize, const void *msg, size_t m namespace local { -inline void byteSwap(void *x, size_t n) +inline void byteSwap(uint8_t *x, size_t n) { - char *p = (char *)x; for (size_t i = 0; i < n / 2; i++) { - fp::swap_(p[i], p[n - 1 - i]); + fp::swap_(x[i], x[n - 1 - i]); } } @@ -266,17 +265,18 @@ public: *pb = false; if (fp::isIoSerializeMode(ioMode)) { const size_t n = getByteSize(); - v_[op_.N - 1] = 0; + uint8_t *buf = (uint8_t*)CYBOZU_ALLOCA(n); size_t readSize; if (ioMode & IoSerializeHexStr) { - readSize = mcl::fp::readHexStr(v_, n, is); + readSize = mcl::fp::readHexStr(buf, n, is); } else { - readSize = cybozu::readSome(v_, n, is); + readSize = cybozu::readSome(buf, n, is); } + if (readSize != n) return; if (isETHserialization_ && ioMode & (IoSerialize | IoSerializeHexStr)) { - fp::local::byteSwap(v_, n); + fp::local::byteSwap(buf, n); } - if (readSize != n) return; + fp::convertArrayAsLE(v_, op_.N, buf, n); } else { char buf[sizeof(*this) * 8 + 2]; // '0b' + max binary format length size_t n = fp::local::loadWord(buf, sizeof(buf), is); @@ -430,28 +430,31 @@ public: } /* set (little endian % p) - error if bufSize > 64 + error if xn > 64 */ - void setLittleEndianMod(bool *pb, const void *buf, size_t bufSize) + void setLittleEndianMod(bool *pb, const uint8_t *x, size_t xn) { - setArrayMod(pb, (const uint8_t *)buf, bufSize); + if (xn > 64) { + *pb = false; + return; + } + setArrayMod(pb, x, xn); } /* set (big endian % p) - error if bufSize > 64 + error if xn > 64 */ - void setBigEndianMod(bool *pb, const void *buf, size_t bufSize) + void setBigEndianMod(bool *pb, const uint8_t *x, size_t xn) { - if (bufSize > 64) { + if (xn > 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]; + uint8_t swapX[64]; + for (size_t i = 0; i < xn; i++) { + swapX[xn - 1 - i] = x[i]; } - setArrayMod(pb, swapBuf, bufSize); + setArrayMod(pb, swapX, xn); } void setByCSPRNG(bool *pb, fp::RandGen rg = fp::RandGen()) { @@ -461,13 +464,13 @@ public: setArrayMask(v_, op_.N); } #ifndef CYBOZU_DONT_USE_EXCEPTION - void setLittleEndianMod(const void *buf, size_t bufSize) + void setLittleEndianMod(const uint8_t *buf, size_t bufSize) { bool b; setLittleEndianMod(&b, buf, bufSize); if (!b) throw cybozu::Exception("setLittleEndianMod"); } - void setBigEndianMod(const void *buf, size_t bufSize) + void setBigEndianMod(const uint8_t *buf, size_t bufSize) { bool b; setBigEndianMod(&b, buf, bufSize); diff --git a/include/mcl/impl/bn_c_impl.hpp b/include/mcl/impl/bn_c_impl.hpp index 4f930e2..dd04b54 100644 --- a/include/mcl/impl/bn_c_impl.hpp +++ b/include/mcl/impl/bn_c_impl.hpp @@ -165,7 +165,7 @@ int mclBnFr_setLittleEndian(mclBnFr *x, const void *buf, mclSize bufSize) int mclBnFr_setBigEndianMod(mclBnFr *x, const void *buf, mclSize bufSize) { bool b; - cast(x)->setBigEndianMod(&b, buf, bufSize); + cast(x)->setBigEndianMod(&b, (const uint8_t*)buf, bufSize); return b ? 0 : -1; } @@ -736,14 +736,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)->setLittleEndianMod(&b, buf, bufSize); + cast(x)->setLittleEndianMod(&b, (const uint8_t*)buf, bufSize); return b ? 0 : -1; } int mclBnFp_setBigEndianMod(mclBnFp *x, const void *buf, mclSize bufSize) { bool b; - cast(x)->setBigEndianMod(&b, buf, bufSize); + cast(x)->setBigEndianMod(&b, (const uint8_t*)buf, bufSize); return b ? 0 : -1; }