aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJ08nY2023-10-02 18:23:26 +0200
committerJ08nY2023-10-02 18:23:26 +0200
commitb0836fed16322546b4e5f7150fec0f83225b6915 (patch)
tree5deeb7b769cfde1fe0c4515e8a7c87c53cbed943
parent1f243b414e94a1014111808bba9da9d4b5c98bf8 (diff)
downloadpyecsca-b0836fed16322546b4e5f7150fec0f83225b6915.tar.gz
pyecsca-b0836fed16322546b4e5f7150fec0f83225b6915.tar.zst
pyecsca-b0836fed16322546b4e5f7150fec0f83225b6915.zip
Allow to compute non-affine factor set.
-rw-r--r--pyecsca/sca/re/zvp.py13
-rw-r--r--test/sca/test_zvp.py10
2 files changed, 15 insertions, 8 deletions
diff --git a/pyecsca/sca/re/zvp.py b/pyecsca/sca/re/zvp.py
index 0f04fe5..53a38a2 100644
--- a/pyecsca/sca/re/zvp.py
+++ b/pyecsca/sca/re/zvp.py
@@ -164,16 +164,20 @@ def is_homogeneous(polynomial: Poly, weighted_variables: List[Tuple[str, int]])
@public
-def compute_factor_set(formula: Formula) -> Set[Poly]:
+def compute_factor_set(formula: Formula, affine: bool = True) -> Set[Poly]:
"""
Compute a set of factors present in the :paramref:`~.compute_factor_set.formula`.
:param formula: Formula to compute the factor set of.
+ :param affine: Whether to transform the polynomials into affine form.
:return: The set of factors present in the formula.
"""
unrolled = unroll_formula(formula)
unrolled = filter_out_nonhomogenous_polynomials(formula, unrolled)
- unrolled = map_to_affine(formula, unrolled)
+ if affine:
+ unrolled = map_to_affine(formula, unrolled)
+
+ curve_params = set(formula.coordinate_model.curve_model.parameter_names)
factors = set()
# Go over all the unrolled intermediates
@@ -184,9 +188,8 @@ def compute_factor_set(formula: Formula) -> Set[Poly]:
for factor, power in factor_list:
# Remove unnecessary variables from the Poly
reduced = factor.exclude()
- # If there are only one-letter variables remaining, those are only curve parameters
- # so we do not care about the polynomial
- if all(len(str(gen)) == 1 for gen in reduced.gens): # type: ignore[attr-defined]
+ # If there are only curve parameters, we do not care about the polynomial
+ if set(reduced.gens).issubset(curve_params): # type: ignore[attr-defined]
continue
# Divide out the GCD of the coefficients from the poly
_, reduced = reduced.primitive()
diff --git a/test/sca/test_zvp.py b/test/sca/test_zvp.py
index 32132fd..ceb8941 100644
--- a/test/sca/test_zvp.py
+++ b/test/sca/test_zvp.py
@@ -39,7 +39,7 @@ def test_map_to_affine(formula):
def test_factor_set(formula):
- factor_set = compute_factor_set(formula)
+ factor_set = compute_factor_set(formula, affine=True)
assert factor_set is not None
assert isinstance(factor_set, set)
expr_set = set(map(lambda poly: poly.as_expr(), factor_set))
@@ -154,15 +154,19 @@ def test_zvp(secp128r1, formula):
unrolled = unroll_formula(formula)
unrolled = map_to_affine(formula, unrolled)
# Try all intermediates, zvp_point should return empty set if ZVP points do not exist
+ # Try with a few scalars to actually test more resulting ZVP points
for name, poly in unrolled:
- points = zvp_points(poly, secp128r1.curve, 5, secp128r1.order)
+ for k in (2, 3, 5):
+ points = zvp_points(poly, secp128r1.curve, k, secp128r1.order)
+ if points:
+ break
assert isinstance(points, set)
# If points are produced, try them all.
for point in points:
inputs = [point.to_model(formula.coordinate_model, secp128r1.curve)]
if formula.num_inputs > 1:
- second_point = secp128r1.curve.affine_multiply(point, 5)
+ second_point = secp128r1.curve.affine_multiply(point, k)
inputs.append(
second_point.to_model(formula.coordinate_model, secp128r1.curve)
)