diff options
| author | J08nY | 2025-10-02 16:53:38 +0200 |
|---|---|---|
| committer | J08nY | 2025-10-02 16:53:38 +0200 |
| commit | 0e5f73a716900bdefaff6156a3b7942a11467bd2 (patch) | |
| tree | 1a9f5c38ba544b67c0328e69981e20faa167d501 | |
| parent | e88c89979a76873fd56c45230fa70596607b7474 (diff) | |
| download | pyecsca-codegen-0e5f73a716900bdefaff6156a3b7942a11467bd2.tar.gz pyecsca-codegen-0e5f73a716900bdefaff6156a3b7942a11467bd2.tar.zst pyecsca-codegen-0e5f73a716900bdefaff6156a3b7942a11467bd2.zip | |
Implement Booth multiplier.
| -rw-r--r-- | pyecsca/codegen/templates/mult_booth.c | 78 | ||||
| -rw-r--r-- | test/test_equivalence.py | 2 | ||||
| -rw-r--r-- | test/test_impl.py | 2 |
3 files changed, 78 insertions, 4 deletions
diff --git a/pyecsca/codegen/templates/mult_booth.c b/pyecsca/codegen/templates/mult_booth.c index e69de29..6dd19c7 100644 --- a/pyecsca/codegen/templates/mult_booth.c +++ b/pyecsca/codegen/templates/mult_booth.c @@ -0,0 +1,78 @@ +#include "mult.h" +#include "point.h" + + + +static void scalar_mult_inner(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) { + point_t *points[{{ 2 ** (scalarmult.width - 1) }}]; + {% if scalarmult.precompute_negation %} + point_t *points_neg[{{ 2 ** (scalarmult.width - 1) }}]; + {% endif %} + + point_t *current = point_copy(point); + point_t *dbl = point_new(); + point_dbl(current, curve, dbl); + points[0] = point_copy(current); + {% if scalarmult.precompute_negation %} + points_neg[0] = point_new(); + point_neg(points[0], curve, points_neg[0]); + {% endif %} + {% if scalarmult.width > 0 %} + points[1] = point_copy(dbl); + {% if scalarmult.precompute_negation %} + points_neg[1] = point_new(); + point_neg(points[1], curve, points_neg[1]); + {% endif %} + {% endif %} + + point_set(dbl, current); + {% if scalarmult.width > 1 %} + for (long i = 2; i < {{ 2 ** (scalarmult.width - 1) }}; i++) { + point_add(current, point, curve, current); + points[i] = point_copy(current); + {% if scalarmult.precompute_negation %} + points_neg[i] = point_new(); + point_neg(points[i], curve, points_neg[i]); + {% endif %} + } + {% endif %} + point_free(current); + point_free(dbl); + + size_t bits = bn_bit_length(&curve->n); + + booth_t *bs = bn_booth(scalar, {{ scalarmult.width }}, bits); + + point_t *q = point_copy(curve->neutral); + point_t *neg = point_new(); + for (long i = 0; i < bs->length; i++) { + for (long j = 0; j < {{ scalarmult.width }}; j++) { + point_dbl(q, curve, q); + } + int32_t val = bs->data[i]; + if (val > 0) { + point_accumulate(q, points[val - 1], curve, q); + } else if (val < 0) { + {% if scalarmult.precompute_negation %} + point_accumulate(q, points_neg[-val - 1], curve, q); + {% else %} + point_neg(points[-val - 1], curve, neg); + point_accumulate(q, neg, curve, q); + {% endif %} + } + } + bn_booth_clear(bs); + point_free(neg); + + {%- if "scl" in scalarmult.formulas %} + point_scl(q, curve, q); + {%- endif %} + point_set(q, out); + for (long i = 0; i < {{ 2 ** (scalarmult.width - 1) }}; i++) { + point_free(points[i]); + {% if scalarmult.precompute_negation %} + point_free(points_neg[i]); + {% endif %} + } + point_free(q); +}
\ No newline at end of file diff --git a/test/test_equivalence.py b/test/test_equivalence.py index fcd5902..6973ceb 100644 --- a/test/test_equivalence.py +++ b/test/test_equivalence.py @@ -57,8 +57,6 @@ class GDBTarget(ImplTarget, BinaryTarget): @pytest.fixture(scope="module") def target(simple_multiplier, secp128r1) -> Generator[GDBTarget, Any, None]: mult_class, mult_kwargs = simple_multiplier - if mult_class == WindowBoothMultiplier: - pytest.skip("WindowBoothMultiplier not implemented yet") mult_name = mult_class.__name__ formulas = ["add-1998-cmo", "dbl-1998-cmo"] if NegationFormula in mult_class.requires: diff --git a/test/test_impl.py b/test/test_impl.py index 0b3abeb..a61aabd 100644 --- a/test/test_impl.py +++ b/test/test_impl.py @@ -33,8 +33,6 @@ def target( simple_multiplier, additional, secp128r1 ) -> Generator[HostTarget, Any, None]: mult_class, mult_kwargs = simple_multiplier - if mult_class == WindowBoothMultiplier: - pytest.skip("WindowBoothMultiplier not implemented yet") mult_name = mult_class.__name__ formulas = ["add-1998-cmo", "dbl-1998-cmo"] if NegationFormula in mult_class.requires: |
