diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index 5308436..4007ae3 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -410,21 +410,23 @@ public: write buf[0] = 0 and return 1 if the value is 0 return written size if success else 0 */ - size_t getLittleEndian(void *buf, size_t maxBufSize) const + size_t getLittleEndian(uint8_t *buf, size_t maxN) const { fp::Block b; getBlock(b); - const uint8_t *src = (const uint8_t *)b.p; - uint8_t *dst = (uint8_t *)buf; - size_t n = b.n * sizeof(b.p[0]); + size_t n = sizeof(fp::Unit) * b.n; + uint8_t *t = (uint8_t*)CYBOZU_ALLOCA(n); + if (!fp::convertArrayAsLE(t, n, b.p, b.n)) { + return 0; + } while (n > 0) { - if (src[n - 1]) break; + if (t[n - 1]) break; n--; } if (n == 0) n = 1; // zero - if (maxBufSize < n) return 0; + if (maxN < n) return 0; for (size_t i = 0; i < n; i++) { - dst[i] = src[i]; + buf[i] = t[i]; } return n; } diff --git a/include/mcl/impl/bn_c_impl.hpp b/include/mcl/impl/bn_c_impl.hpp index dd04b54..5cefadf 100644 --- a/include/mcl/impl/bn_c_impl.hpp +++ b/include/mcl/impl/bn_c_impl.hpp @@ -171,7 +171,7 @@ int mclBnFr_setBigEndianMod(mclBnFr *x, const void *buf, mclSize bufSize) mclSize mclBnFr_getLittleEndian(void *buf, mclSize maxBufSize, const mclBnFr *x) { - return cast(x)->getLittleEndian(buf, maxBufSize); + return cast(x)->getLittleEndian((uint8_t*)buf, maxBufSize); } int mclBnFr_setLittleEndianMod(mclBnFr *x, const void *buf, mclSize bufSize) { @@ -749,7 +749,7 @@ int mclBnFp_setBigEndianMod(mclBnFp *x, const void *buf, mclSize bufSize) mclSize mclBnFp_getLittleEndian(void *buf, mclSize maxBufSize, const mclBnFp *x) { - return cast(x)->getLittleEndian(buf, maxBufSize); + return cast(x)->getLittleEndian((uint8_t*)buf, maxBufSize); } int mclBnFp_isValid(const mclBnFp *x) {