diff --git a/Makefile b/Makefile index 9898e4e..00e23fd 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,6 @@ MACHINE := $(shell uname -m) # Subdirectories for objects etc. # Many of them are mapped to build/obj right now, but could be split later. # The non-build/obj directories are the public interface. -BUILD_ASM = build/obj BUILD_OBJ = build/obj BUILD_C = build/c BUILD_H = build/c @@ -67,7 +66,7 @@ SAGES= $(shell ls test/*.sage) BUILDPYS= $(SAGES:test/%.sage=$(BUILD_PY)/%.py) .PHONY: clean all test test_ct bench todo doc lib bat sage sagetest gen_headers -.PRECIOUS: $(BUILD_ASM)/%.s $(BUILD_C)/*/%.c $(BUILD_H)/*/%.h $(BUILD_IBIN)/% +.PRECIOUS: $(BUILD_C)/*/%.c $(BUILD_H)/*/%.h $(BUILD_IBIN)/% HEADER_SRCS= $(shell find src/public_include -name "*.h*") GEN_HEADERS_0= $(HEADER_SRCS:src/public_include/%=$(BUILD_INC)/%) @@ -113,14 +112,11 @@ endif # Create all the build subdirectories $(BUILD_OBJ)/timestamp: - mkdir -p $(BUILD_ASM) $(BUILD_OBJ) $(BUILD_C) $(BUILD_PY) \ + mkdir -p $(BUILD_OBJ) $(BUILD_C) $(BUILD_PY) \ $(BUILD_LIB) $(BUILD_INC) $(BUILD_BIN) $(BUILD_IBIN) $(BUILD_H) $(BUILD_INC)/decaf \ $(PER_OBJ_DIRS) touch $@ -$(BUILD_OBJ)/%.o: $(BUILD_ASM)/%.s - $(ASM) $(ASFLAGS) -c -o $@ $< - gen_headers: $(GEN_HEADERS) $(BUILD_INC)/%: src/public_include/% $(BUILD_OBJ)/timestamp @@ -148,20 +144,20 @@ $$(BUILD_C)/$(1)/%.c: src/per_field/%.tmpl.c src/gen_headers/* $(HEADERS) $$(BUILD_H)/$(1)/%.h: src/per_field/%.tmpl.h src/gen_headers/* $(HEADERS) python -B src/gen_headers/template.py --per=field --guard=$(1)/`basename $$@` --item=$(1) -o $$@ $$< -$$(BUILD_ASM)/$(1)/%.s: $$(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) \ -I $(BUILD_H)/$(1)/$$(ARCH_FOR_$(1)) -I src/include/$$(ARCH_FOR_$(1)) \ - -S -c -o $$@ $$< + -c -o $$@ $$< -$$(BUILD_ASM)/$(1)/%.s: src/$(1)/%.c $$(HEADERS_OF_$(1)) +$$(BUILD_OBJ)/$(1)/%.o: src/$(1)/%.c $$(HEADERS_OF_$(1)) $$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$$(ARCH_FOR_$(1)) -I $(BUILD_H)/$(1) \ -I $(BUILD_H)/$(1)/$$(ARCH_FOR_$(1)) -I src/include/$$(ARCH_FOR_$(1)) \ - -S -c -o $$@ $$< + -c -o $$@ $$< -$$(BUILD_ASM)/$(1)/%.s: src/$(1)/$$(ARCH_FOR_$(1))/%.c $$(HEADERS_OF_$(1)) +$$(BUILD_OBJ)/$(1)/%.o: src/$(1)/$$(ARCH_FOR_$(1))/%.c $$(HEADERS_OF_$(1)) $$(CC) $$(CFLAGS) -I src/$(1) -I src/$(1)/$$(ARCH_FOR_$(1)) -I $(BUILD_H)/$(1) \ -I $(BUILD_H)/$(1)/$$(ARCH_FOR_$(1)) -I src/include/$$(ARCH_FOR_$(1)) \ - -S -c -o $$@ $$< + -c -o $$@ $$< endef ################################################################ @@ -196,16 +192,16 @@ $$(BUILD_IBIN)/decaf_gen_tables_$(1): $$(BUILD_OBJ)/$(1)/decaf_gen_tables.o \ $$(BUILD_C)/$(1)/decaf_tables.c: $$(BUILD_IBIN)/decaf_gen_tables_$(1) ./$$< > $$@ || (rm $$@; exit 1) -$$(BUILD_ASM)/$(1)/%.s: $$(BUILD_C)/$(1)/%.c $$(HEADERS_OF_$(1)) - $$(CC) $$(CFLAGS) -S -c -o $$@ $$< \ +$$(BUILD_OBJ)/$(1)/%.o: $$(BUILD_C)/$(1)/%.c $$(HEADERS_OF_$(1)) + $$(CC) $$(CFLAGS) -c -o $$@ $$< \ -I build/obj/curve_$(1)/ -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) -I src/include/$$(ARCH_FOR_$(2)) \ -I $(BUILD_H)/$(1) -I $(BUILD_H)/$(2) -I $(BUILD_H)/$(2)/$$(ARCH_FOR_$(2)) -$$(BUILD_ASM)/decaf_gen_tables_$(1).s: src/decaf_gen_tables.c $$(HEADERS_OF_$(1)) +$$(BUILD_OBJ)/decaf_gen_tables_$(1).o: src/decaf_gen_tables.c $$(HEADERS_OF_$(1)) $$(CC) $$(CFLAGS) \ -I build/obj/curve_$(1) -I src/$(2) -I src/$(2)/$$(ARCH_FOR_$(2)) -I src/include/$$(ARCH_FOR_$(2)) \ -I $(BUILD_H)/$(1) -I $(BUILD_H)/$(2) -I $(BUILD_H)/$(2)/$$(ARCH_FOR_$(2)) \ - -S -c -o $$@ $$< + -c -o $$@ $$< endef ################################################################ @@ -237,14 +233,14 @@ endif -$(BUILD_ASM)/%.s: src/%.c $(HEADERS) - $(CC) $(CFLAGS) -S -c -o $@ $< +$(BUILD_OBJ)/%.o: src/%.c $(HEADERS) + $(CC) $(CFLAGS) -c -o $@ $< -$(BUILD_ASM)/%.s: test/%.c $(HEADERS) - $(CC) $(PUB_CFLAGS) -S -c -o $@ $< +$(BUILD_OBJ)/%.o: test/%.c $(HEADERS) + $(CC) $(PUB_CFLAGS) -c -o $@ $< -$(BUILD_ASM)/%.s: test/%.cxx $(HEADERS) - $(CXX) $(CXXFLAGS) -S -c -o $@ $< +$(BUILD_OBJ)/%.o: test/%.cxx $(HEADERS) + $(CXX) $(CXXFLAGS) -c -o $@ $< # The sage test scripts sage: $(BUILDPYS) diff --git a/src/gen_headers/curve_data.py b/src/gen_headers/curve_data.py index 17813b1..67b8175 100644 --- a/src/gen_headers/curve_data.py +++ b/src/gen_headers/curve_data.py @@ -91,15 +91,6 @@ for field,data in field_data.iteritems(): if "gf_bits" not in data: data["gf_bits"] = ceil_log2(data["modulus"]) - - if "x_pub_bytes" not in data: - data["x_pub_bytes"] = (data["gf_bits"]-1)//8 + 1 - - if "x_priv_bytes" not in data: - data["x_priv_bytes"] = (data["gf_bits"]-1)//8 + 1 - - if "x_priv_bits" not in data: - data["x_priv_bits"] = ceil_log2(data["modulus"]*0.99) # not per curve at least in 7748 for curve,data in curve_data.iteritems(): for key in field_data[data["field"]]: @@ -112,34 +103,16 @@ for curve,data in curve_data.iteritems(): if "cxx_ns" not in data: data["cxx_ns"] = data["name"].replace("-","") - - if "c_filename" not in data: - data["c_filename"] = data["iso_to"].replace("-","").lower() - - mod = data["modulus"] - ptwo = 2 - while mod % ptwo == 1: - ptwo *= 2 - data["modulus_type"] = mod % ptwo if "imagine_twist" not in data: - if data["modulus_type"] == 3: data["imagine_twist"] = 0 + if data["modulus"]%4 == 3: data["imagine_twist"] = 0 else: data["imagine_twist"] = 1 data["q"] = (data["modulus"]+1-data["trace"]) // data["cofactor"] data["bits"] = ceil_log2(data["modulus"]) - data["decaf_base"] = ser(msqrt(data["mont_base"],data["modulus"]),8) - - if data["cofactor"] > 4: data["sqrt_one_minus_d"] = ser(msqrt(1-data["d"],data["modulus"]),data["gf_lit_limb_bits"]) - else: data["sqrt_one_minus_d"] = "/* NONE */" - - if "shortname" not in data: - data["shortname"] = str(data["bits"]) if "c_ns" not in data: - data["c_ns"] = "decaf_" + data["shortname"] + data["c_ns"] = "decaf_" + str(data["bits"]) data["C_NS"] = data["c_ns"].upper() - - data["ser_bytes"] = (data["bits"]-2)//8 + 1 # TODO: split for decaf vs non-decaf - data["scalar_ser_bytes"] = (data["scalar_bits"]-1)//8 + 1 + diff --git a/src/gen_headers/template.py b/src/gen_headers/template.py index 0f9e380..9f54824 100644 --- a/src/gen_headers/template.py +++ b/src/gen_headers/template.py @@ -1,5 +1,5 @@ from textwrap import dedent -from curve_data import field_data,curve_data,ser,msqrt +from curve_data import field_data,curve_data,ser,msqrt,ceil_log2 import os import argparse @@ -50,7 +50,7 @@ def fillin(template,data): if template[position] == '(': parens += 1 elif template[position] == ')': parens -= 1 position += 1 - ret += str(eval(template[dollars+2:position-1],{'ser':ser,'msqrt':msqrt},data)) + ret += str(eval(template[dollars+2:position-1],{'ser':ser,'msqrt':msqrt,'ceil_log2':ceil_log2},data)) author = "Mike Hamburg" # FUTURE for name in args.files: diff --git a/src/per_curve/decaf.tmpl.c b/src/per_curve/decaf.tmpl.c index dee9531..2dd9938 100644 --- a/src/per_curve/decaf.tmpl.c +++ b/src/per_curve/decaf.tmpl.c @@ -43,7 +43,7 @@ const uint8_t API_NS(x_base_point)[SER_BYTES] = { $(ser(mont_base,8)) }; #if COFACTOR==8 static const gf SQRT_ONE_MINUS_D = {FIELD_LITERAL( - $(sqrt_one_minus_d) + $(ser(msqrt(1-d,modulus),gf_lit_limb_bits) if cofactor == 8 else "/* NONE */") )}; #endif diff --git a/src/per_curve/decaf.tmpl.h b/src/per_curve/decaf.tmpl.h index fdf1dae..f260256 100644 --- a/src/per_curve/decaf.tmpl.h +++ b/src/per_curve/decaf.tmpl.h @@ -24,16 +24,16 @@ typedef struct gf_$(gf_shortname)_s { /** @endcond */ /** Number of bytes in a serialized point. */ -#define $(C_NS)_SER_BYTES $(ser_bytes) +#define $(C_NS)_SER_BYTES $((gf_bits-2)/8 + 1) /** Number of bytes in a serialized scalar. */ -#define $(C_NS)_SCALAR_BYTES $(scalar_ser_bytes) +#define $(C_NS)_SCALAR_BYTES $((scalar_bits-1)/8 + 1) /** Number of bytes in an x$(gf_shortname) public key */ -#define X$(gf_shortname)_PUBLIC_BYTES $(x_pub_bytes) +#define X$(gf_shortname)_PUBLIC_BYTES $((gf_bits-1)/8 + 1) /** Number of bytes in an x$(gf_shortname) private key */ -#define X$(gf_shortname)_PRIVATE_BYTES $(x_priv_bytes) +#define X$(gf_shortname)_PRIVATE_BYTES $((gf_bits-1)/8 + 1) /** Twisted Edwards extended homogeneous coordinates */ typedef struct $(c_ns)_point_s { diff --git a/src/per_curve/decaf.tmpl.hxx b/src/per_curve/decaf.tmpl.hxx index c4cc01b..fc8ca41 100644 --- a/src/per_curve/decaf.tmpl.hxx +++ b/src/per_curve/decaf.tmpl.hxx @@ -48,7 +48,7 @@ static inline const char *name() { return "$(name)"; } static const int REMOVED_COFACTOR = $(cofactor); /** Residue class of field modulus: p == this mod 2*(this-1) */ -static const int FIELD_MODULUS_TYPE = $(modulus_type); +static const int FIELD_MODULUS_TYPE = $([2**i+1 for i in xrange(1,10) if modulus % 2**(i+1) != 1][0]); /** @cond internal */ class Point; diff --git a/src/per_curve/decaf_gen_tables.tmpl.c b/src/per_curve/decaf_gen_tables.tmpl.c index dc7060c..44afb7f 100644 --- a/src/per_curve/decaf_gen_tables.tmpl.c +++ b/src/per_curve/decaf_gen_tables.tmpl.c @@ -10,7 +10,7 @@ #define API_NS(_id) $(c_ns)_##_id static const unsigned char base_point_ser_for_pregen[SER_BYTES] = { - $(decaf_base) + $(ser(msqrt(mont_base,modulus),8)) }; /* To satisfy linker. */ diff --git a/src/per_field/f_field.tmpl.h b/src/per_field/f_field.tmpl.h index bd444c0..d210e46 100644 --- a/src/per_field/f_field.tmpl.h +++ b/src/per_field/f_field.tmpl.h @@ -37,9 +37,9 @@ typedef struct gf_$(gf_shortname)_s { #define gf_deserialize gf_$(gf_shortname)_deserialize /* RFC 7748 support */ -#define X_PUBLIC_BYTES $(x_pub_bytes) -#define X_PRIVATE_BYTES $(x_priv_bytes) -#define X_PRIVATE_BITS $(x_priv_bits) +#define X_PUBLIC_BYTES $((gf_bits-1)/8 + 1) +#define X_PRIVATE_BYTES X_PUBLIC_BYTES +#define X_PRIVATE_BITS $(gf_bits) #define SQRT_MINUS_ONE P$(gf_shortname)_SQRT_MINUS_ONE /* might not be defined */