aboutsummaryrefslogtreecommitdiffhomepage
path: root/pyecsca/codegen/templates/mult_precomp.c
diff options
context:
space:
mode:
authorJán Jančár2023-10-08 21:12:06 +0200
committerGitHub2023-10-08 21:12:06 +0200
commitffbaa1ae62095eb644eda67571aa8845aa6fb09d (patch)
treedfdfbf9a12acd1662cba56b46b30d8337ae81918 /pyecsca/codegen/templates/mult_precomp.c
parent9c6acdd2409c49c2ae64a8c41df315a1eca3eea7 (diff)
parent1c2e383d8e8df323b4cebb302869fc15599961a0 (diff)
downloadpyecsca-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/mult_precomp.c')
-rw-r--r--pyecsca/codegen/templates/mult_precomp.c85
1 files changed, 85 insertions, 0 deletions
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