-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjadd_generic_inner.c
181 lines (150 loc) · 4.45 KB
/
jadd_generic_inner.c
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* Copyright 2008-2019 Douglas Wikstrom
*
* This file is part of Verificatum Elliptic Curve library (VEC).
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <gmp.h>
#include "vec.h"
#define t1 scratch->t1
#define t2 scratch->t2
#define t3 scratch->t3
#define U1 scratch->t4
#define U2 scratch->t5
#define S1 scratch->t6
#define S2 scratch->t7
#define H scratch->t8
#define r scratch->t9
#define modulus curve->modulus
#define a curve->a
/* 1998 Cohen/Miyaji/Ono Jacobi coordinates with cached powers of
Z2. */
void
vec_jadd_generic_inner(vec_scratch_mpz_t scratch,
mpz_t X3, mpz_t Y3, mpz_t Z3,
vec_curve *curve,
mpz_t X1, mpz_t Y1, mpz_t Z1,
mpz_t X2, mpz_t Y2, mpz_t Z2)
{
/* P1 is point at infinity. */
if (mpz_cmp_si(Z1, 0) == 0)
{
/* P2 is also point at infinity. */
if (mpz_cmp_si(Z2, 0) == 0)
{
mpz_set_si(X3, 0);
mpz_set_si(Y3, 1);
mpz_set_si(Z3, 0);
return;
}
/* P1 is point at infinity and P2 is not. */
else
{
mpz_set(X3, X2);
mpz_set(Y3, Y2);
mpz_set(Z3, Z2);
return;
}
}
/* P2 is point at infinity and P1 is not. */
else if (mpz_cmp_si(Z2, 0) == 0)
{
mpz_set(X3, X1);
mpz_set(Y3, Y1);
mpz_set(Z3, Z1);
return;
}
/* Compute powers of Z2. */
mpz_mul(t1, Z2, Z2); /* t1 = Z2^2 */
mpz_mod(t1, t1, modulus);
mpz_mul(S2, t1, Z2); /* S2 = Z2^3 */
mpz_mod(S2, S2, modulus);
/* Compute powers of Z1 */
mpz_mul(t2, Z1, Z1); /* t2 = Z1^2 */
mpz_mod(t2, t2, modulus);
mpz_mul(t3, t2, Z1); /* t3 = Z1^3 */
mpz_mod(t3, t3, modulus);
/* U1:=X1*Z2^2 */
mpz_mul(U1, X1, t1);
mpz_mod(U1, U1, modulus);
/* U2:=X2*Z1^2 */
mpz_mul(U2, X2, t2);
/* S1:=Y1*Z2^3 */
mpz_mul(S1, Y1, S2);
mpz_mod(S1, S1, modulus);
/* S2:=Y2*Z1^3 */
mpz_mul(S2, Y2, t3);
/* H:=U2-U1 */
mpz_sub(H, U2, U1);
mpz_mod(H, H, modulus);
/* r:=S2-S1 */
mpz_sub(r, S2, S1);
mpz_mod(r, r, modulus);
if (mpz_cmp_si(H, 0) == 0)
{
if (mpz_cmp_si(r, 0) != 0)
{
mpz_set_si(X3, 0);
mpz_set_si(Y3, 1);
mpz_set_si(Z3, 0);
return;
}
else
{
curve->jdbl(scratch,
X3, Y3, Z3,
curve,
X1, Y1, Z1);
return;
}
}
/* Compute square of r */
mpz_mul(t1, r, r); /* t1 = r^2 */
mpz_mod(t1, t1, modulus);
/* Compute powers of H */
mpz_mul(t2, H, H); /* t2 = H^2 */
mpz_mod(t2, t2, modulus);
mpz_mul(t3, t2, H); /* t3 = H^3 */
mpz_mod(t3, t3, modulus);
/* X3:=-H^3-2*U1*H^2+r^2 */
mpz_sub(X3, t1, t3); /* X3 = r^2 - H^3 */
mpz_mul(t1, U1, t2); /* t1 = 2*U1*H^2 */
mpz_mul_si(t1, t1, 2);
mpz_mod(t1, t1, modulus);
mpz_sub(X3, X3, t1);
mpz_mod(X3, X3, modulus);
/* Y3:=-S1*H^3+r*(U1*H^2-X3) */
mpz_mul(t1, U1, t2); /* t1 = r*(U1*H^2-X3) */
mpz_mod(t1, t1, modulus);
mpz_sub(t1, t1, X3);
mpz_mul(t1, r, t1);
mpz_mod(t1, t1, modulus);
mpz_mul(t2, S1, t3); /* t2 = S1*H^3 */
mpz_mod(t2, t2, modulus);
mpz_sub(Y3, t1, t2);
mpz_mod(Y3, Y3, modulus);
/* Z3:=Z1*Z2*H */
mpz_mul(Z3, Z1, Z2);
mpz_mod(Z3, Z3, modulus);
mpz_mul(Z3, Z3, H);
mpz_mod(Z3, Z3, modulus);
}