This source file includes following definitions.
- IsZero
- Add
- Subtract
- ReduceLarge
- Mul
- Square
- Reduce
- Invert
- Contract
- AddJacobian
- DoubleJacobian
- CopyConditional
- ScalarMult
- Get224Bits
- Put224Bits
- SetFromString
- ToString
- ScalarMult
- ScalarBaseMult
- Add
- Negate
#include "crypto/p224.h"
#include <string.h>
#include "base/sys_byteorder.h"
namespace {
using base::HostToNet32;
using base::NetToHost32;
using crypto::p224::FieldElement;
const FieldElement kP = {
1, 0, 0, 268431360,
268435455, 268435455, 268435455, 268435455,
};
void Contract(FieldElement* inout);
uint32 IsZero(const FieldElement& a) {
FieldElement minimal;
memcpy(&minimal, &a, sizeof(minimal));
Contract(&minimal);
uint32 is_zero = 0, is_p = 0;
for (unsigned i = 0; i < 8; i++) {
is_zero |= minimal[i];
is_p |= minimal[i] - kP[i];
}
is_zero |= is_zero >> 16;
is_zero |= is_zero >> 8;
is_zero |= is_zero >> 4;
is_zero |= is_zero >> 2;
is_zero |= is_zero >> 1;
is_p |= is_p >> 16;
is_p |= is_p >> 8;
is_p |= is_p >> 4;
is_p |= is_p >> 2;
is_p |= is_p >> 1;
is_zero &= is_p & 1;
is_zero = (~is_zero) << 31;
is_zero = static_cast<int32>(is_zero) >> 31;
return is_zero;
}
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);
static const FieldElement kZero31ModP = {
kTwo31p3, kTwo31m3, kTwo31m3, kTwo31m15m3,
kTwo31m3, kTwo31m3, kTwo31m3, kTwo31m3
};
void Subtract(FieldElement* out, const FieldElement& a, const FieldElement& b) {
for (int i = 0; i < 8; i++) {
(*out)[i] = a[i] + kZero31ModP[i] - b[i];
}
}
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);
static const uint64 kZero63ModP[8] = {
kTwo63p35, kTwo63m35, kTwo63m35, kTwo63m35,
kTwo63m35m19, kTwo63m35, kTwo63m35, kTwo63m35,
};
static const uint32 kBottom28Bits = 0xfffffff;
typedef uint64 LargeFieldElement[15];
void ReduceLarge(FieldElement* out, LargeFieldElement* inptr) {
LargeFieldElement& in(*inptr);
for (int i = 0; i < 8; i++) {
in[i] += kZero63ModP[i];
}
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;
}
in[8] = 0;
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);
(*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);
}
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);
}
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);
}
void Reduce(FieldElement* in_out) {
FieldElement& a = *in_out;
for (int i = 0; i < 7; i++) {
a[i+1] += a[i] >> 28;
a[i] &= kBottom28Bits;
}
uint32 top = a[7] >> 28;
a[7] &= kBottom28Bits;
uint32 mask = top;
mask |= mask >> 2;
mask |= mask >> 1;
mask <<= 31;
mask = static_cast<uint32>(static_cast<int32>(mask) >> 31);
a[0] -= top;
a[3] += top << 12;
a[3] -= 1 & mask;
a[2] += mask & ((1<<28) - 1);
a[1] += mask & ((1<<28) - 1);
a[0] += mask & (1<<28);
}
void Invert(FieldElement* out, const FieldElement& in) {
FieldElement f1, f2, f3, f4;
Square(&f1, in);
Mul(&f1, f1, in);
Square(&f1, f1);
Mul(&f1, f1, in);
Square(&f2, f1);
Square(&f2, f2);
Square(&f2, f2);
Mul(&f1, f1, f2);
Square(&f2, f1);
for (int i = 0; i < 5; i++) {
Square(&f2, f2);
}
Mul(&f2, f2, f1);
Square(&f3, f2);
for (int i = 0; i < 11; i++) {
Square(&f3, f3);
}
Mul(&f2, f3, f2);
Square(&f3, f2);
for (int i = 0; i < 23; i++) {
Square(&f3, f3);
}
Mul(&f3, f3, f2);
Square(&f4, f3);
for (int i = 0; i < 47; i++) {
Square(&f4, f4);
}
Mul(&f3, f3, f4);
Square(&f4, f3);
for (int i = 0; i < 23; i++) {
Square(&f4, f4);
}
Mul(&f2, f4, f2);
for (int i = 0; i < 6; i++) {
Square(&f2, f2);
}
Mul(&f1, f1, f2);
Square(&f1, f1);
Mul(&f1, f1, in);
for (int i = 0; i < 97; i++) {
Square(&f1, f1);
}
Mul(out, f1, f3);
}
void Contract(FieldElement* inout) {
FieldElement& out = *inout;
for (int i = 0; i < 7; i++) {
out[i+1] += out[i] >> 28;
out[i] &= kBottom28Bits;
}
uint32 top = out[7] >> 28;
out[7] &= kBottom28Bits;
out[0] -= top;
out[3] += top << 12;
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;
}
for (int i = 3; i < 7; i++) {
out[i+1] += out[i] >> 28;
out[i] &= kBottom28Bits;
}
top = out[7] >> 28;
out[7] &= kBottom28Bits;
out[0] -= top;
out[3] += top << 12;
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;
}
uint32 top_4_all_ones = 0xffffffffu;
for (int i = 4; i < 8; i++) {
top_4_all_ones &= out[i];
}
top_4_all_ones |= 0xf0000000;
top_4_all_ones &= top_4_all_ones >> 16;
top_4_all_ones &= top_4_all_ones >> 8;
top_4_all_ones &= top_4_all_ones >> 4;
top_4_all_ones &= top_4_all_ones >> 2;
top_4_all_ones &= top_4_all_ones >> 1;
top_4_all_ones =
static_cast<uint32>(static_cast<int32>(top_4_all_ones << 31) >> 31);
uint32 bottom_3_non_zero = out[0] | out[1] | out[2];
bottom_3_non_zero |= bottom_3_non_zero >> 16;
bottom_3_non_zero |= bottom_3_non_zero >> 8;
bottom_3_non_zero |= bottom_3_non_zero >> 4;
bottom_3_non_zero |= bottom_3_non_zero >> 2;
bottom_3_non_zero |= bottom_3_non_zero >> 1;
bottom_3_non_zero =
static_cast<uint32>(static_cast<int32>(bottom_3_non_zero) >> 31);
uint32 n = out[3] - 0xffff000;
uint32 out_3_equal = n;
out_3_equal |= out_3_equal >> 16;
out_3_equal |= out_3_equal >> 8;
out_3_equal |= out_3_equal >> 4;
out_3_equal |= out_3_equal >> 2;
out_3_equal |= out_3_equal >> 1;
out_3_equal =
~static_cast<uint32>(static_cast<int32>(out_3_equal << 31) >> 31);
uint32 out_3_gt = ~static_cast<uint32>(static_cast<int32>(n << 31) >> 31);
uint32 mask = top_4_all_ones & ((out_3_equal & bottom_3_non_zero) | out_3_gt);
out[0] -= 1 & mask;
out[3] -= 0xffff000 & mask;
out[4] -= 0xfffffff & mask;
out[5] -= 0xfffffff & mask;
out[6] -= 0xfffffff & mask;
out[7] -= 0xfffffff & mask;
}
using crypto::p224::Point;
const FieldElement kB = {
55967668, 11768882, 265861671, 185302395,
39211076, 180311059, 84673715, 188764328,
};
void CopyConditional(Point* out, const Point& a, uint32 mask);
void DoubleJacobian(Point* out, const Point& a);
void AddJacobian(Point *out,
const Point& a,
const Point& b) {
FieldElement z1z1, z2z2, u1, u2, s1, s2, h, i, j, r, v;
uint32 z1_is_zero = IsZero(a.z);
uint32 z2_is_zero = IsZero(b.z);
Square(&z1z1, a.z);
Square(&z2z2, b.z);
Mul(&u1, a.x, z2z2);
Mul(&u2, b.x, z1z1);
Mul(&s1, b.z, z2z2);
Mul(&s1, a.y, s1);
Mul(&s2, a.z, z1z1);
Mul(&s2, b.y, s2);
Subtract(&h, u2, u1);
Reduce(&h);
uint32 x_equal = IsZero(h);
for (int j = 0; j < 8; j++) {
i[j] = h[j] << 1;
}
Reduce(&i);
Square(&i, i);
Mul(&j, h, i);
Subtract(&r, s2, s1);
Reduce(&r);
uint32 y_equal = IsZero(r);
if (x_equal && y_equal && !z1_is_zero && !z2_is_zero) {
DoubleJacobian(out, a);
return;
}
for (int i = 0; i < 8; i++) {
r[i] <<= 1;
}
Reduce(&r);
Mul(&v, u1, i);
Add(&z1z1, z1z1, z2z2);
Add(&z2z2, a.z, b.z);
Reduce(&z2z2);
Square(&z2z2, z2z2);
Subtract(&out->z, z2z2, z1z1);
Reduce(&out->z);
Mul(&out->z, out->z, h);
for (int i = 0; i < 8; i++) {
z1z1[i] = v[i] << 1;
}
Add(&z1z1, j, z1z1);
Reduce(&z1z1);
Square(&out->x, r);
Subtract(&out->x, out->x, z1z1);
Reduce(&out->x);
for (int i = 0; i < 8; i++) {
s1[i] <<= 1;
}
Mul(&s1, s1, j);
Subtract(&z1z1, v, out->x);
Reduce(&z1z1);
Mul(&z1z1, z1z1, r);
Subtract(&out->y, z1z1, s1);
Reduce(&out->y);
CopyConditional(out, a, z2_is_zero);
CopyConditional(out, b, z1_is_zero);
}
void DoubleJacobian(Point* out, const Point& a) {
FieldElement delta, gamma, beta, alpha, t;
Square(&delta, a.z);
Square(&gamma, a.y);
Mul(&beta, a.x, gamma);
Add(&t, a.x, delta);
for (int i = 0; i < 8; i++) {
t[i] += t[i] << 1;
}
Reduce(&t);
Subtract(&alpha, a.x, delta);
Reduce(&alpha);
Mul(&alpha, alpha, t);
Add(&out->z, a.y, a.z);
Reduce(&out->z);
Square(&out->z, out->z);
Subtract(&out->z, out->z, gamma);
Reduce(&out->z);
Subtract(&out->z, out->z, delta);
Reduce(&out->z);
for (int i = 0; i < 8; i++) {
delta[i] = beta[i] << 3;
}
Reduce(&delta);
Square(&out->x, alpha);
Subtract(&out->x, out->x, delta);
Reduce(&out->x);
for (int i = 0; i < 8; i++) {
beta[i] <<= 2;
}
Reduce(&beta);
Subtract(&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);
Subtract(&out->y, out->y, gamma);
Reduce(&out->y);
}
void CopyConditional(Point* out,
const Point& 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]);
}
}
void ScalarMult(Point* out, const Point& a,
const uint8* scalar, size_t scalar_len) {
memset(out, 0, sizeof(*out));
Point tmp;
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, tmp, bit);
}
}
}
void Get224Bits(uint32* out, const uint32* in) {
out[0] = NetToHost32(in[6]) & kBottom28Bits;
out[1] = ((NetToHost32(in[5]) << 4) |
(NetToHost32(in[6]) >> 28)) & kBottom28Bits;
out[2] = ((NetToHost32(in[4]) << 8) |
(NetToHost32(in[5]) >> 24)) & kBottom28Bits;
out[3] = ((NetToHost32(in[3]) << 12) |
(NetToHost32(in[4]) >> 20)) & kBottom28Bits;
out[4] = ((NetToHost32(in[2]) << 16) |
(NetToHost32(in[3]) >> 16)) & kBottom28Bits;
out[5] = ((NetToHost32(in[1]) << 20) |
(NetToHost32(in[2]) >> 12)) & kBottom28Bits;
out[6] = ((NetToHost32(in[0]) << 24) |
(NetToHost32(in[1]) >> 8)) & kBottom28Bits;
out[7] = (NetToHost32(in[0]) >> 4) & kBottom28Bits;
}
void Put224Bits(uint32* out, const uint32* in) {
out[6] = HostToNet32((in[0] >> 0) | (in[1] << 28));
out[5] = HostToNet32((in[1] >> 4) | (in[2] << 24));
out[4] = HostToNet32((in[2] >> 8) | (in[3] << 20));
out[3] = HostToNet32((in[3] >> 12) | (in[4] << 16));
out[2] = HostToNet32((in[4] >> 16) | (in[5] << 12));
out[1] = HostToNet32((in[5] >> 20) | (in[6] << 8));
out[0] = HostToNet32((in[6] >> 24) | (in[7] << 4));
}
}
namespace crypto {
namespace p224 {
bool Point::SetFromString(const base::StringPiece& in) {
if (in.size() != 2*28)
return false;
const uint32* inwords = reinterpret_cast<const uint32*>(in.data());
Get224Bits(x, inwords);
Get224Bits(y, inwords + 7);
memset(&z, 0, sizeof(z));
z[0] = 1;
FieldElement lhs;
Square(&lhs, y);
Contract(&lhs);
FieldElement rhs;
Square(&rhs, x);
Mul(&rhs, x, rhs);
FieldElement three_x;
for (int i = 0; i < 8; i++) {
three_x[i] = x[i] * 3;
}
Reduce(&three_x);
Subtract(&rhs, rhs, three_x);
Reduce(&rhs);
::Add(&rhs, rhs, kB);
Contract(&rhs);
return memcmp(&lhs, &rhs, sizeof(lhs)) == 0;
}
std::string Point::ToString() const {
FieldElement zinv, zinv_sq, x, y;
if (IsZero(this->z)) {
static const char zeros[56] = {0};
return std::string(zeros, sizeof(zeros));
}
Invert(&zinv, this->z);
Square(&zinv_sq, zinv);
Mul(&x, this->x, zinv_sq);
Mul(&zinv_sq, zinv_sq, zinv);
Mul(&y, this->y, zinv_sq);
Contract(&x);
Contract(&y);
uint32 outwords[14];
Put224Bits(outwords, x);
Put224Bits(outwords + 7, y);
return std::string(reinterpret_cast<const char*>(outwords), sizeof(outwords));
}
void ScalarMult(const Point& in, const uint8* scalar, Point* out) {
::ScalarMult(out, in, scalar, 28);
}
static const Point 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 ScalarBaseMult(const uint8* scalar, Point* out) {
::ScalarMult(out, kBasePoint, scalar, 28);
}
void Add(const Point& a, const Point& b, Point* out) {
AddJacobian(out, a, b);
}
void Negate(const Point& in, Point* out) {
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);
Subtract(&out->y, kP, y);
Reduce(&out->y);
memset(&out->z, 0, sizeof(out->z));
out->z[0] = 1;
}
}
}