Skip to content

Commit

Permalink
add test of mulUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Jun 24, 2024
1 parent 63addd0 commit 9853d26
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ TEST_SRC+=ecdsa_test.cpp ecdsa_c_test.cpp
TEST_SRC+=mul_test.cpp
TEST_SRC+=bint_test.cpp
TEST_SRC+=low_func_test.cpp
TEST_SRC+=smallmodp_test.cpp
ifneq ($(MCL_USE_GMP),1)
TEST_SRC+=static_init_test.cpp
endif
Expand Down
42 changes: 42 additions & 0 deletions test/smallmodp_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <mcl/bls12_381.hpp>
#include <cybozu/xorshift.hpp>
#include <cybozu/benchmark.hpp>
#include <cybozu/test.hpp>

using namespace mcl;
using namespace mcl::bn;

template<class F>
void test(const char *s)
{
puts(s);
cybozu::XorShift rg;
F x, z1, z2;
for (size_t i = 0; i < 1000; i++) {
x.setByCSPRNG(rg);
uint32_t y = uint32_t(i);
z1 = x * y;
F::mulUnit(z2, x, y);
CYBOZU_TEST_EQUAL(z1, z2);
}
#ifdef NDEBUG
const int C = 100000;
for (uint32_t i = 1; i < 10; i++) {
printf("i=% 2d ", i);
CYBOZU_BENCH_C("mulUnit", C, F::mulUnit, x, x, i);
}
CYBOZU_BENCH_C("mulUnit [1-256]", C, F::mulUnit, x, x, (*x.getUnit() % 256) + 1);
CYBOZU_BENCH_C("mulUnit [1000-1255]", C, F::mulUnit, x, x, (*x.getUnit() % 256) + 1000);
CYBOZU_BENCH_C("mulUnit all", C, F::mulUnit, x, x, uint32_t(*x.getUnit()));
CYBOZU_BENCH_C("mul(F, u32)", C, F::mul, x, x, uint32_t(*x.getUnit()));
CYBOZU_BENCH_C("mul(F, F)", C, F::mul, x, x, x);
CYBOZU_TEST_ASSERT(x != 0);
#endif
}

CYBOZU_TEST_AUTO(main)
{
initPairing(BLS12_381);
test<Fr>("Fr");
test<Fp>("Fp");
}

0 comments on commit 9853d26

Please sign in to comment.