From 32f0347498c119e243cfda6048aa1c1181b69d3e Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Fri, 7 Oct 2016 16:31:08 +0900 Subject: [PATCH] add generic SqrMont, Mul, Sqr --- Makefile | 4 -- src/fp.cpp | 86 +++---------------------------------- src/fp_proto.hpp | 109 ++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 97 insertions(+), 102 deletions(-) diff --git a/Makefile b/Makefile index 4e9cd7e..55af2cd 100644 --- a/Makefile +++ b/Makefile @@ -50,11 +50,9 @@ ifeq ($(USE_LOW_ASM),1) endif $(MCL_LIB): $(LIB_OBJ) - -$(MKDIR) $(@D) $(AR) $@ $(LIB_OBJ) $(ASM_OBJ): $(ASM_SRC) - -$(MKDIR) $(@D) $(PRE)$(CXX) -c $< -o $@ $(CFLAGS) $(ASM_SRC): $(LLVM_SRC) @@ -87,11 +85,9 @@ VPATH=test sample src .SUFFIXES: .cpp .d .exe $(OBJ_DIR)/%.o: %.cpp - -$(MKDIR) $(@D) $(PRE)$(CXX) $(CFLAGS) -c $< -o $@ -MMD -MP -MF $(@:.o=.d) $(EXE_DIR)/%.exe: $(OBJ_DIR)/%.o $(MCL_LIB) - -$(MKDIR) $(@D) $(PRE)$(CXX) $< -o $@ $(MCL_LIB) $(LDFLAGS) SAMPLE_EXE=$(addprefix $(EXE_DIR)/,$(SAMPLE_SRC:.cpp=.exe)) diff --git a/src/fp.cpp b/src/fp.cpp index 6f09bd0..7ff29fc 100644 --- a/src/fp.cpp +++ b/src/fp.cpp @@ -122,33 +122,14 @@ template<>const void3u MontRed::f = &mcl_fp_montRed ## n ## L; \ template<>const void4u DblAdd::f = &mcl_fpDbl_add ## n ## L; \ template<>const void4u DblSub::f = &mcl_fpDbl_sub ## n ## L; \ +// use Dbl_Mod of gmp template -struct Mul { - static inline void func(Unit *z, const Unit *x, const Unit *y, const Unit *p) - { - Unit xy[N * 2]; - MulPre::f(xy, x, y); - Dbl_Mod::f(z, xy, p); - } - static const void4u f; -}; - -template -const void4u Mul::f = Mul::func; - -template -struct Sqr { - static inline void func(Unit *y, const Unit *x, const Unit *p) - { - Unit xx[N * 2]; - SqrPre::f(xx, x); - Dbl_Mod::f(y, xx, p); - } +struct Dbl_Mod { static const void3u f; }; template -const void3u Sqr::f = Sqr::func; +const void3u Dbl_Mod::f = Dbl_Mod::f; MCL_DEF_LLVM_FUNC(1) MCL_DEF_LLVM_FUNC(2) @@ -201,41 +182,6 @@ struct OpeFunc { { copyArray(y, x, N); } - // z[N] <- mont(x[N], y[N]) - static inline void fp_mulMontC(Unit *z, const Unit *x, const Unit *y, const Unit *p) - { -#if 0 - Unit xy[N * 2]; - MulPre::f(xy, x, y); - fpDbl_modMontC(z, xy, p); -#else - const Unit rp = p[-1]; - Unit buf[N * 2 + 2]; - Unit *c = buf; - Mul_UnitPre::f(c, x, y[0]); // x * y[0] - Unit q = c[0] * rp; - Unit t[N + 2]; - Mul_UnitPre::f(t, p, q); // p * q - t[N + 1] = 0; // always zero - c[N + 1] = AddNC::f(c, c, t); - c++; - for (size_t i = 1; i < N; i++) { - Mul_UnitPre::f(t, x, y[i]); - c[N + 1] = AddNC::f(c, c, t); - q = c[0] * rp; - Mul_UnitPre::f(t, p, q); - AddNC::f(c, c, t); - c++; - } - if (c[N]) { - SubNC::f(z, c, p); - } else { - if (SubNC::f(z, c, p)) { - memcpy(z, c, N * sizeof(Unit)); - } - } -#endif - } /* z[N] <- montRed(xy[N * 2]) REMARK : assume p[-1] = rp @@ -272,24 +218,6 @@ struct OpeFunc { Mul_UnitPre::f(xy, x, y); N1_Mod::f(z, xy, p); } - static inline void fp_mulC(Unit *z, const Unit *x, const Unit *y, const Unit *p) - { - Unit xy[N * 2]; - MulPre::f(xy, x, y); - Dbl_Mod::f(z, xy, p); - } - static inline void fp_sqrC(Unit *y, const Unit *x, const Unit *p) - { - Unit xx[N * 2]; - SqrPre::f(xx, x); - Dbl_Mod::f(y, xx, p); - } - static inline void fp_sqrMontC(Unit *y, const Unit *x, const Unit *p) - { - Unit xx[N * 2]; - SqrPre::f(xx, x); - fpDbl_modMontC(y, xx, p); - } static inline void fp_invOpC(Unit *y, const Unit *x, const Op& op) { mpz_class my; @@ -373,13 +301,13 @@ void setOp(Op& op, Mode mode) op.fp_add = Add::f; op.fp_sub = Sub::f; if (op.isMont) { - op.fp_mul = OpeFunc::fp_mulMontC; - op.fp_sqr = OpeFunc::fp_sqrMontC; + op.fp_mul = Mont::f; + op.fp_sqr = SqrMont::f; op.fp_invOp = OpeFunc::fp_invMontOpC; op.fpDbl_mod = OpeFunc::fpDbl_modMontC; } else { - op.fp_mul = OpeFunc::fp_mulC; - op.fp_sqr = OpeFunc::fp_sqrC; + op.fp_mul = Mul::f; + op.fp_sqr = Sqr::f; op.fp_invOp = OpeFunc::fp_invOpC; op.fpDbl_mod = Dbl_Mod::f; } diff --git a/src/fp_proto.hpp b/src/fp_proto.hpp index 24c5330..68cca54 100644 --- a/src/fp_proto.hpp +++ b/src/fp_proto.hpp @@ -27,28 +27,9 @@ templatestruct Mul_UnitPre { static const void2uI f; }; templatestruct N1_Mod { static const void3u f; }; // z[N] <- x[N * 2] % p[N] templatestruct Dbl_Mod { static const void3u f; }; -// z[N] <- Montgomery(x[N], y[N], p[N]) -templatestruct Mont { static const void4u f; }; // z[N] <- MontRed(xy[N], p[N]) templatestruct MontRed { static const void3u f; }; -// z[N] <- (x[N] * y[N]) % p[N] -templatestruct Mul { static const void4u f; }; -// z[N] <- (x[N] ^ 2) % p[N] -templatestruct Sqr { static const void3u f; }; - -// z[N] <- Montgomery(x[N], x[N], p[N]) -template -struct SqrMont { - static inline void func(Unit *y, const Unit *x, const Unit *p) - { - Mont::f(y, x, x, p); - } - static const void3u f; -}; -template -const void3u SqrMont::f = SqrMont::func; - // z[N] <- (x[N] + y[N]) % p[N] template struct Add { @@ -119,6 +100,96 @@ struct DblSub { template const void4u DblSub::f = DblSub::func; +// z[N] <- Montgomery(x[N], y[N], p[N]) +template +struct Mont { + static inline void func(Unit *z, const Unit *x, const Unit *y, const Unit *p) + { +#if 0 + Unit xy[N * 2]; + MulPre::f(xy, x, y); + fpDbl_modMontC(z, xy, p); +#else + const Unit rp = p[-1]; + Unit buf[N * 2 + 2]; + Unit *c = buf; + Mul_UnitPre::f(c, x, y[0]); // x * y[0] + Unit q = c[0] * rp; + Unit t[N + 2]; + Mul_UnitPre::f(t, p, q); // p * q + t[N + 1] = 0; // always zero + c[N + 1] = AddNC::f(c, c, t); + c++; + for (size_t i = 1; i < N; i++) { + Mul_UnitPre::f(t, x, y[i]); + c[N + 1] = AddNC::f(c, c, t); + q = c[0] * rp; + Mul_UnitPre::f(t, p, q); + AddNC::f(c, c, t); + c++; + } + if (c[N]) { + SubNC::f(z, c, p); + } else { + if (SubNC::f(z, c, p)) { + memcpy(z, c, N * sizeof(Unit)); + } + } +#endif + } + static const void4u f; +}; + +template +const void4u Mont::f = Mont::func; + +// z[N] <- Montgomery(x[N], x[N], p[N]) +template +struct SqrMont { + static inline void func(Unit *y, const Unit *x, const Unit *p) + { +#if 0 + Unit xx[N * 2]; + SqrPre::f(xx, x); + MontRed(y, xx, p); +#else + Mont::f(y, x, x, p); +#endif + } + static const void3u f; +}; +template +const void3u SqrMont::f = SqrMont::func; + +// z[N] <- (x[N] * y[N]) % p[N] +template +struct Mul { + static inline void func(Unit *z, const Unit *x, const Unit *y, const Unit *p) + { + Unit xy[N * 2]; + MulPre::f(xy, x, y); + Dbl_Mod::f(z, xy, p); + } + static const void4u f; +}; +template +const void4u Mul::f = Mul::func; + +// y[N] <- (x[N] * x[N]) % p[N] +template +struct Sqr { + static inline void func(Unit *y, const Unit *x, const Unit *p) + { + Unit xx[N * 2]; + SqrPre::f(xx, x); + Dbl_Mod::f(y, xx, p); + } + static const void3u f; +}; +template +const void3u Sqr::f = Sqr::func; + + } } // mcl::fp #ifdef MCL_USE_LLVM