| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This is an implementation of the P224 elliptic curve group. It's written to | 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. | 6 // be short and simple rather than fast, although it's still constant-time. |
| 7 // | 7 // |
| 8 // See http://www.imperialviolet.org/2010/12/04/ecc.html ([1]) for background. | 8 // See http://www.imperialviolet.org/2010/12/04/ecc.html ([1]) for background. |
| 9 | 9 |
| 10 #include "crypto/p224.h" | 10 #include "crypto/p224.h" |
| 11 | 11 |
| 12 #include <stddef.h> |
| 13 #include <stdint.h> |
| 12 #include <string.h> | 14 #include <string.h> |
| 13 | 15 |
| 14 #include "base/sys_byteorder.h" | 16 #include "base/sys_byteorder.h" |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 | 19 |
| 18 using base::HostToNet32; | 20 using base::HostToNet32; |
| 19 using base::NetToHost32; | 21 using base::NetToHost32; |
| 20 | 22 |
| 21 // Field element functions. | 23 // Field element functions. |
| 22 // | 24 // |
| 23 // The field that we're dealing with is ℤ/pℤ where p = 2**224 - 2**96 + 1. | 25 // The field that we're dealing with is ℤ/pℤ where p = 2**224 - 2**96 + 1. |
| 24 // | 26 // |
| 25 // Field elements are represented by a FieldElement, which is a typedef to an | 27 // 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: | 28 // array of 8 uint32_t's. The value of a FieldElement, a, is: |
| 27 // a[0] + 2**28·a[1] + 2**56·a[1] + ... + 2**196·a[7] | 29 // a[0] + 2**28·a[1] + 2**56·a[1] + ... + 2**196·a[7] |
| 28 // | 30 // |
| 29 // Using 28-bit limbs means that there's only 4 bits of headroom, which is less | 31 // 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 | 32 // 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 // exactly, making the reflections during a reduce much nicer. |
| 32 | 34 |
| 33 using crypto::p224::FieldElement; | 35 using crypto::p224::FieldElement; |
| 34 | 36 |
| 35 // kP is the P224 prime. | 37 // kP is the P224 prime. |
| 36 const FieldElement kP = { | 38 const FieldElement kP = { |
| 37 1, 0, 0, 268431360, | 39 1, 0, 0, 268431360, |
| 38 268435455, 268435455, 268435455, 268435455, | 40 268435455, 268435455, 268435455, 268435455, |
| 39 }; | 41 }; |
| 40 | 42 |
| 41 void Contract(FieldElement* inout); | 43 void Contract(FieldElement* inout); |
| 42 | 44 |
| 43 // IsZero returns 0xffffffff if a == 0 mod p and 0 otherwise. | 45 // IsZero returns 0xffffffff if a == 0 mod p and 0 otherwise. |
| 44 uint32 IsZero(const FieldElement& a) { | 46 uint32_t IsZero(const FieldElement& a) { |
| 45 FieldElement minimal; | 47 FieldElement minimal; |
| 46 memcpy(&minimal, &a, sizeof(minimal)); | 48 memcpy(&minimal, &a, sizeof(minimal)); |
| 47 Contract(&minimal); | 49 Contract(&minimal); |
| 48 | 50 |
| 49 uint32 is_zero = 0, is_p = 0; | 51 uint32_t is_zero = 0, is_p = 0; |
| 50 for (unsigned i = 0; i < 8; i++) { | 52 for (unsigned i = 0; i < 8; i++) { |
| 51 is_zero |= minimal[i]; | 53 is_zero |= minimal[i]; |
| 52 is_p |= minimal[i] - kP[i]; | 54 is_p |= minimal[i] - kP[i]; |
| 53 } | 55 } |
| 54 | 56 |
| 55 // If either is_zero or is_p is 0, then we should return 1. | 57 // If either is_zero or is_p is 0, then we should return 1. |
| 56 is_zero |= is_zero >> 16; | 58 is_zero |= is_zero >> 16; |
| 57 is_zero |= is_zero >> 8; | 59 is_zero |= is_zero >> 8; |
| 58 is_zero |= is_zero >> 4; | 60 is_zero |= is_zero >> 4; |
| 59 is_zero |= is_zero >> 2; | 61 is_zero |= is_zero >> 2; |
| 60 is_zero |= is_zero >> 1; | 62 is_zero |= is_zero >> 1; |
| 61 | 63 |
| 62 is_p |= is_p >> 16; | 64 is_p |= is_p >> 16; |
| 63 is_p |= is_p >> 8; | 65 is_p |= is_p >> 8; |
| 64 is_p |= is_p >> 4; | 66 is_p |= is_p >> 4; |
| 65 is_p |= is_p >> 2; | 67 is_p |= is_p >> 2; |
| 66 is_p |= is_p >> 1; | 68 is_p |= is_p >> 1; |
| 67 | 69 |
| 68 // For is_zero and is_p, the LSB is 0 iff all the bits are zero. | 70 // For is_zero and is_p, the LSB is 0 iff all the bits are zero. |
| 69 is_zero &= is_p & 1; | 71 is_zero &= is_p & 1; |
| 70 is_zero = (~is_zero) << 31; | 72 is_zero = (~is_zero) << 31; |
| 71 is_zero = static_cast<int32>(is_zero) >> 31; | 73 is_zero = static_cast<int32_t>(is_zero) >> 31; |
| 72 return is_zero; | 74 return is_zero; |
| 73 } | 75 } |
| 74 | 76 |
| 75 // Add computes *out = a+b | 77 // Add computes *out = a+b |
| 76 // | 78 // |
| 77 // a[i] + b[i] < 2**32 | 79 // a[i] + b[i] < 2**32 |
| 78 void Add(FieldElement* out, const FieldElement& a, const FieldElement& b) { | 80 void Add(FieldElement* out, const FieldElement& a, const FieldElement& b) { |
| 79 for (int i = 0; i < 8; i++) { | 81 for (int i = 0; i < 8; i++) { |
| 80 (*out)[i] = a[i] + b[i]; | 82 (*out)[i] = a[i] + b[i]; |
| 81 } | 83 } |
| 82 } | 84 } |
| 83 | 85 |
| 84 static const uint32 kTwo31p3 = (1u<<31) + (1u<<3); | 86 static const uint32_t kTwo31p3 = (1u << 31) + (1u << 3); |
| 85 static const uint32 kTwo31m3 = (1u<<31) - (1u<<3); | 87 static const uint32_t kTwo31m3 = (1u << 31) - (1u << 3); |
| 86 static const uint32 kTwo31m15m3 = (1u<<31) - (1u<<15) - (1u<<3); | 88 static const uint32_t 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 | 89 // 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 // subtract smaller amounts without underflow. See the section "Subtraction" in |
| 89 // [1] for why. | 91 // [1] for why. |
| 90 static const FieldElement kZero31ModP = { | 92 static const FieldElement kZero31ModP = { |
| 91 kTwo31p3, kTwo31m3, kTwo31m3, kTwo31m15m3, | 93 kTwo31p3, kTwo31m3, kTwo31m3, kTwo31m15m3, |
| 92 kTwo31m3, kTwo31m3, kTwo31m3, kTwo31m3 | 94 kTwo31m3, kTwo31m3, kTwo31m3, kTwo31m3 |
| 93 }; | 95 }; |
| 94 | 96 |
| 95 // Subtract computes *out = a-b | 97 // Subtract computes *out = a-b |
| 96 // | 98 // |
| 97 // a[i], b[i] < 2**30 | 99 // a[i], b[i] < 2**30 |
| 98 // out[i] < 2**32 | 100 // out[i] < 2**32 |
| 99 void Subtract(FieldElement* out, const FieldElement& a, const FieldElement& b) { | 101 void Subtract(FieldElement* out, const FieldElement& a, const FieldElement& b) { |
| 100 for (int i = 0; i < 8; i++) { | 102 for (int i = 0; i < 8; i++) { |
| 101 // See the section on "Subtraction" in [1] for details. | 103 // See the section on "Subtraction" in [1] for details. |
| 102 (*out)[i] = a[i] + kZero31ModP[i] - b[i]; | 104 (*out)[i] = a[i] + kZero31ModP[i] - b[i]; |
| 103 } | 105 } |
| 104 } | 106 } |
| 105 | 107 |
| 106 static const uint64 kTwo63p35 = (1ull<<63) + (1ull<<35); | 108 static const uint64_t kTwo63p35 = (1ull << 63) + (1ull << 35); |
| 107 static const uint64 kTwo63m35 = (1ull<<63) - (1ull<<35); | 109 static const uint64_t kTwo63m35 = (1ull << 63) - (1ull << 35); |
| 108 static const uint64 kTwo63m35m19 = (1ull<<63) - (1ull<<35) - (1ull<<19); | 110 static const uint64_t kTwo63m35m19 = (1ull << 63) - (1ull << 35) - (1ull << 19); |
| 109 // kZero63ModP is 0 mod p where bit 63 is set in all limbs. See the section | 111 // kZero63ModP is 0 mod p where bit 63 is set in all limbs. See the section |
| 110 // "Subtraction" in [1] for why. | 112 // "Subtraction" in [1] for why. |
| 111 static const uint64 kZero63ModP[8] = { | 113 static const uint64_t kZero63ModP[8] = { |
| 112 kTwo63p35, kTwo63m35, kTwo63m35, kTwo63m35, | 114 kTwo63p35, kTwo63m35, kTwo63m35, kTwo63m35, |
| 113 kTwo63m35m19, kTwo63m35, kTwo63m35, kTwo63m35, | 115 kTwo63m35m19, kTwo63m35, kTwo63m35, kTwo63m35, |
| 114 }; | 116 }; |
| 115 | 117 |
| 116 static const uint32 kBottom28Bits = 0xfffffff; | 118 static const uint32_t kBottom28Bits = 0xfffffff; |
| 117 | 119 |
| 118 // LargeFieldElement also represents an element of the field. The limbs are | 120 // 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 | 121 // 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. | 122 // 0, 28, 56, ..., 392 bits, each 64-bits wide. |
| 121 typedef uint64 LargeFieldElement[15]; | 123 typedef uint64_t LargeFieldElement[15]; |
| 122 | 124 |
| 123 // ReduceLarge converts a LargeFieldElement to a FieldElement. | 125 // ReduceLarge converts a LargeFieldElement to a FieldElement. |
| 124 // | 126 // |
| 125 // in[i] < 2**62 | 127 // in[i] < 2**62 |
| 126 void ReduceLarge(FieldElement* out, LargeFieldElement* inptr) { | 128 void ReduceLarge(FieldElement* out, LargeFieldElement* inptr) { |
| 127 LargeFieldElement& in(*inptr); | 129 LargeFieldElement& in(*inptr); |
| 128 | 130 |
| 129 for (int i = 0; i < 8; i++) { | 131 for (int i = 0; i < 8; i++) { |
| 130 in[i] += kZero63ModP[i]; | 132 in[i] += kZero63ModP[i]; |
| 131 } | 133 } |
| 132 | 134 |
| 133 // Eliminate the coefficients at 2**224 and greater while maintaining the | 135 // Eliminate the coefficients at 2**224 and greater while maintaining the |
| 134 // same value mod p. | 136 // same value mod p. |
| 135 for (int i = 14; i >= 8; i--) { | 137 for (int i = 14; i >= 8; i--) { |
| 136 in[i-8] -= in[i]; // reflection off the "+1" term of p. | 138 in[i-8] -= in[i]; // reflection off the "+1" term of p. |
| 137 in[i-5] += (in[i] & 0xffff) << 12; // part of the "-2**96" reflection. | 139 in[i-5] += (in[i] & 0xffff) << 12; // part of the "-2**96" reflection. |
| 138 in[i-4] += in[i] >> 16; // the rest of the "-2**96" reflection. | 140 in[i-4] += in[i] >> 16; // the rest of the "-2**96" reflection. |
| 139 } | 141 } |
| 140 in[8] = 0; | 142 in[8] = 0; |
| 141 // in[0..8] < 2**64 | 143 // in[0..8] < 2**64 |
| 142 | 144 |
| 143 // As the values become small enough, we start to store them in |out| and use | 145 // As the values become small enough, we start to store them in |out| and use |
| 144 // 32-bit operations. | 146 // 32-bit operations. |
| 145 for (int i = 1; i < 8; i++) { | 147 for (int i = 1; i < 8; i++) { |
| 146 in[i+1] += in[i] >> 28; | 148 in[i+1] += in[i] >> 28; |
| 147 (*out)[i] = static_cast<uint32>(in[i] & kBottom28Bits); | 149 (*out)[i] = static_cast<uint32_t>(in[i] & kBottom28Bits); |
| 148 } | 150 } |
| 149 // Eliminate the term at 2*224 that we introduced while keeping the same | 151 // Eliminate the term at 2*224 that we introduced while keeping the same |
| 150 // value mod p. | 152 // value mod p. |
| 151 in[0] -= in[8]; // reflection off the "+1" term of p. | 153 in[0] -= in[8]; // reflection off the "+1" term of p. |
| 152 (*out)[3] += static_cast<uint32>(in[8] & 0xffff) << 12; // "-2**96" term | 154 (*out)[3] += static_cast<uint32_t>(in[8] & 0xffff) << 12; // "-2**96" term |
| 153 (*out)[4] += static_cast<uint32>(in[8] >> 16); // rest of "-2**96" term | 155 (*out)[4] += static_cast<uint32_t>(in[8] >> 16); // rest of "-2**96" term |
| 154 // in[0] < 2**64 | 156 // in[0] < 2**64 |
| 155 // out[3] < 2**29 | 157 // out[3] < 2**29 |
| 156 // out[4] < 2**29 | 158 // out[4] < 2**29 |
| 157 // out[1,2,5..7] < 2**28 | 159 // out[1,2,5..7] < 2**28 |
| 158 | 160 |
| 159 (*out)[0] = static_cast<uint32>(in[0] & kBottom28Bits); | 161 (*out)[0] = static_cast<uint32_t>(in[0] & kBottom28Bits); |
| 160 (*out)[1] += static_cast<uint32>((in[0] >> 28) & kBottom28Bits); | 162 (*out)[1] += static_cast<uint32_t>((in[0] >> 28) & kBottom28Bits); |
| 161 (*out)[2] += static_cast<uint32>(in[0] >> 56); | 163 (*out)[2] += static_cast<uint32_t>(in[0] >> 56); |
| 162 // out[0] < 2**28 | 164 // out[0] < 2**28 |
| 163 // out[1..4] < 2**29 | 165 // out[1..4] < 2**29 |
| 164 // out[5..7] < 2**28 | 166 // out[5..7] < 2**28 |
| 165 } | 167 } |
| 166 | 168 |
| 167 // Mul computes *out = a*b | 169 // Mul computes *out = a*b |
| 168 // | 170 // |
| 169 // a[i] < 2**29, b[i] < 2**30 (or vice versa) | 171 // a[i] < 2**29, b[i] < 2**30 (or vice versa) |
| 170 // out[i] < 2**29 | 172 // out[i] < 2**29 |
| 171 void Mul(FieldElement* out, const FieldElement& a, const FieldElement& b) { | 173 void Mul(FieldElement* out, const FieldElement& a, const FieldElement& b) { |
| 172 LargeFieldElement tmp; | 174 LargeFieldElement tmp; |
| 173 memset(&tmp, 0, sizeof(tmp)); | 175 memset(&tmp, 0, sizeof(tmp)); |
| 174 | 176 |
| 175 for (int i = 0; i < 8; i++) { | 177 for (int i = 0; i < 8; i++) { |
| 176 for (int j = 0; j < 8; j++) { | 178 for (int j = 0; j < 8; j++) { |
| 177 tmp[i+j] += static_cast<uint64>(a[i]) * static_cast<uint64>(b[j]); | 179 tmp[i + j] += static_cast<uint64_t>(a[i]) * static_cast<uint64_t>(b[j]); |
| 178 } | 180 } |
| 179 } | 181 } |
| 180 | 182 |
| 181 ReduceLarge(out, &tmp); | 183 ReduceLarge(out, &tmp); |
| 182 } | 184 } |
| 183 | 185 |
| 184 // Square computes *out = a*a | 186 // Square computes *out = a*a |
| 185 // | 187 // |
| 186 // a[i] < 2**29 | 188 // a[i] < 2**29 |
| 187 // out[i] < 2**29 | 189 // out[i] < 2**29 |
| 188 void Square(FieldElement* out, const FieldElement& a) { | 190 void Square(FieldElement* out, const FieldElement& a) { |
| 189 LargeFieldElement tmp; | 191 LargeFieldElement tmp; |
| 190 memset(&tmp, 0, sizeof(tmp)); | 192 memset(&tmp, 0, sizeof(tmp)); |
| 191 | 193 |
| 192 for (int i = 0; i < 8; i++) { | 194 for (int i = 0; i < 8; i++) { |
| 193 for (int j = 0; j <= i; j++) { | 195 for (int j = 0; j <= i; j++) { |
| 194 uint64 r = static_cast<uint64>(a[i]) * static_cast<uint64>(a[j]); | 196 uint64_t r = static_cast<uint64_t>(a[i]) * static_cast<uint64_t>(a[j]); |
| 195 if (i == j) { | 197 if (i == j) { |
| 196 tmp[i+j] += r; | 198 tmp[i+j] += r; |
| 197 } else { | 199 } else { |
| 198 tmp[i+j] += r << 1; | 200 tmp[i+j] += r << 1; |
| 199 } | 201 } |
| 200 } | 202 } |
| 201 } | 203 } |
| 202 | 204 |
| 203 ReduceLarge(out, &tmp); | 205 ReduceLarge(out, &tmp); |
| 204 } | 206 } |
| 205 | 207 |
| 206 // Reduce reduces the coefficients of in_out to smaller bounds. | 208 // Reduce reduces the coefficients of in_out to smaller bounds. |
| 207 // | 209 // |
| 208 // On entry: a[i] < 2**31 + 2**30 | 210 // On entry: a[i] < 2**31 + 2**30 |
| 209 // On exit: a[i] < 2**29 | 211 // On exit: a[i] < 2**29 |
| 210 void Reduce(FieldElement* in_out) { | 212 void Reduce(FieldElement* in_out) { |
| 211 FieldElement& a = *in_out; | 213 FieldElement& a = *in_out; |
| 212 | 214 |
| 213 for (int i = 0; i < 7; i++) { | 215 for (int i = 0; i < 7; i++) { |
| 214 a[i+1] += a[i] >> 28; | 216 a[i+1] += a[i] >> 28; |
| 215 a[i] &= kBottom28Bits; | 217 a[i] &= kBottom28Bits; |
| 216 } | 218 } |
| 217 uint32 top = a[7] >> 28; | 219 uint32_t top = a[7] >> 28; |
| 218 a[7] &= kBottom28Bits; | 220 a[7] &= kBottom28Bits; |
| 219 | 221 |
| 220 // top < 2**4 | 222 // top < 2**4 |
| 221 // Constant-time: mask = (top != 0) ? 0xffffffff : 0 | 223 // Constant-time: mask = (top != 0) ? 0xffffffff : 0 |
| 222 uint32 mask = top; | 224 uint32_t mask = top; |
| 223 mask |= mask >> 2; | 225 mask |= mask >> 2; |
| 224 mask |= mask >> 1; | 226 mask |= mask >> 1; |
| 225 mask <<= 31; | 227 mask <<= 31; |
| 226 mask = static_cast<uint32>(static_cast<int32>(mask) >> 31); | 228 mask = static_cast<uint32_t>(static_cast<int32_t>(mask) >> 31); |
| 227 | 229 |
| 228 // Eliminate top while maintaining the same value mod p. | 230 // Eliminate top while maintaining the same value mod p. |
| 229 a[0] -= top; | 231 a[0] -= top; |
| 230 a[3] += top << 12; | 232 a[3] += top << 12; |
| 231 | 233 |
| 232 // We may have just made a[0] negative but, if we did, then we must | 234 // We may have just made a[0] negative but, if we did, then we must |
| 233 // have added something to a[3], thus it's > 2**12. Therefore we can | 235 // have added something to a[3], thus it's > 2**12. Therefore we can |
| 234 // carry down to a[0]. | 236 // carry down to a[0]. |
| 235 a[3] -= 1 & mask; | 237 a[3] -= 1 & mask; |
| 236 a[2] += mask & ((1<<28) - 1); | 238 a[2] += mask & ((1<<28) - 1); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 // On entry, in[i] < 2**29 | 295 // On entry, in[i] < 2**29 |
| 294 // On exit, in[i] < 2**28 | 296 // On exit, in[i] < 2**28 |
| 295 void Contract(FieldElement* inout) { | 297 void Contract(FieldElement* inout) { |
| 296 FieldElement& out = *inout; | 298 FieldElement& out = *inout; |
| 297 | 299 |
| 298 // Reduce the coefficients to < 2**28. | 300 // Reduce the coefficients to < 2**28. |
| 299 for (int i = 0; i < 7; i++) { | 301 for (int i = 0; i < 7; i++) { |
| 300 out[i+1] += out[i] >> 28; | 302 out[i+1] += out[i] >> 28; |
| 301 out[i] &= kBottom28Bits; | 303 out[i] &= kBottom28Bits; |
| 302 } | 304 } |
| 303 uint32 top = out[7] >> 28; | 305 uint32_t top = out[7] >> 28; |
| 304 out[7] &= kBottom28Bits; | 306 out[7] &= kBottom28Bits; |
| 305 | 307 |
| 306 // Eliminate top while maintaining the same value mod p. | 308 // Eliminate top while maintaining the same value mod p. |
| 307 out[0] -= top; | 309 out[0] -= top; |
| 308 out[3] += top << 12; | 310 out[3] += top << 12; |
| 309 | 311 |
| 310 // We may just have made out[0] negative. So we carry down. If we made | 312 // We may just have made out[0] negative. So we carry down. If we made |
| 311 // out[0] negative then we know that out[3] is sufficiently positive | 313 // out[0] negative then we know that out[3] is sufficiently positive |
| 312 // because we just added to it. | 314 // because we just added to it. |
| 313 for (int i = 0; i < 3; i++) { | 315 for (int i = 0; i < 3; i++) { |
| 314 uint32 mask = static_cast<uint32>(static_cast<int32>(out[i]) >> 31); | 316 uint32_t mask = static_cast<uint32_t>(static_cast<int32_t>(out[i]) >> 31); |
| 315 out[i] += (1 << 28) & mask; | 317 out[i] += (1 << 28) & mask; |
| 316 out[i+1] -= 1 & mask; | 318 out[i+1] -= 1 & mask; |
| 317 } | 319 } |
| 318 | 320 |
| 319 // We might have pushed out[3] over 2**28 so we perform another, partial | 321 // We might have pushed out[3] over 2**28 so we perform another, partial |
| 320 // carry chain. | 322 // carry chain. |
| 321 for (int i = 3; i < 7; i++) { | 323 for (int i = 3; i < 7; i++) { |
| 322 out[i+1] += out[i] >> 28; | 324 out[i+1] += out[i] >> 28; |
| 323 out[i] &= kBottom28Bits; | 325 out[i] &= kBottom28Bits; |
| 324 } | 326 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 337 // The first value of top was in [0..16), therefore, prior to eliminating | 339 // The first value of top was in [0..16), therefore, prior to eliminating |
| 338 // the first top, 0xfff1000 <= out[3] <= 0xfffffff. Therefore, after | 340 // the first top, 0xfff1000 <= out[3] <= 0xfffffff. Therefore, after |
| 339 // overflowing and being reduced by the second carry chain, out[3] <= | 341 // overflowing and being reduced by the second carry chain, out[3] <= |
| 340 // 0xf000. Thus it cannot have overflowed when we eliminated top for the | 342 // 0xf000. Thus it cannot have overflowed when we eliminated top for the |
| 341 // second time. | 343 // second time. |
| 342 | 344 |
| 343 // Again, we may just have made out[0] negative, so do the same carry down. | 345 // Again, we may just have made out[0] negative, so do the same carry down. |
| 344 // As before, if we made out[0] negative then we know that out[3] is | 346 // As before, if we made out[0] negative then we know that out[3] is |
| 345 // sufficiently positive. | 347 // sufficiently positive. |
| 346 for (int i = 0; i < 3; i++) { | 348 for (int i = 0; i < 3; i++) { |
| 347 uint32 mask = static_cast<uint32>(static_cast<int32>(out[i]) >> 31); | 349 uint32_t mask = static_cast<uint32_t>(static_cast<int32_t>(out[i]) >> 31); |
| 348 out[i] += (1 << 28) & mask; | 350 out[i] += (1 << 28) & mask; |
| 349 out[i+1] -= 1 & mask; | 351 out[i+1] -= 1 & mask; |
| 350 } | 352 } |
| 351 | 353 |
| 352 // The value is < 2**224, but maybe greater than p. In order to reduce to a | 354 // The value is < 2**224, but maybe greater than p. In order to reduce to a |
| 353 // unique, minimal value we see if the value is >= p and, if so, subtract p. | 355 // unique, minimal value we see if the value is >= p and, if so, subtract p. |
| 354 | 356 |
| 355 // First we build a mask from the top four limbs, which must all be | 357 // First we build a mask from the top four limbs, which must all be |
| 356 // equal to bottom28Bits if the whole value is >= p. If top_4_all_ones | 358 // equal to bottom28Bits if the whole value is >= p. If top_4_all_ones |
| 357 // ends up with any zero bits in the bottom 28 bits, then this wasn't | 359 // ends up with any zero bits in the bottom 28 bits, then this wasn't |
| 358 // true. | 360 // true. |
| 359 uint32 top_4_all_ones = 0xffffffffu; | 361 uint32_t top_4_all_ones = 0xffffffffu; |
| 360 for (int i = 4; i < 8; i++) { | 362 for (int i = 4; i < 8; i++) { |
| 361 top_4_all_ones &= out[i]; | 363 top_4_all_ones &= out[i]; |
| 362 } | 364 } |
| 363 top_4_all_ones |= 0xf0000000; | 365 top_4_all_ones |= 0xf0000000; |
| 364 // Now we replicate any zero bits to all the bits in top_4_all_ones. | 366 // Now we replicate any zero bits to all the bits in top_4_all_ones. |
| 365 top_4_all_ones &= top_4_all_ones >> 16; | 367 top_4_all_ones &= top_4_all_ones >> 16; |
| 366 top_4_all_ones &= top_4_all_ones >> 8; | 368 top_4_all_ones &= top_4_all_ones >> 8; |
| 367 top_4_all_ones &= top_4_all_ones >> 4; | 369 top_4_all_ones &= top_4_all_ones >> 4; |
| 368 top_4_all_ones &= top_4_all_ones >> 2; | 370 top_4_all_ones &= top_4_all_ones >> 2; |
| 369 top_4_all_ones &= top_4_all_ones >> 1; | 371 top_4_all_ones &= top_4_all_ones >> 1; |
| 370 top_4_all_ones = | 372 top_4_all_ones = |
| 371 static_cast<uint32>(static_cast<int32>(top_4_all_ones << 31) >> 31); | 373 static_cast<uint32_t>(static_cast<int32_t>(top_4_all_ones << 31) >> 31); |
| 372 | 374 |
| 373 // Now we test whether the bottom three limbs are non-zero. | 375 // Now we test whether the bottom three limbs are non-zero. |
| 374 uint32 bottom_3_non_zero = out[0] | out[1] | out[2]; | 376 uint32_t bottom_3_non_zero = out[0] | out[1] | out[2]; |
| 375 bottom_3_non_zero |= bottom_3_non_zero >> 16; | 377 bottom_3_non_zero |= bottom_3_non_zero >> 16; |
| 376 bottom_3_non_zero |= bottom_3_non_zero >> 8; | 378 bottom_3_non_zero |= bottom_3_non_zero >> 8; |
| 377 bottom_3_non_zero |= bottom_3_non_zero >> 4; | 379 bottom_3_non_zero |= bottom_3_non_zero >> 4; |
| 378 bottom_3_non_zero |= bottom_3_non_zero >> 2; | 380 bottom_3_non_zero |= bottom_3_non_zero >> 2; |
| 379 bottom_3_non_zero |= bottom_3_non_zero >> 1; | 381 bottom_3_non_zero |= bottom_3_non_zero >> 1; |
| 380 bottom_3_non_zero = | 382 bottom_3_non_zero = |
| 381 static_cast<uint32>(static_cast<int32>(bottom_3_non_zero) >> 31); | 383 static_cast<uint32_t>(static_cast<int32_t>(bottom_3_non_zero) >> 31); |
| 382 | 384 |
| 383 // Everything depends on the value of out[3]. | 385 // Everything depends on the value of out[3]. |
| 384 // If it's > 0xffff000 and top_4_all_ones != 0 then the whole value is >= p | 386 // If it's > 0xffff000 and top_4_all_ones != 0 then the whole value is >= p |
| 385 // If it's = 0xffff000 and top_4_all_ones != 0 and bottom_3_non_zero != 0, | 387 // If it's = 0xffff000 and top_4_all_ones != 0 and bottom_3_non_zero != 0, |
| 386 // then the whole value is >= p | 388 // then the whole value is >= p |
| 387 // If it's < 0xffff000, then the whole value is < p | 389 // If it's < 0xffff000, then the whole value is < p |
| 388 uint32 n = out[3] - 0xffff000; | 390 uint32_t n = out[3] - 0xffff000; |
| 389 uint32 out_3_equal = n; | 391 uint32_t out_3_equal = n; |
| 390 out_3_equal |= out_3_equal >> 16; | 392 out_3_equal |= out_3_equal >> 16; |
| 391 out_3_equal |= out_3_equal >> 8; | 393 out_3_equal |= out_3_equal >> 8; |
| 392 out_3_equal |= out_3_equal >> 4; | 394 out_3_equal |= out_3_equal >> 4; |
| 393 out_3_equal |= out_3_equal >> 2; | 395 out_3_equal |= out_3_equal >> 2; |
| 394 out_3_equal |= out_3_equal >> 1; | 396 out_3_equal |= out_3_equal >> 1; |
| 395 out_3_equal = | 397 out_3_equal = |
| 396 ~static_cast<uint32>(static_cast<int32>(out_3_equal << 31) >> 31); | 398 ~static_cast<uint32_t>(static_cast<int32_t>(out_3_equal << 31) >> 31); |
| 397 | 399 |
| 398 // If out[3] > 0xffff000 then n's MSB will be zero. | 400 // If out[3] > 0xffff000 then n's MSB will be zero. |
| 399 uint32 out_3_gt = ~static_cast<uint32>(static_cast<int32>(n << 31) >> 31); | 401 uint32_t out_3_gt = |
| 402 ~static_cast<uint32_t>(static_cast<int32_t>(n << 31) >> 31); |
| 400 | 403 |
| 401 uint32 mask = top_4_all_ones & ((out_3_equal & bottom_3_non_zero) | out_3_gt); | 404 uint32_t mask = |
| 405 top_4_all_ones & ((out_3_equal & bottom_3_non_zero) | out_3_gt); |
| 402 out[0] -= 1 & mask; | 406 out[0] -= 1 & mask; |
| 403 out[3] -= 0xffff000 & mask; | 407 out[3] -= 0xffff000 & mask; |
| 404 out[4] -= 0xfffffff & mask; | 408 out[4] -= 0xfffffff & mask; |
| 405 out[5] -= 0xfffffff & mask; | 409 out[5] -= 0xfffffff & mask; |
| 406 out[6] -= 0xfffffff & mask; | 410 out[6] -= 0xfffffff & mask; |
| 407 out[7] -= 0xfffffff & mask; | 411 out[7] -= 0xfffffff & mask; |
| 408 } | 412 } |
| 409 | 413 |
| 410 | 414 |
| 411 // Group element functions. | 415 // Group element functions. |
| 412 // | 416 // |
| 413 // These functions deal with group elements. The group is an elliptic curve | 417 // These functions deal with group elements. The group is an elliptic curve |
| 414 // group with a = -3 defined in FIPS 186-3, section D.2.2. | 418 // group with a = -3 defined in FIPS 186-3, section D.2.2. |
| 415 | 419 |
| 416 using crypto::p224::Point; | 420 using crypto::p224::Point; |
| 417 | 421 |
| 418 // kB is parameter of the elliptic curve. | 422 // kB is parameter of the elliptic curve. |
| 419 const FieldElement kB = { | 423 const FieldElement kB = { |
| 420 55967668, 11768882, 265861671, 185302395, | 424 55967668, 11768882, 265861671, 185302395, |
| 421 39211076, 180311059, 84673715, 188764328, | 425 39211076, 180311059, 84673715, 188764328, |
| 422 }; | 426 }; |
| 423 | 427 |
| 424 void CopyConditional(Point* out, const Point& a, uint32 mask); | 428 void CopyConditional(Point* out, const Point& a, uint32_t mask); |
| 425 void DoubleJacobian(Point* out, const Point& a); | 429 void DoubleJacobian(Point* out, const Point& a); |
| 426 | 430 |
| 427 // AddJacobian computes *out = a+b where a != b. | 431 // AddJacobian computes *out = a+b where a != b. |
| 428 void AddJacobian(Point *out, | 432 void AddJacobian(Point *out, |
| 429 const Point& a, | 433 const Point& a, |
| 430 const Point& b) { | 434 const Point& b) { |
| 431 // See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-a
dd-2007-bl | 435 // See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-a
dd-2007-bl |
| 432 FieldElement z1z1, z2z2, u1, u2, s1, s2, h, i, j, r, v; | 436 FieldElement z1z1, z2z2, u1, u2, s1, s2, h, i, j, r, v; |
| 433 | 437 |
| 434 uint32 z1_is_zero = IsZero(a.z); | 438 uint32_t z1_is_zero = IsZero(a.z); |
| 435 uint32 z2_is_zero = IsZero(b.z); | 439 uint32_t z2_is_zero = IsZero(b.z); |
| 436 | 440 |
| 437 // Z1Z1 = Z1² | 441 // Z1Z1 = Z1² |
| 438 Square(&z1z1, a.z); | 442 Square(&z1z1, a.z); |
| 439 | 443 |
| 440 // Z2Z2 = Z2² | 444 // Z2Z2 = Z2² |
| 441 Square(&z2z2, b.z); | 445 Square(&z2z2, b.z); |
| 442 | 446 |
| 443 // U1 = X1*Z2Z2 | 447 // U1 = X1*Z2Z2 |
| 444 Mul(&u1, a.x, z2z2); | 448 Mul(&u1, a.x, z2z2); |
| 445 | 449 |
| 446 // U2 = X2*Z1Z1 | 450 // U2 = X2*Z1Z1 |
| 447 Mul(&u2, b.x, z1z1); | 451 Mul(&u2, b.x, z1z1); |
| 448 | 452 |
| 449 // S1 = Y1*Z2*Z2Z2 | 453 // S1 = Y1*Z2*Z2Z2 |
| 450 Mul(&s1, b.z, z2z2); | 454 Mul(&s1, b.z, z2z2); |
| 451 Mul(&s1, a.y, s1); | 455 Mul(&s1, a.y, s1); |
| 452 | 456 |
| 453 // S2 = Y2*Z1*Z1Z1 | 457 // S2 = Y2*Z1*Z1Z1 |
| 454 Mul(&s2, a.z, z1z1); | 458 Mul(&s2, a.z, z1z1); |
| 455 Mul(&s2, b.y, s2); | 459 Mul(&s2, b.y, s2); |
| 456 | 460 |
| 457 // H = U2-U1 | 461 // H = U2-U1 |
| 458 Subtract(&h, u2, u1); | 462 Subtract(&h, u2, u1); |
| 459 Reduce(&h); | 463 Reduce(&h); |
| 460 uint32 x_equal = IsZero(h); | 464 uint32_t x_equal = IsZero(h); |
| 461 | 465 |
| 462 // I = (2*H)² | 466 // I = (2*H)² |
| 463 for (int k = 0; k < 8; k++) { | 467 for (int k = 0; k < 8; k++) { |
| 464 i[k] = h[k] << 1; | 468 i[k] = h[k] << 1; |
| 465 } | 469 } |
| 466 Reduce(&i); | 470 Reduce(&i); |
| 467 Square(&i, i); | 471 Square(&i, i); |
| 468 | 472 |
| 469 // J = H*I | 473 // J = H*I |
| 470 Mul(&j, h, i); | 474 Mul(&j, h, i); |
| 471 // r = 2*(S2-S1) | 475 // r = 2*(S2-S1) |
| 472 Subtract(&r, s2, s1); | 476 Subtract(&r, s2, s1); |
| 473 Reduce(&r); | 477 Reduce(&r); |
| 474 uint32 y_equal = IsZero(r); | 478 uint32_t y_equal = IsZero(r); |
| 475 | 479 |
| 476 if (x_equal && y_equal && !z1_is_zero && !z2_is_zero) { | 480 if (x_equal && y_equal && !z1_is_zero && !z2_is_zero) { |
| 477 // The two input points are the same therefore we must use the dedicated | 481 // The two input points are the same therefore we must use the dedicated |
| 478 // doubling function as the slope of the line is undefined. | 482 // doubling function as the slope of the line is undefined. |
| 479 DoubleJacobian(out, a); | 483 DoubleJacobian(out, a); |
| 480 return; | 484 return; |
| 481 } | 485 } |
| 482 | 486 |
| 483 for (int k = 0; k < 8; k++) { | 487 for (int k = 0; k < 8; k++) { |
| 484 r[k] <<= 1; | 488 r[k] <<= 1; |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 571 gamma[i] <<= 3; | 575 gamma[i] <<= 3; |
| 572 } | 576 } |
| 573 Reduce(&gamma); | 577 Reduce(&gamma); |
| 574 Mul(&out->y, alpha, beta); | 578 Mul(&out->y, alpha, beta); |
| 575 Subtract(&out->y, out->y, gamma); | 579 Subtract(&out->y, out->y, gamma); |
| 576 Reduce(&out->y); | 580 Reduce(&out->y); |
| 577 } | 581 } |
| 578 | 582 |
| 579 // CopyConditional sets *out=a if mask is 0xffffffff. mask must be either 0 of | 583 // CopyConditional sets *out=a if mask is 0xffffffff. mask must be either 0 of |
| 580 // 0xffffffff. | 584 // 0xffffffff. |
| 581 void CopyConditional(Point* out, | 585 void CopyConditional(Point* out, const Point& a, uint32_t mask) { |
| 582 const Point& a, | |
| 583 uint32 mask) { | |
| 584 for (int i = 0; i < 8; i++) { | 586 for (int i = 0; i < 8; i++) { |
| 585 out->x[i] ^= mask & (a.x[i] ^ out->x[i]); | 587 out->x[i] ^= mask & (a.x[i] ^ out->x[i]); |
| 586 out->y[i] ^= mask & (a.y[i] ^ out->y[i]); | 588 out->y[i] ^= mask & (a.y[i] ^ out->y[i]); |
| 587 out->z[i] ^= mask & (a.z[i] ^ out->z[i]); | 589 out->z[i] ^= mask & (a.z[i] ^ out->z[i]); |
| 588 } | 590 } |
| 589 } | 591 } |
| 590 | 592 |
| 591 // ScalarMult calculates *out = a*scalar where scalar is a big-endian number of | 593 // ScalarMult calculates *out = a*scalar where scalar is a big-endian number of |
| 592 // length scalar_len and != 0. | 594 // length scalar_len and != 0. |
| 593 void ScalarMult(Point* out, const Point& a, | 595 void ScalarMult(Point* out, |
| 594 const uint8* scalar, size_t scalar_len) { | 596 const Point& a, |
| 597 const uint8_t* scalar, |
| 598 size_t scalar_len) { |
| 595 memset(out, 0, sizeof(*out)); | 599 memset(out, 0, sizeof(*out)); |
| 596 Point tmp; | 600 Point tmp; |
| 597 | 601 |
| 598 for (size_t i = 0; i < scalar_len; i++) { | 602 for (size_t i = 0; i < scalar_len; i++) { |
| 599 for (unsigned int bit_num = 0; bit_num < 8; bit_num++) { | 603 for (unsigned int bit_num = 0; bit_num < 8; bit_num++) { |
| 600 DoubleJacobian(out, *out); | 604 DoubleJacobian(out, *out); |
| 601 uint32 bit = static_cast<uint32>(static_cast<int32>( | 605 uint32_t bit = static_cast<uint32_t>(static_cast<int32_t>( |
| 602 (((scalar[i] >> (7 - bit_num)) & 1) << 31) >> 31)); | 606 (((scalar[i] >> (7 - bit_num)) & 1) << 31) >> 31)); |
| 603 AddJacobian(&tmp, a, *out); | 607 AddJacobian(&tmp, a, *out); |
| 604 CopyConditional(out, tmp, bit); | 608 CopyConditional(out, tmp, bit); |
| 605 } | 609 } |
| 606 } | 610 } |
| 607 } | 611 } |
| 608 | 612 |
| 609 // Get224Bits reads 7 words from in and scatters their contents in | 613 // Get224Bits reads 7 words from in and scatters their contents in |
| 610 // little-endian form into 8 words at out, 28 bits per output word. | 614 // little-endian form into 8 words at out, 28 bits per output word. |
| 611 void Get224Bits(uint32* out, const uint32* in) { | 615 void Get224Bits(uint32_t* out, const uint32_t* in) { |
| 612 out[0] = NetToHost32(in[6]) & kBottom28Bits; | 616 out[0] = NetToHost32(in[6]) & kBottom28Bits; |
| 613 out[1] = ((NetToHost32(in[5]) << 4) | | 617 out[1] = ((NetToHost32(in[5]) << 4) | |
| 614 (NetToHost32(in[6]) >> 28)) & kBottom28Bits; | 618 (NetToHost32(in[6]) >> 28)) & kBottom28Bits; |
| 615 out[2] = ((NetToHost32(in[4]) << 8) | | 619 out[2] = ((NetToHost32(in[4]) << 8) | |
| 616 (NetToHost32(in[5]) >> 24)) & kBottom28Bits; | 620 (NetToHost32(in[5]) >> 24)) & kBottom28Bits; |
| 617 out[3] = ((NetToHost32(in[3]) << 12) | | 621 out[3] = ((NetToHost32(in[3]) << 12) | |
| 618 (NetToHost32(in[4]) >> 20)) & kBottom28Bits; | 622 (NetToHost32(in[4]) >> 20)) & kBottom28Bits; |
| 619 out[4] = ((NetToHost32(in[2]) << 16) | | 623 out[4] = ((NetToHost32(in[2]) << 16) | |
| 620 (NetToHost32(in[3]) >> 16)) & kBottom28Bits; | 624 (NetToHost32(in[3]) >> 16)) & kBottom28Bits; |
| 621 out[5] = ((NetToHost32(in[1]) << 20) | | 625 out[5] = ((NetToHost32(in[1]) << 20) | |
| 622 (NetToHost32(in[2]) >> 12)) & kBottom28Bits; | 626 (NetToHost32(in[2]) >> 12)) & kBottom28Bits; |
| 623 out[6] = ((NetToHost32(in[0]) << 24) | | 627 out[6] = ((NetToHost32(in[0]) << 24) | |
| 624 (NetToHost32(in[1]) >> 8)) & kBottom28Bits; | 628 (NetToHost32(in[1]) >> 8)) & kBottom28Bits; |
| 625 out[7] = (NetToHost32(in[0]) >> 4) & kBottom28Bits; | 629 out[7] = (NetToHost32(in[0]) >> 4) & kBottom28Bits; |
| 626 } | 630 } |
| 627 | 631 |
| 628 // Put224Bits performs the inverse operation to Get224Bits: taking 28 bits from | 632 // Put224Bits performs the inverse operation to Get224Bits: taking 28 bits from |
| 629 // each of 8 input words and writing them in big-endian order to 7 words at | 633 // each of 8 input words and writing them in big-endian order to 7 words at |
| 630 // out. | 634 // out. |
| 631 void Put224Bits(uint32* out, const uint32* in) { | 635 void Put224Bits(uint32_t* out, const uint32_t* in) { |
| 632 out[6] = HostToNet32((in[0] >> 0) | (in[1] << 28)); | 636 out[6] = HostToNet32((in[0] >> 0) | (in[1] << 28)); |
| 633 out[5] = HostToNet32((in[1] >> 4) | (in[2] << 24)); | 637 out[5] = HostToNet32((in[1] >> 4) | (in[2] << 24)); |
| 634 out[4] = HostToNet32((in[2] >> 8) | (in[3] << 20)); | 638 out[4] = HostToNet32((in[2] >> 8) | (in[3] << 20)); |
| 635 out[3] = HostToNet32((in[3] >> 12) | (in[4] << 16)); | 639 out[3] = HostToNet32((in[3] >> 12) | (in[4] << 16)); |
| 636 out[2] = HostToNet32((in[4] >> 16) | (in[5] << 12)); | 640 out[2] = HostToNet32((in[4] >> 16) | (in[5] << 12)); |
| 637 out[1] = HostToNet32((in[5] >> 20) | (in[6] << 8)); | 641 out[1] = HostToNet32((in[5] >> 20) | (in[6] << 8)); |
| 638 out[0] = HostToNet32((in[6] >> 24) | (in[7] << 4)); | 642 out[0] = HostToNet32((in[6] >> 24) | (in[7] << 4)); |
| 639 } | 643 } |
| 640 | 644 |
| 641 } // anonymous namespace | 645 } // anonymous namespace |
| 642 | 646 |
| 643 namespace crypto { | 647 namespace crypto { |
| 644 | 648 |
| 645 namespace p224 { | 649 namespace p224 { |
| 646 | 650 |
| 647 bool Point::SetFromString(const base::StringPiece& in) { | 651 bool Point::SetFromString(const base::StringPiece& in) { |
| 648 if (in.size() != 2*28) | 652 if (in.size() != 2*28) |
| 649 return false; | 653 return false; |
| 650 const uint32* inwords = reinterpret_cast<const uint32*>(in.data()); | 654 const uint32_t* inwords = reinterpret_cast<const uint32_t*>(in.data()); |
| 651 Get224Bits(x, inwords); | 655 Get224Bits(x, inwords); |
| 652 Get224Bits(y, inwords + 7); | 656 Get224Bits(y, inwords + 7); |
| 653 memset(&z, 0, sizeof(z)); | 657 memset(&z, 0, sizeof(z)); |
| 654 z[0] = 1; | 658 z[0] = 1; |
| 655 | 659 |
| 656 // Check that the point is on the curve, i.e. that y² = x³ - 3x + b. | 660 // Check that the point is on the curve, i.e. that y² = x³ - 3x + b. |
| 657 FieldElement lhs; | 661 FieldElement lhs; |
| 658 Square(&lhs, y); | 662 Square(&lhs, y); |
| 659 Contract(&lhs); | 663 Contract(&lhs); |
| 660 | 664 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 686 | 690 |
| 687 Invert(&zinv, this->z); | 691 Invert(&zinv, this->z); |
| 688 Square(&zinv_sq, zinv); | 692 Square(&zinv_sq, zinv); |
| 689 Mul(&xx, x, zinv_sq); | 693 Mul(&xx, x, zinv_sq); |
| 690 Mul(&zinv_sq, zinv_sq, zinv); | 694 Mul(&zinv_sq, zinv_sq, zinv); |
| 691 Mul(&yy, y, zinv_sq); | 695 Mul(&yy, y, zinv_sq); |
| 692 | 696 |
| 693 Contract(&xx); | 697 Contract(&xx); |
| 694 Contract(&yy); | 698 Contract(&yy); |
| 695 | 699 |
| 696 uint32 outwords[14]; | 700 uint32_t outwords[14]; |
| 697 Put224Bits(outwords, xx); | 701 Put224Bits(outwords, xx); |
| 698 Put224Bits(outwords + 7, yy); | 702 Put224Bits(outwords + 7, yy); |
| 699 return std::string(reinterpret_cast<const char*>(outwords), sizeof(outwords)); | 703 return std::string(reinterpret_cast<const char*>(outwords), sizeof(outwords)); |
| 700 } | 704 } |
| 701 | 705 |
| 702 void ScalarMult(const Point& in, const uint8* scalar, Point* out) { | 706 void ScalarMult(const Point& in, const uint8_t* scalar, Point* out) { |
| 703 ::ScalarMult(out, in, scalar, 28); | 707 ::ScalarMult(out, in, scalar, 28); |
| 704 } | 708 } |
| 705 | 709 |
| 706 // kBasePoint is the base point (generator) of the elliptic curve group. | 710 // kBasePoint is the base point (generator) of the elliptic curve group. |
| 707 static const Point kBasePoint = { | 711 static const Point kBasePoint = { |
| 708 {22813985, 52956513, 34677300, 203240812, | 712 {22813985, 52956513, 34677300, 203240812, |
| 709 12143107, 133374265, 225162431, 191946955}, | 713 12143107, 133374265, 225162431, 191946955}, |
| 710 {83918388, 223877528, 122119236, 123340192, | 714 {83918388, 223877528, 122119236, 123340192, |
| 711 266784067, 263504429, 146143011, 198407736}, | 715 266784067, 263504429, 146143011, 198407736}, |
| 712 {1, 0, 0, 0, 0, 0, 0, 0}, | 716 {1, 0, 0, 0, 0, 0, 0, 0}, |
| 713 }; | 717 }; |
| 714 | 718 |
| 715 void ScalarBaseMult(const uint8* scalar, Point* out) { | 719 void ScalarBaseMult(const uint8_t* scalar, Point* out) { |
| 716 ::ScalarMult(out, kBasePoint, scalar, 28); | 720 ::ScalarMult(out, kBasePoint, scalar, 28); |
| 717 } | 721 } |
| 718 | 722 |
| 719 void Add(const Point& a, const Point& b, Point* out) { | 723 void Add(const Point& a, const Point& b, Point* out) { |
| 720 AddJacobian(out, a, b); | 724 AddJacobian(out, a, b); |
| 721 } | 725 } |
| 722 | 726 |
| 723 void Negate(const Point& in, Point* out) { | 727 void Negate(const Point& in, Point* out) { |
| 724 // Guide to elliptic curve cryptography, page 89 suggests that (X : X+Y : Z) | 728 // Guide to elliptic curve cryptography, page 89 suggests that (X : X+Y : Z) |
| 725 // is the negative in Jacobian coordinates, but it doesn't actually appear to | 729 // is the negative in Jacobian coordinates, but it doesn't actually appear to |
| 726 // be true in testing so this performs the negation in affine coordinates. | 730 // be true in testing so this performs the negation in affine coordinates. |
| 727 FieldElement zinv, zinv_sq, y; | 731 FieldElement zinv, zinv_sq, y; |
| 728 Invert(&zinv, in.z); | 732 Invert(&zinv, in.z); |
| 729 Square(&zinv_sq, zinv); | 733 Square(&zinv_sq, zinv); |
| 730 Mul(&out->x, in.x, zinv_sq); | 734 Mul(&out->x, in.x, zinv_sq); |
| 731 Mul(&zinv_sq, zinv_sq, zinv); | 735 Mul(&zinv_sq, zinv_sq, zinv); |
| 732 Mul(&y, in.y, zinv_sq); | 736 Mul(&y, in.y, zinv_sq); |
| 733 | 737 |
| 734 Subtract(&out->y, kP, y); | 738 Subtract(&out->y, kP, y); |
| 735 Reduce(&out->y); | 739 Reduce(&out->y); |
| 736 | 740 |
| 737 memset(&out->z, 0, sizeof(out->z)); | 741 memset(&out->z, 0, sizeof(out->z)); |
| 738 out->z[0] = 1; | 742 out->z[0] = 1; |
| 739 } | 743 } |
| 740 | 744 |
| 741 } // namespace p224 | 745 } // namespace p224 |
| 742 | 746 |
| 743 } // namespace crypto | 747 } // namespace crypto |
| OLD | NEW |