aboutsummaryrefslogtreecommitdiffhomepage
path: root/pyecsca/codegen/templates
diff options
context:
space:
mode:
authorJ08nY2020-02-20 15:06:18 +0100
committerJ08nY2020-02-20 15:06:18 +0100
commit92cb16e8103da998aa1bf226d24ef6771a92c5d5 (patch)
tree760e5f4921e8813b29748e7353a32168d99140cc /pyecsca/codegen/templates
parent5da1d167c203395103d220c450e29fece08f4198 (diff)
downloadpyecsca-codegen-92cb16e8103da998aa1bf226d24ef6771a92c5d5.tar.gz
pyecsca-codegen-92cb16e8103da998aa1bf226d24ef6771a92c5d5.tar.zst
pyecsca-codegen-92cb16e8103da998aa1bf226d24ef6771a92c5d5.zip
Allocate and initialize formula variables only once.
Diffstat (limited to 'pyecsca/codegen/templates')
-rw-r--r--pyecsca/codegen/templates/Makefile2
-rw-r--r--pyecsca/codegen/templates/formula_add.c10
-rw-r--r--pyecsca/codegen/templates/formula_dadd.c10
-rw-r--r--pyecsca/codegen/templates/formula_dbl.c10
-rw-r--r--pyecsca/codegen/templates/formula_ladd.c10
-rw-r--r--pyecsca/codegen/templates/formula_neg.c10
-rw-r--r--pyecsca/codegen/templates/formula_scl.c10
-rw-r--r--pyecsca/codegen/templates/formula_tpl.c10
-rw-r--r--pyecsca/codegen/templates/formulas.c15
-rw-r--r--pyecsca/codegen/templates/main.c15
-rw-r--r--pyecsca/codegen/templates/mult.c28
-rw-r--r--pyecsca/codegen/templates/mult_bnaf.c4
-rw-r--r--pyecsca/codegen/templates/ops.c75
-rw-r--r--pyecsca/codegen/templates/point.c5
14 files changed, 174 insertions, 40 deletions
diff --git a/pyecsca/codegen/templates/Makefile b/pyecsca/codegen/templates/Makefile
index 9526640..5d842c4 100644
--- a/pyecsca/codegen/templates/Makefile
+++ b/pyecsca/codegen/templates/Makefile
@@ -1,6 +1,6 @@
TARGET = pyecsca-codegen
-SRC += main.c bn/bn.c asn1/asn1.c hash/hash.c prng/prng.c gen/point.c gen/curve.c gen/mult.c
+SRC += main.c bn/bn.c asn1/asn1.c hash/hash.c prng/prng.c $(wildcard gen/*.c)
PLATFORM = {{ platform }}
diff --git a/pyecsca/codegen/templates/formula_add.c b/pyecsca/codegen/templates/formula_add.c
index 05fd541..0a04757 100644
--- a/pyecsca/codegen/templates/formula_add.c
+++ b/pyecsca/codegen/templates/formula_add.c
@@ -1,3 +1,10 @@
+#include "point.h"
+{% import "ops.c" as ops %}
+
+{{ ops.render_static_init(allocations, initializations, formula.shortname) }}
+
+{{ ops.render_static_clear(frees, formula.shortname) }}
+
void point_add(const point_t *one, const point_t *other, const curve_t *curve, point_t *out_one) {
{%- if short_circuit %}
if (point_equals(one, curve->neutral)) {
@@ -9,5 +16,6 @@ void point_add(const point_t *one, const point_t *other, const curve_t *curve, p
return;
}
{%- endif %}
- {%- include "ops.c" %}
+ {{ ops.render_ops(operations) }}
+ {{ ops.render_returns(returns) }}
} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/formula_dadd.c b/pyecsca/codegen/templates/formula_dadd.c
index 7f4d2e5..0cdefe6 100644
--- a/pyecsca/codegen/templates/formula_dadd.c
+++ b/pyecsca/codegen/templates/formula_dadd.c
@@ -1,4 +1,12 @@
+#include "point.h"
+{% import "ops.c" as ops %}
+
+{{ ops.render_static_init(allocations, initializations, 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) {
// TODO: short-circuits
- {%- include "ops.c" %}
+ {{ ops.render_ops(operations) }}
+ {{ ops.render_returns(returns) }}
}
diff --git a/pyecsca/codegen/templates/formula_dbl.c b/pyecsca/codegen/templates/formula_dbl.c
index bf91efb..6410e7c 100644
--- a/pyecsca/codegen/templates/formula_dbl.c
+++ b/pyecsca/codegen/templates/formula_dbl.c
@@ -1,3 +1,10 @@
+#include "point.h"
+{% import "ops.c" as ops %}
+
+{{ ops.render_static_init(allocations, initializations, formula.shortname) }}
+
+{{ ops.render_static_clear(frees, formula.shortname) }}
+
void point_dbl(const point_t *one, const curve_t *curve, point_t *out_one) {
{%- if short_circuit %}
if (point_equals(one, curve->neutral)) {
@@ -5,5 +12,6 @@ void point_dbl(const point_t *one, const curve_t *curve, point_t *out_one) {
return;
}
{%- endif %}
- {%- include "ops.c" %}
+ {{ ops.render_ops(operations) }}
+ {{ ops.render_returns(returns) }}
} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/formula_ladd.c b/pyecsca/codegen/templates/formula_ladd.c
index 81bcf73..32903e9 100644
--- a/pyecsca/codegen/templates/formula_ladd.c
+++ b/pyecsca/codegen/templates/formula_ladd.c
@@ -1,4 +1,12 @@
+#include "point.h"
+{% import "ops.c" as ops %}
+
+{{ ops.render_static_init(allocations, initializations, 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) {
// TODO: short-circuits
- {%- include "ops.c" %}
+ {{ ops.render_ops(operations) }}
+ {{ ops.render_returns(returns) }}
} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/formula_neg.c b/pyecsca/codegen/templates/formula_neg.c
index fc790de..c728e70 100644
--- a/pyecsca/codegen/templates/formula_neg.c
+++ b/pyecsca/codegen/templates/formula_neg.c
@@ -1,3 +1,10 @@
+#include "point.h"
+{% import "ops.c" as ops %}
+
+{{ ops.render_static_init(allocations, initializations, formula.shortname) }}
+
+{{ ops.render_static_clear(frees, formula.shortname) }}
+
void point_neg(const point_t *one, const curve_t *curve, point_t *out_one) {
{%- if short_circuit %}
if (point_equals(one, curve->neutral)) {
@@ -5,5 +12,6 @@ void point_neg(const point_t *one, const curve_t *curve, point_t *out_one) {
return;
}
{%- endif %}
- {%- include "ops.c" %}
+ {{ ops.render_ops(operations) }}
+ {{ ops.render_returns(returns) }}
} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/formula_scl.c b/pyecsca/codegen/templates/formula_scl.c
index b5db4d7..b49fa8a 100644
--- a/pyecsca/codegen/templates/formula_scl.c
+++ b/pyecsca/codegen/templates/formula_scl.c
@@ -1,3 +1,10 @@
+#include "point.h"
+{% import "ops.c" as ops %}
+
+{{ ops.render_static_init(allocations, initializations, formula.shortname) }}
+
+{{ ops.render_static_clear(frees, formula.shortname) }}
+
void point_scl(const point_t *one, const curve_t *curve, point_t *out_one) {
{%- if short_circuit %}
if (point_equals(one, curve->neutral)) {
@@ -5,5 +12,6 @@ void point_scl(const point_t *one, const curve_t *curve, point_t *out_one) {
return;
}
{%- endif %}
- {%- include "ops.c" %}
+ {{ ops.render_ops(operations) }}
+ {{ ops.render_returns(returns) }}
} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/formula_tpl.c b/pyecsca/codegen/templates/formula_tpl.c
index 9b5df25..73f34d2 100644
--- a/pyecsca/codegen/templates/formula_tpl.c
+++ b/pyecsca/codegen/templates/formula_tpl.c
@@ -1,3 +1,10 @@
+#include "point.h"
+{% import "ops.c" as ops %}
+
+{{ ops.render_static_init(allocations, initializations, formula.shortname) }}
+
+{{ ops.render_static_clear(frees, formula.shortname) }}
+
void point_tpl(const point_t *one, const curve_t *curve, point_t *out_one) {
{%- if short_circuit %}
if (point_equals(one, curve->neutral)) {
@@ -5,5 +12,6 @@ void point_tpl(const point_t *one, const curve_t *curve, point_t *out_one) {
return;
}
{%- endif %}
- {%- include "ops.c" %}
+ {{ ops.render_ops(operations) }}
+ {{ ops.render_returns(returns) }}
} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/formulas.c b/pyecsca/codegen/templates/formulas.c
new file mode 100644
index 0000000..7135727
--- /dev/null
+++ b/pyecsca/codegen/templates/formulas.c
@@ -0,0 +1,15 @@
+#include "point.h"
+#include "formulas.h"
+
+
+void formulas_init(void) {
+ {%- for name in names %}
+ point_{{ name }}_init();
+ {%- endfor %}
+}
+
+void formulas_clear(void) {
+ {%- for name in names %}
+ point_{{ name }}_clear();
+ {%- endfor %}
+} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/main.c b/pyecsca/codegen/templates/main.c
index ab69858..ae36bfd 100644
--- a/pyecsca/codegen/templates/main.c
+++ b/pyecsca/codegen/templates/main.c
@@ -9,6 +9,7 @@
#include "point.h"
#include "curve.h"
#include "fat.h"
+#include "formulas.h"
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
@@ -47,12 +48,8 @@ static size_t parse_data(const uint8_t *data, size_t len, const char *path, void
return parsed;
}
-static void parse_init_prng(const char *path, const uint8_t *data, size_t len, void *arg) {
- prng_seed(data, len);
-}
-
static uint8_t cmd_init_prng(uint8_t *data, uint16_t len) {
- parse_data(data, len, "", parse_init_prng, NULL);
+ prng_seed(data, len);
return 0;
}
@@ -110,7 +107,6 @@ static uint8_t cmd_set_params(uint8_t *data, uint16_t len) {
static uint8_t cmd_generate(uint8_t *data, uint16_t len) {
// generate a keypair, export privkey and affine pubkey
trigger_high();
- bn_init(&privkey);
bn_rand_mod(&privkey, &curve->n);
size_t priv_size = bn_to_bin_size(&privkey);
size_t coord_size = bn_to_bin_size(&curve->p);
@@ -426,6 +422,7 @@ static uint8_t cmd_debug(uint8_t *data, uint16_t len) {
int main(void) {
platform_init();
prng_init();
+ formulas_init();
init_uart();
trigger_setup();
@@ -450,6 +447,12 @@ int main(void) {
simpleserial_addcmd('v', MAX_SS_LEN, cmd_ecdsa_verify);
{%- endif %}
simpleserial_addcmd('d', MAX_SS_LEN, cmd_debug);
+
while(simpleserial_get());
+
+ bn_clear(&privkey);
+ curve_free(curve);
+ point_free(pubkey);
+ formulas_clear();
return 0;
} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/mult.c b/pyecsca/codegen/templates/mult.c
index 6851b6b..896fdd8 100644
--- a/pyecsca/codegen/templates/mult.c
+++ b/pyecsca/codegen/templates/mult.c
@@ -2,17 +2,31 @@
#include "mult.h"
{%- if isinstance(scalarmult, LTRMultiplier) -%}
-{% include "mult_ltr.c" %}
+
+ {% include "mult_ltr.c" %}
+
{%- elif isinstance(scalarmult, RTLMultiplier) -%}
-{% include "mult_rtl.c" %}
+
+ {% include "mult_rtl.c" %}
+
{%- elif isinstance(scalarmult, CoronMultiplier) -%}
-{% include "mult_coron.c" %}
+
+ {% include "mult_coron.c" %}
+
{%- elif isinstance(scalarmult, LadderMultiplier) -%}
-{% include "mult_ldr.c" %}
+
+ {% include "mult_ldr.c" %}
+
{%- elif isinstance(scalarmult, SimpleLadderMultiplier) -%}
-{% include "mult_simple_ldr.c" %}
+
+ {% include "mult_simple_ldr.c" %}
+
{%- elif isinstance(scalarmult, DifferentialLadderMultiplier) -%}
-{% include "mult_diff_ldr.c" %}
+
+ {% include "mult_diff_ldr.c" %}
+
{%- elif isinstance(scalarmult, BinaryNAFMultiplier) -%}
-{% include "mult_bnaf.c" %}
+
+ {% include "mult_bnaf.c" %}
+
{%- endif -%}
diff --git a/pyecsca/codegen/templates/mult_bnaf.c b/pyecsca/codegen/templates/mult_bnaf.c
index 0b9c4de..9ede934 100644
--- a/pyecsca/codegen/templates/mult_bnaf.c
+++ b/pyecsca/codegen/templates/mult_bnaf.c
@@ -4,11 +4,11 @@
void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) {
point_t *neg = point_new();
point_neg(point, curve, neg);
- point_t *q = point_copy(&curve->neutral);
+ point_t *q = point_copy(curve->neutral);
wnaf_t *naf = bn_bnaf(scalar);
- for (size_t i = naf->length; i >= 0; i--) {
+ for (long i = naf->length - 1; i >= 0; i--) {
point_dbl(q, curve, q);
if (naf->data[i] == 1) {
point_add(q, point, curve, q);
diff --git a/pyecsca/codegen/templates/ops.c b/pyecsca/codegen/templates/ops.c
index b6c22ba..623c585 100644
--- a/pyecsca/codegen/templates/ops.c
+++ b/pyecsca/codegen/templates/ops.c
@@ -1,19 +1,64 @@
-{%- for alloc in allocations %}
- bn_t {{ alloc }}; bn_init(&{{ alloc }});
-{%- endfor %}
+{% macro render_full_allocs(allocations) -%}
+ {%- for alloc in allocations %}
+ bn_t {{ alloc }}; bn_init(&{{ alloc }});
+ {%- endfor %}
+{%- endmacro %}
-{%- for init, value in initializations.items() %}
- bn_from_int({{ value }}, &{{ init }});
-{%- endfor %}
+{% macro render_static_allocs(allocations) -%}
+ {%- for alloc in allocations %}
+ static bn_t {{ alloc }};
+ {%- endfor %}
+{%- endmacro %}
-{%- for op, result, left, right in operations %}
- {{ render_op(op, result, left, right, "curve->p")}}
-{%- endfor %}
+{% macro render_init_allocs(allocations) -%}
+ {%- for alloc in allocations %}
+ bn_init(&{{ alloc }});
+ {%- endfor %}
+{%- endmacro %}
-{%- for src, dst in returns.items() %}
- bn_copy(&{{ src }}, &{{ dst }});
-{%- endfor %}
+{% macro render_initializations(initializations) -%}
+ {%- for init, value in initializations.items() %}
+ bn_from_int({{ value }}, &{{ init }});
+ {%- endfor %}
+{%- endmacro %}
-{%- for free in frees %}
- bn_clear(&{{ free }});
-{%- endfor %} \ No newline at end of file
+{% macro render_ops(operations) -%}
+ {%- for op, result, left, right in operations %}
+ {{ render_op(op, result, left, right, "curve->p")}}
+ {%- endfor %}
+{%- endmacro %}
+
+{% macro render_returns(returns) -%}
+ {%- for src, dst in returns.items() %}
+ bn_copy(&{{ src }}, &{{ dst }});
+ {%- endfor %}
+{%- endmacro %}
+
+{% macro render_frees(frees) -%}
+ {%- for free in frees %}
+ bn_clear(&{{ free }});
+ {%- endfor %}
+{%- endmacro %}
+
+{% macro render_static_init(allocations, initializations, name) -%}
+ {{ render_static_allocs(allocations) }}
+
+ void point_{{ name }}_init(void) {
+ {{ render_init_allocs(allocations) }}
+ {{ render_initializations(initializations) }}
+ }
+{%- endmacro %}
+
+{% macro render_static_clear(frees, name) -%}
+ void point_{{ name }}_clear(void) {
+ {{ render_frees(frees) }}
+ }
+{%- endmacro %}
+
+{% macro render_all(allocations, initializations, operations, returns, frees) -%}
+ {{ render_full_allocs(allocations) }}
+ {{ render_initializations(initializations) }}
+ {{ render_ops(operations) }}
+ {{ render_returns(returns) }}
+ {{ render_frees(frees) }}
+{%- endmacro %}
diff --git a/pyecsca/codegen/templates/point.c b/pyecsca/codegen/templates/point.c
index 445bdc4..aebb7c7 100644
--- a/pyecsca/codegen/templates/point.c
+++ b/pyecsca/codegen/templates/point.c
@@ -1,12 +1,13 @@
#include "point.h"
#include <stdlib.h>
+{% import "ops.c" as ops %}
point_t *point_new(void) {
point_t *result = malloc(sizeof(point_t));
{%- for variable in variables %}
bn_init(&result->{{ variable }});
{%- endfor %}
-
+ result->infinity = false;
return result;
}
@@ -74,7 +75,7 @@ bool point_equals_affine(const point_t *one, const point_t *other, const curve_t
}
void point_to_affine(const point_t *point, const curve_t *curve, bn_t *out_x, bn_t *out_y) {
- {%- include "ops.c" %}
+ {{ ops.render_all(allocations, initializations, operations, returns, frees) }}
{%- if "x" in allocations %}
if (out_x) {
bn_copy(&x, out_x);