aboutsummaryrefslogtreecommitdiffhomepage
path: root/pyecsca/codegen
diff options
context:
space:
mode:
authorJ08nY2020-03-02 15:11:25 +0100
committerJ08nY2020-03-02 15:11:25 +0100
commit81e5f24363cf8f50099b7fbed0fd3bcfaac9d5ea (patch)
tree2e4abe38a7e3b2a53bb8bb2d31b6fe1cfbfb2e13 /pyecsca/codegen
parent02d091ad7e7bb2d80e4ce6649437aad41b479346 (diff)
downloadpyecsca-codegen-81e5f24363cf8f50099b7fbed0fd3bcfaac9d5ea.tar.gz
pyecsca-codegen-81e5f24363cf8f50099b7fbed0fd3bcfaac9d5ea.tar.zst
pyecsca-codegen-81e5f24363cf8f50099b7fbed0fd3bcfaac9d5ea.zip
Make Barrett reduction work.
Diffstat (limited to 'pyecsca/codegen')
-rw-r--r--pyecsca/codegen/Makefile.inc2
-rw-r--r--pyecsca/codegen/bn/bn.c12
-rw-r--r--pyecsca/codegen/bn/bn.h2
-rw-r--r--pyecsca/codegen/curve.h2
-rw-r--r--pyecsca/codegen/render.py16
-rw-r--r--pyecsca/codegen/templates/curve.c12
-rw-r--r--pyecsca/codegen/templates/formula_add.c3
-rw-r--r--pyecsca/codegen/templates/formula_dadd.c3
-rw-r--r--pyecsca/codegen/templates/formula_dbl.c3
-rw-r--r--pyecsca/codegen/templates/formula_ladd.c3
-rw-r--r--pyecsca/codegen/templates/formula_neg.c3
-rw-r--r--pyecsca/codegen/templates/formula_scl.c3
-rw-r--r--pyecsca/codegen/templates/formula_tpl.c3
-rw-r--r--pyecsca/codegen/templates/main.c15
-rw-r--r--pyecsca/codegen/templates/ops.c6
-rw-r--r--pyecsca/codegen/templates/point.c3
16 files changed, 46 insertions, 45 deletions
diff --git a/pyecsca/codegen/Makefile.inc b/pyecsca/codegen/Makefile.inc
index 87e85ac..f5454c7 100644
--- a/pyecsca/codegen/Makefile.inc
+++ b/pyecsca/codegen/Makefile.inc
@@ -129,7 +129,7 @@ CFLAGS += -funsigned-char
CFLAGS += -funsigned-bitfields
# Note: -fpack-struct is dangerous! This is only included in XMEGA/AVR HAL
#CFLAGS += -fpack-struct
-CFLAGS += -flto
+#CFLAGS += -flto
CFLAGS += -fshort-enums
CFLAGS += -Wall
CFLAGS += -Wstrict-prototypes
diff --git a/pyecsca/codegen/bn/bn.c b/pyecsca/codegen/bn/bn.c
index 22e1d49..71b71a7 100644
--- a/pyecsca/codegen/bn/bn.c
+++ b/pyecsca/codegen/bn/bn.c
@@ -139,7 +139,7 @@ bn_err bn_red_init(red_t *out) {
#if REDUCTION == RED_MONTGOMERY
return bn_init(&out->montgomery_renorm);
#elif REDUCTION == RED_BARRETT
- return bn_init(&out->barret);
+ return bn_init(&out->barrett);
#endif
return BN_OKAY;
}
@@ -155,7 +155,7 @@ bn_err bn_red_setup(const bn_t *mod, red_t *out) {
}
return mp_sqrmod(&out->montgomery_renorm, mod, &out->montgomery_renorm_sqr);
#elif REDUCTION == RED_BARRETT
- return mp_reduce_setup(mod, &out->barret);
+ return mp_reduce_setup(&out->barrett, mod);
#endif
return BN_OKAY;
}
@@ -277,12 +277,14 @@ bn_err bn_red_pow(const bn_t *base, const bn_t *exp, const bn_t *mod, const red_
bn_clear(&result);
return err;
}
- for (int i = blen - 2; i > 0; --i) {
+ for (int i = blen - 2; i >= 0; --i) {
bn_red_sqr(&result, mod, red, &result);
if (bn_get_bit(exp, i)) {
bn_red_mul(&result, base, mod, red, &result);
}
}
+ bn_copy(&result, out);
+ bn_clear(&result);
return BN_OKAY;
}
@@ -290,7 +292,7 @@ bn_err bn_red_reduce(const bn_t *mod, const red_t *red, bn_t *what) {
#if REDUCTION == RED_MONTGOMERY
return mp_montgomery_reduce(what, mod, red->montgomery_digit);
#elif REDUCTION == RED_BARRETT
- return mp_reduce(what, mod, red->barrett);
+ return mp_reduce(what, mod, &red->barrett);
#endif
return mp_mod(what, mod, what);
}
@@ -299,7 +301,7 @@ void bn_red_clear(red_t *out) {
#if REDUCTION == RED_MONTGOMERY
bn_clear(&out->montgomery_renorm);
#elif REDUCTION == RED_BARRETT
- bn_clear(&out->barret);
+ bn_clear(&out->barrett);
#endif
}
diff --git a/pyecsca/codegen/bn/bn.h b/pyecsca/codegen/bn/bn.h
index f1a3483..cb0be48 100644
--- a/pyecsca/codegen/bn/bn.h
+++ b/pyecsca/codegen/bn/bn.h
@@ -33,7 +33,7 @@ typedef struct {
bn_t montgomery_renorm;
bn_t montgomery_renorm_sqr;
#elif REDUCTION == RED_BARRETT
- bn_t barret;
+ bn_t barrett;
#endif
} red_t;
diff --git a/pyecsca/codegen/curve.h b/pyecsca/codegen/curve.h
index f498695..b8f876c 100644
--- a/pyecsca/codegen/curve.h
+++ b/pyecsca/codegen/curve.h
@@ -7,6 +7,4 @@ curve_t* curve_new(void);
void curve_free(curve_t *curve);
-void curve_set_param(curve_t *curve, char name, const bn_t *value);
-
#endif //CURVE_H_ \ No newline at end of file
diff --git a/pyecsca/codegen/render.py b/pyecsca/codegen/render.py
index 0d0bc51..8350146 100644
--- a/pyecsca/codegen/render.py
+++ b/pyecsca/codegen/render.py
@@ -27,21 +27,21 @@ env = Environment(
env.globals["isinstance"] = isinstance
-def render_op(op: OpType, result: str, left: str, right: str, mod: str) -> Optional[str]:
+def render_op(op: OpType, result: str, left: str, right: str, mod: str, red: str) -> Optional[str]:
if op == OpType.Add:
- return "bn_mod_add(&{}, &{}, &{}, &{});".format(left, right, mod, result)
+ return "bn_red_add(&{}, &{}, &{}, &{}, &{});".format(left, right, mod, red, result)
elif op == OpType.Sub:
- return "bn_mod_sub(&{}, &{}, &{}, &{});".format(left, right, mod, result)
+ return "bn_red_sub(&{}, &{}, &{}, &{}, &{});".format(left, right, mod, red, result)
elif op == OpType.Neg:
- return "bn_mod_neg(&{}, &{}, &{});".format(right, mod, result)
+ return "bn_red_neg(&{}, &{}, &{}, &{});".format(right, mod, red, result)
elif op == OpType.Mult:
- return "bn_mod_mul(&{}, &{}, &{}, &{});".format(left, right, mod, result)
+ return "bn_red_mul(&{}, &{}, &{}, &{}, &{});".format(left, right, mod, red, result)
elif op == OpType.Div or op == OpType.Inv:
- return "bn_mod_div(&{}, &{}, &{}, &{});".format(left, right, mod, result)
+ return "bn_red_div(&{}, &{}, &{}, &{}, &{});".format(left, right, mod, red, result)
elif op == OpType.Sqr:
- return "bn_mod_sqr(&{}, &{}, &{});".format(left, mod, result)
+ return "bn_red_sqr(&{}, &{}, &{}, &{});".format(left, mod, red, result)
elif op == OpType.Pow:
- return "bn_mod_pow(&{}, &{}, &{}, &{});".format(left, right, mod, result)
+ return "bn_red_pow(&{}, &{}, &{}, &{}, &{});".format(left, right, mod, red, result)
elif op == OpType.Id:
return "bn_copy(&{}, &{});".format(left, result)
else:
diff --git a/pyecsca/codegen/templates/curve.c b/pyecsca/codegen/templates/curve.c
index 4fef683..1bb48f7 100644
--- a/pyecsca/codegen/templates/curve.c
+++ b/pyecsca/codegen/templates/curve.c
@@ -26,16 +26,4 @@ void curve_free(curve_t *curve) {
point_free(curve->neutral);
}
free(curve);
-}
-
-void curve_set_param(curve_t *curve, char name, const bn_t *value) {
- switch (name) {
- {%- for param in params + ["p", "n", "h"] %}
- case '{{ param }}': bn_copy(value, &curve->{{ param }});
- {% if param == "p" %}
- bn_red_setup(value, &curve->{{ param }}_red);
- {%- endif %}
- break;
- {%- endfor %}
- }
} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/formula_add.c b/pyecsca/codegen/templates/formula_add.c
index 4b4ea88..ffd7774 100644
--- a/pyecsca/codegen/templates/formula_add.c
+++ b/pyecsca/codegen/templates/formula_add.c
@@ -3,7 +3,7 @@
{% import "ops.c" as ops %}
{% from "action.c" import start_action, end_action %}
-{{ ops.render_static_init(allocations, initializations, formula.shortname) }}
+{{ ops.render_static_init(allocations, formula.shortname) }}
{{ ops.render_static_clear(frees, formula.shortname) }}
@@ -19,6 +19,7 @@ void point_add(const point_t *one, const point_t *other, const curve_t *curve, p
return;
}
{%- endif %}
+ {{ ops.render_initializations(initializations) }}
{{ ops.render_ops(operations) }}
{{ ops.render_returns(returns) }}
{{ end_action("add") }}
diff --git a/pyecsca/codegen/templates/formula_dadd.c b/pyecsca/codegen/templates/formula_dadd.c
index d409cce..46ef196 100644
--- a/pyecsca/codegen/templates/formula_dadd.c
+++ b/pyecsca/codegen/templates/formula_dadd.c
@@ -3,13 +3,14 @@
{% import "ops.c" as ops %}
{% from "action.c" import start_action, end_action %}
-{{ ops.render_static_init(allocations, initializations, formula.shortname) }}
+{{ ops.render_static_init(allocations, formula.shortname) }}
{{ ops.render_static_clear(frees, formula.shortname) }}
void point_dadd(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out_one) {
{{ start_action("dadd") }}
// TODO: short-circuits
+ {{ ops.render_initializations(initializations) }}
{{ ops.render_ops(operations) }}
{{ ops.render_returns(returns) }}
{{ end_action("dadd") }}
diff --git a/pyecsca/codegen/templates/formula_dbl.c b/pyecsca/codegen/templates/formula_dbl.c
index e70846e..45103bc 100644
--- a/pyecsca/codegen/templates/formula_dbl.c
+++ b/pyecsca/codegen/templates/formula_dbl.c
@@ -3,7 +3,7 @@
{% import "ops.c" as ops %}
{% from "action.c" import start_action, end_action %}
-{{ ops.render_static_init(allocations, initializations, formula.shortname) }}
+{{ ops.render_static_init(allocations, formula.shortname) }}
{{ ops.render_static_clear(frees, formula.shortname) }}
@@ -15,6 +15,7 @@ void point_dbl(const point_t *one, const curve_t *curve, point_t *out_one) {
return;
}
{%- endif %}
+ {{ ops.render_initializations(initializations) }}
{{ ops.render_ops(operations) }}
{{ ops.render_returns(returns) }}
{{ end_action("dbl") }}
diff --git a/pyecsca/codegen/templates/formula_ladd.c b/pyecsca/codegen/templates/formula_ladd.c
index 1ac62ec..4ae738c 100644
--- a/pyecsca/codegen/templates/formula_ladd.c
+++ b/pyecsca/codegen/templates/formula_ladd.c
@@ -3,13 +3,14 @@
{% import "ops.c" as ops %}
{% from "action.c" import start_action, end_action %}
-{{ ops.render_static_init(allocations, initializations, formula.shortname) }}
+{{ ops.render_static_init(allocations, formula.shortname) }}
{{ ops.render_static_clear(frees, formula.shortname) }}
void point_ladd(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out_one, point_t *out_other) {
{{ start_action("ladd") }}
// TODO: short-circuits
+ {{ ops.render_initializations(initializations) }}
{{ ops.render_ops(operations) }}
{{ ops.render_returns(returns) }}
{{ end_action("ladd") }}
diff --git a/pyecsca/codegen/templates/formula_neg.c b/pyecsca/codegen/templates/formula_neg.c
index 39a4f5c..e5b47ef 100644
--- a/pyecsca/codegen/templates/formula_neg.c
+++ b/pyecsca/codegen/templates/formula_neg.c
@@ -3,7 +3,7 @@
{% import "ops.c" as ops %}
{% from "action.c" import start_action, end_action %}
-{{ ops.render_static_init(allocations, initializations, formula.shortname) }}
+{{ ops.render_static_init(allocations, formula.shortname) }}
{{ ops.render_static_clear(frees, formula.shortname) }}
@@ -15,6 +15,7 @@ void point_neg(const point_t *one, const curve_t *curve, point_t *out_one) {
return;
}
{%- endif %}
+ {{ ops.render_initializations(initializations) }}
{{ ops.render_ops(operations) }}
{{ ops.render_returns(returns) }}
{{ end_action("neg") }}
diff --git a/pyecsca/codegen/templates/formula_scl.c b/pyecsca/codegen/templates/formula_scl.c
index cc46724..32bad57 100644
--- a/pyecsca/codegen/templates/formula_scl.c
+++ b/pyecsca/codegen/templates/formula_scl.c
@@ -3,7 +3,7 @@
{% import "ops.c" as ops %}
{% from "action.c" import start_action, end_action %}
-{{ ops.render_static_init(allocations, initializations, formula.shortname) }}
+{{ ops.render_static_init(allocations, formula.shortname) }}
{{ ops.render_static_clear(frees, formula.shortname) }}
@@ -15,6 +15,7 @@ void point_scl(const point_t *one, const curve_t *curve, point_t *out_one) {
return;
}
{%- endif %}
+ {{ ops.render_initializations(initializations) }}
{{ ops.render_ops(operations) }}
{{ ops.render_returns(returns) }}
{{ end_action("scl") }}
diff --git a/pyecsca/codegen/templates/formula_tpl.c b/pyecsca/codegen/templates/formula_tpl.c
index b1863c2..2be8dee 100644
--- a/pyecsca/codegen/templates/formula_tpl.c
+++ b/pyecsca/codegen/templates/formula_tpl.c
@@ -3,7 +3,7 @@
{% import "ops.c" as ops %}
{% from "action.c" import start_action, end_action %}
-{{ ops.render_static_init(allocations, initializations, formula.shortname) }}
+{{ ops.render_static_init(allocations, formula.shortname) }}
{{ ops.render_static_clear(frees, formula.shortname) }}
@@ -15,6 +15,7 @@ void point_tpl(const point_t *one, const curve_t *curve, point_t *out_one) {
return;
}
{%- endif %}
+ {{ ops.render_initializations(initializations) }}
{{ ops.render_ops(operations) }}
{{ ops.render_returns(returns) }}
{{ end_action("tpl") }}
diff --git a/pyecsca/codegen/templates/main.c b/pyecsca/codegen/templates/main.c
index d987ab9..b30020f 100644
--- a/pyecsca/codegen/templates/main.c
+++ b/pyecsca/codegen/templates/main.c
@@ -59,12 +59,17 @@ static uint8_t cmd_init_prng(uint8_t *data, uint16_t len) {
}
static void parse_set_params(const char *path, const uint8_t *data, size_t len, void *arg) {
- {%- for param in curve_parameters + ["p", "n", "h"] %}
- if (strcmp(path, "{{ param }}") == 0) {
- bn_from_bin(data, len, &curve->{{ param }});
- return;
+ if (strlen(path) == 1) {
+ switch (*path) {
+ {%- for param in curve_parameters + ["p", "n", "h"] %}
+ case '{{ param }}': bn_from_bin(data, len, &curve->{{ param }});
+ {% if param == "p" %}
+ bn_red_setup(&curve->{{ param }}, &curve->{{ param }}_red);
+ {%- endif %}
+ return;
+ {%- endfor %}
+ }
}
- {%- endfor %}
fat_t *affine = (fat_t *) arg;
if (strcmp(path, "gx") == 0) {
diff --git a/pyecsca/codegen/templates/ops.c b/pyecsca/codegen/templates/ops.c
index 64a4ab4..12b126f 100644
--- a/pyecsca/codegen/templates/ops.c
+++ b/pyecsca/codegen/templates/ops.c
@@ -18,12 +18,13 @@
{% macro render_initializations(initializations) -%}
{%- for init, value in initializations.items() %}
bn_from_int({{ value }}, &{{ init }});
+ bn_red_encode(&{{ init }}, &curve->p, &curve->p_red);
{%- endfor %}
{%- endmacro %}
{% macro render_ops(operations) -%}
{%- for op, result, left, right in operations %}
- {{ render_op(op, result, left, right, "curve->p")}}
+ {{ render_op(op, result, left, right, "curve->p", "curve->p_red")}}
{%- endfor %}
{%- endmacro %}
@@ -39,7 +40,7 @@
{%- endif %}
{%- endmacro %}
-{% macro render_static_init(allocations, initializations, name) -%}
+{% macro render_static_init(allocations, name) -%}
{{ render_static_allocs(allocations) }}
bool point_{{ name }}_init(void) {
@@ -48,7 +49,6 @@
if (err != BN_OKAY) {
return false;
}
- {{ render_initializations(initializations) }}
return true;
}
{%- endmacro %}
diff --git a/pyecsca/codegen/templates/point.c b/pyecsca/codegen/templates/point.c
index 1ea116f..8b7b07e 100644
--- a/pyecsca/codegen/templates/point.c
+++ b/pyecsca/codegen/templates/point.c
@@ -123,9 +123,10 @@ void point_from_affine(bn_t *x, bn_t *y, const curve_t *curve, point_t *out) {
{%- endif %}
{%- if variable == "Z" %}
bn_from_int(1, &out->Z);
+ bn_red_encode(&out->Z, &curve->p, &curve->p_red);
{%- endif %}
{%- if variable == "T" %}
- bn_mod_mul(x, y, &curve->p, &out->T);
+ bn_red_mul(x, y, &curve->p, &out->T);
{%- endif %}
{%- endfor %}
{{ end_action("coord_map") }}