From b861a41edebe285168cbd2d06f41621d5ae36255 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Fri, 8 Mar 2019 21:59:21 +0900 Subject: add blsSecretKeySetLittleEndianMod --- include/bls/bls.h | 3 +++ src/bls_c_impl.hpp | 6 ++++++ test/bls_c_test.hpp | 32 +++++++++++++++++++++++++++++--- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/include/bls/bls.h b/include/bls/bls.h index 39cbf40..5e590ae 100644 --- a/include/bls/bls.h +++ b/include/bls/bls.h @@ -87,6 +87,9 @@ BLS_DLL_API void blsIdSetInt(blsId *id, int x); // return 0 if success // mask buf with (1 << (bitLen(r) - 1)) - 1 if buf >= r BLS_DLL_API int blsSecretKeySetLittleEndian(blsSecretKey *sec, const void *buf, mclSize bufSize); +// return 0 if success (bufSize <= 64) else -1 +// set (buf mod r) to sec +BLS_DLL_API int blsSecretKeySetLittleEndianMod(blsSecretKey *sec, const void *buf, mclSize bufSize); BLS_DLL_API void blsGetPublicKey(blsPublicKey *pub, const blsSecretKey *sec); diff --git a/src/bls_c_impl.hpp b/src/bls_c_impl.hpp index 197c146..b38c1ad 100644 --- a/src/bls_c_impl.hpp +++ b/src/bls_c_impl.hpp @@ -163,6 +163,12 @@ int blsSecretKeySetLittleEndian(blsSecretKey *sec, const void *buf, mclSize bufS cast(&sec->v)->setArrayMask((const char *)buf, bufSize); return 0; } +int blsSecretKeySetLittleEndianMod(blsSecretKey *sec, const void *buf, mclSize bufSize) +{ + bool b; + cast(&sec->v)->setArray(&b, (const char *)buf, bufSize, mcl::fp::Mod); + return b ? 0 : -1; +} void blsGetPublicKey(blsPublicKey *pub, const blsSecretKey *sec) { diff --git a/test/bls_c_test.hpp b/test/bls_c_test.hpp index cb5c912..b058e6c 100644 --- a/test/bls_c_test.hpp +++ b/test/bls_c_test.hpp @@ -3,6 +3,7 @@ #include #include #include +#include size_t pubSize(size_t FrSize) { @@ -78,7 +79,7 @@ void blsDataTest() CYBOZU_TEST_ASSERT(blsSignatureIsEqual(&sig1, &sig2)); } -void blsOrderTest(const char *curveOrder, const char *fieldOrder) +void blsOrderTest(const char *curveOrder/*Fr*/, const char *fieldOrder/*Fp*/) { char buf[1024]; size_t len; @@ -350,6 +351,30 @@ void blsTrivialShareTest() CYBOZU_TEST_ASSERT(blsPublicKeyIsEqual(&pub1, &pub2)); } +void modTest(const char *rStr) +{ + std::cout << std::hex; + unsigned char buf[1024] = {}; + int ret; + blsSecretKey sec; + const size_t pos = 63; + buf[pos] = 0xff; + mpz_class x = mpz_class(buf[pos]) << (pos * 8); + ret = blsSecretKeySetLittleEndianMod(&sec, buf, pos + 1); + CYBOZU_TEST_EQUAL(ret, 0); + mpz_class r(rStr); + x %= r; + size_t n = blsSecretKeySerialize(buf, sizeof(buf), &sec); + CYBOZU_TEST_ASSERT(n > 0); + // serialized data to mpz_class + mpz_class y = 0; + for (size_t i = 0; i < n; i++) { + y <<= 8; + y += buf[n - 1 - i]; + } + CYBOZU_TEST_EQUAL(x, y); +} + void blsBench() { blsSecretKey sec; @@ -370,8 +395,8 @@ CYBOZU_TEST_AUTO(all) { const struct { int curveType; - const char *p; const char *r; + const char *p; } tbl[] = { { MCL_BN254, @@ -403,11 +428,12 @@ CYBOZU_TEST_AUTO(all) } bls_use_stackTest(); blsDataTest(); - blsOrderTest(tbl[i].p, tbl[i].r); + blsOrderTest(tbl[i].r, tbl[i].p); blsSerializeTest(); if (tbl[i].curveType == MCL_BLS12_381) blsVerifyOrderTest(); blsAddSubTest(); blsTrivialShareTest(); + modTest(tbl[i].r); blsBench(); } } -- cgit v1.2.3