diff options
| author | J08nY | 2020-02-20 15:06:18 +0100 |
|---|---|---|
| committer | J08nY | 2020-02-20 15:06:18 +0100 |
| commit | 92cb16e8103da998aa1bf226d24ef6771a92c5d5 (patch) | |
| tree | 760e5f4921e8813b29748e7353a32168d99140cc /pyecsca/codegen | |
| parent | 5da1d167c203395103d220c450e29fece08f4198 (diff) | |
| download | pyecsca-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')
| -rw-r--r-- | pyecsca/codegen/bn/bn.c | 5 | ||||
| -rw-r--r-- | pyecsca/codegen/bn/bn.h | 1 | ||||
| -rw-r--r-- | pyecsca/codegen/formulas.h | 8 | ||||
| -rw-r--r-- | pyecsca/codegen/point.h | 14 | ||||
| -rw-r--r-- | pyecsca/codegen/render.py | 42 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/Makefile | 2 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/formula_add.c | 10 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/formula_dadd.c | 10 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/formula_dbl.c | 10 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/formula_ladd.c | 10 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/formula_neg.c | 10 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/formula_scl.c | 10 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/formula_tpl.c | 10 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/formulas.c | 15 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/main.c | 15 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/mult.c | 28 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/mult_bnaf.c | 4 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/ops.c | 75 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/point.c | 5 |
19 files changed, 217 insertions, 67 deletions
diff --git a/pyecsca/codegen/bn/bn.c b/pyecsca/codegen/bn/bn.c index 635eaae..c953119 100644 --- a/pyecsca/codegen/bn/bn.c +++ b/pyecsca/codegen/bn/bn.c @@ -72,6 +72,11 @@ void bn_mod_sub(const bn_t *one, const bn_t *other, const bn_t *mod, bn_t *out) mp_submod(one, other, mod, out); } +void bn_mod_neg(const bn_t *one, const bn_t *mod, bn_t *out) { + mp_neg(one, out); + mp_mod(out, mod, out); +} + void bn_mod_mul(const bn_t *one, const bn_t *other, const bn_t *mod, bn_t *out) { mp_mulmod(one, other, mod, out); } diff --git a/pyecsca/codegen/bn/bn.h b/pyecsca/codegen/bn/bn.h index a5b1ba9..e323170 100644 --- a/pyecsca/codegen/bn/bn.h +++ b/pyecsca/codegen/bn/bn.h @@ -41,6 +41,7 @@ void bn_rand_mod_reduce(bn_t *out, const bn_t *mod); void bn_mod_add(const bn_t *one, const bn_t *other, const bn_t *mod, bn_t *out); void bn_mod_sub(const bn_t *one, const bn_t *other, const bn_t *mod, bn_t *out); +void bn_mod_neg(const bn_t *one, const bn_t *mod, bn_t *out); void bn_mod_mul(const bn_t *one, const bn_t *other, const bn_t *mod, bn_t *out); void bn_mod_sqr(const bn_t *one, const bn_t *mod, bn_t *out); void bn_mod_div(const bn_t *one, const bn_t *other, const bn_t *mod, bn_t *out); diff --git a/pyecsca/codegen/formulas.h b/pyecsca/codegen/formulas.h new file mode 100644 index 0000000..763b85d --- /dev/null +++ b/pyecsca/codegen/formulas.h @@ -0,0 +1,8 @@ +#ifndef FORMULAS_H_ +#define FORMULAS_H_ + +void formulas_init(void); + +void formulas_clear(void); + +#endif //FORMULAS_H_
\ No newline at end of file diff --git a/pyecsca/codegen/point.h b/pyecsca/codegen/point.h index f85a818..1711e42 100644 --- a/pyecsca/codegen/point.h +++ b/pyecsca/codegen/point.h @@ -20,17 +20,31 @@ void point_to_affine(const point_t *point, const curve_t *curve, bn_t *out_x, bn void point_from_affine(bn_t *x, bn_t *y, const curve_t *curve, point_t *out); void point_add(const point_t *one, const point_t *other, const curve_t *curve, point_t *out_one); +void point_add_init(void); +void point_add_clear(void); void point_dbl(const point_t *one, const curve_t *curve, point_t *out_one); +void point_dbl_init(void); +void point_dbl_clear(void); void point_tpl(const point_t *one, const curve_t *curve, point_t *out_one); +void point_tpl_init(void); +void point_tpl_clear(void); void point_neg(const point_t *one, const curve_t *curve, point_t *out_one); +void point_neg_init(void); +void point_neg_clear(void); void point_scl(const point_t *one, const curve_t *curve, point_t *out_one); +void point_scl_init(void); +void point_scl_clear(void); void point_dadd(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out_one); +void point_dadd_init(void); +void point_dadd_clear(void); 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); +void point_ladd_init(void); +void point_ladd_clear(void); #endif //POINT_H_
\ No newline at end of file diff --git a/pyecsca/codegen/render.py b/pyecsca/codegen/render.py index 6ea1e36..5cfe569 100644 --- a/pyecsca/codegen/render.py +++ b/pyecsca/codegen/render.py @@ -11,9 +11,7 @@ from pkg_resources import resource_filename from public import public from pyecsca.ec.configuration import HashType, RandomMod from pyecsca.ec.coordinates import CoordinateModel -from pyecsca.ec.formula import (Formula, AdditionFormula, DoublingFormula, TriplingFormula, - NegationFormula, ScalingFormula, DifferentialAdditionFormula, - LadderFormula) +from pyecsca.ec.formula import (Formula) from pyecsca.ec.model import CurveModel from pyecsca.ec.mult import (ScalarMultiplier, LTRMultiplier, RTLMultiplier, CoronMultiplier, LadderMultiplier, SimpleLadderMultiplier, DifferentialLadderMultiplier, @@ -34,6 +32,8 @@ def render_op(op: OpType, result: str, left: str, right: str, mod: str) -> Optio return "bn_mod_add(&{}, &{}, &{}, &{});".format(left, right, mod, result) elif op == OpType.Sub: return "bn_mod_sub(&{}, &{}, &{}, &{});".format(left, right, mod, result) + elif op == OpType.Neg: + return "bn_mod_neg(&{}, &{}, &{});".format(right, mod, result) elif op == OpType.Mult: return "bn_mod_mul(&{}, &{}, &{}, &{});".format(left, right, mod, result) elif op == OpType.Div or op == OpType.Inv: @@ -138,24 +138,13 @@ def render_coords_impl(coords: CoordinateModel) -> str: to_affine_rets=returns, to_affine_frees=frees) +def render_formulas_impl(formulas: Set[Formula]) -> str: + names = {formula.shortname for formula in formulas} + return env.get_template("formulas.c").render(names=names) + + def render_formula_impl(formula: Formula, short_circuit: bool = False) -> str: - if isinstance(formula, AdditionFormula): - tname = "formula_add.c" - elif isinstance(formula, DoublingFormula): - tname = "formula_dbl.c" - elif isinstance(formula, TriplingFormula): - tname = "formula_tpl.c" - elif isinstance(formula, NegationFormula): - tname = "formula_neg.c" - elif isinstance(formula, ScalingFormula): - tname = "formula_scl.c" - elif isinstance(formula, DifferentialAdditionFormula): - tname = "formula_dadd.c" - elif isinstance(formula, LadderFormula): - tname = "formula_ladd.c" - else: - raise ValueError - template = env.get_template(tname) + template = env.get_template(f"formula_{formula.shortname}.c") inputs = ["one", "other", "diff"] outputs = ["out_one", "out_other"] renames = {} @@ -172,6 +161,7 @@ def render_formula_impl(formula: Formula, short_circuit: bool = False) -> str: namespace = transform_ops(formula.code, formula.coordinate_model.curve_model.parameter_names, formula.outputs, renames) namespace["short_circuit"] = short_circuit + namespace["formula"] = formula return template.render(namespace) @@ -212,7 +202,7 @@ def render(config: DeviceConfiguration) -> Tuple[str, str, str]: """ temp = tempfile.mkdtemp() symlinks = ["asn1", "bn", "hal", "hash", "mult", "prng", "simpleserial", "tommath", "fat.h", - "point.h", "curve.h", "mult.h", "Makefile.inc"] + "point.h", "curve.h", "mult.h", "formulas.h", "Makefile.inc"] for sym in symlinks: os.symlink(resource_filename("pyecsca.codegen", sym), path.join(temp, sym)) gen_dir = path.join(temp, "gen") @@ -222,11 +212,10 @@ def render(config: DeviceConfiguration) -> Tuple[str, str, str]: save_render(temp, "main.c", render_main(config.model, config.coords, config.keygen, config.ecdh, config.ecdsa)) save_render(gen_dir, "defs.h", render_defs(config.model, config.coords)) - point_render = render_coords_impl(config.coords) + save_render(gen_dir, "point.c", render_coords_impl(config.coords)) + save_render(gen_dir, "formulas.c", render_formulas_impl(config.formulas)) for formula in config.formulas: - point_render += "\n" - point_render += render_formula_impl(formula, config.scalarmult.short_circuit) - save_render(gen_dir, "point.c", point_render) + save_render(gen_dir, f"formula_{formula.shortname}.c", render_formula_impl(formula, config.scalarmult.short_circuit)) save_render(gen_dir, "curve.c", render_curve_impl(config.model)) save_render(gen_dir, "mult.c", render_scalarmult_impl(config.scalarmult)) return temp, "pyecsca-codegen-{}.elf".format( @@ -250,8 +239,7 @@ def build(dir: str, elf_file: str, hex_file: str, outdir: str, strip: bool = Fal if res.returncode != 0: raise ValueError("Build failed!") if strip: - s = subprocess.run(["strip", elf_file], cwd=dir) - s.returncode + subprocess.run(["strip", elf_file], cwd=dir) full_elf_path = path.join(dir, elf_file) full_hex_path = path.join(dir, hex_file) shutil.copy(full_elf_path, outdir) 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); |
