Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Aug 17, 2023
2 parents 54a9964 + cf0a74b commit d9c94b0
Show file tree
Hide file tree
Showing 14 changed files with 649 additions and 92 deletions.
3 changes: 2 additions & 1 deletion 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 static_init_test.cpp
TEST_SRC+=invmod_test.cpp
LIB_OBJ=$(OBJ_DIR)/fp.o
ifeq ($(MCL_STATIC_CODE),1)
LIB_OBJ+=obj/static_code.o
Expand Down Expand Up @@ -195,7 +196,7 @@ else
python3 src/gen_bint_x64.py -win > $@
endif
$(BINT_SRC): src/bint$(BIT).ll
$(CLANG) -S $< -o $@ -no-integrated-as -fpic -O2 -DNDEBUG -Wall -Wextra $(CLANG_TARGET) $(CFLAGS_USER)
$(CLANG) -S $< -o $@ -no-integrated-as -fpic -O2 -DNDEBUG -Wall -Wextra $(CFLAGS) $(CFLAGS_USER)
#$(BINT_OBJ): $(BINT_SRC)
# $(AS) $< -o $@
header:
Expand Down
26 changes: 26 additions & 0 deletions Makefile.cross
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
include common.mk

LIB_DIR=lib
OBJ_DIR=obj
EXE_DIR=bin
SRC_SRC=fp.cpp bn_c384_256.cpp
VPATH=src
MCL_LIB=$(LIB_DIR)/libmclbn384_256.a
LIB_OBJ=$(addprefix $(OBJ_DIR)/,$(SRC_SRC:.cpp=.o) bint.o base.o)

CFLAGS=-std=c++03 -I ./include -I ./src
CFLAGS+=-fno-threadsafe-statics -fno-exceptions -fno-rtti -DMCL_MAX_BIT_SIZE=384 -DCYBOZU_DONT_USE_STRING -DCYBOZU_DONT_USE_EXCEPTION -DNDEBUG
CFLAGS+=-target $(TARGET) $(TARGET_OPT)

CXX=$(PRE)clang++$(LLVM_VER)

$(MCL_LIB): $(LIB_OBJ)
$(AR) $(ARFLAGS) $@ $(LIB_OBJ)

$(OBJ_DIR)/%.o: %.cpp
$(CXX) -c $< -o $@ $(CFLAGS)

$(OBJ_DIR)/bint.o: src/bint$(BIT).ll
$(CXX) -c $< -o $@ $(CFLAGS)
$(OBJ_DIR)/base.o: src/base$(BIT).ll
$(CXX) -c $< -o $@ $(CFLAGS)
69 changes: 68 additions & 1 deletion include/mcl/ec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,33 @@ bool isEqualJacobi(const E& P1, const E& P2)
return t1 == t2;
}

// return (P1 == P2) ? 1 : (P1 == -P2) ? -1 : 0
template<class E>
int isEqualOrMinusJacobi(const E& P1, const E& P2)
{
typedef typename E::Fp F;
bool zero1 = P1.isZero();
bool zero2 = P2.isZero();
if (zero1) {
return zero2 ? 1 : 0;
}
if (zero2) return 0;
F s1, s2, t1, t2;
F::sqr(s1, P1.z);
F::sqr(s2, P2.z);
F::mul(t1, P1.x, s2);
F::mul(t2, P2.x, s1);
if (t1 != t2) return 0;
F::mul(t1, P1.y, s2);
F::mul(t2, P2.y, s1);
t1 *= P2.z;
t2 *= P1.z;
if (t1 == t2) return 1;
F::neg(t1, t1);
if (t1 == t2) return -1;
return 0;
}

// Y^2 == X(X^2 + aZ^4) + bZ^6
template<class E>
bool isValidJacobi(const E& P)
Expand Down Expand Up @@ -548,6 +575,29 @@ bool isEqualProj(const E& P1, const E& P2)
return t1 == t2;
}

// return (P1 == P2) ? 1 : (P1 == -P2) ? -1 : 0
template<class E>
int isEqualOrMinusProj(const E& P1, const E& P2)
{
typedef typename E::Fp F;
bool zero1 = P1.isZero();
bool zero2 = P2.isZero();
if (zero1) {
return zero2 ? 1 : 0;
}
if (zero2) return 0;
F t1, t2;
F::mul(t1, P1.x, P2.z);
F::mul(t2, P2.x, P1.z);
if (t1 != t2) return 0;
F::mul(t1, P1.y, P2.z);
F::mul(t2, P2.y, P1.z);
if (t1 == t2) return 1;
F::neg(t1, t1);
if (t1 == t2) return -1;
return 0;
}

/*
|a=0|-3| generic
mul| 8| 8| 9
Expand Down Expand Up @@ -822,10 +872,10 @@ size_t mulVecCore(G& z, G *xVec, const Unit *yVec, size_t yUnitSize, size_t next
}

size_t c, tblN;
G *tbl_ = 0; // malloc is used if tbl_ != 0
G *tbl = 0;

#ifndef MCL_DONT_USE_MALLOC
G *tbl_ = 0; // malloc is used if tbl_ != 0
// if n is large then try to use malloc
if (n > MCL_MAX_N_TO_USE_STACK_FOR_MUL_VEC) {
c = argminForMulVec(n);
Expand Down Expand Up @@ -1752,6 +1802,23 @@ class EcT : public fp::Serializable<EcT<_Fp> > {
return x == rhs.x && y == rhs.y && z == rhs.z;
}
}
// return (==rhs) ? 1 : (==-rhs) ? -1 : 0
int isEqualOrMinus(const EcT& rhs) const
{
switch (mode_) {
case ec::Jacobi:
return ec::isEqualOrMinusJacobi(*this, rhs);
case ec::Proj:
return ec::isEqualOrMinusProj(*this, rhs);
case ec::Affine:
default:
if (x == rhs.x && z == rhs.z) {
if (y == rhs.y) return 1;
if (y == -rhs.y) return -1;
}
return 0;
}
}
bool operator!=(const EcT& rhs) const { return !operator==(rhs); }
bool operator<(const EcT& rhs) const
{
Expand Down
Loading

0 comments on commit d9c94b0

Please sign in to comment.