diff options
Diffstat (limited to 'pyecsca')
| -rw-r--r-- | pyecsca/ec/curve.py | 2 | ||||
| -rw-r--r-- | pyecsca/ec/formula.py | 2 | ||||
| -rw-r--r-- | pyecsca/ec/model.py | 3 | ||||
| -rw-r--r-- | pyecsca/ec/mult.py | 48 | ||||
| -rw-r--r-- | pyecsca/ec/point.py | 1 |
5 files changed, 56 insertions, 0 deletions
diff --git a/pyecsca/ec/curve.py b/pyecsca/ec/curve.py index 20dd3e0..ea18233 100644 --- a/pyecsca/ec/curve.py +++ b/pyecsca/ec/curve.py @@ -15,6 +15,7 @@ class EllipticCurve(object): def __init__(self, model: CurveModel, coordinate_model: CoordinateModel, parameters: Mapping[str, int], neutral: Point): + # TODO: Add base_point arg, order arg, cofactor arg. if coordinate_model not in model.coordinates.values(): raise ValueError if set(model.parameter_names).symmetric_difference(parameters.keys()): @@ -27,6 +28,7 @@ class EllipticCurve(object): self.neutral = neutral def is_on_curve(self, point: Point) -> bool: + #TODO pass def is_neutral(self, point: Point) -> bool: diff --git a/pyecsca/ec/formula.py b/pyecsca/ec/formula.py index e155282..fd58a31 100644 --- a/pyecsca/ec/formula.py +++ b/pyecsca/ec/formula.py @@ -14,6 +14,8 @@ class Formula(object): _inputs: int _outputs: int + # TODO: Separate into EFDFormula? + def __init__(self, path: str, name: str, coordinate_model: Any): self.name = name self.coordinate_model = coordinate_model diff --git a/pyecsca/ec/model.py b/pyecsca/ec/model.py index 40c19e9..cc474f5 100644 --- a/pyecsca/ec/model.py +++ b/pyecsca/ec/model.py @@ -20,6 +20,9 @@ class CurveModel(object): to_weierstrass: List[Module] from_weierstrass: List[Module] + #TODO: move the base_formulas into methods, operatin on affine points? + # Also to_weierstrass anf from_weierstrass. + class EFDCurveModel(CurveModel): _efd_name: str diff --git a/pyecsca/ec/mult.py b/pyecsca/ec/mult.py index d7c5bc6..aba2739 100644 --- a/pyecsca/ec/mult.py +++ b/pyecsca/ec/mult.py @@ -77,6 +77,11 @@ class ScalarMultiplier(object): @public class LTRMultiplier(ScalarMultiplier): + """ + Classic double and add scalar multiplication algorithm, that scans the scalar left-to-right (msb to lsb) + + The `always` parameter determines whether the double and add always method is used. + """ always: bool def __init__(self, curve: EllipticCurve, add: AdditionFormula, dbl: DoublingFormula, @@ -101,6 +106,11 @@ class LTRMultiplier(ScalarMultiplier): @public class RTLMultiplier(ScalarMultiplier): + """ + Classic double and add scalar multiplication algorithm, that scans the scalar right-to-left (lsb to msb) + + The `always` parameter determines whether the double and add always method is used. + """ always: bool def __init__(self, curve: EllipticCurve, add: AdditionFormula, dbl: DoublingFormula, @@ -124,8 +134,37 @@ class RTLMultiplier(ScalarMultiplier): return r +class CoronMultiplier(ScalarMultiplier): + """ + Coron's double and add resistant against SPA, from: + + Resistance against Differential Power Analysis for Elliptic Curve Cryptosystems + + https://link.springer.com/content/pdf/10.1007/3-540-48059-5_25.pdf + """ + + def __init__(self, curve: EllipticCurve, add: AdditionFormula, dbl: DoublingFormula, + scl: ScalingFormula = None, ctx: Context = None): + super().__init__(curve, ctx, add=add, dbl=dbl, scl=scl) + + def multiply(self, scalar: int, point: Optional[Point] = None): + q = self._init_multiply(point) + p0 = copy(q) + for i in range(scalar.bit_length() - 2, -1, -1): + p0 = self._dbl(p0) + p1 = self._add(p0, q) + if scalar & (1 << i) != 0: + p0 = p1 + if "scl" in self.formulas: + p0 = self._scl(p0) + return p0 + + @public class LadderMultiplier(ScalarMultiplier): + """ + Montgomery ladder multiplier, using a three input, two output ladder formula. + """ def __init__(self, curve: EllipticCurve, ladd: LadderFormula, scl: ScalingFormula = None, ctx: Context = None): @@ -147,6 +186,9 @@ class LadderMultiplier(ScalarMultiplier): @public class SimpleLadderMultiplier(ScalarMultiplier): + """ + Montgomery ladder multiplier, using addition and doubling formulas. + """ def __init__(self, curve: EllipticCurve, add: AdditionFormula, dbl: DoublingFormula, scl: ScalingFormula = None, ctx: Context = None): @@ -170,6 +212,9 @@ class SimpleLadderMultiplier(ScalarMultiplier): @public class BinaryNAFMultiplier(ScalarMultiplier): + """ + Binary NAF (Non Adjacent Form) multiplier, left-to-right. + """ _point_neg: Point def __init__(self, curve: EllipticCurve, add: AdditionFormula, dbl: DoublingFormula, @@ -197,6 +242,9 @@ class BinaryNAFMultiplier(ScalarMultiplier): @public class WindowNAFMultiplier(ScalarMultiplier): + """ + Window NAF (Non Adjacent Form) multiplier, left-to-right. + """ _points: MutableMapping[int, Point] _width: int diff --git a/pyecsca/ec/point.py b/pyecsca/ec/point.py index 265a0ea..05f2503 100644 --- a/pyecsca/ec/point.py +++ b/pyecsca/ec/point.py @@ -15,6 +15,7 @@ class Point(object): self.coords = coords def __eq__(self, other): + # TODO: Somehow compare projective points. Via a map to an affinepoint? if type(other) is not Point: return False return self.coordinate_model == other.coordinate_model and self.coords == other.coords |
