@@ -30,6 +30,8 @@ LD = $(CC) | |||||
LDXX = $(CXX) | LDXX = $(CXX) | ||||
ASM ?= $(CC) | ASM ?= $(CC) | ||||
PYTHON ?= python | |||||
WARNFLAGS = -pedantic -Wall -Wextra -Werror -Wunreachable-code \ | WARNFLAGS = -pedantic -Wall -Wextra -Werror -Wunreachable-code \ | ||||
-Wmissing-declarations -Wunused-function -Wno-overlength-strings $(EXWARN) | -Wmissing-declarations -Wunused-function -Wno-overlength-strings $(EXWARN) | ||||
@@ -77,7 +79,8 @@ GEN_CODE= $(GEN_CODE_1:%.tmpl.hxx=%.hxx) | |||||
HEADERS= Makefile $(shell find src test -name "*.h") $(BUILD_OBJ)/timestamp $(GEN_CODE) | HEADERS= Makefile $(shell find src test -name "*.h") $(BUILD_OBJ)/timestamp $(GEN_CODE) | ||||
# components needed by the lib | # components needed by the lib | ||||
LIBCOMPONENTS = $(BUILD_OBJ)/utils.o $(BUILD_OBJ)/shake.o $(BUILD_OBJ)/sha512.o $(BUILD_OBJ)/spongerng.o # and per-field components | |||||
LIBCOMPONENTS = $(BUILD_OBJ)/utils.o $(BUILD_OBJ)/shake.o $(BUILD_OBJ)/sha512.o $(BUILD_OBJ)/spongerng.o | |||||
# and per-field components | |||||
BENCHCOMPONENTS = $(BUILD_OBJ)/bench.o $(BUILD_OBJ)/shake.o | BENCHCOMPONENTS = $(BUILD_OBJ)/bench.o $(BUILD_OBJ)/shake.o | ||||
@@ -125,16 +128,16 @@ $(BUILD_INC)/%: src/public_include/% $(BUILD_OBJ)/timestamp | |||||
cp -f $< $@ | cp -f $< $@ | ||||
$(BUILD_INC)/%.h: src/public_include/%.tmpl.h src/generator/* | $(BUILD_INC)/%.h: src/public_include/%.tmpl.h src/generator/* | ||||
python -B src/generator/template.py --per=global --guard=$(@:$(BUILD_INC)/%=%) -o $@ $< | |||||
$(PYTHON) -B src/generator/template.py --per=global --guard=$(@:$(BUILD_INC)/%=%) -o $@ $< | |||||
$(BUILD_C)/%.h: src/include/%.tmpl.h src/generator/* | $(BUILD_C)/%.h: src/include/%.tmpl.h src/generator/* | ||||
python -B src/generator/template.py --per=global --guard=$(@:$(BUILD_C)/%=%) -o $@ $< | |||||
$(PYTHON) -B src/generator/template.py --per=global --guard=$(@:$(BUILD_C)/%=%) -o $@ $< | |||||
$(BUILD_INC)/%.hxx: src/public_include/%.tmpl.hxx src/generator/* | $(BUILD_INC)/%.hxx: src/public_include/%.tmpl.hxx src/generator/* | ||||
python -B src/generator/template.py --per=global --guard=$(@:$(BUILD_INC)/%=%) -o $@ $< | |||||
$(PYTHON) -B src/generator/template.py --per=global --guard=$(@:$(BUILD_INC)/%=%) -o $@ $< | |||||
$(BUILD_C)/%.hxx: src/include/%.tmpl.hxx src/generator/* | $(BUILD_C)/%.hxx: src/include/%.tmpl.hxx src/generator/* | ||||
python -B src/generator/template.py --per=global --guard=$(@:$(BUILD_C)/%=%) -o $@ $< | |||||
$(PYTHON) -B src/generator/template.py --per=global --guard=$(@:$(BUILD_C)/%=%) -o $@ $< | |||||
################################################################ | ################################################################ | ||||
# Per-field code: call with field, arch | # Per-field code: call with field, arch | ||||
@@ -147,10 +150,10 @@ LIBCOMPONENTS += $$(COMPONENTS_OF_$(1)) | |||||
PER_OBJ_DIRS += $$(BUILD_OBJ)/$(1) | PER_OBJ_DIRS += $$(BUILD_OBJ)/$(1) | ||||
$$(BUILD_C)/$(1)/%.c: src/per_field/%.tmpl.c src/generator/* Makefile | $$(BUILD_C)/$(1)/%.c: src/per_field/%.tmpl.c src/generator/* Makefile | ||||
python -B src/generator/template.py --per=field --guard=$(1)/`basename $$@` --item=$(1) -o $$@ $$< | |||||
$(PYTHON) -B src/generator/template.py --per=field --guard=$(1)/`basename $$@` --item=$(1) -o $$@ $$< | |||||
$$(BUILD_H)/$(1)/%.h: src/per_field/%.tmpl.h src/generator/* Makefile | $$(BUILD_H)/$(1)/%.h: src/per_field/%.tmpl.h src/generator/* Makefile | ||||
python -B src/generator/template.py --per=field --guard=$(1)/`basename $$@` --item=$(1) -o $$@ $$< | |||||
$(PYTHON) -B src/generator/template.py --per=field --guard=$(1)/`basename $$@` --item=$(1) -o $$@ $$< | |||||
$$(BUILD_OBJ)/$(1)/%.o: $$(BUILD_C)/$(1)/%.c $$(HEADERS_OF_$(1)) | $$(BUILD_OBJ)/$(1)/%.o: $$(BUILD_C)/$(1)/%.c $$(HEADERS_OF_$(1)) | ||||
$$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$$(ARCH_FOR_$(1)) -I $(BUILD_H)/$(1) \ | $$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$$(ARCH_FOR_$(1)) -I $(BUILD_H)/$(1) \ | ||||
@@ -182,22 +185,22 @@ HEADERS_OF_$(1) = $$(HEADERS_OF_$(2)) $$(GLOBAL_HEADERS_OF_$(1)) | |||||
HEADERS += $$(GLOBAL_HEADERS_OF_$(1)) | HEADERS += $$(GLOBAL_HEADERS_OF_$(1)) | ||||
$$(BUILD_C)/$(1)/%.c: src/per_curve/%.tmpl.c src/generator/* Makefile | $$(BUILD_C)/$(1)/%.c: src/per_curve/%.tmpl.c src/generator/* Makefile | ||||
python -B src/generator/template.py --per=curve --item=$(1) --guard=$(1)/`basename $$@` -o $$@ $$< | |||||
$(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$(1)/`basename $$@` -o $$@ $$< | |||||
$$(BUILD_H)/$(1)/%.h: src/per_curve/%.tmpl.h src/generator/* Makefile | $$(BUILD_H)/$(1)/%.h: src/per_curve/%.tmpl.h src/generator/* Makefile | ||||
python -B src/generator/template.py --per=curve --item=$(1) --guard=$(1)/`basename $$@` -o $$@ $$< | |||||
$(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$(1)/`basename $$@` -o $$@ $$< | |||||
$$(BUILD_INC)/decaf/point_$(3).%: src/per_curve/point.tmpl.% src/generator/* Makefile | $$(BUILD_INC)/decaf/point_$(3).%: src/per_curve/point.tmpl.% src/generator/* Makefile | ||||
python -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$< | |||||
$(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$< | |||||
$$(BUILD_INC)/decaf/ed$(3).%: src/per_curve/eddsa.tmpl.% src/generator/* Makefile | $$(BUILD_INC)/decaf/ed$(3).%: src/per_curve/eddsa.tmpl.% src/generator/* Makefile | ||||
python -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$< | |||||
$(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$< | |||||
$$(BUILD_INC)/decaf/elligator_$(3).%: src/per_curve/elligator.tmpl.% src/generator/* Makefile | $$(BUILD_INC)/decaf/elligator_$(3).%: src/per_curve/elligator.tmpl.% src/generator/* Makefile | ||||
python -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$< | |||||
$(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$< | |||||
$$(BUILD_INC)/decaf/scalar_$(3).%: src/per_curve/scalar.tmpl.% src/generator/* Makefile | $$(BUILD_INC)/decaf/scalar_$(3).%: src/per_curve/scalar.tmpl.% src/generator/* Makefile | ||||
python -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$< | |||||
$(PYTHON) -B src/generator/template.py --per=curve --item=$(1) --guard=$$(@:$(BUILD_INC)/%=%) -o $$@ $$< | |||||
$$(BUILD_IBIN)/decaf_gen_tables_$(1): $$(BUILD_OBJ)/$(1)/decaf_gen_tables.o \ | $$(BUILD_IBIN)/decaf_gen_tables_$(1): $$(BUILD_OBJ)/$(1)/decaf_gen_tables.o \ | ||||
$$(BUILD_OBJ)/$(1)/decaf.o $$(BUILD_OBJ)/$(1)/scalar.o $$(BUILD_OBJ)/utils.o \ | $$(BUILD_OBJ)/$(1)/decaf.o $$(BUILD_OBJ)/$(1)/scalar.o $$(BUILD_OBJ)/utils.o \ | ||||
@@ -74,7 +74,7 @@ def ser(x,bits,paren=None): | |||||
def msqrt(x,p,hi_bit_clear = True): | def msqrt(x,p,hi_bit_clear = True): | ||||
if p % 4 == 3: ret = pow(x,(p+1)//4,p) | if p % 4 == 3: ret = pow(x,(p+1)//4,p) | ||||
elif p % 8 == 5: | elif p % 8 == 5: | ||||
for u in xrange(1,1000): | |||||
for u in range(1,1000): | |||||
if pow(u,(p-1)//2,p) != 1: break | if pow(u,(p-1)//2,p) != 1: break | ||||
u = pow(u,(p-1)//4,p) | u = pow(u,(p-1)//4,p) | ||||
ret = pow(x,(p+3)//8,p) | ret = pow(x,(p+3)//8,p) | ||||
@@ -93,14 +93,14 @@ def ceil_log2(x): | |||||
out += 1 | out += 1 | ||||
return out | return out | ||||
for field,data in field_data.iteritems(): | |||||
for field,data in field_data.items(): | |||||
if "modulus" not in data: | if "modulus" not in data: | ||||
data["modulus"] = eval(data["gf_desc"].replace("^","**")) | data["modulus"] = eval(data["gf_desc"].replace("^","**")) | ||||
if "gf_bits" not in data: | if "gf_bits" not in data: | ||||
data["gf_bits"] = ceil_log2(data["modulus"]) | data["gf_bits"] = ceil_log2(data["modulus"]) | ||||
for curve,data in curve_data.iteritems(): | |||||
for curve,data in curve_data.items(): | |||||
for key in field_data[data["field"]]: | for key in field_data[data["field"]]: | ||||
if key not in data: | if key not in data: | ||||
data[key] = field_data[data["field"]][key] | data[key] = field_data[data["field"]][key] | ||||
@@ -6,5 +6,5 @@ | |||||
*/ | */ | ||||
$("\n".join([ | $("\n".join([ | ||||
"#include <decaf/crypto_%s.hxx>" % g for g in sorted([c["bits"] for _,c in curve.iteritems()]) | |||||
"#include <decaf/crypto_%s.hxx>" % g for g in sorted([c["bits"] for _,c in curve.items()]) | |||||
])) | ])) |
@@ -9,7 +9,7 @@ extern "C" { | |||||
#endif | #endif | ||||
/** Number of bytes in an EdDSA public key. */ | /** Number of bytes in an EdDSA public key. */ | ||||
#define DECAF_EDDSA_$(gf_shortname)_PUBLIC_BYTES $((gf_bits)/8 + 1) | |||||
#define DECAF_EDDSA_$(gf_shortname)_PUBLIC_BYTES $((gf_bits)//8 + 1) | |||||
/** Number of bytes in an EdDSA private key. */ | /** Number of bytes in an EdDSA private key. */ | ||||
#define DECAF_EDDSA_$(gf_shortname)_PRIVATE_BYTES DECAF_EDDSA_$(gf_shortname)_PUBLIC_BYTES | #define DECAF_EDDSA_$(gf_shortname)_PRIVATE_BYTES DECAF_EDDSA_$(gf_shortname)_PUBLIC_BYTES | ||||
@@ -24,24 +24,24 @@ typedef struct gf_$(gf_shortname)_s { | |||||
/** @endcond */ | /** @endcond */ | ||||
/** Number of bytes in a serialized point. */ | /** Number of bytes in a serialized point. */ | ||||
#define $(C_NS)_SER_BYTES $((gf_bits-2)/8 + 1) | |||||
#define $(C_NS)_SER_BYTES $((gf_bits-2)//8 + 1) | |||||
/** Number of bytes in an elligated point. For now set the same as SER_BYTES | /** Number of bytes in an elligated point. For now set the same as SER_BYTES | ||||
* but could be different for other curves. | * but could be different for other curves. | ||||
*/ | */ | ||||
#define $(C_NS)_HASH_BYTES $((gf_bits-2)/8 + 1) | |||||
#define $(C_NS)_HASH_BYTES $((gf_bits-2)//8 + 1) | |||||
/** Number of bytes in a serialized scalar. */ | /** Number of bytes in a serialized scalar. */ | ||||
#define $(C_NS)_SCALAR_BYTES $((scalar_bits-1)/8 + 1) | |||||
#define $(C_NS)_SCALAR_BYTES $((scalar_bits-1)//8 + 1) | |||||
/** Number of bits in the "which" field of an elligator inverse */ | /** Number of bits in the "which" field of an elligator inverse */ | ||||
#define $(C_NS)_INVERT_ELLIGATOR_WHICH_BITS $(ceil_log2(cofactor) + 7 + elligator_onto - ((gf_bits-2) % 8)) | #define $(C_NS)_INVERT_ELLIGATOR_WHICH_BITS $(ceil_log2(cofactor) + 7 + elligator_onto - ((gf_bits-2) % 8)) | ||||
/** Number of bytes in an x$(gf_shortname) public key */ | /** Number of bytes in an x$(gf_shortname) public key */ | ||||
#define DECAF_X$(gf_shortname)_PUBLIC_BYTES $((gf_bits-1)/8 + 1) | |||||
#define DECAF_X$(gf_shortname)_PUBLIC_BYTES $((gf_bits-1)//8 + 1) | |||||
/** Number of bytes in an x$(gf_shortname) private key */ | /** Number of bytes in an x$(gf_shortname) private key */ | ||||
#define DECAF_X$(gf_shortname)_PRIVATE_BYTES $((gf_bits-1)/8 + 1) | |||||
#define DECAF_X$(gf_shortname)_PRIVATE_BYTES $((gf_bits-1)//8 + 1) | |||||
/** Twisted Edwards extended homogeneous coordinates */ | /** Twisted Edwards extended homogeneous coordinates */ | ||||
typedef struct $(c_ns)_point_s { | typedef struct $(c_ns)_point_s { | ||||
@@ -49,7 +49,7 @@ static inline const char *name() { return "$(name)"; } | |||||
static const int REMOVED_COFACTOR = $(cofactor); | static const int REMOVED_COFACTOR = $(cofactor); | ||||
/** Residue class of field modulus: p == this mod 2*(this-1) */ | /** Residue class of field modulus: p == this mod 2*(this-1) */ | ||||
static const int FIELD_MODULUS_TYPE = $([2**i+1 for i in xrange(1,10) if modulus % 2**(i+1) != 1][0]); | |||||
static const int FIELD_MODULUS_TYPE = $(modulus &~ (modulus-3)); | |||||
/** @cond internal */ | /** @cond internal */ | ||||
class Point; | class Point; | ||||
@@ -15,7 +15,7 @@ static const decaf_word_t MONTGOMERY_FACTOR = (decaf_word_t)0x$("%x" % pow(-q,2* | |||||
static const scalar_t sc_p = {{{ | static const scalar_t sc_p = {{{ | ||||
$(ser(q,64,"SC_LIMB")) | $(ser(q,64,"SC_LIMB")) | ||||
}}}, sc_r2 = {{{ | }}}, sc_r2 = {{{ | ||||
$(ser(((2**128)**((scalar_bits+63)/64))%q,64,"SC_LIMB")) | |||||
$(ser(((2**128)**((scalar_bits+63)//64))%q,64,"SC_LIMB")) | |||||
}}}; | }}}; | ||||
/* End of template stuff */ | /* End of template stuff */ | ||||
@@ -7,9 +7,9 @@ | |||||
#include "word.h" | #include "word.h" | ||||
#define __DECAF_$(gf_shortname)_GF_DEFINED__ 1 | #define __DECAF_$(gf_shortname)_GF_DEFINED__ 1 | ||||
#define NLIMBS ($(gf_impl_bits/8)/sizeof(word_t)) | |||||
#define X_SER_BYTES $(((gf_bits-1)/8 + 1)) | |||||
#define SER_BYTES $(((gf_bits-2)/8 + 1)) | |||||
#define NLIMBS ($(gf_impl_bits//8)/sizeof(word_t)) | |||||
#define X_SER_BYTES $(((gf_bits-1)//8 + 1)) | |||||
#define SER_BYTES $(((gf_bits-2)//8 + 1)) | |||||
typedef struct gf_$(gf_shortname)_s { | typedef struct gf_$(gf_shortname)_s { | ||||
word_t limb[NLIMBS]; | word_t limb[NLIMBS]; | ||||
} __attribute__((aligned(32))) gf_$(gf_shortname)_s, gf_$(gf_shortname)_t[1]; | } __attribute__((aligned(32))) gf_$(gf_shortname)_s, gf_$(gf_shortname)_t[1]; | ||||
@@ -14,5 +14,5 @@ | |||||
*/ | */ | ||||
$("\n".join([ | $("\n".join([ | ||||
"#include <decaf/point_%s.h>" % g for g in sorted([c["bits"] for _,c in curve.iteritems()]) | |||||
"#include <decaf/point_%s.h>" % g for g in sorted([c["bits"] for _,c in curve.items()]) | |||||
])) | ])) |
@@ -1,7 +1,7 @@ | |||||
/** Master header for Decaf library, C++ version. */ | /** Master header for Decaf library, C++ version. */ | ||||
$("\n".join([ | $("\n".join([ | ||||
"#include <decaf/point_%s.hxx>" % g for g in sorted([c["bits"] for _,c in curve.iteritems()]) | |||||
"#include <decaf/point_%s.hxx>" % g for g in sorted([c["bits"] for _,c in curve.items()]) | |||||
])) | ])) | ||||
namespace decaf { | namespace decaf { | ||||
@@ -4,5 +4,5 @@ | |||||
namespace decaf { enum Prehashed { PURE, PREHASHED }; } | namespace decaf { enum Prehashed { PURE, PREHASHED }; } | ||||
$("\n".join([ | $("\n".join([ | ||||
"#include <decaf/ed%s.hxx>" % g for g in sorted([c["bits"] for _,c in curve.iteritems()]) | |||||
"#include <decaf/ed%s.hxx>" % g for g in sorted([c["bits"] for _,c in curve.items()]) | |||||
])) | ])) |