aboutsummaryrefslogtreecommitdiffhomepage
path: root/pyecsca/codegen
diff options
context:
space:
mode:
authorJ08nY2025-10-01 14:29:24 +0200
committerJ08nY2025-10-01 14:29:24 +0200
commit8801d172995151526da7463f1e99ffd41d43f92c (patch)
tree6c4d2aecdf891459ffe7868f274ac6df9dbce5f5 /pyecsca/codegen
parentf0811f0c99f4f5a0b9b3cfe8b463b4934879d505 (diff)
downloadpyecsca-codegen-8801d172995151526da7463f1e99ffd41d43f92c.tar.gz
pyecsca-codegen-8801d172995151526da7463f1e99ffd41d43f92c.tar.zst
pyecsca-codegen-8801d172995151526da7463f1e99ffd41d43f92c.zip
Handle always in Binary NAF mult.
Diffstat (limited to 'pyecsca/codegen')
-rw-r--r--pyecsca/codegen/templates/mult_bnaf.c60
1 files changed, 50 insertions, 10 deletions
diff --git a/pyecsca/codegen/templates/mult_bnaf.c b/pyecsca/codegen/templates/mult_bnaf.c
index 68d1569..d0cafdf 100644
--- a/pyecsca/codegen/templates/mult_bnaf.c
+++ b/pyecsca/codegen/templates/mult_bnaf.c
@@ -2,35 +2,73 @@
#include "point.h"
point_t *scalar_mult_ltr(point_t *point, point_t *neg, curve_t *curve, wnaf_t *naf) {
+ {% if scalarmult.always %}
+ point_t *q_copy = point_new();
+ point_t *dummy = point_new();
+ {% endif %}
+
point_t *q = point_copy(curve->neutral);
for (long i = naf->length - 1; i >= 0; i--) {
point_dbl(q, curve, q);
+ {% if scalarmult.always %}
+ point_set(q, q_copy);
+ {% endif %}
+
if (naf->data[i] == 1) {
point_accumulate(q, point, curve, q);
+ {% if scalarmult.always %}
+ point_accumulate(q_copy, neg, curve, dummy);
+ {% endif %}
} else if (naf->data[i] == -1) {
point_accumulate(q, neg, curve, q);
+ {% if scalarmult.always %}
+ point_accumulate(q_copy, point, curve, dummy);
+ {% endif %}
}
}
+ {% if scalarmult.always %}
+ point_free(q_copy);
+ point_free(dummy);
+ {% endif %}
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();
+ {% if scalarmult.always %}
+ point_t *r_copy = point_new();
+ point_t *dummy = point_new();
+ {% endif %}
+
+ point_t *q = point_copy(point);
+ point_t *r = point_copy(curve->neutral);
+ point_t *q_neg = point_new();
for (long i = 0; i < naf->length; i++) {
+ {% if scalarmult.always %}
+ point_set(r, r_copy);
+ {% endif %}
if (naf->data[i] == 1) {
- point_accumulate(q, r, curve, q);
+ point_accumulate(r, q, curve, r);
+ {% if scalarmult.always %}
+ point_neg(q, curve, q_neg);
+ point_accumulate(r_copy, q_neg, curve, dummy);
+ {% endif %}
} else if (naf->data[i] == -1) {
- point_neg(r, curve, r_neg);
- point_accumulate(q, r_neg, curve, q);
+ point_neg(q, curve, q_neg);
+ point_accumulate(r, q_neg, curve, r);
+ {% if scalarmult.always %}
+ point_accumulate(r_copy, q, curve, dummy);
+ {% endif %}
}
- point_dbl(r, curve, r);
+ point_dbl(q, curve, q);
}
- point_free(r_neg);
- point_free(r);
+ point_free(q_neg);
+ point_free(q);
- return q;
+ {% if scalarmult.always %}
+ point_free(r_copy);
+ point_free(dummy);
+ {% endif %}
+ return r;
}
static void scalar_mult_inner(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) {
@@ -38,6 +76,8 @@ static void scalar_mult_inner(bn_t *scalar, point_t *point, curve_t *curve, poin
point_neg(point, curve, neg);
wnaf_t *naf = bn_bnaf(scalar);
+ {# TODO: Handle the ".complete" option #}
+
{% if scalarmult.direction == ProcessingDirection.LTR %}
point_t *q = scalar_mult_ltr(point, neg, curve, naf);
{% elif scalarmult.direction == ProcessingDirection.RTL %}