diff options
| author | J08nY | 2023-10-01 22:55:17 +0200 |
|---|---|---|
| committer | J08nY | 2023-10-01 22:55:17 +0200 |
| commit | 7889941ce0c198113509738c1f0e84bb7826080f (patch) | |
| tree | 692a96904713e56810ebdafa1906faeeb6260f59 | |
| parent | 9e01e2c7d9dfdadc653c05e80ed0f6aa235597ff (diff) | |
| download | pyecsca-codegen-7889941ce0c198113509738c1f0e84bb7826080f.tar.gz pyecsca-codegen-7889941ce0c198113509738c1f0e84bb7826080f.tar.zst pyecsca-codegen-7889941ce0c198113509738c1f0e84bb7826080f.zip | |
Add Sliding window multiplier.
| -rw-r--r-- | pyecsca/codegen/bn/bn.c | 1 | ||||
| -rw-r--r-- | pyecsca/codegen/common.py | 18 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/mult_sliding_w.c | 42 | ||||
| -rw-r--r-- | test/test_impl.py | 21 |
4 files changed, 77 insertions, 5 deletions
diff --git a/pyecsca/codegen/bn/bn.c b/pyecsca/codegen/bn/bn.c index 77df5ab..e0c4bcb 100644 --- a/pyecsca/codegen/bn/bn.c +++ b/pyecsca/codegen/bn/bn.c @@ -543,7 +543,6 @@ wsliding_t *bn_wsliding_rtl(const bn_t *bn, int w) { goto exit_mask; } - int u = 0; int i = 0; while (!bn_is_0(&k) && !(bn_get_sign(&k) == BN_NEG)) { if (!bn_get_bit(&k, 0)) { diff --git a/pyecsca/codegen/common.py b/pyecsca/codegen/common.py index 56ca261..2f3dfbd 100644 --- a/pyecsca/codegen/common.py +++ b/pyecsca/codegen/common.py @@ -7,9 +7,17 @@ from pyecsca.ec.configuration import EnumDefine, Configuration from pyecsca.ec.coordinates import CoordinateModel from pyecsca.ec.model import (CurveModel, ShortWeierstrassModel, MontgomeryModel, EdwardsModel, TwistedEdwardsModel) -from pyecsca.ec.mult import (LTRMultiplier, RTLMultiplier, CoronMultiplier, - LadderMultiplier, SimpleLadderMultiplier, DifferentialLadderMultiplier, - WindowNAFMultiplier, BinaryNAFMultiplier) +from pyecsca.ec.mult import ( + LTRMultiplier, + RTLMultiplier, + CoronMultiplier, + LadderMultiplier, + SimpleLadderMultiplier, + DifferentialLadderMultiplier, + WindowNAFMultiplier, + BinaryNAFMultiplier, + SlidingWindowMultiplier, +) @public @@ -65,6 +73,10 @@ MULTIPLIERS = [ { "name": ("wnaf", "WindowNAFMultiplier"), "class": WindowNAFMultiplier + }, + { + "name": ("sliding", "SlidingWindowMultiplier"), + "class": SlidingWindowMultiplier } ] diff --git a/pyecsca/codegen/templates/mult_sliding_w.c b/pyecsca/codegen/templates/mult_sliding_w.c new file mode 100644 index 0000000..87135cc --- /dev/null +++ b/pyecsca/codegen/templates/mult_sliding_w.c @@ -0,0 +1,42 @@ +#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 *q = point_copy(curve->neutral); + point_t *points[{{ 2 ** (scalarmult.width - 1) }}]; + + point_t *current = point_copy(point); + point_t *dbl = point_new(); + point_dbl(current, curve, dbl); + for (long i = 0; i < {{ 2 ** (scalarmult.width - 1) }}; i++) { + points[i] = point_copy(current); + point_add(current, dbl, curve, current); + } + point_free(current); + point_free(dbl); + + {% if scalarmult.recoding_direction == ProcessingDirection.LTR %} + wsliding_t *ws = bn_wsliding_ltr(scalar, {{ scalarmult.width }}); + {% elif scalarmult.recoding_direction == ProcessingDirection.RTL %} + wsliding_t *ws = bn_wsliding_rtl(scalar, {{ scalarmult.width }}); + {% endif %} + + for (long i = 0; i < ws->length; i++) { + point_dbl(q, curve, q); + uint8_t val = ws->data[i]; + if (val) { + point_accumulate(q, points[(val - 1) / 2], curve, q); + } + } + free(ws->data); + free(ws); + + {%- 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]); + } + point_free(q); +}
\ No newline at end of file diff --git a/test/test_impl.py b/test/test_impl.py index 1082a28..147792e 100644 --- a/test/test_impl.py +++ b/test/test_impl.py @@ -11,6 +11,7 @@ from pyecsca.ec.mult import ( CoronMultiplier, BinaryNAFMultiplier, WindowNAFMultiplier, + SlidingWindowMultiplier, AccumulationOrder, ProcessingDirection, ScalarMultiplier, @@ -129,6 +130,24 @@ def additional(request): ), id="WNAF2", ), + pytest.param( + ( + SlidingWindowMultiplier, + "sliding", + ["add-1998-cmo", "dbl-1998-cmo"], + {"width": 3}, + ), + id="SLI1", + ), + pytest.param( + ( + SlidingWindowMultiplier, + "sliding", + ["add-1998-cmo", "dbl-1998-cmo"], + {"width": 3, "recoding_direction": ProcessingDirection.RTL}, + ), + id="SLI2", + ), ], ) def target(request, additional, secp128r1) -> ImplTarget: @@ -254,4 +273,4 @@ def test_ecdsa(target, mult, secp128r1): expected = ecdsa.sign_data(message).to_DER() assert target.ecdsa_verify(message, expected) - target.disconnect()
\ No newline at end of file + target.disconnect() |
