diff options
| author | Ján Jančár | 2023-10-08 21:12:06 +0200 |
|---|---|---|
| committer | GitHub | 2023-10-08 21:12:06 +0200 |
| commit | ffbaa1ae62095eb644eda67571aa8845aa6fb09d (patch) | |
| tree | dfdfbf9a12acd1662cba56b46b30d8337ae81918 /pyecsca/codegen/templates | |
| parent | 9c6acdd2409c49c2ae64a8c41df315a1eca3eea7 (diff) | |
| parent | 1c2e383d8e8df323b4cebb302869fc15599961a0 (diff) | |
| download | pyecsca-codegen-ffbaa1ae62095eb644eda67571aa8845aa6fb09d.tar.gz pyecsca-codegen-ffbaa1ae62095eb644eda67571aa8845aa6fb09d.tar.zst pyecsca-codegen-ffbaa1ae62095eb644eda67571aa8845aa6fb09d.zip | |
Merge pull request #4 from J08nY/feat/more-mults
More scalar multipliers
Diffstat (limited to 'pyecsca/codegen/templates')
| -rw-r--r-- | pyecsca/codegen/templates/Makefile | 4 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/formula_add.c | 4 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/formula_dadd.c | 4 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/formula_dbl.c | 4 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/formula_ladd.c | 4 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/formula_neg.c | 4 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/formula_scl.c | 4 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/formula_tpl.c | 4 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/main.c | 20 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/mult.c | 24 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/mult_bgmw.c | 64 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/mult_bnaf.c | 48 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/mult_comb.c | 77 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/mult_fixed_w.c | 63 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/mult_ltr.c | 4 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/mult_precomp.c | 85 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/mult_rtl.c | 4 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/mult_sliding_w.c | 43 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/mult_wnaf.c | 60 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/point.c | 8 |
20 files changed, 494 insertions, 38 deletions
diff --git a/pyecsca/codegen/templates/Makefile b/pyecsca/codegen/templates/Makefile index 79e5c20..1b6922c 100644 --- a/pyecsca/codegen/templates/Makefile +++ b/pyecsca/codegen/templates/Makefile @@ -6,6 +6,10 @@ PLATFORM = {{ platform }} CDEFS += -DHASH={{ hash_type }} -DMOD_RAND={{ mod_rand }} -DREDUCTION={{ reduction }} -DMUL={{ mul }} -DSQR={{ sqr }} +{%- if defines %} +CDEFS += {%- for def, value in defines.items() -%}-D{{def}}={{value}} {%- endfor -%} +{%- endif %} + MKDIR_LIST += hash prng asn1 bn gen EXTRAINCDIRS += hash prng asn1 bn gen tommath diff --git a/pyecsca/codegen/templates/formula_add.c b/pyecsca/codegen/templates/formula_add.c index 3d7e394..39d12b6 100644 --- a/pyecsca/codegen/templates/formula_add.c +++ b/pyecsca/codegen/templates/formula_add.c @@ -10,7 +10,7 @@ void point_add(const point_t *one, const point_t *other, const curve_t *curve, point_t *out_one) { {{ start_action("add") }} - NOP_128(); + //NOP_128(); {%- if short_circuit %} if (point_equals(one, curve->neutral)) { point_set(other, out_one); @@ -24,6 +24,6 @@ void point_add(const point_t *one, const point_t *other, const curve_t *curve, p {{ ops.render_initializations(initializations) }} {{ ops.render_ops(operations) }} {{ ops.render_returns(returns) }} - NOP_128(); + //NOP_128(); {{ end_action("add") }} }
\ No newline at end of file diff --git a/pyecsca/codegen/templates/formula_dadd.c b/pyecsca/codegen/templates/formula_dadd.c index 0605b23..855cf5d 100644 --- a/pyecsca/codegen/templates/formula_dadd.c +++ b/pyecsca/codegen/templates/formula_dadd.c @@ -10,11 +10,11 @@ void point_dadd(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out_one) { {{ start_action("dadd") }} - NOP_128(); + //NOP_128(); // TODO: short-circuits {{ ops.render_initializations(initializations) }} {{ ops.render_ops(operations) }} {{ ops.render_returns(returns) }} - NOP_128(); + //NOP_128(); {{ end_action("dadd") }} } diff --git a/pyecsca/codegen/templates/formula_dbl.c b/pyecsca/codegen/templates/formula_dbl.c index b741572..3c10bd7 100644 --- a/pyecsca/codegen/templates/formula_dbl.c +++ b/pyecsca/codegen/templates/formula_dbl.c @@ -10,7 +10,7 @@ void point_dbl(const point_t *one, const curve_t *curve, point_t *out_one) { {{ start_action("dbl") }} - NOP_128(); + //NOP_128(); {%- if short_circuit %} if (point_equals(one, curve->neutral)) { point_set(one, out_one); @@ -20,6 +20,6 @@ void point_dbl(const point_t *one, const curve_t *curve, point_t *out_one) { {{ ops.render_initializations(initializations) }} {{ ops.render_ops(operations) }} {{ ops.render_returns(returns) }} - NOP_128(); + //NOP_128(); {{ end_action("dbl") }} }
\ No newline at end of file diff --git a/pyecsca/codegen/templates/formula_ladd.c b/pyecsca/codegen/templates/formula_ladd.c index 6155185..2eaa531 100644 --- a/pyecsca/codegen/templates/formula_ladd.c +++ b/pyecsca/codegen/templates/formula_ladd.c @@ -10,11 +10,11 @@ void point_ladd(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out_one, point_t *out_other) { {{ start_action("ladd") }} - NOP_128(); + //NOP_128(); // TODO: short-circuits {{ ops.render_initializations(initializations) }} {{ ops.render_ops(operations) }} {{ ops.render_returns(returns) }} - NOP_128(); + //NOP_128(); {{ end_action("ladd") }} }
\ No newline at end of file diff --git a/pyecsca/codegen/templates/formula_neg.c b/pyecsca/codegen/templates/formula_neg.c index f8d0e14..4eaf62d 100644 --- a/pyecsca/codegen/templates/formula_neg.c +++ b/pyecsca/codegen/templates/formula_neg.c @@ -10,7 +10,7 @@ void point_neg(const point_t *one, const curve_t *curve, point_t *out_one) { {{ start_action("neg") }} - NOP_128(); + //NOP_128(); {%- if short_circuit %} if (point_equals(one, curve->neutral)) { point_set(one, out_one); @@ -20,6 +20,6 @@ void point_neg(const point_t *one, const curve_t *curve, point_t *out_one) { {{ ops.render_initializations(initializations) }} {{ ops.render_ops(operations) }} {{ ops.render_returns(returns) }} - NOP_128(); + //NOP_128(); {{ end_action("neg") }} }
\ No newline at end of file diff --git a/pyecsca/codegen/templates/formula_scl.c b/pyecsca/codegen/templates/formula_scl.c index 8a840e9..dfcfcbe 100644 --- a/pyecsca/codegen/templates/formula_scl.c +++ b/pyecsca/codegen/templates/formula_scl.c @@ -10,7 +10,7 @@ void point_scl(const point_t *one, const curve_t *curve, point_t *out_one) { {{ start_action("scl") }} - NOP_128(); + //NOP_128(); {%- if short_circuit %} if (point_equals(one, curve->neutral)) { point_set(one, out_one); @@ -20,6 +20,6 @@ void point_scl(const point_t *one, const curve_t *curve, point_t *out_one) { {{ ops.render_initializations(initializations) }} {{ ops.render_ops(operations) }} {{ ops.render_returns(returns) }} - NOP_128(); + //NOP_128(); {{ end_action("scl") }} }
\ No newline at end of file diff --git a/pyecsca/codegen/templates/formula_tpl.c b/pyecsca/codegen/templates/formula_tpl.c index 08a6278..40cf114 100644 --- a/pyecsca/codegen/templates/formula_tpl.c +++ b/pyecsca/codegen/templates/formula_tpl.c @@ -10,7 +10,7 @@ void point_tpl(const point_t *one, const curve_t *curve, point_t *out_one) { {{ start_action("tpl") }} - NOP_128(); + //NOP_128(); {%- if short_circuit %} if (point_equals(one, curve->neutral)) { point_set(one, out_one); @@ -20,6 +20,6 @@ void point_tpl(const point_t *one, const curve_t *curve, point_t *out_one) { {{ ops.render_initializations(initializations) }} {{ ops.render_ops(operations) }} {{ ops.render_returns(returns) }} - NOP_128(); + //NOP_128(); {{ end_action("tpl") }} }
\ No newline at end of file diff --git a/pyecsca/codegen/templates/main.c b/pyecsca/codegen/templates/main.c index 4d50cc2..8aed65a 100644 --- a/pyecsca/codegen/templates/main.c +++ b/pyecsca/codegen/templates/main.c @@ -562,15 +562,6 @@ static uint8_t cmd_set_trigger(uint8_t *data, uint16_t len) { return 0; } -__attribute__((noinline)) void init(void) { - // Initalize the platform, UART, triggers. - platform_init(); - init_uart(); - trigger_setup(); - - init_implementation(); -} - __attribute__((noinline)) void init_implementation(void) { // Initialize some components that preallocate stuff. prng_init(); @@ -583,6 +574,15 @@ __attribute__((noinline)) void init_implementation(void) { bn_init(&privkey); } +__attribute__((noinline)) void init(void) { + // Initalize the platform, UART, triggers. + platform_init(); + init_uart(); + trigger_setup(); + + init_implementation(); +} + __attribute__((noinline)) void deinit(void) { // Clear up allocated stuff. bn_clear(&privkey); @@ -616,7 +616,7 @@ int main(void) { // Execute commands while SimpleSerial is alive. //led_ok(1); - while(simpleserial_get()); + while(simpleserial_get()) {} //led_ok(0); deinit(); diff --git a/pyecsca/codegen/templates/mult.c b/pyecsca/codegen/templates/mult.c index 0603bc0..22a385d 100644 --- a/pyecsca/codegen/templates/mult.c +++ b/pyecsca/codegen/templates/mult.c @@ -27,6 +27,30 @@ {% include "mult_bnaf.c" %} +{%- elif isinstance(scalarmult, WindowNAFMultiplier) -%} + + {% include "mult_wnaf.c" %} + +{%- elif isinstance(scalarmult, SlidingWindowMultiplier) -%} + + {% include "mult_sliding_w.c" %} + +{%- elif isinstance(scalarmult, FixedWindowLTRMultiplier) -%} + + {% include "mult_fixed_w.c" %} + +{%- elif isinstance(scalarmult, FullPrecompMultiplier) -%} + + {% include "mult_precomp.c" %} + +{%- elif isinstance(scalarmult, BGMWMultiplier) -%} + + {% include "mult_bgmw.c" %} + +{%- elif isinstance(scalarmult, CombMultiplier) -%} + + {% include "mult_comb.c" %} + {%- endif %} diff --git a/pyecsca/codegen/templates/mult_bgmw.c b/pyecsca/codegen/templates/mult_bgmw.c new file mode 100644 index 0000000..5298fb1 --- /dev/null +++ b/pyecsca/codegen/templates/mult_bgmw.c @@ -0,0 +1,64 @@ +#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 *a = point_copy(curve->neutral); + point_t *b = point_copy(curve->neutral); + + int order_blen = bn_bit_length(&curve->n); + int d = (order_blen + {{ scalarmult.width }} - 1) / {{ scalarmult.width }}; + + point_t **points = malloc(sizeof(point_t *) * d); + + point_t *current = point_copy(point); + for (int i = 0; i < d; i++) { + points[i] = point_copy(current); + if (i != d - 1) { + for (int j = 0; j < {{ scalarmult.width }}; j++) { + point_dbl(current, curve, current); + } + } + } + point_free(current); + + small_base_t *bs = bn_convert_base_small(scalar, {{ 2**scalarmult.width }}); + + for (int j = {{ 2**scalarmult.width }}; j > 0; j--) { + {%- if scalarmult.direction == ProcessingDirection.RTL %} + for (int i = 0; i < bs->length; i++) { + if (bs->data[i] == j) { + point_accumulate(b, points[i], curve, b); + } + } + {%- else %} + for (int i = bs->length - 1; i >= 0; i--) { + if (bs->data[i] == j) { + point_accumulate(b, points[i], curve, b); + } + } + {%- endif -%} + + {%- if scalarmult.short_circuit %} + if (point_equals(a, b)) { + point_dbl(b, curve, a); + continue; + } + {%- endif %} + point_accumulate(a, b, curve, a); + } + free(bs->data); + free(bs); + + {%- if "scl" in scalarmult.formulas %} + point_scl(a, curve, a); + {%- endif %} + point_set(a, out); + for (long i = 0; i < d; i++) { + point_free(points[i]); + } + free(points); + point_free(a); + point_free(b); +}
\ No newline at end of file diff --git a/pyecsca/codegen/templates/mult_bnaf.c b/pyecsca/codegen/templates/mult_bnaf.c index 33e7302..68d1569 100644 --- a/pyecsca/codegen/templates/mult_bnaf.c +++ b/pyecsca/codegen/templates/mult_bnaf.c @@ -1,22 +1,50 @@ #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 *neg = point_new(); - point_neg(point, curve, neg); - point_t *q = point_copy(curve->neutral); - - wnaf_t *naf = bn_bnaf(scalar); - +point_t *scalar_mult_ltr(point_t *point, point_t *neg, curve_t *curve, wnaf_t *naf) { + point_t *q = point_copy(curve->neutral); for (long i = naf->length - 1; i >= 0; i--) { point_dbl(q, curve, q); if (naf->data[i] == 1) { - point_add(q, point, curve, q); + point_accumulate(q, point, curve, q); } else if (naf->data[i] == -1) { - point_add(q, neg, curve, q); + point_accumulate(q, neg, curve, q); } } - free(naf->data); + return q; +} + +point_t* scalar_mult_rtl(point_t *point, point_t *neg, curve_t *curve, wnaf_t *naf) { + point_t *r = point_copy(point); + point_t *q = point_copy(curve->neutral); + point_t *r_neg = point_new(); + for (long i = 0; i < naf->length; i++) { + if (naf->data[i] == 1) { + point_accumulate(q, r, curve, q); + } else if (naf->data[i] == -1) { + point_neg(r, curve, r_neg); + point_accumulate(q, r_neg, curve, q); + } + point_dbl(r, curve, r); + } + point_free(r_neg); + point_free(r); + + return q; +} + +static void scalar_mult_inner(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) { + point_t *neg = point_new(); + point_neg(point, curve, neg); + wnaf_t *naf = bn_bnaf(scalar); + + {% if scalarmult.direction == ProcessingDirection.LTR %} + point_t *q = scalar_mult_ltr(point, neg, curve, naf); + {% elif scalarmult.direction == ProcessingDirection.RTL %} + point_t *q = scalar_mult_rtl(point, neg, curve, naf); + {% endif %} + + free(naf->data); free(naf); {%- if "scl" in scalarmult.formulas %} diff --git a/pyecsca/codegen/templates/mult_comb.c b/pyecsca/codegen/templates/mult_comb.c new file mode 100644 index 0000000..9df9796 --- /dev/null +++ b/pyecsca/codegen/templates/mult_comb.c @@ -0,0 +1,77 @@ +#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); + + int order_blen = bn_bit_length(&curve->n); + int d = (order_blen + {{ scalarmult.width }} - 1) / {{ scalarmult.width }}; + + point_t *base_points[{{ scalarmult.width }}]; + + point_t *current = point_copy(point); + for (int i = 0; i < {{ scalarmult.width }}; i++) { + base_points[i] = point_copy(current); + if (i != d - 1) { + for (int j = 0; j < d; j++) { + point_dbl(current, curve, current); + } + } + } + point_free(current); + + point_t *points[{{ 2**scalarmult.width }}]; + for (int j = 0; j < {{ 2**scalarmult.width }}; j++) { + point_t *alloc_point = NULL; + for (int i = 0; i < {{ scalarmult.width }}; i++) { + if (j & (1 << i)) { + if (alloc_point) { + point_accumulate(alloc_point, base_points[i], curve, alloc_point); + } else { + alloc_point = point_copy(base_points[i]); + } + } + } + points[j] = alloc_point; + } + + bn_t base; bn_init(&base); + bn_from_int(1, &base); + bn_lsh(&base, d, &base); + + large_base_t *bs = bn_convert_base_large(scalar, &base); + for (int i = d - 1; i >= 0; i--) { + point_dbl(q, curve, q); + int word = 0; + for (int j = 0; j < {{ scalarmult.width }}; j++) { + if (j < bs->length) { + word |= bn_get_bit(&bs->data[j], i) << j; + } + } + if (word) { + point_accumulate(q, points[word], curve, q); + } + } + for (int i = 0; i < bs->length; i++) { + bn_clear(&bs->data[i]); + } + free(bs->data); + bn_clear(&bs->m); + free(bs); + bn_clear(&base); + + + {%- if "scl" in scalarmult.formulas %} + point_scl(a, curve, a); + {%- endif %} + point_set(q, out); + for (int i = 0; i < {{ scalarmult.width }}; i++) { + point_free(base_points[i]); + } + for (int i = 0; i < {{ 2**scalarmult.width }}; i++) { + if (points[i]) { + point_free(points[i]); + } + } + point_free(q); +}
\ No newline at end of file diff --git a/pyecsca/codegen/templates/mult_fixed_w.c b/pyecsca/codegen/templates/mult_fixed_w.c new file mode 100644 index 0000000..b0a4bb0 --- /dev/null +++ b/pyecsca/codegen/templates/mult_fixed_w.c @@ -0,0 +1,63 @@ +#include "mult.h" +#include "point.h" + +void scalar_mult_by_m_pow2(point_t *point, curve_t *curve) { + unsigned int m = {{ scalarmult.m }} >> 1; + while (m) { + point_dbl(point, curve, point); + m >>= 1; + } +} + +void scalar_mult_by_m_base(point_t *point, curve_t *curve) { + point_t *orig = point_copy(point); + point_dbl(orig, curve, point); + for (int i = 0; i < {{ scalarmult.m - 2}}; i++) { + point_add(point, orig, curve, point); + } + point_free(orig); +} + +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[{{ scalarmult.m }}]; + + point_t *current = point_copy(point); + point_t *dbl = point_new(); + point_dbl(current, curve, dbl); + points[0] = point_copy(current); + points[1] = point_copy(dbl); + point_set(dbl, current); + for (long i = 2; i < {{ scalarmult.m }}; i++) { + point_add(current, point, curve, current); + points[i] = point_copy(current); + } + point_free(current); + point_free(dbl); + + small_base_t *bs = bn_convert_base_small(scalar, {{ scalarmult.m }}); + + for (long i = bs->length - 1; i >= 0; i--) { + {%- if bin(scalarmult.m).count("1") == 1 %} + scalar_mult_by_m_pow2(q, curve); + {%- else %} + scalar_mult_by_m_base(q, curve); + {%- endif %} + + int val = bs->data[i]; + if (val) { + point_accumulate(q, points[val-1], curve, q); + } + } + free(bs->data); + free(bs); + + {%- if "scl" in scalarmult.formulas %} + point_scl(q, curve, q); + {%- endif %} + point_set(q, out); + for (long i = 0; i < {{ scalarmult.m }}; i++) { + point_free(points[i]); + } + point_free(q); +}
\ No newline at end of file diff --git a/pyecsca/codegen/templates/mult_ltr.c b/pyecsca/codegen/templates/mult_ltr.c index f8bee19..d4aaf10 100644 --- a/pyecsca/codegen/templates/mult_ltr.c +++ b/pyecsca/codegen/templates/mult_ltr.c @@ -19,10 +19,10 @@ void scalar_mult_inner(bn_t *scalar, point_t *point, curve_t *curve, point_t *ou for (int i = nbits; i >= 0; i--) { point_dbl(r, curve, r); if (bn_get_bit(scalar, i) == 1) { - point_add(r, q, curve, r); + point_accumulate(r, q, curve, r); } else { {%- if scalarmult.always %} - point_add(r, q, curve, dummy); + point_accumulate(r, q, curve, dummy); {%- endif %} } } diff --git a/pyecsca/codegen/templates/mult_precomp.c b/pyecsca/codegen/templates/mult_precomp.c new file mode 100644 index 0000000..cafa0a9 --- /dev/null +++ b/pyecsca/codegen/templates/mult_precomp.c @@ -0,0 +1,85 @@ +#include "mult.h" +#include "point.h" + +void scalar_mult_ltr(int order_blen, point_t **points, bn_t *scalar, point_t *point, curve_t *curve) { + {%- if scalarmult.complete %} + int nbits = order_blen - 1; + {%- else %} + int nbits = bn_bit_length(scalar) - 1; + {%- endif %} + + {%- if scalarmult.always %} + point_t *dummy = point_new(); + {%- endif %} + + for (int i = nbits; i >= 0; i--) { + if (bn_get_bit(scalar, i) == 1) { + point_accumulate(point, points[i], curve, point); + } else { + {%- if scalarmult.always %} + point_accumulate(point, points[i], curve, dummy); + {%- endif %} + } + } + + {%- if scalarmult.always %} + point_free(dummy); + {%- endif %} +} + +void scalar_mult_rtl(int order_blen, point_t **points, bn_t *scalar, point_t *point, curve_t *curve) { + {%- if scalarmult.complete %} + int nbits = order_blen; + {%- else %} + int nbits = bn_bit_length(scalar); + {%- endif %} + + {%- if scalarmult.always %} + point_t *dummy = point_new(); + {%- endif %} + + for (int i = 0; i < nbits; i++) { + if (bn_get_bit(scalar, i) == 1) { + point_accumulate(point, points[i], curve, point); + } else { + {%- if scalarmult.always %} + point_accumulate(point, points[i], curve, dummy); + {%- endif %} + } + } + + {%- if scalarmult.always %} + point_free(dummy); + {%- endif %} +} + +static void scalar_mult_inner(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) { + point_t *q = point_copy(curve->neutral); + int order_blen = bn_bit_length(&curve->n); + point_t **points = malloc(sizeof(point_t *) * (order_blen + 1)); + + point_t *current = point_copy(point); + for (int i = 0; i < order_blen + 1; i++) { + points[i] = point_copy(current); + if (i != order_blen) { + point_dbl(current, curve, current); + } + } + point_free(current); + + {%- if scalarmult.direction == ProcessingDirection.LTR %} + scalar_mult_ltr(order_blen, points, scalar, q, curve); + {%- else %} + scalar_mult_rtl(order_blen, points, scalar, q, curve); + {%- endif %} + + {%- if "scl" in scalarmult.formulas %} + point_scl(q, curve, q); + {%- endif %} + point_set(q, out); + for (int i = 0; i < order_blen + 1; i++) { + point_free(points[i]); + } + free(points); + point_free(q); +}
\ No newline at end of file diff --git a/pyecsca/codegen/templates/mult_rtl.c b/pyecsca/codegen/templates/mult_rtl.c index 9db12fb..71949b4 100644 --- a/pyecsca/codegen/templates/mult_rtl.c +++ b/pyecsca/codegen/templates/mult_rtl.c @@ -14,10 +14,10 @@ void scalar_mult_inner(bn_t *scalar, point_t *point, curve_t *curve, point_t *ou while (!bn_is_0(©)) { if (bn_get_bit(©, 0) == 1) { - point_add(r, q, curve, r); + point_accumulate(r, q, curve, r); } else { {%- if scalarmult.always %} - point_add(r, q, curve, dummy); + point_accumulate(r, q, curve, dummy); {%- endif %} } point_dbl(q, curve, q); diff --git a/pyecsca/codegen/templates/mult_sliding_w.c b/pyecsca/codegen/templates/mult_sliding_w.c new file mode 100644 index 0000000..1e80a84 --- /dev/null +++ b/pyecsca/codegen/templates/mult_sliding_w.c @@ -0,0 +1,43 @@ +#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); + } + } + + {%- if "scl" in scalarmult.formulas %} + point_scl(q, curve, q); + {%- endif %} + point_set(q, out); + + free(ws->data); + free(ws); + 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/pyecsca/codegen/templates/mult_wnaf.c b/pyecsca/codegen/templates/mult_wnaf.c new file mode 100644 index 0000000..3c5f2b2 --- /dev/null +++ b/pyecsca/codegen/templates/mult_wnaf.c @@ -0,0 +1,60 @@ +#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 - 2) }}]; + {%- if scalarmult.precompute_negation %} + point_t *points_neg[{{ 2 ** (scalarmult.width - 2) }}]; + {%- else %} + point_t *neg = point_new(); + {%- endif %} + + point_t *current = point_copy(point); + point_t *dbl = point_new(); + point_dbl(current, curve, dbl); + for (long i = 0; i < {{ 2 ** (scalarmult.width - 2) }}; i++) { + points[i] = point_copy(current); + {%- if scalarmult.precompute_negation %} + points_neg[i] = point_copy(current); + point_neg(points_neg[i], curve, points_neg[i]); + {%- endif %} + point_add(current, dbl, curve, current); + } + point_free(current); + point_free(dbl); + + wnaf_t *naf = bn_wnaf(scalar, {{ scalarmult.width }}); + + for (long i = naf->length - 1; i >= 0; i--) { + point_dbl(q, curve, q); + int8_t val = naf->data[i]; + if (val > 0) { + point_accumulate(q, points[(val - 1) / 2], curve, q); + } else if (val < 0) { + {%- if scalarmult.precompute_negation %} + point_accumulate(q, points_neg[(-val - 1) / 2], curve, q); + {%- else %} + point_neg(points[(-val - 1) / 2], curve, neg); + point_accumulate(q, neg, curve, q); + {%- endif %} + } + } + free(naf->data); + free(naf); + + {%- if "scl" in scalarmult.formulas %} + point_scl(q, curve, q); + {%- endif %} + point_set(q, out); + for (long i = 0; i < {{ 2 ** (scalarmult.width - 2) }}; i++) { + point_free(points[i]); + {%- if scalarmult.precompute_negation %} + point_free(points_neg[i]); + {%- endif %} + } + {%- if not scalarmult.precompute_negation %} + point_free(neg); + {%- endif %} + point_free(q); +}
\ No newline at end of file diff --git a/pyecsca/codegen/templates/point.c b/pyecsca/codegen/templates/point.c index e87ae36..bad232a 100644 --- a/pyecsca/codegen/templates/point.c +++ b/pyecsca/codegen/templates/point.c @@ -137,3 +137,11 @@ void point_from_affine(bn_t *x, bn_t *y, const curve_t *curve, point_t *out) { {%- endfor %} {{ end_action("coord_map") }} } + +void point_accumulate(const point_t *one, const point_t *other, const curve_t *curve, point_t *out_one) { + {% if accumulation_order == AccumulationOrder.PeqPR %} + point_add(one, other, curve, out_one); + {% elif accumulation_order == AccumulationOrder.PeqRP %} + point_add(other, one, curve, out_one); + {% endif %} +}
\ No newline at end of file |
