diff options
| author | J08nY | 2023-10-03 23:29:18 +0200 |
|---|---|---|
| committer | J08nY | 2023-10-03 23:29:18 +0200 |
| commit | 07026a90ba04e164fadeb40dc2ba80e7743abf00 (patch) | |
| tree | b7011593d35684a2bf3191f44b068e5ac8574f3d /pyecsca/codegen/templates | |
| parent | 7889941ce0c198113509738c1f0e84bb7826080f (diff) | |
| download | pyecsca-codegen-07026a90ba04e164fadeb40dc2ba80e7743abf00.tar.gz pyecsca-codegen-07026a90ba04e164fadeb40dc2ba80e7743abf00.tar.zst pyecsca-codegen-07026a90ba04e164fadeb40dc2ba80e7743abf00.zip | |
Add fixed width scalarmult.
Diffstat (limited to 'pyecsca/codegen/templates')
| -rw-r--r-- | pyecsca/codegen/templates/mult_fixed_w.c | 63 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/point.c | 4 |
2 files changed, 65 insertions, 2 deletions
diff --git a/pyecsca/codegen/templates/mult_fixed_w.c b/pyecsca/codegen/templates/mult_fixed_w.c new file mode 100644 index 0000000..aa95ebd --- /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); + + base_t *bs = bn_convert_base(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 %} + + uint8_t 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/point.c b/pyecsca/codegen/templates/point.c index b4d6e94..bad232a 100644 --- a/pyecsca/codegen/templates/point.c +++ b/pyecsca/codegen/templates/point.c @@ -139,9 +139,9 @@ void point_from_affine(bn_t *x, bn_t *y, const curve_t *curve, point_t *out) { } void point_accumulate(const point_t *one, const point_t *other, const curve_t *curve, point_t *out_one) { - {% if accumulation_order == "PeqPR" %} + {% if accumulation_order == AccumulationOrder.PeqPR %} point_add(one, other, curve, out_one); - {% elif accumulation_order == "PeqRP" %} + {% elif accumulation_order == AccumulationOrder.PeqRP %} point_add(other, one, curve, out_one); {% endif %} }
\ No newline at end of file |
