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.
8 // See http://www.imperialviolet.org/2010/12/04/ecc.html ([1]) for background.
10 #include "crypto/p224.h"
14 #include "base/sys_byteorder.h"
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
= {
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
) {
46 memcpy(&minimal
, &a
, sizeof(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;
68 // For is_zero and is_p, the LSB is 0 iff all the bits are zero.
70 is_zero
= (~is_zero
) << 31;
71 is_zero
= static_cast<int32
>(is_zero
) >> 31;
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
90 static const FieldElement kZero31ModP
= {
91 kTwo31p3
, kTwo31m3
, kTwo31m3
, kTwo31m15m3
,
92 kTwo31m3
, kTwo31m3
, kTwo31m3
, kTwo31m3
95 // Subtract computes *out = a-b
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.
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")
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
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.
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
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
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);
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
182 // Mul computes *out = a*b
184 // a[i] < 2**29, b[i] < 2**30 (or vice versa)
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
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
]);
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
;
236 // Constant-time: mask = (top != 0) ? 0xffffffff : 0
241 mask
= static_cast<uint32
>(static_cast<int32
>(mask
) >> 31);
243 // Eliminate top while maintaining the same value mod p.
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].
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
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
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
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
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
293 Mul(&f2
, f4
, f2
); // 2**120 - 1
294 for (int i
= 0; i
< 6; i
++) { // 2**126 - 2**6
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
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.
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
336 for (int i
= 3; i
< 7; i
++) {
337 out
[i
+1] += out
[i
] >> 28;
338 out
[i
] &= kBottom28Bits
;
341 out
[7] &= kBottom28Bits
;
343 // Eliminate top while maintaining the same value mod p.
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
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
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
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;
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;
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;
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
);
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
,
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
);
473 Subtract(&h
, u2
, u1
);
475 uint32 x_equal
= IsZero(h
);
478 for (int j
= 0; j
< 8; j
++) {
487 Subtract(&r
, s2
, s1
);
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
);
498 for (int i
= 0; i
< 8; i
++) {
506 // Z3 = ((Z1+Z2)²-Z1Z1-Z2Z2)*H
507 Add(&z1z1
, z1z1
, z2z2
);
508 Add(&z2z2
, a
.z
, b
.z
);
511 Subtract(&out
->z
, z2z2
, z1z1
);
513 Mul(&out
->z
, out
->z
, h
);
516 for (int i
= 0; i
< 8; i
++) {
522 Subtract(&out
->x
, out
->x
, z1z1
);
525 // Y3 = r*(V-X3)-2*S1*J
526 for (int i
= 0; i
< 8; i
++) {
530 Subtract(&z1z1
, v
, out
->x
);
533 Subtract(&out
->y
, z1z1
, s1
);
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
;
547 Mul(&beta
, a
.x
, gamma
);
549 // alpha = 3*(X1-delta)*(X1+delta)
551 for (int i
= 0; i
< 8; i
++) {
555 Subtract(&alpha
, a
.x
, delta
);
557 Mul(&alpha
, alpha
, t
);
559 // Z3 = (Y1+Z1)²-gamma-delta
560 Add(&out
->z
, a
.y
, a
.z
);
562 Square(&out
->z
, out
->z
);
563 Subtract(&out
->z
, out
->z
, gamma
);
565 Subtract(&out
->z
, out
->z
, delta
);
568 // X3 = alpha²-8*beta
569 for (int i
= 0; i
< 8; i
++) {
570 delta
[i
] = beta
[i
] << 3;
573 Square(&out
->x
, alpha
);
574 Subtract(&out
->x
, out
->x
, delta
);
577 // Y3 = alpha*(4*beta-X3)-8*gamma²
578 for (int i
= 0; i
< 8; i
++) {
582 Subtract(&beta
, beta
, out
->x
);
584 Square(&gamma
, gamma
);
585 for (int i
= 0; i
< 8; i
++) {
589 Mul(&out
->y
, alpha
, beta
);
590 Subtract(&out
->y
, out
->y
, gamma
);
594 // CopyConditional sets *out=a if mask is 0xffffffff. mask must be either 0 of
596 void CopyConditional(Point
* out
,
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
));
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
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
662 bool Point::SetFromString(const base::StringPiece
& in
) {
663 if (in
.size() != 2*28)
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
));
671 // Check that the point is on the curve, i.e. that y² = x³ - 3x + b.
680 FieldElement three_x
;
681 for (int i
= 0; i
< 8; i
++) {
682 three_x
[i
] = x
[i
] * 3;
685 Subtract(&rhs
, rhs
, three_x
);
688 ::Add(&rhs
, rhs
, kB
);
690 return memcmp(&lhs
, &rhs
, sizeof(lhs
)) == 0;
693 std::string
Point::ToString() const {
694 FieldElement zinv
, zinv_sq
, x
, y
;
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(&x
, this->x
, zinv_sq
);
705 Mul(&zinv_sq
, zinv_sq
, zinv
);
706 Mul(&y
, this->y
, zinv_sq
);
712 Put224Bits(outwords
, x
);
713 Put224Bits(outwords
+ 7, y
);
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
;
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
);
752 memset(&out
->z
, 0, sizeof(out
->z
));
758 } // namespace crypto