diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp index 8c16468..62d592f 100644 --- a/include/mcl/fp.hpp +++ b/include/mcl/fp.hpp @@ -366,6 +366,29 @@ public: b.p = &v_[0]; } } + /* + write a value with little endian + 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 + { + 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]); + while (n > 0) { + if (src[n - 1]) break; + n--; + } + if (n == 0) n = 1; // zero + if (maxBufSize < n) return 0; + for (size_t i = 0; i < n; i++) { + dst[i] = src[i]; + } + return n; + } void setByCSPRNG(bool *pb, fp::RandGen rg = fp::RandGen()) { if (rg.isZero()) rg = fp::RandGen::get(); diff --git a/test/fp_test.cpp b/test/fp_test.cpp index 6b71075..36ba4d6 100644 --- a/test/fp_test.cpp +++ b/test/fp_test.cpp @@ -672,6 +672,36 @@ void getInt64Test() } } +void getLittleEndianTest() +{ + if (Fp::getOp().bitSize < 80) return; + const struct { + const char *in; + uint8_t out[16]; + size_t size; + } tbl[] = { + { "0", { 0 }, 1 }, + { "1", { 1 }, 1 }, + { "0x1200", { 0x00, 0x12 }, 2 }, + { "0x123400", { 0x00, 0x34, 0x12 }, 3 }, + { "0x1234567890123456ab", { 0xab, 0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12 }, 9 }, + }; + for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) { + Fp x(tbl[i].in); + uint8_t buf[128]; + size_t n = x.getLittleEndian(buf, tbl[i].size); + CYBOZU_TEST_EQUAL(n, tbl[i].size); + CYBOZU_TEST_EQUAL_ARRAY(buf, tbl[i].out, n); + + n = x.getLittleEndian(buf, tbl[i].size + 1); + CYBOZU_TEST_EQUAL(n, tbl[i].size); + CYBOZU_TEST_EQUAL_ARRAY(buf, tbl[i].out, n); + + n = x.getLittleEndian(buf, tbl[i].size - 1); + CYBOZU_TEST_EQUAL(n, 0); + } +} + void divBy2Test() { const int tbl[] = { -4, -3, -2, -1, 0, 1, 2, 3 }; @@ -951,6 +981,7 @@ void sub(mcl::fp::Mode mode) setArrayModTest(); getUint64Test(); getInt64Test(); + getLittleEndianTest(); divBy2Test(); getStrTest(); setHashOfTest();