diff options
| author | J08nY | 2018-12-13 17:50:16 +0100 |
|---|---|---|
| committer | J08nY | 2019-03-21 11:00:14 +0100 |
| commit | 1943d7e8fc0e1e6b578e9be04fac0690e888ed88 (patch) | |
| tree | f376a7c152526454439145217c21df75c006b455 /pyecsca/ec/context.py | |
| parent | 70a37f8134f177e02d23a402cb47344229ce4df5 (diff) | |
| download | pyecsca-1943d7e8fc0e1e6b578e9be04fac0690e888ed88.tar.gz pyecsca-1943d7e8fc0e1e6b578e9be04fac0690e888ed88.tar.zst pyecsca-1943d7e8fc0e1e6b578e9be04fac0690e888ed88.zip | |
Diffstat (limited to 'pyecsca/ec/context.py')
| -rw-r--r-- | pyecsca/ec/context.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/pyecsca/ec/context.py b/pyecsca/ec/context.py new file mode 100644 index 0000000..a65f461 --- /dev/null +++ b/pyecsca/ec/context.py @@ -0,0 +1,38 @@ +from typing import List, Tuple + +from .formula import Formula +from .mod import Mod +from .point import Point + + +class Context(object): + intermediates: List[Tuple[str, Mod]] + + def __init__(self): + self.intermediates = [] + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + pass + + def execute(self, formula: Formula, *points: Point, **params: Mod) -> Point: + coords = {} + for i, point in enumerate(points): + if point.coordinate_model != formula.coordinate_model: + raise ValueError + for coord, value in point.coords.items(): + coords[coord + str(i + 1)] = value + locals = {**coords, **params} + previous_locals = set(locals.keys()) + for line in formula.code: + exec(compile(line, "", mode="exec"), {}, locals) + diff = set(locals.keys()).difference(previous_locals) + previous_locals = set(locals.keys()) + for key in diff: + self.intermediates.append((key, locals[key])) + resulting = {variable: locals[variable + "3"] + for variable in formula.coordinate_model.variables + if variable + "3" in locals} + return Point(formula.coordinate_model, **resulting) |
