aboutsummaryrefslogtreecommitdiffhomepage
path: root/pyecsca/codegen/templates/mult_precomp.c
blob: cafa0a9a01c02e8c14b659bb17854ece9d0883df (plain) (blame)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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);
}