Move GN build files for cacheinvalidation into the main tree.
[chromium-blink-merge.git] / crypto / p224.cc
blob11946a9413c5b06fe20601a99be19cc8327d1d2f
1 // Copyright (c) 2012 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.
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.
10 #include "crypto/p224.h"
12 #include <string.h>
14 #include "base/sys_byteorder.h"
16 namespace {
18 using base::HostToNet32;
19 using base::NetToHost32;
21 // Field element functions.
23 // The field that we're dealing with is ℤ/pℤ where p = 2**224 - 2**96 + 1.
25 // Field elements are represented by a FieldElement, which is a typedef to an
26 // array of 8 uint32's. The value of a FieldElement, a, is:
27 // a[0] + 2**28·a[1] + 2**56·a[1] + ... + 2**196·a[7]
29 // Using 28-bit limbs means that there's only 4 bits of headroom, which is less
30 // than we would really like. But it has the useful feature that we hit 2**224
31 // exactly, making the reflections during a reduce much nicer.
33 using crypto::p224::FieldElement;
35 // kP is the P224 prime.
36 const FieldElement kP = {
37 1, 0, 0, 268431360,
38 268435455, 268435455, 268435455, 268435455,
41 void Contract(FieldElement* inout);
43 // IsZero returns 0xffffffff if a == 0 mod p and 0 otherwise.
44 uint32 IsZero(const FieldElement& a) {
45 FieldElement minimal;
46 memcpy(&minimal, &a, sizeof(minimal));
47 Contract(&minimal);
49 uint32 is_zero = 0, is_p = 0;
50 for (unsigned i = 0; i < 8; i++) {
51 is_zero |= minimal[i];
52 is_p |= minimal[i] - kP[i];
55 // If either is_zero or is_p is 0, then we should return 1.
56 is_zero |= is_zero >> 16;
57 is_zero |= is_zero >> 8;
58 is_zero |= is_zero >> 4;
59 is_zero |= is_zero >> 2;
60 is_zero |= is_zero >> 1;
62 is_p |= is_p >> 16;
63 is_p |= is_p >> 8;
64 is_p |= is_p >> 4;
65 is_p |= is_p >> 2;
66 is_p |= is_p >> 1;
68 // For is_zero and is_p, the LSB is 0 iff all the bits are zero.
69 is_zero &= is_p & 1;
70 is_zero = (~is_zero) << 31;
71 is_zero = static_cast<int32>(is_zero) >> 31;
72 return is_zero;
75 // Add computes *out = a+b
77 // a[i] + b[i] < 2**32
78 void Add(FieldElement* out, const FieldElement& a, const FieldElement& b) {
79 for (int i = 0; i < 8; i++) {
80 (*out)[i] = a[i] + b[i];
84 static const uint32 kTwo31p3 = (1u<<31) + (1u<<3);
85 static const uint32 kTwo31m3 = (1u<<31) - (1u<<3);
86 static const uint32 kTwo31m15m3 = (1u<<31) - (1u<<15) - (1u<<3);
87 // kZero31ModP is 0 mod p where bit 31 is set in all limbs so that we can
88 // subtract smaller amounts without underflow. See the section "Subtraction" in
89 // [1] for why.
90 static const FieldElement kZero31ModP = {
91 kTwo31p3, kTwo31m3, kTwo31m3, kTwo31m15m3,
92 kTwo31m3, kTwo31m3, kTwo31m3, kTwo31m3
95 // Subtract computes *out = a-b
97 // a[i], b[i] < 2**30
98 // out[i] < 2**32
99 void Subtract(FieldElement* out, const FieldElement& a, const FieldElement& b) {
100 for (int i = 0; i < 8; i++) {
101 // See the section on "Subtraction" in [1] for details.
102 (*out)[i] = a[i] + kZero31ModP[i] - b[i];
106 static const uint64 kTwo63p35 = (1ull<<63) + (1ull<<35);
107 static const uint64 kTwo63m35 = (1ull<<63) - (1ull<<35);
108 static const uint64 kTwo63m35m19 = (1ull<<63) - (1ull<<35) - (1ull<<19);
109 // kZero63ModP is 0 mod p where bit 63 is set in all limbs. See the section
110 // "Subtraction" in [1] for why.
111 static const uint64 kZero63ModP[8] = {
112 kTwo63p35, kTwo63m35, kTwo63m35, kTwo63m35,
113 kTwo63m35m19, kTwo63m35, kTwo63m35, kTwo63m35,
116 static const uint32 kBottom28Bits = 0xfffffff;
118 // LargeFieldElement also represents an element of the field. The limbs are
119 // still spaced 28-bits apart and in little-endian order. So the limbs are at
120 // 0, 28, 56, ..., 392 bits, each 64-bits wide.
121 typedef uint64 LargeFieldElement[15];
123 // ReduceLarge converts a LargeFieldElement to a FieldElement.
125 // in[i] < 2**62
127 // GCC 4.9 incorrectly vectorizes the first coefficient elimination loop, so
128 // disable that optimization via pragma. Don't use the pragma under Clang, since
129 // clang doesn't understand it.
130 // TODO(wez): Remove this when crbug.com/439566 is fixed.
131 #if defined(__GNUC__) && !defined(__clang__)
132 #pragma GCC optimize("no-tree-vectorize")
133 #endif
135 void ReduceLarge(FieldElement* out, LargeFieldElement* inptr) {
136 LargeFieldElement& in(*inptr);
138 for (int i = 0; i < 8; i++) {
139 in[i] += kZero63ModP[i];
142 // Eliminate the coefficients at 2**224 and greater while maintaining the
143 // same value mod p.
144 for (int i = 14; i >= 8; i--) {
145 in[i-8] -= in[i]; // reflection off the "+1" term of p.
146 in[i-5] += (in[i] & 0xffff) << 12; // part of the "-2**96" reflection.
147 in[i-4] += in[i] >> 16; // the rest of the "-2**96" reflection.
149 in[8] = 0;
150 // in[0..8] < 2**64
152 // As the values become small enough, we start to store them in |out| and use
153 // 32-bit operations.
154 for (int i = 1; i < 8; i++) {
155 in[i+1] += in[i] >> 28;
156 (*out)[i] = static_cast<uint32>(in[i] & kBottom28Bits);
158 // Eliminate the term at 2*224 that we introduced while keeping the same
159 // value mod p.
160 in[0] -= in[8]; // reflection off the "+1" term of p.
161 (*out)[3] += static_cast<uint32>(in[8] & 0xffff) << 12; // "-2**96" term
162 (*out)[4] += static_cast<uint32>(in[8] >> 16); // rest of "-2**96" term
163 // in[0] < 2**64
164 // out[3] < 2**29
165 // out[4] < 2**29
166 // out[1,2,5..7] < 2**28
168 (*out)[0] = static_cast<uint32>(in[0] & kBottom28Bits);
169 (*out)[1] += static_cast<uint32>((in[0] >> 28) & kBottom28Bits);
170 (*out)[2] += static_cast<uint32>(in[0] >> 56);
171 // out[0] < 2**28
172 // out[1..4] < 2**29
173 // out[5..7] < 2**28
176 // TODO(wez): Remove this when crbug.com/439566 is fixed.
177 #if defined(__GNUC__) && !defined(__clang__)
178 // Reenable "tree-vectorize" optimization if it got disabled for ReduceLarge.
179 #pragma GCC reset_options
180 #endif
182 // Mul computes *out = a*b
184 // a[i] < 2**29, b[i] < 2**30 (or vice versa)
185 // out[i] < 2**29
186 void Mul(FieldElement* out, const FieldElement& a, const FieldElement& b) {
187 LargeFieldElement tmp;
188 memset(&tmp, 0, sizeof(tmp));
190 for (int i = 0; i < 8; i++) {
191 for (int j = 0; j < 8; j++) {
192 tmp[i+j] += static_cast<uint64>(a[i]) * static_cast<uint64>(b[j]);
196 ReduceLarge(out, &tmp);
199 // Square computes *out = a*a
201 // a[i] < 2**29
202 // out[i] < 2**29
203 void Square(FieldElement* out, const FieldElement& a) {
204 LargeFieldElement tmp;
205 memset(&tmp, 0, sizeof(tmp));
207 for (int i = 0; i < 8; i++) {
208 for (int j = 0; j <= i; j++) {
209 uint64 r = static_cast<uint64>(a[i]) * static_cast<uint64>(a[j]);
210 if (i == j) {
211 tmp[i+j] += r;
212 } else {
213 tmp[i+j] += r << 1;
218 ReduceLarge(out, &tmp);
221 // Reduce reduces the coefficients of in_out to smaller bounds.
223 // On entry: a[i] < 2**31 + 2**30
224 // On exit: a[i] < 2**29
225 void Reduce(FieldElement* in_out) {
226 FieldElement& a = *in_out;
228 for (int i = 0; i < 7; i++) {
229 a[i+1] += a[i] >> 28;
230 a[i] &= kBottom28Bits;
232 uint32 top = a[7] >> 28;
233 a[7] &= kBottom28Bits;
235 // top < 2**4
236 // Constant-time: mask = (top != 0) ? 0xffffffff : 0
237 uint32 mask = top;
238 mask |= mask >> 2;
239 mask |= mask >> 1;
240 mask <<= 31;
241 mask = static_cast<uint32>(static_cast<int32>(mask) >> 31);
243 // Eliminate top while maintaining the same value mod p.
244 a[0] -= top;
245 a[3] += top << 12;
247 // We may have just made a[0] negative but, if we did, then we must
248 // have added something to a[3], thus it's > 2**12. Therefore we can
249 // carry down to a[0].
250 a[3] -= 1 & mask;
251 a[2] += mask & ((1<<28) - 1);
252 a[1] += mask & ((1<<28) - 1);
253 a[0] += mask & (1<<28);
256 // Invert calcuates *out = in**-1 by computing in**(2**224 - 2**96 - 1), i.e.
257 // Fermat's little theorem.
258 void Invert(FieldElement* out, const FieldElement& in) {
259 FieldElement f1, f2, f3, f4;
261 Square(&f1, in); // 2
262 Mul(&f1, f1, in); // 2**2 - 1
263 Square(&f1, f1); // 2**3 - 2
264 Mul(&f1, f1, in); // 2**3 - 1
265 Square(&f2, f1); // 2**4 - 2
266 Square(&f2, f2); // 2**5 - 4
267 Square(&f2, f2); // 2**6 - 8
268 Mul(&f1, f1, f2); // 2**6 - 1
269 Square(&f2, f1); // 2**7 - 2
270 for (int i = 0; i < 5; i++) { // 2**12 - 2**6
271 Square(&f2, f2);
273 Mul(&f2, f2, f1); // 2**12 - 1
274 Square(&f3, f2); // 2**13 - 2
275 for (int i = 0; i < 11; i++) { // 2**24 - 2**12
276 Square(&f3, f3);
278 Mul(&f2, f3, f2); // 2**24 - 1
279 Square(&f3, f2); // 2**25 - 2
280 for (int i = 0; i < 23; i++) { // 2**48 - 2**24
281 Square(&f3, f3);
283 Mul(&f3, f3, f2); // 2**48 - 1
284 Square(&f4, f3); // 2**49 - 2
285 for (int i = 0; i < 47; i++) { // 2**96 - 2**48
286 Square(&f4, f4);
288 Mul(&f3, f3, f4); // 2**96 - 1
289 Square(&f4, f3); // 2**97 - 2
290 for (int i = 0; i < 23; i++) { // 2**120 - 2**24
291 Square(&f4, f4);
293 Mul(&f2, f4, f2); // 2**120 - 1
294 for (int i = 0; i < 6; i++) { // 2**126 - 2**6
295 Square(&f2, f2);
297 Mul(&f1, f1, f2); // 2**126 - 1
298 Square(&f1, f1); // 2**127 - 2
299 Mul(&f1, f1, in); // 2**127 - 1
300 for (int i = 0; i < 97; i++) { // 2**224 - 2**97
301 Square(&f1, f1);
303 Mul(out, f1, f3); // 2**224 - 2**96 - 1
306 // Contract converts a FieldElement to its minimal, distinguished form.
308 // On entry, in[i] < 2**29
309 // On exit, in[i] < 2**28
310 void Contract(FieldElement* inout) {
311 FieldElement& out = *inout;
313 // Reduce the coefficients to < 2**28.
314 for (int i = 0; i < 7; i++) {
315 out[i+1] += out[i] >> 28;
316 out[i] &= kBottom28Bits;
318 uint32 top = out[7] >> 28;
319 out[7] &= kBottom28Bits;
321 // Eliminate top while maintaining the same value mod p.
322 out[0] -= top;
323 out[3] += top << 12;
325 // We may just have made out[0] negative. So we carry down. If we made
326 // out[0] negative then we know that out[3] is sufficiently positive
327 // because we just added to it.
328 for (int i = 0; i < 3; i++) {
329 uint32 mask = static_cast<uint32>(static_cast<int32>(out[i]) >> 31);
330 out[i] += (1 << 28) & mask;
331 out[i+1] -= 1 & mask;
334 // We might have pushed out[3] over 2**28 so we perform another, partial
335 // carry chain.
336 for (int i = 3; i < 7; i++) {
337 out[i+1] += out[i] >> 28;
338 out[i] &= kBottom28Bits;
340 top = out[7] >> 28;
341 out[7] &= kBottom28Bits;
343 // Eliminate top while maintaining the same value mod p.
344 out[0] -= top;
345 out[3] += top << 12;
347 // There are two cases to consider for out[3]:
348 // 1) The first time that we eliminated top, we didn't push out[3] over
349 // 2**28. In this case, the partial carry chain didn't change any values
350 // and top is zero.
351 // 2) We did push out[3] over 2**28 the first time that we eliminated top.
352 // The first value of top was in [0..16), therefore, prior to eliminating
353 // the first top, 0xfff1000 <= out[3] <= 0xfffffff. Therefore, after
354 // overflowing and being reduced by the second carry chain, out[3] <=
355 // 0xf000. Thus it cannot have overflowed when we eliminated top for the
356 // second time.
358 // Again, we may just have made out[0] negative, so do the same carry down.
359 // As before, if we made out[0] negative then we know that out[3] is
360 // sufficiently positive.
361 for (int i = 0; i < 3; i++) {
362 uint32 mask = static_cast<uint32>(static_cast<int32>(out[i]) >> 31);
363 out[i] += (1 << 28) & mask;
364 out[i+1] -= 1 & mask;
367 // The value is < 2**224, but maybe greater than p. In order to reduce to a
368 // unique, minimal value we see if the value is >= p and, if so, subtract p.
370 // First we build a mask from the top four limbs, which must all be
371 // equal to bottom28Bits if the whole value is >= p. If top_4_all_ones
372 // ends up with any zero bits in the bottom 28 bits, then this wasn't
373 // true.
374 uint32 top_4_all_ones = 0xffffffffu;
375 for (int i = 4; i < 8; i++) {
376 top_4_all_ones &= out[i];
378 top_4_all_ones |= 0xf0000000;
379 // Now we replicate any zero bits to all the bits in top_4_all_ones.
380 top_4_all_ones &= top_4_all_ones >> 16;
381 top_4_all_ones &= top_4_all_ones >> 8;
382 top_4_all_ones &= top_4_all_ones >> 4;
383 top_4_all_ones &= top_4_all_ones >> 2;
384 top_4_all_ones &= top_4_all_ones >> 1;
385 top_4_all_ones =
386 static_cast<uint32>(static_cast<int32>(top_4_all_ones << 31) >> 31);
388 // Now we test whether the bottom three limbs are non-zero.
389 uint32 bottom_3_non_zero = out[0] | out[1] | out[2];
390 bottom_3_non_zero |= bottom_3_non_zero >> 16;
391 bottom_3_non_zero |= bottom_3_non_zero >> 8;
392 bottom_3_non_zero |= bottom_3_non_zero >> 4;
393 bottom_3_non_zero |= bottom_3_non_zero >> 2;
394 bottom_3_non_zero |= bottom_3_non_zero >> 1;
395 bottom_3_non_zero =
396 static_cast<uint32>(static_cast<int32>(bottom_3_non_zero) >> 31);
398 // Everything depends on the value of out[3].
399 // If it's > 0xffff000 and top_4_all_ones != 0 then the whole value is >= p
400 // If it's = 0xffff000 and top_4_all_ones != 0 and bottom_3_non_zero != 0,
401 // then the whole value is >= p
402 // If it's < 0xffff000, then the whole value is < p
403 uint32 n = out[3] - 0xffff000;
404 uint32 out_3_equal = n;
405 out_3_equal |= out_3_equal >> 16;
406 out_3_equal |= out_3_equal >> 8;
407 out_3_equal |= out_3_equal >> 4;
408 out_3_equal |= out_3_equal >> 2;
409 out_3_equal |= out_3_equal >> 1;
410 out_3_equal =
411 ~static_cast<uint32>(static_cast<int32>(out_3_equal << 31) >> 31);
413 // If out[3] > 0xffff000 then n's MSB will be zero.
414 uint32 out_3_gt = ~static_cast<uint32>(static_cast<int32>(n << 31) >> 31);
416 uint32 mask = top_4_all_ones & ((out_3_equal & bottom_3_non_zero) | out_3_gt);
417 out[0] -= 1 & mask;
418 out[3] -= 0xffff000 & mask;
419 out[4] -= 0xfffffff & mask;
420 out[5] -= 0xfffffff & mask;
421 out[6] -= 0xfffffff & mask;
422 out[7] -= 0xfffffff & mask;
426 // Group element functions.
428 // These functions deal with group elements. The group is an elliptic curve
429 // group with a = -3 defined in FIPS 186-3, section D.2.2.
431 using crypto::p224::Point;
433 // kB is parameter of the elliptic curve.
434 const FieldElement kB = {
435 55967668, 11768882, 265861671, 185302395,
436 39211076, 180311059, 84673715, 188764328,
439 void CopyConditional(Point* out, const Point& a, uint32 mask);
440 void DoubleJacobian(Point* out, const Point& a);
442 // AddJacobian computes *out = a+b where a != b.
443 void AddJacobian(Point *out,
444 const Point& a,
445 const Point& b) {
446 // See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl
447 FieldElement z1z1, z2z2, u1, u2, s1, s2, h, i, j, r, v;
449 uint32 z1_is_zero = IsZero(a.z);
450 uint32 z2_is_zero = IsZero(b.z);
452 // Z1Z1 = Z1²
453 Square(&z1z1, a.z);
455 // Z2Z2 = Z2²
456 Square(&z2z2, b.z);
458 // U1 = X1*Z2Z2
459 Mul(&u1, a.x, z2z2);
461 // U2 = X2*Z1Z1
462 Mul(&u2, b.x, z1z1);
464 // S1 = Y1*Z2*Z2Z2
465 Mul(&s1, b.z, z2z2);
466 Mul(&s1, a.y, s1);
468 // S2 = Y2*Z1*Z1Z1
469 Mul(&s2, a.z, z1z1);
470 Mul(&s2, b.y, s2);
472 // H = U2-U1
473 Subtract(&h, u2, u1);
474 Reduce(&h);
475 uint32 x_equal = IsZero(h);
477 // I = (2*H)²
478 for (int k = 0; k < 8; k++) {
479 i[k] = h[k] << 1;
481 Reduce(&i);
482 Square(&i, i);
484 // J = H*I
485 Mul(&j, h, i);
486 // r = 2*(S2-S1)
487 Subtract(&r, s2, s1);
488 Reduce(&r);
489 uint32 y_equal = IsZero(r);
491 if (x_equal && y_equal && !z1_is_zero && !z2_is_zero) {
492 // The two input points are the same therefore we must use the dedicated
493 // doubling function as the slope of the line is undefined.
494 DoubleJacobian(out, a);
495 return;
498 for (int k = 0; k < 8; k++) {
499 r[k] <<= 1;
501 Reduce(&r);
503 // V = U1*I
504 Mul(&v, u1, i);
506 // Z3 = ((Z1+Z2)²-Z1Z1-Z2Z2)*H
507 Add(&z1z1, z1z1, z2z2);
508 Add(&z2z2, a.z, b.z);
509 Reduce(&z2z2);
510 Square(&z2z2, z2z2);
511 Subtract(&out->z, z2z2, z1z1);
512 Reduce(&out->z);
513 Mul(&out->z, out->z, h);
515 // X3 = r²-J-2*V
516 for (int k = 0; k < 8; k++) {
517 z1z1[k] = v[k] << 1;
519 Add(&z1z1, j, z1z1);
520 Reduce(&z1z1);
521 Square(&out->x, r);
522 Subtract(&out->x, out->x, z1z1);
523 Reduce(&out->x);
525 // Y3 = r*(V-X3)-2*S1*J
526 for (int k = 0; k < 8; k++) {
527 s1[k] <<= 1;
529 Mul(&s1, s1, j);
530 Subtract(&z1z1, v, out->x);
531 Reduce(&z1z1);
532 Mul(&z1z1, z1z1, r);
533 Subtract(&out->y, z1z1, s1);
534 Reduce(&out->y);
536 CopyConditional(out, a, z2_is_zero);
537 CopyConditional(out, b, z1_is_zero);
540 // DoubleJacobian computes *out = a+a.
541 void DoubleJacobian(Point* out, const Point& a) {
542 // See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b
543 FieldElement delta, gamma, beta, alpha, t;
545 Square(&delta, a.z);
546 Square(&gamma, a.y);
547 Mul(&beta, a.x, gamma);
549 // alpha = 3*(X1-delta)*(X1+delta)
550 Add(&t, a.x, delta);
551 for (int i = 0; i < 8; i++) {
552 t[i] += t[i] << 1;
554 Reduce(&t);
555 Subtract(&alpha, a.x, delta);
556 Reduce(&alpha);
557 Mul(&alpha, alpha, t);
559 // Z3 = (Y1+Z1)²-gamma-delta
560 Add(&out->z, a.y, a.z);
561 Reduce(&out->z);
562 Square(&out->z, out->z);
563 Subtract(&out->z, out->z, gamma);
564 Reduce(&out->z);
565 Subtract(&out->z, out->z, delta);
566 Reduce(&out->z);
568 // X3 = alpha²-8*beta
569 for (int i = 0; i < 8; i++) {
570 delta[i] = beta[i] << 3;
572 Reduce(&delta);
573 Square(&out->x, alpha);
574 Subtract(&out->x, out->x, delta);
575 Reduce(&out->x);
577 // Y3 = alpha*(4*beta-X3)-8*gamma²
578 for (int i = 0; i < 8; i++) {
579 beta[i] <<= 2;
581 Reduce(&beta);
582 Subtract(&beta, beta, out->x);
583 Reduce(&beta);
584 Square(&gamma, gamma);
585 for (int i = 0; i < 8; i++) {
586 gamma[i] <<= 3;
588 Reduce(&gamma);
589 Mul(&out->y, alpha, beta);
590 Subtract(&out->y, out->y, gamma);
591 Reduce(&out->y);
594 // CopyConditional sets *out=a if mask is 0xffffffff. mask must be either 0 of
595 // 0xffffffff.
596 void CopyConditional(Point* out,
597 const Point& a,
598 uint32 mask) {
599 for (int i = 0; i < 8; i++) {
600 out->x[i] ^= mask & (a.x[i] ^ out->x[i]);
601 out->y[i] ^= mask & (a.y[i] ^ out->y[i]);
602 out->z[i] ^= mask & (a.z[i] ^ out->z[i]);
606 // ScalarMult calculates *out = a*scalar where scalar is a big-endian number of
607 // length scalar_len and != 0.
608 void ScalarMult(Point* out, const Point& a,
609 const uint8* scalar, size_t scalar_len) {
610 memset(out, 0, sizeof(*out));
611 Point tmp;
613 for (size_t i = 0; i < scalar_len; i++) {
614 for (unsigned int bit_num = 0; bit_num < 8; bit_num++) {
615 DoubleJacobian(out, *out);
616 uint32 bit = static_cast<uint32>(static_cast<int32>(
617 (((scalar[i] >> (7 - bit_num)) & 1) << 31) >> 31));
618 AddJacobian(&tmp, a, *out);
619 CopyConditional(out, tmp, bit);
624 // Get224Bits reads 7 words from in and scatters their contents in
625 // little-endian form into 8 words at out, 28 bits per output word.
626 void Get224Bits(uint32* out, const uint32* in) {
627 out[0] = NetToHost32(in[6]) & kBottom28Bits;
628 out[1] = ((NetToHost32(in[5]) << 4) |
629 (NetToHost32(in[6]) >> 28)) & kBottom28Bits;
630 out[2] = ((NetToHost32(in[4]) << 8) |
631 (NetToHost32(in[5]) >> 24)) & kBottom28Bits;
632 out[3] = ((NetToHost32(in[3]) << 12) |
633 (NetToHost32(in[4]) >> 20)) & kBottom28Bits;
634 out[4] = ((NetToHost32(in[2]) << 16) |
635 (NetToHost32(in[3]) >> 16)) & kBottom28Bits;
636 out[5] = ((NetToHost32(in[1]) << 20) |
637 (NetToHost32(in[2]) >> 12)) & kBottom28Bits;
638 out[6] = ((NetToHost32(in[0]) << 24) |
639 (NetToHost32(in[1]) >> 8)) & kBottom28Bits;
640 out[7] = (NetToHost32(in[0]) >> 4) & kBottom28Bits;
643 // Put224Bits performs the inverse operation to Get224Bits: taking 28 bits from
644 // each of 8 input words and writing them in big-endian order to 7 words at
645 // out.
646 void Put224Bits(uint32* out, const uint32* in) {
647 out[6] = HostToNet32((in[0] >> 0) | (in[1] << 28));
648 out[5] = HostToNet32((in[1] >> 4) | (in[2] << 24));
649 out[4] = HostToNet32((in[2] >> 8) | (in[3] << 20));
650 out[3] = HostToNet32((in[3] >> 12) | (in[4] << 16));
651 out[2] = HostToNet32((in[4] >> 16) | (in[5] << 12));
652 out[1] = HostToNet32((in[5] >> 20) | (in[6] << 8));
653 out[0] = HostToNet32((in[6] >> 24) | (in[7] << 4));
656 } // anonymous namespace
658 namespace crypto {
660 namespace p224 {
662 bool Point::SetFromString(const base::StringPiece& in) {
663 if (in.size() != 2*28)
664 return false;
665 const uint32* inwords = reinterpret_cast<const uint32*>(in.data());
666 Get224Bits(x, inwords);
667 Get224Bits(y, inwords + 7);
668 memset(&z, 0, sizeof(z));
669 z[0] = 1;
671 // Check that the point is on the curve, i.e. that y² = x³ - 3x + b.
672 FieldElement lhs;
673 Square(&lhs, y);
674 Contract(&lhs);
676 FieldElement rhs;
677 Square(&rhs, x);
678 Mul(&rhs, x, rhs);
680 FieldElement three_x;
681 for (int i = 0; i < 8; i++) {
682 three_x[i] = x[i] * 3;
684 Reduce(&three_x);
685 Subtract(&rhs, rhs, three_x);
686 Reduce(&rhs);
688 ::Add(&rhs, rhs, kB);
689 Contract(&rhs);
690 return memcmp(&lhs, &rhs, sizeof(lhs)) == 0;
693 std::string Point::ToString() const {
694 FieldElement zinv, zinv_sq, xx, yy;
696 // If this is the point at infinity we return a string of all zeros.
697 if (IsZero(this->z)) {
698 static const char zeros[56] = {0};
699 return std::string(zeros, sizeof(zeros));
702 Invert(&zinv, this->z);
703 Square(&zinv_sq, zinv);
704 Mul(&xx, x, zinv_sq);
705 Mul(&zinv_sq, zinv_sq, zinv);
706 Mul(&yy, y, zinv_sq);
708 Contract(&xx);
709 Contract(&yy);
711 uint32 outwords[14];
712 Put224Bits(outwords, xx);
713 Put224Bits(outwords + 7, yy);
714 return std::string(reinterpret_cast<const char*>(outwords), sizeof(outwords));
717 void ScalarMult(const Point& in, const uint8* scalar, Point* out) {
718 ::ScalarMult(out, in, scalar, 28);
721 // kBasePoint is the base point (generator) of the elliptic curve group.
722 static const Point kBasePoint = {
723 {22813985, 52956513, 34677300, 203240812,
724 12143107, 133374265, 225162431, 191946955},
725 {83918388, 223877528, 122119236, 123340192,
726 266784067, 263504429, 146143011, 198407736},
727 {1, 0, 0, 0, 0, 0, 0, 0},
730 void ScalarBaseMult(const uint8* scalar, Point* out) {
731 ::ScalarMult(out, kBasePoint, scalar, 28);
734 void Add(const Point& a, const Point& b, Point* out) {
735 AddJacobian(out, a, b);
738 void Negate(const Point& in, Point* out) {
739 // Guide to elliptic curve cryptography, page 89 suggests that (X : X+Y : Z)
740 // is the negative in Jacobian coordinates, but it doesn't actually appear to
741 // be true in testing so this performs the negation in affine coordinates.
742 FieldElement zinv, zinv_sq, y;
743 Invert(&zinv, in.z);
744 Square(&zinv_sq, zinv);
745 Mul(&out->x, in.x, zinv_sq);
746 Mul(&zinv_sq, zinv_sq, zinv);
747 Mul(&y, in.y, zinv_sq);
749 Subtract(&out->y, kP, y);
750 Reduce(&out->y);
752 memset(&out->z, 0, sizeof(out->z));
753 out->z[0] = 1;
756 } // namespace p224
758 } // namespace crypto