aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJ08nY2024-08-28 14:33:04 +0200
committerJ08nY2024-08-28 14:33:04 +0200
commit57b68a73b1c49b6eeb5b668bff4efb5ac1aef881 (patch)
treef01704e1277a2e7da27edb050d313e294e5c2ab6
parent979d86979313de02c4dab71f99ce1c5dddd5877a (diff)
downloadpyecsca-57b68a73b1c49b6eeb5b668bff4efb5ac1aef881.tar.gz
pyecsca-57b68a73b1c49b6eeb5b668bff4efb5ac1aef881.tar.zst
pyecsca-57b68a73b1c49b6eeb5b668bff4efb5ac1aef881.zip
Remove SwapLadderMultiplier.
It is the same as LadderMultiplier. The swaps are not tracked.
-rw-r--r--pyecsca/ec/mult/ladder.py94
-rw-r--r--pyecsca/sca/re/rpa.py6
-rw-r--r--test/ec/test_key_agreement.py9
-rw-r--r--test/ec/test_mult.py76
-rw-r--r--test/sca/test_rpa.py4
-rw-r--r--test/sca/test_rpa_context.py41
6 files changed, 53 insertions, 177 deletions
diff --git a/pyecsca/ec/mult/ladder.py b/pyecsca/ec/mult/ladder.py
index 9f31b7f..3c141cd 100644
--- a/pyecsca/ec/mult/ladder.py
+++ b/pyecsca/ec/mult/ladder.py
@@ -107,100 +107,6 @@ class LadderMultiplier(ScalarMultiplier):
@public
-class SwapLadderMultiplier(ScalarMultiplier):
- """
- Montgomery ladder multiplier, using a three input, two output ladder formula.
-
- Optionally takes a doubling formula, and if `complete` is false, it requires one.
-
- :param short_circuit: Whether the use of formulas will be guarded by short-circuit on inputs
- of the point at infinity.
- :param complete: Whether it starts processing at full order-bit-length.
- :param full: Whether it start processing at top bit of the scalar.
- """
-
- requires = {LadderFormula}
- optionals = {DoublingFormula, ScalingFormula}
- complete: bool
- """Whether it starts processing at full order-bit-length."""
- full: bool
- """Whether it start processing at top bit of the scalar."""
-
- def __init__(
- self,
- ladd: LadderFormula,
- dbl: Optional[DoublingFormula] = None,
- scl: Optional[ScalingFormula] = None,
- complete: bool = True,
- short_circuit: bool = True,
- full: bool = False,
- ):
- super().__init__(short_circuit=short_circuit, ladd=ladd, dbl=dbl, scl=scl)
- self.complete = complete
- self.full = full
-
- if complete and full:
- raise ValueError("Only one of `complete` and `full` can be set.")
-
- if dbl is None:
- if short_circuit:
- raise ValueError(
- "When `short_circuit` is set SwapLadderMultiplier requires a doubling formula."
- )
- if not (complete or full):
- raise ValueError(
- "When neither `complete` nor `full` is not set SwapLadderMultiplier requires a doubling formula."
- )
-
- def __hash__(self):
- return hash((SwapLadderMultiplier, super().__hash__(), self.complete, self.full))
-
- def __eq__(self, other):
- if not isinstance(other, SwapLadderMultiplier):
- return False
- return (
- self.formulas == other.formulas
- and self.short_circuit == other.short_circuit
- and self.complete == other.complete
- and self.full == other.full
- )
-
- def __repr__(self):
- return f"{self.__class__.__name__}({', '.join(map(str, self.formulas.values()))}, short_circuit={self.short_circuit}, complete={self.complete}, full={self.full})"
-
- def multiply(self, scalar: int) -> Point:
- if not self._initialized:
- raise ValueError("ScalarMultiplier not initialized.")
- with ScalarMultiplicationAction(self._point, self._params, scalar) as action:
- if scalar == 0:
- return action.exit(copy(self._params.curve.neutral))
- q = self._point
- if self.complete:
- p0 = copy(self._params.curve.neutral)
- p1 = self._point
- top = self._params.full_order.bit_length() - 1
- elif self.full:
- p0 = copy(self._params.curve.neutral)
- p1 = self._point
- top = scalar.bit_length() - 1
- else:
- p0 = copy(q)
- p1 = self._dbl(q)
- top = scalar.bit_length() - 2
- prev_bit = 0
- for i in range(top, -1, -1):
- k = (scalar & (1 << i)) >> i
- swap = prev_bit ^ k
- prev_bit = k
- p0, p1 = (p1, p0) if swap else (p0, p1)
- p0, p1 = self._ladd(q, p0, p1)
- p0, p1 = (p1, p0) if prev_bit else (p0, p1)
- if "scl" in self.formulas:
- p0 = self._scl(p0)
- return action.exit(p0)
-
-
-@public
class SimpleLadderMultiplier(ScalarMultiplier):
"""
Montgomery ladder multiplier, using addition and doubling formulas.
diff --git a/pyecsca/sca/re/rpa.py b/pyecsca/sca/re/rpa.py
index 61a4237..c65c3f6 100644
--- a/pyecsca/sca/re/rpa.py
+++ b/pyecsca/sca/re/rpa.py
@@ -66,14 +66,14 @@ class MultipleContext(Context):
self.base = action.point
self.neutral = action.params.curve.neutral
self.points = {self.base: 1, self.neutral: 0}
- self.parents = {self.base: []}
- self.formulas = {self.base: ""}
+ self.parents = {self.base: [], self.neutral: []}
+ self.formulas = {self.base: "", self.neutral: ""}
else:
self.base = action.point
self.neutral = action.params.curve.neutral
self.points = {self.base: 1, self.neutral: 0}
self.parents = {self.base: []}
- self.formulas = {self.base: ""}
+ self.formulas = {self.base: "", self.neutral: ""}
self.inside = True
def exit_action(self, action: Action) -> None:
diff --git a/test/ec/test_key_agreement.py b/test/ec/test_key_agreement.py
index a045c49..aa09051 100644
--- a/test/ec/test_key_agreement.py
+++ b/test/ec/test_key_agreement.py
@@ -19,7 +19,6 @@ from pyecsca.ec.mod import Mod, mod
from pyecsca.ec.mult import (
LTRMultiplier,
LadderMultiplier,
- SwapLadderMultiplier,
DifferentialLadderMultiplier,
)
import test.data.ec
@@ -59,7 +58,7 @@ def test_ka(algo, mult, secp128r1, keypair_a, keypair_b):
assert result_ab == result_ba
-def test_ka_secg():
+def test_ecdh_secg():
with files(test.data.ec).joinpath("ecdh_tv.json").open("r") as f:
secg_data = json.load(f)
secp160r1 = get_params("secg", "secp160r1", "projective")
@@ -102,10 +101,9 @@ def test_ka_secg():
"mult_args",
[
(LadderMultiplier, "ladd-1987-m", "dbl-1987-m", "scale"),
- (SwapLadderMultiplier, "ladd-1987-m", "dbl-1987-m", "scale"),
(DifferentialLadderMultiplier, "dadd-1987-m", "dbl-1987-m", "scale"),
],
- ids=["ladd", "swap", "diff"]
+ ids=["ladd", "diff"]
)
@pytest.mark.parametrize("complete", [True, False], ids=["complete", ""])
@pytest.mark.parametrize("short_circuit", [True, False], ids=["shorted", ""])
@@ -167,10 +165,9 @@ def test_x25519(
"mult_args",
[
(LadderMultiplier, "ladd-1987-m", "dbl-1987-m", "scale"),
- (SwapLadderMultiplier, "ladd-1987-m", "dbl-1987-m", "scale"),
(DifferentialLadderMultiplier, "dadd-1987-m", "dbl-1987-m", "scale"),
],
- ids=["ladd", "swap", "diff"]
+ ids=["ladd", "diff"]
)
@pytest.mark.parametrize("complete", [True, False], ids=["complete", ""])
@pytest.mark.parametrize("short_circuit", [True, False], ids=["shorted", ""])
diff --git a/test/ec/test_mult.py b/test/ec/test_mult.py
index e65dfdd..a7719dc 100644
--- a/test/ec/test_mult.py
+++ b/test/ec/test_mult.py
@@ -23,11 +23,9 @@ from pyecsca.ec.mult import (
BGMWMultiplier,
CombMultiplier,
WindowBoothMultiplier,
- SwapLadderMultiplier,
)
from pyecsca.ec.mult.fixed import FullPrecompMultiplier
from pyecsca.ec.point import InfinityPoint, Point
-from pyecsca.sca import MultipleContext
def get_formulas(coords, *names):
@@ -235,57 +233,29 @@ def test_simple_ladder(secp128r1, add, dbl, scale):
0x1000000000000000000000000000000014DEF9DEA2F79CD65812631A5CF5D3ED - 1,
],
)
-@pytest.mark.parametrize("complete", [True, False])
-@pytest.mark.parametrize("short_circuit", [True, False])
-def test_ladder_swap(curve25519, num, complete, short_circuit):
- ladder = LadderMultiplier(
- curve25519.curve.coordinate_model.formulas["ladd-1987-m"],
- curve25519.curve.coordinate_model.formulas["dbl-1987-m"],
- curve25519.curve.coordinate_model.formulas["scale"],
- complete=complete,
- short_circuit=short_circuit,
- )
- swap = SwapLadderMultiplier(
- curve25519.curve.coordinate_model.formulas["ladd-1987-m"],
- curve25519.curve.coordinate_model.formulas["dbl-1987-m"],
- curve25519.curve.coordinate_model.formulas["scale"],
- complete=complete,
- short_circuit=short_circuit,
- )
- ladder.init(curve25519, curve25519.generator)
- res_ladder = ladder.multiply(num)
- swap.init(curve25519, curve25519.generator)
- res_swap = swap.multiply(num)
- assert res_ladder == res_swap
- assert curve25519.curve.neutral == swap.multiply(0)
-
-
-@pytest.mark.parametrize(
- "num",
- [
- 15,
- 2355498743,
- 325385790209017329644351321912443757746,
- 0x1000000000000000000000000000000014DEF9DEA2F79CD65812631A5CF5D3ED - 1,
- ],
-)
-@pytest.mark.parametrize("complete", [True, False])
-@pytest.mark.parametrize("short_circuit", [True, False])
-def test_ladder_differential(curve25519, num, complete, short_circuit):
- ladder = LadderMultiplier(
- curve25519.curve.coordinate_model.formulas["ladd-1987-m"],
- curve25519.curve.coordinate_model.formulas["dbl-1987-m"],
- curve25519.curve.coordinate_model.formulas["scale"],
- complete=complete,
- short_circuit=short_circuit,
- )
- differential = DifferentialLadderMultiplier(
- curve25519.curve.coordinate_model.formulas["dadd-1987-m"],
- curve25519.curve.coordinate_model.formulas["dbl-1987-m"],
- curve25519.curve.coordinate_model.formulas["scale"],
- complete=complete,
- short_circuit=short_circuit,
- )
+@pytest.mark.parametrize("complete", [True, False], ids=["complete", ""])
+@pytest.mark.parametrize("short_circuit", [True, False], ids=["shorted", ""])
+@pytest.mark.parametrize("full", [True, False], ids=["full", ""])
+def test_ladder_differential(curve25519, num, complete, short_circuit, full):
+ try:
+ ladder = LadderMultiplier(
+ curve25519.curve.coordinate_model.formulas["ladd-1987-m"],
+ curve25519.curve.coordinate_model.formulas["dbl-1987-m"],
+ curve25519.curve.coordinate_model.formulas["scale"],
+ complete=complete,
+ short_circuit=short_circuit,
+ full=full
+ )
+ differential = DifferentialLadderMultiplier(
+ curve25519.curve.coordinate_model.formulas["dadd-1987-m"],
+ curve25519.curve.coordinate_model.formulas["dbl-1987-m"],
+ curve25519.curve.coordinate_model.formulas["scale"],
+ complete=complete,
+ short_circuit=short_circuit,
+ full=full
+ )
+ except ValueError:
+ return
ladder.init(curve25519, curve25519.generator)
res_ladder = ladder.multiply(num)
differential.init(curve25519, curve25519.generator)
diff --git a/test/sca/test_rpa.py b/test/sca/test_rpa.py
index 8924eae..d87e248 100644
--- a/test/sca/test_rpa.py
+++ b/test/sca/test_rpa.py
@@ -20,7 +20,6 @@ from pyecsca.ec.mult import (
CombMultiplier,
WindowBoothMultiplier,
LadderMultiplier,
- SwapLadderMultiplier,
DifferentialLadderMultiplier,
)
from pyecsca.ec.params import DomainParameters
@@ -215,11 +214,8 @@ def test_distinguish_ladders(curve25519):
multipliers = [
LadderMultiplier(ladd, None, None, True, False, False),
- SwapLadderMultiplier(ladd, None, None, True, False, False),
LadderMultiplier(ladd, dbl, None, False, False, False),
- SwapLadderMultiplier(ladd, dbl, None, False, False, False),
LadderMultiplier(ladd, None, None, False, False, True),
- SwapLadderMultiplier(ladd, None, None, False, False, True),
DifferentialLadderMultiplier(dadd, dbl, None, True, False, False),
DifferentialLadderMultiplier(dadd, dbl, None, False, False, True),
DifferentialLadderMultiplier(dadd, dbl, None, False, False, False),
diff --git a/test/sca/test_rpa_context.py b/test/sca/test_rpa_context.py
index 78191bc..fdb4788 100644
--- a/test/sca/test_rpa_context.py
+++ b/test/sca/test_rpa_context.py
@@ -3,14 +3,19 @@ from typing import cast
import pytest
from pyecsca.ec.context import local
-from pyecsca.ec.formula import LadderFormula, DifferentialAdditionFormula, DoublingFormula, \
- ScalingFormula
+from pyecsca.ec.formula import (
+ LadderFormula,
+ DifferentialAdditionFormula,
+ DoublingFormula,
+ ScalingFormula,
+)
+from pyecsca.ec.mod import Mod
from pyecsca.ec.mult import (
LTRMultiplier,
BinaryNAFMultiplier,
WindowNAFMultiplier,
LadderMultiplier,
- DifferentialLadderMultiplier
+ DifferentialLadderMultiplier,
)
from pyecsca.sca.re.rpa import MultipleContext
@@ -35,17 +40,19 @@ def scale(secp128r1):
return secp128r1.curve.coordinate_model.formulas["z"]
-@pytest.mark.parametrize("name,scalar",
- [
- ("5", 5),
- ("10", 10),
- ("2355498743", 2355498743),
- (
- "325385790209017329644351321912443757746",
- 325385790209017329644351321912443757746,
- ),
- ("13613624287328732", 13613624287328732),
- ])
+@pytest.mark.parametrize(
+ "name,scalar",
+ [
+ ("5", 5),
+ ("10", 10),
+ ("2355498743", 2355498743),
+ (
+ "325385790209017329644351321912443757746",
+ 325385790209017329644351321912443757746,
+ ),
+ ("13613624287328732", 13613624287328732),
+ ],
+)
def test_basic(secp128r1, add, dbl, scale, name, scalar):
mult = LTRMultiplier(
add,
@@ -77,9 +84,7 @@ def test_precomp(secp128r1, add, dbl, neg, scale):
def test_window(secp128r1, add, dbl, neg):
- mult = WindowNAFMultiplier(
- add, dbl, neg, 3, precompute_negation=True
- )
+ mult = WindowNAFMultiplier(add, dbl, neg, 3, precompute_negation=True)
with local(MultipleContext()):
mult.init(secp128r1, secp128r1.generator)
mult.multiply(5)
@@ -92,12 +97,14 @@ def test_ladder(curve25519):
dadd = cast(DifferentialAdditionFormula, coords.formulas["dadd-1987-m"])
dbl = cast(DoublingFormula, coords.formulas["dbl-1987-m"])
scale = cast(ScalingFormula, coords.formulas["scale"])
+
ladd_mult = LadderMultiplier(ladd, dbl, scale)
with local(MultipleContext()) as ctx:
ladd_mult.init(curve25519, base)
ladd_mult.multiply(1339278426732672313)
muls = list(ctx.points.values())
assert muls[-2] == 1339278426732672313
+
dadd_mult = DifferentialLadderMultiplier(dadd, dbl, scale)
with local(MultipleContext()) as ctx:
dadd_mult.init(curve25519, base)