Chromium Code Reviews| Index: crypto/p224.cc |
| diff --git a/crypto/p224.cc b/crypto/p224.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ad765a98178dbb3159834bd1b3829710f1e50dda |
| --- /dev/null |
| +++ b/crypto/p224.cc |
| @@ -0,0 +1,612 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// This is an implementation of the P224 elliptic curve group. It's written to |
| +// be short and simple rather than fast, although it's still constant-time. |
| + |
| +#include <string.h> |
| +#include <arpa/inet.h> |
| + |
| +#include "crypto/p224.h" |
| + |
| +namespace { |
| + |
| +// Field element functions. |
| +// |
| +// The field that we're dealing with is ℤ/pℤ where p = 2**224 - 2**96 + 1. |
| +// |
| +// Field elements are represented by a FieldElement, which is a typedef to an |
| +// array of 8 uint32's. The value of a FieldElement, a, is: |
| +// a[0] + 2**28·a[1] + 2**56·a[1] + ... + 2**196·a[7] |
| +// |
| +// Using 28-bit limbs means that there's only 4 bits of headroom, which is less |
| +// than we would really like. But it has the useful feature that we hit 2**224 |
| +// exactly, making the reflections during a reduce much nicer. |
| + |
| +typedef crypto::P224::FieldElement FieldElement; |
|
Wez
2011/11/02 23:46:06
nit: Why not just "using crypto::P224::FieldElemen
agl
2011/11/03 17:20:49
Doesn't work I'm afraid:
../../crypto/p224.cc:35:
|
| + |
| +// Add computes *out = a+b |
| +// |
| +// Bounds on a and b are such that the sum of each corresponding limb of a and |
| +// b mustn't exceed 2**32. |
|
Wez
2011/11/02 23:46:06
Express this similarly to the comment on Sub(), fo
agl
2011/11/03 17:20:49
Done.
|
| +void Add(FieldElement* out, const FieldElement& a, const FieldElement& b) { |
| + for (int i = 0; i < 8; i++) { |
| + (*out)[i] = a[i] + b[i]; |
| + } |
| +} |
| + |
| +static const uint32 kTwo31p3 = (1u<<31) + (1u<<3); |
| +static const uint32 kTwo31m3 = (1u<<31) - (1u<<3); |
| +static const uint32 kTwo31m15m3 = (1u<<31) - (1u<<15) - (1u<<3); |
| +// kZero31ModP is 0 mod p where bit 31 is set in all limbs. |
| +static const FieldElement kZero31ModP = { |
| + kTwo31p3, kTwo31m3, kTwo31m3, kTwo31m15m3, |
| + kTwo31m3, kTwo31m3, kTwo31m3, kTwo31m3 |
|
Wez
2011/11/02 23:46:06
It would be great to clarify with _why_ we want to
agl
2011/11/03 17:20:49
That could go on for a while so I've cited hthe su
Wez
2011/11/03 20:03:46
What I had in mind was a sentence explaining that
agl
2011/11/04 20:13:22
Have updated the comment.
|
| +}; |
| + |
| +// Sub computes *out = a-b |
| +// |
| +// a[i], b[i] < 2**30 |
| +// out[i] < 2**32 |
| +void Sub(FieldElement* out, const FieldElement& a, const FieldElement& b) { |
|
Wez
2011/11/02 23:46:06
nit: Sub -> Subtract, according to the style guide
agl
2011/11/03 17:20:49
Done.
|
| + for (int i = 0; i < 8; i++) { |
| + (*out)[i] = a[i] + kZero31ModP[i] - b[i]; |
|
Wez
2011/11/02 23:46:06
It looks like the "zero" constant here is being us
agl
2011/11/03 17:20:49
Have also cited the same section in ecc.html.
|
| + } |
| +} |
| + |
| +static const uint64 kTwo63p35 = (1ull<<63) + (1ull<<35); |
| +static const uint64 kTwo63m35 = (1ull<<63) - (1ull<<35); |
| +static const uint64 kTwo63m35m19 = (1ull<<63) - (1ull<<35) - (1ull<<19); |
| +// kZero31ModP is 0 mod p where bit 63 is set in all limbs. |
|
Wez
2011/11/02 23:46:06
kZero31ModP -> kZero63ModP
agl
2011/11/03 17:20:49
Done.
|
| +static const uint64 kZero63ModP[8] = { |
| + kTwo63p35, kTwo63m35, kTwo63m35, kTwo63m35, |
| + kTwo63m35m19, kTwo63m35, kTwo63m35, kTwo63m35, |
| +}; |
| + |
| +static const uint32 kBottom12Bits = 0xfff; |
|
Wez
2011/11/02 23:46:06
You define this but never use it?
agl
2011/11/03 17:20:49
Removed.
|
| +static const uint32 kBottom28Bits = 0xfffffff; |
| + |
| +// LargeFieldElement also represents an element of the field. The limbs are |
| +// still spaced 28-bits apart and in little-endian order. |
|
Wez
2011/11/02 23:46:06
Since LargeFieldElement still represents 28-bits w
agl
2011/11/03 17:20:49
It only has coefficients up to 392 bits, but it's
Wez
2011/11/03 20:03:46
Would it be correct to say that each limb "represe
agl
2011/11/04 20:13:22
"represents"? Not really I'm afraid.
|
| +typedef uint64 LargeFieldElement[15]; |
| + |
| +// ReduceLarge converts a LargeFieldElement to a FieldElement. |
| +// |
| +// in[i] < 2**62 |
| +void ReduceLarge(FieldElement* out, LargeFieldElement& in) { |
|
Wez
2011/11/02 23:46:06
You're modifying LargeFieldElement, so it should r
agl
2011/11/03 17:20:49
Done.
|
| + for (int i = 0; i < 8; i++) { |
| + in[i] += kZero63ModP[i]; |
| + } |
| + |
| + // Eliminate the coefficients at 2**224 and greater. |
|
Wez
2011/11/02 23:46:06
Explain that you're using a mod p operation to red
agl
2011/11/03 17:20:49
Done.
|
| + for (int i = 14; i >= 8; i--) { |
| + in[i-8] -= in[i]; |
| + in[i-5] += (in[i] & 0xffff) << 12; |
| + in[i-4] += in[i] >> 16; |
|
Wez
2011/11/02 23:46:06
nit: It would help laymen like me to have each of
agl
2011/11/03 17:20:49
Done.
|
| + } |
| + in[8] = 0; |
| + // in[0..8] < 2**64 |
| + |
| + // As the values become small enough, we start to store them in |out| and use |
| + // 32-bit operations. |
| + for (int i = 1; i < 8; i++) { |
| + in[i+1] += in[i] >> 28; |
| + (*out)[i] = static_cast<uint32>(in[i] & kBottom28Bits); |
| + } |
| + in[0] -= in[8]; |
| + (*out)[3] += static_cast<uint32>(in[8] & 0xffff) << 12; |
| + (*out)[4] += static_cast<uint32>(in[8] >> 16); |
|
Wez
2011/11/02 23:46:06
This looks like another mod p operation?
agl
2011/11/03 17:20:49
Done.
|
| + // in[0] < 2**64 |
| + // out[3] < 2**29 |
| + // out[4] < 2**29 |
| + // out[1,2,5..7] < 2**28 |
| + |
| + (*out)[0] = static_cast<uint32>(in[0] & kBottom28Bits); |
| + (*out)[1] += static_cast<uint32>((in[0] >> 28) & kBottom28Bits); |
| + (*out)[2] += static_cast<uint32>(in[0] >> 56); |
| + // out[0] < 2**28 |
| + // out[1..4] < 2**29 |
| + // out[5..7] < 2**28 |
| +} |
| + |
| +// Mul computes *out = a*b |
| +// |
| +// a[i] < 2**29, b[i] < 2**30 (or vice versa) |
| +// out[i] < 2**29 |
| +void Mul(FieldElement* out, const FieldElement& a, const FieldElement& b) { |
| + LargeFieldElement tmp; |
| + memset(&tmp, 0, sizeof(tmp)); |
| + |
| + for (int i = 0; i < 8; i++) { |
| + for (int j = 0; j < 8; j++) { |
| + tmp[i+j] += static_cast<uint64>(a[i]) * static_cast<uint64>(b[j]); |
| + } |
| + } |
| + |
| + ReduceLarge(out, tmp); |
| +} |
| + |
| +// Square computes *out = a*a |
| +// |
| +// a[i] < 2**29 |
| +// out[i] < 2**29 |
| +void Square(FieldElement* out, const FieldElement& a) { |
| + LargeFieldElement tmp; |
| + memset(&tmp, 0, sizeof(tmp)); |
| + |
| + for (int i = 0; i < 8; i++) { |
| + for (int j = 0; j <= i; j++) { |
| + uint64 r = static_cast<uint64>(a[i]) * static_cast<uint64>(a[j]); |
| + if (i == j) { |
| + tmp[i+j] += r; |
| + } else { |
| + tmp[i+j] += r << 1; |
| + } |
| + } |
| + } |
| + |
| + ReduceLarge(out, tmp); |
| +} |
| + |
| +// Reduce reduces the coefficients of a to smaller bounds. |
| +// |
| +// On entry: a[i] < 2**31 + 2**30 |
| +// On exit: a[i] < 2**29 |
| +void Reduce(FieldElement* in) { |
|
Wez
2011/11/02 23:46:06
Either rename in to in_out, or update the comment.
agl
2011/11/03 17:20:49
Done.
|
| + FieldElement& a = *in; |
| + |
| + for (int i = 0; i < 7; i++) { |
| + a[i+1] += a[i] >> 28; |
| + a[i] &= kBottom28Bits; |
| + } |
| + uint32 top = a[7] >> 28; |
| + a[7] &= kBottom28Bits; |
| + |
| + // top < 2**4 |
| + uint32 mask = top; |
| + mask |= mask >> 2; |
| + mask |= mask >> 1; |
| + mask <<= 31; |
| + mask = static_cast<uint32>(static_cast<int32>(mask) >> 31); |
| + // Mask is all ones if top != 0, all zero otherwise |
|
Wez
2011/11/02 23:46:06
I think you're doing this to ensure constant-time
agl
2011/11/03 17:20:49
Done.
|
| + |
| + a[0] -= top; |
| + a[3] += top << 12; |
|
Wez
2011/11/02 23:46:06
This comes, again, as a result of folding things d
agl
2011/11/03 17:20:49
Yes. Commented.
|
| + |
| + // We may have just made a[0] negative but, if we did, then we must |
| + // have added something to a[3], this it's > 2**12. Therefore we can |
|
Wez
2011/11/02 23:46:06
typo: this -> thus?
agl
2011/11/03 17:20:49
Done.
|
| + // carry down to a[0]. |
| + a[3] -= 1 & mask; |
| + a[2] += mask & ((1<<28) - 1); |
| + a[1] += mask & ((1<<28) - 1); |
| + a[0] += mask & (1<<28); |
| +} |
| + |
| +// Invert calcuates *out = in^-1 using Fermat's little theorem. |
|
Wez
2011/11/02 23:46:06
Suggest indicating as part of this comment that th
agl
2011/11/03 17:20:49
Done.
|
| +void Invert(FieldElement* out, const FieldElement& in) { |
| + FieldElement f1, f2, f3, f4; |
| + |
| + Square(&f1, in); // 2 |
| + Mul(&f1, f1, in); // 2**2 - 1 |
| + Square(&f1, f1); // 2**3 - 2 |
| + Mul(&f1, f1, in); // 2**3 - 1 |
| + Square(&f2, f1); // 2**4 - 2 |
| + Square(&f2, f2); // 2**5 - 4 |
| + Square(&f2, f2); // 2**6 - 8 |
| + Mul(&f1, f1, f2); // 2**6 - 1 |
| + Square(&f2, f1); // 2**7 - 2 |
| + for (int i = 0; i < 5; i++) { // 2**12 - 2**6 |
| + Square(&f2, f2); |
|
Wez
2011/11/02 23:46:06
I like the exuberance of your indentation, but the
agl
2011/11/03 17:20:49
Done.
|
| + } |
| + Mul(&f2, f2, f1); // 2**12 - 1 |
| + Square(&f3, f2); // 2**13 - 2 |
| + for (int i = 0; i < 11; i++) { // 2**24 - 2**12 |
| + Square(&f3, f3); |
| + } |
| + Mul(&f2, f3, f2); // 2**24 - 1 |
| + Square(&f3, f2); // 2**25 - 2 |
| + for (int i = 0; i < 23; i++) { // 2**48 - 2**24 |
| + Square(&f3, f3); |
| + } |
| + Mul(&f3, f3, f2); // 2**48 - 1 |
| + Square(&f4, f3); // 2**49 - 2 |
| + for (int i = 0; i < 47; i++) { // 2**96 - 2**48 |
| + Square(&f4, f4); |
| + } |
| + Mul(&f3, f3, f4); // 2**96 - 1 |
| + Square(&f4, f3); // 2**97 - 2 |
| + for (int i = 0; i < 23; i++) { // 2**120 - 2**24 |
| + Square(&f4, f4); |
| + } |
| + Mul(&f2, f4, f2); // 2**120 - 1 |
| + for (int i = 0; i < 6; i++) { // 2**126 - 2**6 |
| + Square(&f2, f2); |
| + } |
| + Mul(&f1, f1, f2); // 2**126 - 1 |
| + Square(&f1, f1); // 2**127 - 2 |
| + Mul(&f1, f1, in); // 2**127 - 1 |
| + for (int i = 0; i < 97; i++) { // 2**224 - 2**97 |
| + Square(&f1, f1); |
| + } |
| + Mul(out, f1, f3); // 2**224 - 2**96 - 1 |
| +} |
| + |
| +// Contract converts a FieldElement to its minimal, distinguished form. |
| +// |
| +// On entry, in[i] < 2**32 |
| +// On exit, in[i] < 2**28 |
| +void Contract(FieldElement* inout) { |
| + FieldElement& out = *inout; |
| + |
| + for (int i = 0; i < 7; i++) { |
|
Wez
2011/11/02 23:46:06
Again, a comment on this block indicating that we'
agl
2011/11/03 17:20:49
Done.
|
| + out[i+1] += out[i] >> 28; |
| + out[i] &= kBottom28Bits; |
| + } |
| + uint32 top = out[7] >> 28; |
| + out[7] &= kBottom28Bits; |
| + |
| + out[0] -= top; |
|
Wez
2011/11/02 23:46:06
... and a comment to indicate that we're then goin
agl
2011/11/03 17:20:49
Done.
|
| + out[3] += top << 12; |
| + |
| + // We may just have made out[0] negative. So we carry down. If we made |
| + // out[0] negative then we know that out[3] is sufficiently positive |
| + // because we just added to it. |
| + for (int i = 0; i < 3; i++) { |
| + uint32 mask = static_cast<uint32>(static_cast<int32>(out[i]) >> 31); |
| + out[i] += (1 << 28) & mask; |
| + out[i+1] -= 1 & mask; |
| + } |
| + |
| + // Now we see if the value is >= p and, if so, subtract p. |
|
Wez
2011/11/02 23:46:06
For what purpose? Is the value in the range 0-2p,
agl
2011/11/03 17:20:49
Done.
|
| + |
| + // First we build a mask from the top four limbs, which must all be |
| + // equal to bottom28Bits if the whole value is >= p. If top4AllOnes |
| + // ends up with any zero bits in the bottom 28 bits, then this wasn't |
| + // true. |
| + uint32 top4AllOnes = 0xffffffffu; |
| + for (int i = 4; i < 8; i++) { |
| + top4AllOnes &= (out[i] & kBottom28Bits) - 1; |
| + } |
| + top4AllOnes |= 0xf0000000; |
| + // Now we replicate any zero bits to all the bits in top4AllOnes. |
| + top4AllOnes &= top4AllOnes >> 16; |
| + top4AllOnes &= top4AllOnes >> 8; |
| + top4AllOnes &= top4AllOnes >> 4; |
| + top4AllOnes &= top4AllOnes >> 2; |
| + top4AllOnes &= top4AllOnes >> 1; |
| + top4AllOnes = |
| + static_cast<uint32>(static_cast<int32>(top4AllOnes << 31) >> 31); |
| + |
| + // Now we test whether the bottom three limbs are non-zero. |
| + uint32 bottom3NonZero = out[0] | out[1] | out[2]; |
| + bottom3NonZero |= bottom3NonZero >> 16; |
| + bottom3NonZero |= bottom3NonZero >> 8; |
| + bottom3NonZero |= bottom3NonZero >> 4; |
| + bottom3NonZero |= bottom3NonZero >> 2; |
| + bottom3NonZero |= bottom3NonZero >> 1; |
| + bottom3NonZero = |
| + static_cast<uint32>(static_cast<int32>(bottom3NonZero << 31) >> 31); |
| + |
| + // Everything depends on the value of out[3]. |
| + // If it's > 0xffff000 and top4AllOnes != 0 then the whole value is >= p |
| + // If it's = 0xffff000 and top4AllOnes != 0 and bottom3NonZero != 0, |
| + // then the whole value is >= p |
| + // If it's < 0xffff000, then the whole value is < p |
| + uint32 n = out[3] - 0xffff000; |
| + uint32 out3Equal = n; |
| + out3Equal |= out3Equal >> 16; |
| + out3Equal |= out3Equal >> 8; |
| + out3Equal |= out3Equal >> 4; |
| + out3Equal |= out3Equal >> 2; |
| + out3Equal |= out3Equal >> 1; |
| + out3Equal = |
| + ~static_cast<uint32>(static_cast<int32>(out3Equal << 31) >> 31); |
| + |
| + // If out[3] > 0xffff000 then n's MSB will be zero. |
| + uint32 out3GT = ~static_cast<uint32>(static_cast<int32>(n << 31) >> 31); |
| + |
| + uint32 mask = top4AllOnes & ((out3Equal & bottom3NonZero) | out3GT); |
| + out[0] -= 1 & mask; |
| + out[3] -= 0xffff000 & mask; |
| + out[4] -= 0xfffffff & mask; |
| + out[5] -= 0xfffffff & mask; |
| + out[6] -= 0xfffffff & mask; |
| + out[7] -= 0xfffffff & mask; |
| +} |
| + |
| + |
| +// Group element functions. |
| +// |
| +// These functions deal with group elements. The group is an elliptic curve |
| +// group with a = -3 defined in FIPS 186-3, section D.2.2. |
| + |
| +typedef crypto::P224::InternalPoint GroupElement; |
| + |
| +// kP is the P224 prime. |
| +const FieldElement kP = { |
| + 1, 0, 0, 268431360, |
| + 268435455, 268435455, 268435455, 268435455, |
| +}; |
| + |
| +// kB is parameter of the elliptic curve. |
| +const FieldElement kB = { |
| + 55967668, 11768882, 265861671, 185302395, |
| + 39211076, 180311059, 84673715, 188764328, |
| +}; |
| + |
| +// AddJacobian computes *out = a+b where a != b. |
|
Wez
2011/11/02 23:46:06
nit: The body of this function may be easier to re
agl
2011/11/03 17:20:49
Done.
|
| +void AddJacobian(GroupElement *out, |
| + const GroupElement& a, |
| + const GroupElement& b) { |
| + // See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl |
| + FieldElement z1z1, z2z2, u1, u2, s1, s2, h, i, j, r, v; |
| + |
| + // Z1Z1 = Z1² |
| + Square(&z1z1, a.z); |
| + // Z2Z2 = Z2² |
| + Square(&z2z2, b.z); |
| + // U1 = X1*Z2Z2 |
| + Mul(&u1, a.x, z2z2); |
| + // U2 = X2*Z1Z1 |
| + Mul(&u2, b.x, z1z1); |
| + // S1 = Y1*Z2*Z2Z2 |
| + Mul(&s1, b.z, z2z2); |
| + Mul(&s1, a.y, s1); |
| + // S2 = Y2*Z1*Z1Z1 |
| + Mul(&s2, a.z, z1z1); |
| + Mul(&s2, b.y, s2); |
| + // H = U2-U1 |
| + Sub(&h, u2, u1); |
| + Reduce(&h); |
| + // I = (2*H)² |
| + for (int j = 0; j < 8; j++) { |
| + i[j] = h[j] << 1; |
| + } |
| + Reduce(&i); |
| + Square(&i, i); |
| + // J = H*I |
| + Mul(&j, h, i); |
| + // r = 2*(S2-S1) |
| + Sub(&r, s2, s1); |
| + Reduce(&r); |
| + for (int i = 0; i < 8; i++) { |
| + r[i] <<= 1; |
| + } |
| + Reduce(&r); |
| + // V = U1*I |
| + Mul(&v, u1, i); |
| + // Z3 = ((Z1+Z2)²-Z1Z1-Z2Z2)*H |
| + Add(&z1z1, z1z1, z2z2); |
| + Add(&z2z2, a.z, b.z); |
| + Reduce(&z2z2); |
| + Square(&z2z2, z2z2); |
| + Sub(&out->z, z2z2, z1z1); |
| + Reduce(&out->z); |
| + Mul(&out->z, out->z, h); |
| + // X3 = r²-J-2*V |
| + for (int i = 0; i < 8; i++) { |
| + z1z1[i] = v[i] << 1; |
| + } |
| + Add(&z1z1, j, z1z1); |
| + Reduce(&z1z1); |
| + Square(&out->x, r); |
| + Sub(&out->x, out->x, z1z1); |
| + Reduce(&out->x); |
| + // Y3 = r*(V-X3)-2*S1*J |
| + for (int i = 0; i < 8; i++) { |
| + s1[i] <<= 1; |
| + } |
| + Mul(&s1, s1, j); |
| + Sub(&z1z1, v, out->x); |
| + Reduce(&z1z1); |
| + Mul(&z1z1, z1z1, r); |
| + Sub(&out->y, z1z1, s1); |
| + Reduce(&out->y); |
| +} |
| + |
| +// DoubleJacobian computes *out = a+a. |
| +void DoubleJacobian(GroupElement* out, const GroupElement& a) { |
| + // See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b |
| + FieldElement delta, gamma, beta, alpha, t; |
| + |
| + Square(&delta, a.z); |
| + Square(&gamma, a.y); |
| + Mul(&beta, a.x, gamma); |
| + |
| + // alpha = 3*(X1-delta)*(X1+delta) |
| + Add(&t, a.x, delta); |
| + for (int i = 0; i < 8; i++) { |
| + t[i] += t[i] << 1; |
| + } |
| + Reduce(&t); |
| + Sub(&alpha, a.x, delta); |
| + Reduce(&alpha); |
| + Mul(&alpha, alpha, t); |
| + |
| + // Z3 = (Y1+Z1)²-gamma-delta |
| + Add(&out->z, a.y, a.z); |
| + Reduce(&out->z); |
| + Square(&out->z, out->z); |
| + Sub(&out->z, out->z, gamma); |
| + Reduce(&out->z); |
| + Sub(&out->z, out->z, delta); |
| + Reduce(&out->z); |
| + |
| + // X3 = alpha²-8*beta |
| + for (int i = 0; i < 8; i++) { |
| + delta[i] = beta[i] << 3; |
| + } |
| + Reduce(&delta); |
| + Square(&out->x, alpha); |
| + Sub(&out->x, out->x, delta); |
| + Reduce(&out->x); |
| + |
| + // Y3 = alpha*(4*beta-X3)-8*gamma² |
| + for (int i = 0; i < 8; i++) { |
| + beta[i] <<= 2; |
| + } |
| + Reduce(&beta); |
| + Sub(&beta, beta, out->x); |
| + Reduce(&beta); |
| + Square(&gamma, gamma); |
| + for (int i = 0; i < 8; i++) { |
| + gamma[i] <<= 3; |
| + } |
| + Reduce(&gamma); |
| + Mul(&out->y, alpha, beta); |
| + Sub(&out->y, out->y, gamma); |
| + Reduce(&out->y); |
| +} |
| + |
| +// CopyConditional sets *out=a if mask is 0xffffffff. mask must be either 0 of |
| +// 0xffffffff. |
| +void CopyConditional(GroupElement* out, |
| + const GroupElement& a, |
| + uint32 mask) { |
| + for (int i = 0; i < 8; i++) { |
| + out->x[i] ^= mask & (a.x[i] ^ out->x[i]); |
| + out->y[i] ^= mask & (a.y[i] ^ out->y[i]); |
| + out->z[i] ^= mask & (a.z[i] ^ out->z[i]); |
| + } |
| +} |
| + |
| +// ScalarMult calculates *out = a*scalar where scalar is a big-endian number of |
| +// length scalar_len and != 0. |
| +void ScalarMult(GroupElement* out, const GroupElement& a, |
| + const uint8* scalar, size_t scalar_len) { |
| + memset(out, 0, sizeof(*out)); |
| + GroupElement tmp; |
| + |
| + uint32 first_bit = 0xffffffff; |
| + for (size_t i = 0; i < scalar_len; i++) { |
| + for (unsigned int bit_num = 0; bit_num < 8; bit_num++) { |
| + DoubleJacobian(out, *out); |
| + uint32 bit = static_cast<uint32>(static_cast<int32>( |
| + (((scalar[i] >> (7 - bit_num)) & 1) << 31) >> 31)); |
| + AddJacobian(&tmp, a, *out); |
| + CopyConditional(out, a, first_bit & bit); |
| + CopyConditional(out, tmp, ~first_bit & bit); |
| + first_bit = first_bit & ~bit; |
| + } |
| + } |
| +} |
| + |
| +// Get224Bits reads 7 words from in and scatters their contents in |
| +// little-endian form into 8 words at out, 28 bits per output word. |
| +void Get224Bits(uint32* out, const uint32* in) { |
| + out[0] = ntohl(in[6]) & kBottom28Bits; |
| + out[1] = ((ntohl(in[5]) << 4) | (ntohl(in[6]) >> 28)) & kBottom28Bits; |
| + out[2] = ((ntohl(in[4]) << 8) | (ntohl(in[5]) >> 24)) & kBottom28Bits; |
| + out[3] = ((ntohl(in[3]) << 12) | (ntohl(in[4]) >> 20)) & kBottom28Bits; |
| + out[4] = ((ntohl(in[2]) << 16) | (ntohl(in[3]) >> 16)) & kBottom28Bits; |
| + out[5] = ((ntohl(in[1]) << 20) | (ntohl(in[2]) >> 12)) & kBottom28Bits; |
| + out[6] = ((ntohl(in[0]) << 24) | (ntohl(in[1]) >> 8)) & kBottom28Bits; |
| + out[7] = (ntohl(in[0]) >> 4) & kBottom28Bits; |
| +} |
| + |
| +// Put224Bits performs the inverse operation to Get224Bits: taking 28 bits from |
| +// each of 8 input words and writing them in big-endian order to 7 words at |
| +// out. |
| +void Put224Bits(uint32* out, const uint32* in) { |
| + out[6] = htonl((in[0] >> 0) | (in[1] << 28)); |
| + out[5] = htonl((in[1] >> 4) | (in[2] << 24)); |
| + out[4] = htonl((in[2] >> 8) | (in[3] << 20)); |
| + out[3] = htonl((in[3] >> 12) | (in[4] << 16)); |
| + out[2] = htonl((in[4] >> 16) | (in[5] << 12)); |
| + out[1] = htonl((in[5] >> 20) | (in[6] << 8)); |
| + out[0] = htonl((in[6] >> 24) | (in[7] << 4)); |
| +} |
| + |
| +} // anonymous namespace |
| + |
| +namespace crypto { |
| + |
| +bool P224::ToInternal(const ExternalPoint& in, InternalPoint* out) { |
| + const uint32* inwords = reinterpret_cast<const uint32*>(in.affine); |
| + Get224Bits(out->x, inwords); |
| + Get224Bits(out->y, inwords + 7); |
| + memset(&out->z, 0, sizeof(out->z)); |
| + out->z[0] = 1; |
| + |
| + // Check that the point is on the curve, i.e. that y² = x³ - 3x + b. |
| + FieldElement lhs; |
| + Square(&lhs, out->y); |
| + Contract(&lhs); |
| + |
| + FieldElement rhs; |
| + Square(&rhs, out->x); |
| + Mul(&rhs, out->x, rhs); |
| + |
| + FieldElement three_x; |
| + for (int i = 0; i < 8; i++) { |
| + three_x[i] = out->x[i] * 3; |
| + } |
| + Reduce(&three_x); |
| + Sub(&rhs, rhs, three_x); |
| + Reduce(&rhs); |
| + |
| + ::Add(&rhs, rhs, kB); |
| + Contract(&rhs); |
| + return memcmp(&lhs, &rhs, sizeof(lhs)) == 0; |
| +} |
| + |
| +void P224::ToExternal(const InternalPoint& in, ExternalPoint* out) { |
| + FieldElement zinv, zinv_sq, x, y; |
| + |
| + Invert(&zinv, in.z); |
| + Square(&zinv_sq, zinv); |
| + Mul(&x, in.x, zinv_sq); |
| + Mul(&zinv_sq, zinv_sq, zinv); |
| + Mul(&y, in.y, zinv_sq); |
| + |
| + Contract(&x); |
| + Contract(&y); |
| + |
| + uint32* outwords = reinterpret_cast<uint32*>(out->affine); |
| + Put224Bits(outwords, x); |
| + Put224Bits(outwords + 7, y); |
| +} |
| + |
| +void P224::ScalarMult(const InternalPoint& in, |
| + const uint8* scalar, |
| + InternalPoint* out) { |
| + ::ScalarMult(out, in, scalar, 28); |
| +} |
| + |
| +static const P224::InternalPoint kBasePoint = { |
| + {22813985, 52956513, 34677300, 203240812, |
| + 12143107, 133374265, 225162431, 191946955}, |
| + {83918388, 223877528, 122119236, 123340192, |
| + 266784067, 263504429, 146143011, 198407736}, |
| + {1, 0, 0, 0, 0, 0, 0, 0}, |
| +}; |
| + |
| +void P224::ScalarBaseMult(const uint8* scalar, InternalPoint* out) { |
| + ::ScalarMult(out, kBasePoint, scalar, 28); |
| +} |
| + |
| +void P224::Add(const InternalPoint& a, const InternalPoint& b, |
| + InternalPoint* out) { |
| + AddJacobian(out, a, b); |
| +} |
| + |
| +void P224::Negate(const InternalPoint& in, InternalPoint* out) { |
| + // Guide to elliptic curve cryptography, page 89 suggests that (X : X+Y : Z) |
| + // is the negative in Jacobian coordinates, but it doesn't actually appear to |
| + // be true in testing so this performs the negation in affine coordinates. |
| + FieldElement zinv, zinv_sq, y; |
| + Invert(&zinv, in.z); |
| + Square(&zinv_sq, zinv); |
| + Mul(&out->x, in.x, zinv_sq); |
| + Mul(&zinv_sq, zinv_sq, zinv); |
| + Mul(&y, in.y, zinv_sq); |
| + |
| + Sub(&out->y, kP, y); |
| + Reduce(&out->y); |
| + |
| + memset(&out->z, 0, sizeof(out->z)); |
| + out->z[0] = 1; |
| +} |
| + |
| +} // namespace crypto |