aboutsummaryrefslogtreecommitdiffhomepage
path: root/pyecsca/codegen/templates
diff options
context:
space:
mode:
authorJ08nY2020-03-02 15:11:25 +0100
committerJ08nY2020-03-02 15:11:25 +0100
commit81e5f24363cf8f50099b7fbed0fd3bcfaac9d5ea (patch)
tree2e4abe38a7e3b2a53bb8bb2d31b6fe1cfbfb2e13 /pyecsca/codegen/templates
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/templates')
-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
11 files changed, 29 insertions, 28 deletions
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") }}