Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: crypto/p224.cc

Issue 8431007: crypto: add simple P224 implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ... Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // This is an implementation of the P224 elliptic curve group. It's written to
6 // be short and simple rather than fast, although it's still constant-time.
7 //
8 // See http://www.imperialviolet.org/2010/12/04/ecc.html ([1]) for background.
9
10 #include "crypto/p224.h"
11
12 #include <string.h>
13
14 #include "build/build_config.h"
15
16 // For htonl and ntohl.
17 #if defined(OS_WIN)
18 #include <winsock2.h>
19 #else
20 #include <arpa/inet.h>
21 #endif
22
23 namespace {
24
25 // Field element functions.
26 //
27 // The field that we're dealing with is ℤ/pℤ where p = 2**224 - 2**96 + 1.
28 //
29 // Field elements are represented by a FieldElement, which is a typedef to an
30 // array of 8 uint32's. The value of a FieldElement, a, is:
31 // a[0] + 2**28·a[1] + 2**56·a[1] + ... + 2**196·a[7]
32 //
33 // Using 28-bit limbs means that there's only 4 bits of headroom, which is less
34 // than we would really like. But it has the useful feature that we hit 2**224
35 // exactly, making the reflections during a reduce much nicer.
36
37 using crypto::p224::FieldElement;
38
39 // Add computes *out = a+b
40 //
41 // a[i] + b[i] < 2**32
42 void Add(FieldElement* out, const FieldElement& a, const FieldElement& b) {
43 for (int i = 0; i < 8; i++) {
44 (*out)[i] = a[i] + b[i];
45 }
46 }
47
48 static const uint32 kTwo31p3 = (1u<<31) + (1u<<3);
49 static const uint32 kTwo31m3 = (1u<<31) - (1u<<3);
50 static const uint32 kTwo31m15m3 = (1u<<31) - (1u<<15) - (1u<<3);
51 // kZero31ModP is 0 mod p where bit 31 is set in all limbs so that we can
52 // subtract smaller amounts without underflow. See the section "Subtraction" in
53 // [1] for why.
54 static const FieldElement kZero31ModP = {
55 kTwo31p3, kTwo31m3, kTwo31m3, kTwo31m15m3,
56 kTwo31m3, kTwo31m3, kTwo31m3, kTwo31m3
57 };
58
59 // Subtract computes *out = a-b
60 //
61 // a[i], b[i] < 2**30
62 // out[i] < 2**32
63 void Subtract(FieldElement* out, const FieldElement& a, const FieldElement& b) {
64 for (int i = 0; i < 8; i++) {
65 // See the section on "Subtraction" in [1] for details.
66 (*out)[i] = a[i] + kZero31ModP[i] - b[i];
67 }
68 }
69
70 static const uint64 kTwo63p35 = (1ull<<63) + (1ull<<35);
71 static const uint64 kTwo63m35 = (1ull<<63) - (1ull<<35);
72 static const uint64 kTwo63m35m19 = (1ull<<63) - (1ull<<35) - (1ull<<19);
73 // kZero63ModP is 0 mod p where bit 63 is set in all limbs. See the section
74 // "Subtraction" in [1] for why.
75 static const uint64 kZero63ModP[8] = {
76 kTwo63p35, kTwo63m35, kTwo63m35, kTwo63m35,
77 kTwo63m35m19, kTwo63m35, kTwo63m35, kTwo63m35,
78 };
79
80 static const uint32 kBottom28Bits = 0xfffffff;
81
82 // LargeFieldElement also represents an element of the field. The limbs are
83 // still spaced 28-bits apart and in little-endian order. So the limbs are at
84 // 0, 28, 56, ..., 392 bits, each 64-bits wide.
85 typedef uint64 LargeFieldElement[15];
86
87 // ReduceLarge converts a LargeFieldElement to a FieldElement.
88 //
89 // in[i] < 2**62
90 void ReduceLarge(FieldElement* out, LargeFieldElement* inptr) {
91 LargeFieldElement& in(*inptr);
92
93 for (int i = 0; i < 8; i++) {
94 in[i] += kZero63ModP[i];
95 }
96
97 // Eliminate the coefficients at 2**224 and greater while maintaining the
98 // same value mod p.
99 for (int i = 14; i >= 8; i--) {
100 in[i-8] -= in[i]; // reflection off the "+1" term of p.
101 in[i-5] += (in[i] & 0xffff) << 12; // part of the "-2**96" reflection.
102 in[i-4] += in[i] >> 16; // the rest of the "-2**96" reflection.
103 }
104 in[8] = 0;
105 // in[0..8] < 2**64
106
107 // As the values become small enough, we start to store them in |out| and use
108 // 32-bit operations.
109 for (int i = 1; i < 8; i++) {
110 in[i+1] += in[i] >> 28;
111 (*out)[i] = static_cast<uint32>(in[i] & kBottom28Bits);
112 }
113 // Eliminate the term at 2*224 that we introduced while keeping the same
114 // value mod p.
115 in[0] -= in[8]; // reflection off the "+1" term of p.
116 (*out)[3] += static_cast<uint32>(in[8] & 0xffff) << 12; // "-2**96" term
117 (*out)[4] += static_cast<uint32>(in[8] >> 16); // rest of "-2**96" term
118 // in[0] < 2**64
119 // out[3] < 2**29
120 // out[4] < 2**29
121 // out[1,2,5..7] < 2**28
122
123 (*out)[0] = static_cast<uint32>(in[0] & kBottom28Bits);
124 (*out)[1] += static_cast<uint32>((in[0] >> 28) & kBottom28Bits);
125 (*out)[2] += static_cast<uint32>(in[0] >> 56);
126 // out[0] < 2**28
127 // out[1..4] < 2**29
128 // out[5..7] < 2**28
129 }
130
131 // Mul computes *out = a*b
132 //
133 // a[i] < 2**29, b[i] < 2**30 (or vice versa)
134 // out[i] < 2**29
135 void Mul(FieldElement* out, const FieldElement& a, const FieldElement& b) {
136 LargeFieldElement tmp;
137 memset(&tmp, 0, sizeof(tmp));
138
139 for (int i = 0; i < 8; i++) {
140 for (int j = 0; j < 8; j++) {
141 tmp[i+j] += static_cast<uint64>(a[i]) * static_cast<uint64>(b[j]);
142 }
143 }
144
145 ReduceLarge(out, &tmp);
146 }
147
148 // Square computes *out = a*a
149 //
150 // a[i] < 2**29
151 // out[i] < 2**29
152 void Square(FieldElement* out, const FieldElement& a) {
153 LargeFieldElement tmp;
154 memset(&tmp, 0, sizeof(tmp));
155
156 for (int i = 0; i < 8; i++) {
157 for (int j = 0; j <= i; j++) {
158 uint64 r = static_cast<uint64>(a[i]) * static_cast<uint64>(a[j]);
159 if (i == j) {
160 tmp[i+j] += r;
161 } else {
162 tmp[i+j] += r << 1;
163 }
164 }
165 }
166
167 ReduceLarge(out, &tmp);
168 }
169
170 // Reduce reduces the coefficients of in_out to smaller bounds.
171 //
172 // On entry: a[i] < 2**31 + 2**30
173 // On exit: a[i] < 2**29
174 void Reduce(FieldElement* in_out) {
175 FieldElement& a = *in_out;
176
177 for (int i = 0; i < 7; i++) {
178 a[i+1] += a[i] >> 28;
179 a[i] &= kBottom28Bits;
180 }
181 uint32 top = a[7] >> 28;
182 a[7] &= kBottom28Bits;
183
184 // top < 2**4
185 // Constant-time: mask = (top != 0) ? 0xffffffff : 0
186 uint32 mask = top;
187 mask |= mask >> 2;
188 mask |= mask >> 1;
189 mask <<= 31;
190 mask = static_cast<uint32>(static_cast<int32>(mask) >> 31);
191
192 // Eliminate top while maintaining the same value mod p.
193 a[0] -= top;
194 a[3] += top << 12;
195
196 // We may have just made a[0] negative but, if we did, then we must
197 // have added something to a[3], thus it's > 2**12. Therefore we can
198 // carry down to a[0].
199 a[3] -= 1 & mask;
200 a[2] += mask & ((1<<28) - 1);
201 a[1] += mask & ((1<<28) - 1);
202 a[0] += mask & (1<<28);
203 }
204
205 // Invert calcuates *out = in**-1 by computing in**(2**224 - 2**96 - 1), i.e.
206 // Fermat's little theorem.
207 void Invert(FieldElement* out, const FieldElement& in) {
208 FieldElement f1, f2, f3, f4;
209
210 Square(&f1, in); // 2
211 Mul(&f1, f1, in); // 2**2 - 1
212 Square(&f1, f1); // 2**3 - 2
213 Mul(&f1, f1, in); // 2**3 - 1
214 Square(&f2, f1); // 2**4 - 2
215 Square(&f2, f2); // 2**5 - 4
216 Square(&f2, f2); // 2**6 - 8
217 Mul(&f1, f1, f2); // 2**6 - 1
218 Square(&f2, f1); // 2**7 - 2
219 for (int i = 0; i < 5; i++) { // 2**12 - 2**6
220 Square(&f2, f2);
221 }
222 Mul(&f2, f2, f1); // 2**12 - 1
223 Square(&f3, f2); // 2**13 - 2
224 for (int i = 0; i < 11; i++) { // 2**24 - 2**12
225 Square(&f3, f3);
226 }
227 Mul(&f2, f3, f2); // 2**24 - 1
228 Square(&f3, f2); // 2**25 - 2
229 for (int i = 0; i < 23; i++) { // 2**48 - 2**24
230 Square(&f3, f3);
231 }
232 Mul(&f3, f3, f2); // 2**48 - 1
233 Square(&f4, f3); // 2**49 - 2
234 for (int i = 0; i < 47; i++) { // 2**96 - 2**48
235 Square(&f4, f4);
236 }
237 Mul(&f3, f3, f4); // 2**96 - 1
238 Square(&f4, f3); // 2**97 - 2
239 for (int i = 0; i < 23; i++) { // 2**120 - 2**24
240 Square(&f4, f4);
241 }
242 Mul(&f2, f4, f2); // 2**120 - 1
243 for (int i = 0; i < 6; i++) { // 2**126 - 2**6
244 Square(&f2, f2);
245 }
246 Mul(&f1, f1, f2); // 2**126 - 1
247 Square(&f1, f1); // 2**127 - 2
248 Mul(&f1, f1, in); // 2**127 - 1
249 for (int i = 0; i < 97; i++) { // 2**224 - 2**97
250 Square(&f1, f1);
251 }
252 Mul(out, f1, f3); // 2**224 - 2**96 - 1
253 }
254
255 // Contract converts a FieldElement to its minimal, distinguished form.
256 //
257 // On entry, in[i] < 2**32
258 // On exit, in[i] < 2**28
259 void Contract(FieldElement* inout) {
260 FieldElement& out = *inout;
261
262 // Reduce the coefficients to < 2**28.
263 for (int i = 0; i < 7; i++) {
264 out[i+1] += out[i] >> 28;
265 out[i] &= kBottom28Bits;
266 }
267 uint32 top = out[7] >> 28;
268 out[7] &= kBottom28Bits;
269
270 // Eliminate top while maintaining the same value mod p.
271 out[0] -= top;
272 out[3] += top << 12;
273
274 // We may just have made out[0] negative. So we carry down. If we made
275 // out[0] negative then we know that out[3] is sufficiently positive
276 // because we just added to it.
277 for (int i = 0; i < 3; i++) {
278 uint32 mask = static_cast<uint32>(static_cast<int32>(out[i]) >> 31);
279 out[i] += (1 << 28) & mask;
280 out[i+1] -= 1 & mask;
281 }
282
283 // The value is < 2**224, but maybe greater than p. In order to reduce to a
284 // unique, minimal value we see if the value is >= p and, if so, subtract p.
285
286 // First we build a mask from the top four limbs, which must all be
287 // equal to bottom28Bits if the whole value is >= p. If top4AllOnes
288 // ends up with any zero bits in the bottom 28 bits, then this wasn't
289 // true.
290 uint32 top4AllOnes = 0xffffffffu;
291 for (int i = 4; i < 8; i++) {
292 top4AllOnes &= (out[i] & kBottom28Bits) - 1;
293 }
294 top4AllOnes |= 0xf0000000;
295 // Now we replicate any zero bits to all the bits in top4AllOnes.
296 top4AllOnes &= top4AllOnes >> 16;
297 top4AllOnes &= top4AllOnes >> 8;
298 top4AllOnes &= top4AllOnes >> 4;
299 top4AllOnes &= top4AllOnes >> 2;
300 top4AllOnes &= top4AllOnes >> 1;
301 top4AllOnes =
302 static_cast<uint32>(static_cast<int32>(top4AllOnes << 31) >> 31);
303
304 // Now we test whether the bottom three limbs are non-zero.
305 uint32 bottom3NonZero = out[0] | out[1] | out[2];
306 bottom3NonZero |= bottom3NonZero >> 16;
307 bottom3NonZero |= bottom3NonZero >> 8;
308 bottom3NonZero |= bottom3NonZero >> 4;
309 bottom3NonZero |= bottom3NonZero >> 2;
310 bottom3NonZero |= bottom3NonZero >> 1;
311 bottom3NonZero =
312 static_cast<uint32>(static_cast<int32>(bottom3NonZero << 31) >> 31);
313
314 // Everything depends on the value of out[3].
315 // If it's > 0xffff000 and top4AllOnes != 0 then the whole value is >= p
316 // If it's = 0xffff000 and top4AllOnes != 0 and bottom3NonZero != 0,
317 // then the whole value is >= p
318 // If it's < 0xffff000, then the whole value is < p
319 uint32 n = out[3] - 0xffff000;
320 uint32 out3Equal = n;
321 out3Equal |= out3Equal >> 16;
322 out3Equal |= out3Equal >> 8;
323 out3Equal |= out3Equal >> 4;
324 out3Equal |= out3Equal >> 2;
325 out3Equal |= out3Equal >> 1;
326 out3Equal =
327 ~static_cast<uint32>(static_cast<int32>(out3Equal << 31) >> 31);
328
329 // If out[3] > 0xffff000 then n's MSB will be zero.
330 uint32 out3GT = ~static_cast<uint32>(static_cast<int32>(n << 31) >> 31);
331
332 uint32 mask = top4AllOnes & ((out3Equal & bottom3NonZero) | out3GT);
333 out[0] -= 1 & mask;
334 out[3] -= 0xffff000 & mask;
335 out[4] -= 0xfffffff & mask;
336 out[5] -= 0xfffffff & mask;
337 out[6] -= 0xfffffff & mask;
338 out[7] -= 0xfffffff & mask;
339 }
340
341
342 // Group element functions.
343 //
344 // These functions deal with group elements. The group is an elliptic curve
345 // group with a = -3 defined in FIPS 186-3, section D.2.2.
346
347 using crypto::p224::Point;
348
349 // kP is the P224 prime.
350 const FieldElement kP = {
351 1, 0, 0, 268431360,
352 268435455, 268435455, 268435455, 268435455,
353 };
354
355 // kB is parameter of the elliptic curve.
356 const FieldElement kB = {
357 55967668, 11768882, 265861671, 185302395,
358 39211076, 180311059, 84673715, 188764328,
359 };
360
361 // AddJacobian computes *out = a+b where a != b.
362 void AddJacobian(Point *out,
363 const Point& a,
364 const Point& b) {
365 // See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-a dd-2007-bl
366 FieldElement z1z1, z2z2, u1, u2, s1, s2, h, i, j, r, v;
367
368 // Z1Z1 = Z1²
369 Square(&z1z1, a.z);
370
371 // Z2Z2 = Z2²
372 Square(&z2z2, b.z);
373
374 // U1 = X1*Z2Z2
375 Mul(&u1, a.x, z2z2);
376
377 // U2 = X2*Z1Z1
378 Mul(&u2, b.x, z1z1);
379
380 // S1 = Y1*Z2*Z2Z2
381 Mul(&s1, b.z, z2z2);
382 Mul(&s1, a.y, s1);
383
384 // S2 = Y2*Z1*Z1Z1
385 Mul(&s2, a.z, z1z1);
386 Mul(&s2, b.y, s2);
387
388 // H = U2-U1
389 Subtract(&h, u2, u1);
390 Reduce(&h);
391
392 // I = (2*H)²
393 for (int j = 0; j < 8; j++) {
394 i[j] = h[j] << 1;
395 }
396 Reduce(&i);
397 Square(&i, i);
398
399 // J = H*I
400 Mul(&j, h, i);
401 // r = 2*(S2-S1)
402 Subtract(&r, s2, s1);
403 Reduce(&r);
404 for (int i = 0; i < 8; i++) {
405 r[i] <<= 1;
406 }
407 Reduce(&r);
408
409 // V = U1*I
410 Mul(&v, u1, i);
411
412 // Z3 = ((Z1+Z2)²-Z1Z1-Z2Z2)*H
413 Add(&z1z1, z1z1, z2z2);
414 Add(&z2z2, a.z, b.z);
415 Reduce(&z2z2);
416 Square(&z2z2, z2z2);
417 Subtract(&out->z, z2z2, z1z1);
418 Reduce(&out->z);
419 Mul(&out->z, out->z, h);
420
421 // X3 = r²-J-2*V
422 for (int i = 0; i < 8; i++) {
423 z1z1[i] = v[i] << 1;
424 }
425 Add(&z1z1, j, z1z1);
426 Reduce(&z1z1);
427 Square(&out->x, r);
428 Subtract(&out->x, out->x, z1z1);
429 Reduce(&out->x);
430
431 // Y3 = r*(V-X3)-2*S1*J
432 for (int i = 0; i < 8; i++) {
433 s1[i] <<= 1;
434 }
435 Mul(&s1, s1, j);
436 Subtract(&z1z1, v, out->x);
437 Reduce(&z1z1);
438 Mul(&z1z1, z1z1, r);
439 Subtract(&out->y, z1z1, s1);
440 Reduce(&out->y);
441 }
442
443 // DoubleJacobian computes *out = a+a.
444 void DoubleJacobian(Point* out, const Point& a) {
445 // See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-d bl-2001-b
446 FieldElement delta, gamma, beta, alpha, t;
447
448 Square(&delta, a.z);
449 Square(&gamma, a.y);
450 Mul(&beta, a.x, gamma);
451
452 // alpha = 3*(X1-delta)*(X1+delta)
453 Add(&t, a.x, delta);
454 for (int i = 0; i < 8; i++) {
455 t[i] += t[i] << 1;
456 }
457 Reduce(&t);
458 Subtract(&alpha, a.x, delta);
459 Reduce(&alpha);
460 Mul(&alpha, alpha, t);
461
462 // Z3 = (Y1+Z1)²-gamma-delta
463 Add(&out->z, a.y, a.z);
464 Reduce(&out->z);
465 Square(&out->z, out->z);
466 Subtract(&out->z, out->z, gamma);
467 Reduce(&out->z);
468 Subtract(&out->z, out->z, delta);
469 Reduce(&out->z);
470
471 // X3 = alpha²-8*beta
472 for (int i = 0; i < 8; i++) {
473 delta[i] = beta[i] << 3;
474 }
475 Reduce(&delta);
476 Square(&out->x, alpha);
477 Subtract(&out->x, out->x, delta);
478 Reduce(&out->x);
479
480 // Y3 = alpha*(4*beta-X3)-8*gamma²
481 for (int i = 0; i < 8; i++) {
482 beta[i] <<= 2;
483 }
484 Reduce(&beta);
485 Subtract(&beta, beta, out->x);
486 Reduce(&beta);
487 Square(&gamma, gamma);
488 for (int i = 0; i < 8; i++) {
489 gamma[i] <<= 3;
490 }
491 Reduce(&gamma);
492 Mul(&out->y, alpha, beta);
493 Subtract(&out->y, out->y, gamma);
494 Reduce(&out->y);
495 }
496
497 // CopyConditional sets *out=a if mask is 0xffffffff. mask must be either 0 of
498 // 0xffffffff.
499 void CopyConditional(Point* out,
500 const Point& a,
501 uint32 mask) {
502 for (int i = 0; i < 8; i++) {
503 out->x[i] ^= mask & (a.x[i] ^ out->x[i]);
504 out->y[i] ^= mask & (a.y[i] ^ out->y[i]);
505 out->z[i] ^= mask & (a.z[i] ^ out->z[i]);
506 }
507 }
508
509 // ScalarMult calculates *out = a*scalar where scalar is a big-endian number of
510 // length scalar_len and != 0.
511 void ScalarMult(Point* out, const Point& a,
512 const uint8* scalar, size_t scalar_len) {
513 memset(out, 0, sizeof(*out));
514 Point tmp;
515
516 uint32 first_bit = 0xffffffff;
517 for (size_t i = 0; i < scalar_len; i++) {
518 for (unsigned int bit_num = 0; bit_num < 8; bit_num++) {
519 DoubleJacobian(out, *out);
520 uint32 bit = static_cast<uint32>(static_cast<int32>(
521 (((scalar[i] >> (7 - bit_num)) & 1) << 31) >> 31));
522 AddJacobian(&tmp, a, *out);
523 CopyConditional(out, a, first_bit & bit);
524 CopyConditional(out, tmp, ~first_bit & bit);
525 first_bit = first_bit & ~bit;
526 }
527 }
528 }
529
530 // Get224Bits reads 7 words from in and scatters their contents in
531 // little-endian form into 8 words at out, 28 bits per output word.
532 void Get224Bits(uint32* out, const uint32* in) {
533 out[0] = ntohl(in[6]) & kBottom28Bits;
534 out[1] = ((ntohl(in[5]) << 4) | (ntohl(in[6]) >> 28)) & kBottom28Bits;
535 out[2] = ((ntohl(in[4]) << 8) | (ntohl(in[5]) >> 24)) & kBottom28Bits;
536 out[3] = ((ntohl(in[3]) << 12) | (ntohl(in[4]) >> 20)) & kBottom28Bits;
537 out[4] = ((ntohl(in[2]) << 16) | (ntohl(in[3]) >> 16)) & kBottom28Bits;
538 out[5] = ((ntohl(in[1]) << 20) | (ntohl(in[2]) >> 12)) & kBottom28Bits;
539 out[6] = ((ntohl(in[0]) << 24) | (ntohl(in[1]) >> 8)) & kBottom28Bits;
540 out[7] = (ntohl(in[0]) >> 4) & kBottom28Bits;
541 }
542
543 // Put224Bits performs the inverse operation to Get224Bits: taking 28 bits from
544 // each of 8 input words and writing them in big-endian order to 7 words at
545 // out.
546 void Put224Bits(uint32* out, const uint32* in) {
547 out[6] = htonl((in[0] >> 0) | (in[1] << 28));
548 out[5] = htonl((in[1] >> 4) | (in[2] << 24));
549 out[4] = htonl((in[2] >> 8) | (in[3] << 20));
550 out[3] = htonl((in[3] >> 12) | (in[4] << 16));
551 out[2] = htonl((in[4] >> 16) | (in[5] << 12));
552 out[1] = htonl((in[5] >> 20) | (in[6] << 8));
553 out[0] = htonl((in[6] >> 24) | (in[7] << 4));
554 }
555
556 } // anonymous namespace
557
558 namespace crypto {
559
560 namespace p224 {
561
562 bool Point::Set(const base::StringPiece& in) {
563 if (in.size() != 2*28)
564 return false;
565 const uint32* inwords = reinterpret_cast<const uint32*>(in.data());
566 Get224Bits(x, inwords);
567 Get224Bits(y, inwords + 7);
568 memset(&z, 0, sizeof(z));
569 z[0] = 1;
570
571 // Check that the point is on the curve, i.e. that y² = x³ - 3x + b.
572 FieldElement lhs;
573 Square(&lhs, y);
574 Contract(&lhs);
575
576 FieldElement rhs;
577 Square(&rhs, x);
578 Mul(&rhs, x, rhs);
579
580 FieldElement three_x;
581 for (int i = 0; i < 8; i++) {
582 three_x[i] = x[i] * 3;
583 }
584 Reduce(&three_x);
585 Subtract(&rhs, rhs, three_x);
586 Reduce(&rhs);
587
588 ::Add(&rhs, rhs, kB);
589 Contract(&rhs);
590 return memcmp(&lhs, &rhs, sizeof(lhs)) == 0;
591 }
592
593 std::string Point::ToString() const {
594 FieldElement zinv, zinv_sq, x, y;
595
596 Invert(&zinv, this->z);
597 Square(&zinv_sq, zinv);
598 Mul(&x, this->x, zinv_sq);
599 Mul(&zinv_sq, zinv_sq, zinv);
600 Mul(&y, this->y, zinv_sq);
601
602 Contract(&x);
603 Contract(&y);
604
605 uint32 outwords[14];
606 Put224Bits(outwords, x);
607 Put224Bits(outwords + 7, y);
608 return std::string(reinterpret_cast<const char*>(outwords), sizeof(outwords));
609 }
610
611 void ScalarMult(const Point& in, const uint8* scalar, Point* out) {
612 ::ScalarMult(out, in, scalar, 28);
613 }
614
615 // kBasePoint is the base point (generator) of the elliptic curve group.
616 static const Point kBasePoint = {
617 {22813985, 52956513, 34677300, 203240812,
618 12143107, 133374265, 225162431, 191946955},
619 {83918388, 223877528, 122119236, 123340192,
620 266784067, 263504429, 146143011, 198407736},
621 {1, 0, 0, 0, 0, 0, 0, 0},
622 };
623
624 void ScalarBaseMult(const uint8* scalar, Point* out) {
625 ::ScalarMult(out, kBasePoint, scalar, 28);
626 }
627
628 void Add(const Point& a, const Point& b, Point* out) {
629 AddJacobian(out, a, b);
630 }
631
632 void Negate(const Point& in, Point* out) {
633 // Guide to elliptic curve cryptography, page 89 suggests that (X : X+Y : Z)
634 // is the negative in Jacobian coordinates, but it doesn't actually appear to
635 // be true in testing so this performs the negation in affine coordinates.
636 FieldElement zinv, zinv_sq, y;
637 Invert(&zinv, in.z);
638 Square(&zinv_sq, zinv);
639 Mul(&out->x, in.x, zinv_sq);
640 Mul(&zinv_sq, zinv_sq, zinv);
641 Mul(&y, in.y, zinv_sq);
642
643 Subtract(&out->y, kP, y);
644 Reduce(&out->y);
645
646 memset(&out->z, 0, sizeof(out->z));
647 out->z[0] = 1;
648 }
649
650 } // namespace p224
651
652 } // namespace crypto
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698