setBigEndianMod

update-fork
MITSUNARI Shigeo 5 years ago
parent 7b4eb83d5b
commit d51fd79c86
  1. 2
      include/mcl/bn.h
  2. 37
      include/mcl/fp.hpp
  3. 9
      include/mcl/impl/bn_c_impl.hpp
  4. 7
      include/mcl/mapto_wb19.hpp

@ -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);

@ -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;

@ -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;
}

@ -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;
}
}

Loading…
Cancel
Save