aboutsummaryrefslogtreecommitdiffhomepage
path: root/pyecsca/codegen
diff options
context:
space:
mode:
Diffstat (limited to 'pyecsca/codegen')
-rw-r--r--pyecsca/codegen/__init__.py87
-rw-r--r--pyecsca/codegen/formulas.h14
-rw-r--r--pyecsca/codegen/templates/coords.c4
-rw-r--r--pyecsca/codegen/templates/curve.c1
-rw-r--r--pyecsca/codegen/templates/curve.h2
-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
12 files changed, 98 insertions, 31 deletions
diff --git a/pyecsca/codegen/__init__.py b/pyecsca/codegen/__init__.py
index 7848a60..33320ac 100644
--- a/pyecsca/codegen/__init__.py
+++ b/pyecsca/codegen/__init__.py
@@ -1,15 +1,19 @@
from ast import operator, Add, Sub, Mult, Div, Pow
-from typing import List
+from typing import List, Set, Mapping
from jinja2 import Environment, PackageLoader
from pyecsca.ec.coordinates import CoordinateModel
-from pyecsca.ec.model import CurveModel, ShortWeierstrassModel, MontgomeryModel
+from pyecsca.ec.formula import (Formula, AdditionFormula, DoublingFormula, TriplingFormula,
+ NegationFormula, ScalingFormula, DifferentialAdditionFormula,
+ LadderFormula)
+from pyecsca.ec.model import CurveModel, MontgomeryModel
from pyecsca.ec.op import CodeOp
env = Environment(
loader=PackageLoader("pyecsca.codegen")
)
+
def render_op(op: operator, result: str, left: str, right: str, mod: str):
if isinstance(op, Add):
return "bn_mod_add(&{}, &{}, &{}, &{});".format(left, right, mod, result)
@@ -22,22 +26,29 @@ def render_op(op: operator, result: str, left: str, right: str, mod: str):
elif isinstance(op, Pow) and right == 2:
return "bn_mod_sqr(&{}, &{}, &{});".format(left, mod, result)
+
env.globals["render_op"] = render_op
-def get_curve_definition(model: CurveModel):
+def render_curve_definition(model: CurveModel):
return env.get_template("curve.h").render(params=model.parameter_names)
-def get_curve_impl(model: CurveModel):
+def render_curve_impl(model: CurveModel):
return env.get_template("curve.c").render(params=model.parameter_names)
-def get_coords_definition(coords: CoordinateModel):
+def render_coords_definition(coords: CoordinateModel):
return env.get_template("coords.h").render(variables=coords.variables)
-def transform_ops(ops: List[CodeOp], parameters: List[str], outputs: List[str]):
+def transform_ops(ops: List[CodeOp], parameters: List[str], outputs: Set[str],
+ renames: Mapping[str, str] = None):
+ def rename(name: str):
+ if renames is not None:
+ return renames.get(name, name)
+ return name
+
allocations = []
initializations = {}
const_mapping = {}
@@ -57,14 +68,21 @@ def transform_ops(ops: List[CodeOp], parameters: List[str], outputs: List[str]):
initializations[name] = const
const_mapping[const] = name
frees.append(name)
- operations.append((op.operator, op.result, op.left, op.right))
- return env.get_template("ops.c").render(allocations=allocations,
- initializations=initializations,
- const_mapping=const_mapping, operations=operations,
- frees=frees)
+ operations.append((op.operator, op.result, rename(op.left), rename(op.right)))
+
+ return dict(allocations=allocations,
+ initializations=initializations,
+ const_mapping=const_mapping, operations=operations,
+ frees=frees)
+
+
+def render_ops(ops: List[CodeOp], parameters: List[str], outputs: Set[str],
+ renames: Mapping[str, str] = None):
+ namespace = transform_ops(ops, parameters, outputs, renames)
+ return env.get_template("ops.c").render(namespace)
-def get_coords_impl(coords: CoordinateModel):
+def render_coords_impl(coords: CoordinateModel):
ops = []
for s in coords.satisfying:
try:
@@ -72,23 +90,46 @@ def get_coords_impl(coords: CoordinateModel):
except:
pass
transform_ops(ops, coords.curve_model.parameter_names, coords.curve_model.coordinate_names)
+ # TODO: do point_from_affine, and point_to_affine
return env.get_template("coords.c").render(variables=coords.variables)
-if __name__ == "__main__":
- model = ShortWeierstrassModel()
- s = get_curve_definition(model)
-
- s = get_curve_impl(model)
-
- coords = model.coordinates["projective"]
+def render_formula_impl(formula: Formula):
+ 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)
+ inputs = ["one", "other", "diff"]
+ outputs = ["out_one", "out_other"]
+ renames = {}
+ for input in formula.inputs:
+ var = input[0]
+ num = int(input[1:]) - formula.input_index
+ renames[input] = "{}->{}".format(inputs[num], var)
+ for output in formula.outputs:
+ var = output[0]
+ num = int(output[1:]) - formula.output_index
+ renames[output] = "{}->{}".format(outputs[num], var)
+ namespace = transform_ops(formula.code, formula.coordinate_model.curve_model.parameter_names, formula.outputs, renames)
+ return template.render(namespace)
- s = get_coords_definition(coords)
-
- s = get_coords_impl(coords)
+if __name__ == "__main__":
mont = MontgomeryModel()
mcoords = mont.coordinates["xz"]
dbl = mcoords.formulas["dbl-1987-m"]
t = transform_ops(dbl.code, mont.parameter_names, dbl.outputs)
- print(t)
+ print(render_formula_impl(dbl))
diff --git a/pyecsca/codegen/formulas.h b/pyecsca/codegen/formulas.h
index c8ba6d4..a2278bb 100644
--- a/pyecsca/codegen/formulas.h
+++ b/pyecsca/codegen/formulas.h
@@ -4,16 +4,18 @@
#include "coords.h"
#include "defs.h"
-int point_add(const point_t *one, const point_t *other, const curve_t *curve, point_t *out);
+int point_add(const point_t *one, const point_t *other, const curve_t *curve, point_t *out_one);
-int point_dbl(const point_t *one, const curve_t *curve, point_t *out);
+int point_dbl(const point_t *one, const curve_t *curve, point_t *out_one);
-int point_neg(const point_t *one, const curve_t *curve, point_t *out);
+int point_tpl(const point_t *one, const curve_t *curve, point_t *out_one);
-int point_scl(const point_t *one, const curve_t *curve, point_t *out);
+int point_neg(const point_t *one, const curve_t *curve, point_t *out_one);
-int point_dadd(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out);
+int point_scl(const point_t *one, const curve_t *curve, point_t *out_one);
-int point_ldr(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out_one, point_t *out_other);
+int point_dadd(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out_one);
+
+int 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);
#endif //FORMULAS_H_ \ No newline at end of file
diff --git a/pyecsca/codegen/templates/coords.c b/pyecsca/codegen/templates/coords.c
index 00f6086..98ab86f 100644
--- a/pyecsca/codegen/templates/coords.c
+++ b/pyecsca/codegen/templates/coords.c
@@ -30,4 +30,6 @@ int point_to_affine(point_t *point, const char coord, curve_t *curve, bn_t *out)
}
-int point_from_affine(bn_t *x, bn_t *y, curve_t *curve, point_t *out);
+int point_from_affine(bn_t *x, bn_t *y, curve_t *curve, point_t *out) {
+
+}
diff --git a/pyecsca/codegen/templates/curve.c b/pyecsca/codegen/templates/curve.c
index 675f748..3d98571 100644
--- a/pyecsca/codegen/templates/curve.c
+++ b/pyecsca/codegen/templates/curve.c
@@ -5,6 +5,7 @@ curve_t* curve_new(const named_bn_t **params, int num_params) {
bn_init(&result->{{ param }});
{%- endfor %}
bn_init(&result->n);
+ result->neutral = NULL;
for (int i = 0; i < num_params; ++i) {
switch (params[i]->name) {
diff --git a/pyecsca/codegen/templates/curve.h b/pyecsca/codegen/templates/curve.h
index 42445a4..e901304 100644
--- a/pyecsca/codegen/templates/curve.h
+++ b/pyecsca/codegen/templates/curve.h
@@ -5,5 +5,5 @@ typedef struct {
{%- endfor %}
bn_t n;
bn_t h;
- point_t neutral;
+ point_t *neutral;
} curve_t; \ No newline at end of file
diff --git a/pyecsca/codegen/templates/formula_add.c b/pyecsca/codegen/templates/formula_add.c
new file mode 100644
index 0000000..483fdac
--- /dev/null
+++ b/pyecsca/codegen/templates/formula_add.c
@@ -0,0 +1,3 @@
+int point_add(const point_t *one, const point_t *other, const curve_t *curve, point_t *out_one) {
+ {%- include "ops.c" %}
+} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/formula_dadd.c b/pyecsca/codegen/templates/formula_dadd.c
new file mode 100644
index 0000000..16d49c1
--- /dev/null
+++ b/pyecsca/codegen/templates/formula_dadd.c
@@ -0,0 +1,3 @@
+int point_dadd(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out_one) {
+ {%- include "ops.c" %}
+}
diff --git a/pyecsca/codegen/templates/formula_dbl.c b/pyecsca/codegen/templates/formula_dbl.c
new file mode 100644
index 0000000..13da1c0
--- /dev/null
+++ b/pyecsca/codegen/templates/formula_dbl.c
@@ -0,0 +1,3 @@
+int point_dbl(const point_t *one, const curve_t *curve, point_t *out_one) {
+ {%- include "ops.c" %}
+} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/formula_ladd.c b/pyecsca/codegen/templates/formula_ladd.c
new file mode 100644
index 0000000..d1c5c91
--- /dev/null
+++ b/pyecsca/codegen/templates/formula_ladd.c
@@ -0,0 +1,3 @@
+int 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) {
+ {%- include "ops.c" %}
+} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/formula_neg.c b/pyecsca/codegen/templates/formula_neg.c
new file mode 100644
index 0000000..0b7703a
--- /dev/null
+++ b/pyecsca/codegen/templates/formula_neg.c
@@ -0,0 +1,3 @@
+int point_neg(const point_t *one, const curve_t *curve, point_t *out_one) {
+ {%- include "ops.c" %}
+} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/formula_scl.c b/pyecsca/codegen/templates/formula_scl.c
new file mode 100644
index 0000000..3f1338d
--- /dev/null
+++ b/pyecsca/codegen/templates/formula_scl.c
@@ -0,0 +1,3 @@
+int point_scl(const point_t *one, const curve_t *curve, point_t *out_one) {
+ {%- include "ops.c" %}
+} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/formula_tpl.c b/pyecsca/codegen/templates/formula_tpl.c
new file mode 100644
index 0000000..dbef1ae
--- /dev/null
+++ b/pyecsca/codegen/templates/formula_tpl.c
@@ -0,0 +1,3 @@
+int point_tpl(const point_t *one, const curve_t *curve, point_t *out_one) {
+ {%- include "ops.c" %}
+} \ No newline at end of file