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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
*
* LibTomCrypt is a library that provides various cryptographic
* algorithms in a highly modular and flexible manner.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
*
* All curves taken from NIST recommendation paper of July 1999
* Available at http://csrc.nist.gov/cryptval/dss.htm
*/
#include "ecc.h"
/**
@file ltc_ecc_projective_add_point.c
ECC Crypto, Tom St Denis
*/
#if defined(LTC_MECC) && (!defined(LTC_MECC_ACCEL) || defined(LTM_LTC_DESC))
/**
Add two ECC points
@param P The point to add
@param Q The point to add
@param R [out] The destination of the double
@param modulus The modulus of the field the ECC curve is in
@param mp The "b" value from montgomery_setup()
@return 0 on success
*/
int ltc_ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, mpz_t modulus)
{
mpz_t t1, t2, x, y, z;
int err;
assert(P != NULL);
assert(Q != NULL);
assert(R != NULL);
assert(modulus != NULL);
if ((err = mp_init_multi(&t1, &t2, &x, &y, &z, NULL)) != 0) {
return err;
}
/* should we dbl instead? */
mpz_sub(t1, modulus, Q->y);
if ( (mpz_cmp(P->x, Q->x) == 0) &&
(Q->z != NULL && mpz_cmp(P->z, Q->z) == 0) &&
(mpz_cmp(P->y, Q->y) == 0 || mpz_cmp(P->y, t1) == 0)) {
mp_clear_multi(&t1, &t2, &x, &y, &z, NULL);
return ltc_ecc_projective_dbl_point(P, R, modulus);
}
mpz_set(x, P->x);
mpz_set(y, P->y);
mpz_set(z, P->z);
/* if Z is one then these are no-operations */
if (mpz_cmp_ui(Q->z, 1) != 0) {
/* T1 = Z' * Z' */
mpz_mul(t1, Q->z, Q->z);
mpz_mod(t1, t1, modulus);
/* X = X * T1 */
mpz_mul(x, x, t1);
mpz_mod(x, x, modulus);
/* T1 = Z' * T1 */
mpz_mul(t1, t1, Q->z);
mpz_mod(t1, t1, modulus);
/* Y = Y * T1 */
mpz_mul(y, y, t1);
mpz_mod(y, y, modulus);
}
/* T1 = Z*Z */
mpz_mul(t1, z, z);
mpz_mod(t1, t1, modulus);
/* T2 = X' * T1 */
mpz_mul(t2, t1, Q->x);
mpz_mod(t2, t2, modulus);
/* T1 = Z * T1 */
mpz_mul(t1, t1, z);
mpz_mod(t1, t1, modulus);
/* T1 = Y' * T1 */
mpz_mul(t1, t1, Q->y);
mpz_mod(t1, t1, modulus);
/* Y = Y - T1 */
mpz_sub(y, y, t1);
if (mpz_cmp_ui(y, 0) < 0) {
mpz_add(y, y, modulus);
}
/* T1 = 2T1 */
mpz_add(t1, t1, t1);
if (mpz_cmp(t1, modulus) >= 0) {
mpz_sub(t1, t1, modulus);
}
/* T1 = Y + T1 */
mpz_add(t1, t1, y);
if (mpz_cmp(t1, modulus) >= 0) {
mpz_sub(t1, t1, modulus);
}
/* X = X - T2 */
mpz_sub(x, x, t2);
if (mpz_cmp_ui(x, 0) < 0) {
mpz_add(x, x, modulus);
}
/* T2 = 2T2 */
mpz_add(t2, t2, t2);
if (mpz_cmp(t2, modulus) >= 0) {
mpz_sub(t2, t2, modulus);
}
/* T2 = X + T2 */
mpz_add(t2, t2, x);
if (mpz_cmp(t2, modulus) >= 0) {
mpz_sub(t2, t2, modulus);
}
/* if Z' != 1 */
if (mpz_cmp_ui(Q->z, 1) != 0) {
/* Z = Z * Z' */
mpz_mul(z, z, Q->z);
mpz_mod(z, z, modulus);
}
/* Z = Z * X */
mpz_mul(z, z, x);
mpz_mod(z, z, modulus);
/* T1 = T1 * X */
mpz_mul(t1, t1, x);
mpz_mod(t1, t1, modulus);
/* X = X * X */
mpz_mul(x, x, x);
mpz_mod(x, x, modulus);
/* T2 = T2 * x */
mpz_mul(t2, t2, x);
mpz_mod(t2, t2, modulus);
/* T1 = T1 * X */
mpz_mul(t1, t1, x);
mpz_mod(t1, t1, modulus);
/* X = Y*Y */
mpz_mul(x, y, y);
mpz_mod(x, x, modulus);
/* X = X - T2 */
mpz_sub(x, x, t2);
if (mpz_cmp_ui(x, 0) < 0) {
mpz_add(x, x, modulus);
}
/* T2 = T2 - X */
mpz_sub(t2, t2, x);
if (mpz_cmp_ui(t2, 0) < 0) {
mpz_add(t2, t2, modulus);
}
/* T2 = T2 - X */
mpz_sub(t2, t2, x);
if (mpz_cmp_ui(t2, 0) < 0) {
mpz_add(t2, t2, modulus);
}
/* T2 = T2 * Y */
mpz_mul(t2, t2, y);
mpz_mod(t2, t2, modulus);
/* Y = T2 - T1 */
mpz_sub(y, t2, t1);
if (mpz_cmp_ui(y, 0) < 0) {
mpz_add(y, y, modulus);
}
/* Y = Y/2 */
if (mp_isodd(y)) {
mpz_add(y, y, modulus);
}
mpz_divexact_ui(y, y, 2);
mpz_set(R->x, x);
mpz_set(R->y, y);
mpz_set(R->z, z);
err = 0;
mp_clear_multi(&t1, &t2, &x, &y, &z, NULL);
return err;
}
#endif
/* $Source: /cvs/libtom/libtomcrypt/src/pk/ecc/ltc_ecc_projective_add_point.c,v $ */
/* $Revision: 1.16 $ */
/* $Date: 2007/05/12 14:32:35 $ */
|