set{Little/Big}EndianMod takes const uint8_t*

2merge^2
MITSUNARI Shigeo 4 years ago
parent 6c5fda3e62
commit f83f1ccb72
  1. 45
      include/mcl/fp.hpp
  2. 6
      include/mcl/impl/bn_c_impl.hpp

@ -71,11 +71,10 @@ void expand_message_xmd(uint8_t out[], size_t outSize, const void *msg, size_t m
namespace local { 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++) { 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; *pb = false;
if (fp::isIoSerializeMode(ioMode)) { if (fp::isIoSerializeMode(ioMode)) {
const size_t n = getByteSize(); const size_t n = getByteSize();
v_[op_.N - 1] = 0; uint8_t *buf = (uint8_t*)CYBOZU_ALLOCA(n);
size_t readSize; size_t readSize;
if (ioMode & IoSerializeHexStr) { if (ioMode & IoSerializeHexStr) {
readSize = mcl::fp::readHexStr(v_, n, is); readSize = mcl::fp::readHexStr(buf, n, is);
} else { } else {
readSize = cybozu::readSome(v_, n, is); readSize = cybozu::readSome(buf, n, is);
} }
if (readSize != n) return;
if (isETHserialization_ && ioMode & (IoSerialize | IoSerializeHexStr)) { 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 { } else {
char buf[sizeof(*this) * 8 + 2]; // '0b' + max binary format length char buf[sizeof(*this) * 8 + 2]; // '0b' + max binary format length
size_t n = fp::local::loadWord(buf, sizeof(buf), is); size_t n = fp::local::loadWord(buf, sizeof(buf), is);
@ -430,28 +430,31 @@ public:
} }
/* /*
set (little endian % p) 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) 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; *pb = false;
return; return;
} }
const uint8_t *p = (const uint8_t*)buf; uint8_t swapX[64];
uint8_t swapBuf[64]; for (size_t i = 0; i < xn; i++) {
for (size_t i = 0; i < bufSize; i++) { swapX[xn - 1 - i] = x[i];
swapBuf[bufSize - 1 - i] = p[i];
} }
setArrayMod(pb, swapBuf, bufSize); setArrayMod(pb, swapX, xn);
} }
void setByCSPRNG(bool *pb, fp::RandGen rg = fp::RandGen()) void setByCSPRNG(bool *pb, fp::RandGen rg = fp::RandGen())
{ {
@ -461,13 +464,13 @@ public:
setArrayMask(v_, op_.N); setArrayMask(v_, op_.N);
} }
#ifndef CYBOZU_DONT_USE_EXCEPTION #ifndef CYBOZU_DONT_USE_EXCEPTION
void setLittleEndianMod(const void *buf, size_t bufSize) void setLittleEndianMod(const uint8_t *buf, size_t bufSize)
{ {
bool b; bool b;
setLittleEndianMod(&b, buf, bufSize); setLittleEndianMod(&b, buf, bufSize);
if (!b) throw cybozu::Exception("setLittleEndianMod"); 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; bool b;
setBigEndianMod(&b, buf, bufSize); setBigEndianMod(&b, buf, bufSize);

@ -165,7 +165,7 @@ int mclBnFr_setLittleEndian(mclBnFr *x, const void *buf, mclSize bufSize)
int mclBnFr_setBigEndianMod(mclBnFr *x, const void *buf, mclSize bufSize) int mclBnFr_setBigEndianMod(mclBnFr *x, const void *buf, mclSize bufSize)
{ {
bool b; bool b;
cast(x)->setBigEndianMod(&b, buf, bufSize); cast(x)->setBigEndianMod(&b, (const uint8_t*)buf, bufSize);
return b ? 0 : -1; 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) int mclBnFp_setLittleEndianMod(mclBnFp *x, const void *buf, mclSize bufSize)
{ {
bool b; bool b;
cast(x)->setLittleEndianMod(&b, buf, bufSize); cast(x)->setLittleEndianMod(&b, (const uint8_t*)buf, bufSize);
return b ? 0 : -1; return b ? 0 : -1;
} }
int mclBnFp_setBigEndianMod(mclBnFp *x, const void *buf, mclSize bufSize) int mclBnFp_setBigEndianMod(mclBnFp *x, const void *buf, mclSize bufSize)
{ {
bool b; bool b;
cast(x)->setBigEndianMod(&b, buf, bufSize); cast(x)->setBigEndianMod(&b, (const uint8_t*)buf, bufSize);
return b ? 0 : -1; return b ? 0 : -1;
} }

Loading…
Cancel
Save