1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
#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 - 1 }}];
point_t *current = point_copy(point);
point_t *dbl = point_new();
point_dbl(current, curve, dbl);
points[0] = point_copy(current);
{% if scalarmult.m > 2 %}
points[1] = point_copy(dbl);
{% endif %}
point_set(dbl, current);
{% if scalarmult.m > 3 %}
for (long i = 2; i < {{ scalarmult.m - 1 }}; i++) {
point_add(current, point, curve, current);
points[i] = point_copy(current);
}
{% endif %}
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);
}
}
bn_small_base_clear(bs);
{%- if "scl" in scalarmult.formulas %}
point_scl(q, curve, q);
{%- endif %}
point_set(q, out);
for (long i = 0; i < {{ scalarmult.m - 1 }}; i++) {
point_free(points[i]);
}
point_free(q);
}
|