aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ08nY2020-02-20 15:06:51 +0100
committerJ08nY2020-02-20 15:06:51 +0100
commit1672be7f66050579fba457153f31bd38f08f6465 (patch)
treeb949ff6b3cfe8636f3fdd811fdd6493055e74fdc
parent9788c9af717a49f5e52d6db85afdcd940bb3578d (diff)
downloadpyecsca-1672be7f66050579fba457153f31bd38f08f6465.tar.gz
pyecsca-1672be7f66050579fba457153f31bd38f08f6465.tar.bz2
pyecsca-1672be7f66050579fba457153f31bd38f08f6465.zip
Fix handling of formulas with unary negation.
-rw-r--r--pyecsca/ec/mult.py4
-rw-r--r--pyecsca/ec/op.py20
2 files changed, 17 insertions, 7 deletions
diff --git a/pyecsca/ec/mult.py b/pyecsca/ec/mult.py
index e13003f..710594c 100644
--- a/pyecsca/ec/mult.py
+++ b/pyecsca/ec/mult.py
@@ -349,6 +349,8 @@ class BinaryNAFMultiplier(ScalarMultiplier):
"""
Binary NAF (Non Adjacent Form) multiplier, left-to-right.
"""
+ requires = {AdditionFormula, DoublingFormula, NegationFormula}
+ optionals = {ScalingFormula}
_point_neg: Point
def __init__(self, add: AdditionFormula, dbl: DoublingFormula,
@@ -383,6 +385,8 @@ class WindowNAFMultiplier(ScalarMultiplier):
"""
Window NAF (Non Adjacent Form) multiplier, left-to-right.
"""
+ requires = {AdditionFormula, DoublingFormula, NegationFormula}
+ optionals = {ScalingFormula}
_points: MutableMapping[int, Point]
_points_neg: MutableMapping[int, Point]
precompute_negation: bool = False
diff --git a/pyecsca/ec/op.py b/pyecsca/ec/op.py
index 0685572..637b627 100644
--- a/pyecsca/ec/op.py
+++ b/pyecsca/ec/op.py
@@ -1,5 +1,5 @@
-from ast import (Module, walk, Name, BinOp, Constant, Mult, Div, Add, Sub, Pow, Assign,
- operator as ast_operator)
+from ast import (Module, walk, Name, BinOp, UnaryOp, Constant, Mult, Div, Add, Sub, Pow, Assign,
+ operator as ast_operator, USub)
from enum import Enum
from types import CodeType
from typing import FrozenSet, cast, Any, Optional
@@ -14,6 +14,7 @@ from .mod import Mod
class OpType(Enum):
Add = (2, "+")
Sub = (2, "-")
+ Neg = (1, "-")
Mult = (2, "*")
Div = (2, "/")
Inv = (1, "/")
@@ -54,11 +55,14 @@ class CodeOp(object):
params.add(name)
elif isinstance(node, Constant):
constants.add(node.value)
- elif isinstance(node, BinOp):
- op = node.op
- self.left = self.__to_name(node.left)
- self.right = self.__to_name(node.right)
- if isinstance(assign.value, Name):
+ if isinstance(assign.value, BinOp):
+ op = assign.value.op
+ self.left = self.__to_name(assign.value.left)
+ self.right = self.__to_name(assign.value.right)
+ elif isinstance(assign.value, UnaryOp):
+ op = assign.value.op
+ self.right = self.__to_name(assign.value.operand)
+ elif isinstance(assign.value, Name):
self.left = assign.value.id
elif isinstance(assign.value, Constant):
self.left = assign.value.value
@@ -87,6 +91,8 @@ class CodeOp(object):
return OpType.Add
elif isinstance(op, Sub):
return OpType.Sub
+ elif isinstance(op, USub):
+ return OpType.Neg
elif isinstance(op, Pow):
if right == 2:
return OpType.Sqr