DeepCover Embedded Security in IoT: Public-key Secured Data Paths

Dependencies:   MaximInterface

The MAXREFDES155# is an internet-of-things (IoT) embedded-security reference design, built to authenticate and control a sensing node using elliptic-curve-based public-key cryptography with control and notification from a web server.

The hardware includes an ARM® mbed™ shield and attached sensor endpoint. The shield contains a DS2476 DeepCover® ECDSA/SHA-2 coprocessor, Wifi communication, LCD push-button controls, and status LEDs. The sensor endpoint is attached to the shield using a 300mm cable and contains a DS28C36 DeepCover ECDSA/SHA-2 authenticator, IR-thermal sensor, and aiming laser for the IR sensor. The MAXREFDES155# is equipped with a standard Arduino® form-factor shield connector for immediate testing using an mbed board such as the MAX32600MBED#. The combination of these two devices represent an IoT device. Communication to the web server is accomplished with the shield Wifi circuitry. Communication from the shield to the attached sensor module is accomplished over I2C . The sensor module represents an IoT endpoint that generates small data with a requirement for message authenticity/integrity and secure on/off operational control.

The design is hierarchical with each mbed platform and shield communicating data from the sensor node to a web server that maintains a centralized log and dispatches notifications as necessary. The simplicity of this design enables rapid integration into any star-topology IoT network to provide security with the low overhead and cost provided by the ECDSA-P256 asymmetric-key and SHA-256 symmetric-key algorithms.

More information about the MAXREFDES155# is available on the Maxim Integrated website.

Committer:
IanBenzMaxim
Date:
Fri Feb 24 11:23:12 2017 -0600
Revision:
0:33d4e66780c0
Initial commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IanBenzMaxim 0:33d4e66780c0 1 // Tencent is pleased to support the open source community by making RapidJSON available.
IanBenzMaxim 0:33d4e66780c0 2 //
IanBenzMaxim 0:33d4e66780c0 3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
IanBenzMaxim 0:33d4e66780c0 4 //
IanBenzMaxim 0:33d4e66780c0 5 // Licensed under the MIT License (the "License"); you may not use this file except
IanBenzMaxim 0:33d4e66780c0 6 // in compliance with the License. You may obtain a copy of the License at
IanBenzMaxim 0:33d4e66780c0 7 //
IanBenzMaxim 0:33d4e66780c0 8 // http://opensource.org/licenses/MIT
IanBenzMaxim 0:33d4e66780c0 9 //
IanBenzMaxim 0:33d4e66780c0 10 // Unless required by applicable law or agreed to in writing, software distributed
IanBenzMaxim 0:33d4e66780c0 11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
IanBenzMaxim 0:33d4e66780c0 12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
IanBenzMaxim 0:33d4e66780c0 13 // specific language governing permissions and limitations under the License.
IanBenzMaxim 0:33d4e66780c0 14
IanBenzMaxim 0:33d4e66780c0 15 #ifndef RAPIDJSON_BIGINTEGER_H_
IanBenzMaxim 0:33d4e66780c0 16 #define RAPIDJSON_BIGINTEGER_H_
IanBenzMaxim 0:33d4e66780c0 17
IanBenzMaxim 0:33d4e66780c0 18 #include "../rapidjson.h"
IanBenzMaxim 0:33d4e66780c0 19
IanBenzMaxim 0:33d4e66780c0 20 #if defined(_MSC_VER) && defined(_M_AMD64)
IanBenzMaxim 0:33d4e66780c0 21 #include <intrin.h> // for _umul128
IanBenzMaxim 0:33d4e66780c0 22 #pragma intrinsic(_umul128)
IanBenzMaxim 0:33d4e66780c0 23 #endif
IanBenzMaxim 0:33d4e66780c0 24
IanBenzMaxim 0:33d4e66780c0 25 RAPIDJSON_NAMESPACE_BEGIN
IanBenzMaxim 0:33d4e66780c0 26 namespace internal {
IanBenzMaxim 0:33d4e66780c0 27
IanBenzMaxim 0:33d4e66780c0 28 class BigInteger {
IanBenzMaxim 0:33d4e66780c0 29 public:
IanBenzMaxim 0:33d4e66780c0 30 typedef uint64_t Type;
IanBenzMaxim 0:33d4e66780c0 31
IanBenzMaxim 0:33d4e66780c0 32 BigInteger(const BigInteger& rhs) : count_(rhs.count_) {
IanBenzMaxim 0:33d4e66780c0 33 std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type));
IanBenzMaxim 0:33d4e66780c0 34 }
IanBenzMaxim 0:33d4e66780c0 35
IanBenzMaxim 0:33d4e66780c0 36 explicit BigInteger(uint64_t u) : count_(1) {
IanBenzMaxim 0:33d4e66780c0 37 digits_[0] = u;
IanBenzMaxim 0:33d4e66780c0 38 }
IanBenzMaxim 0:33d4e66780c0 39
IanBenzMaxim 0:33d4e66780c0 40 BigInteger(const char* decimals, size_t length) : count_(1) {
IanBenzMaxim 0:33d4e66780c0 41 RAPIDJSON_ASSERT(length > 0);
IanBenzMaxim 0:33d4e66780c0 42 digits_[0] = 0;
IanBenzMaxim 0:33d4e66780c0 43 size_t i = 0;
IanBenzMaxim 0:33d4e66780c0 44 const size_t kMaxDigitPerIteration = 19; // 2^64 = 18446744073709551616 > 10^19
IanBenzMaxim 0:33d4e66780c0 45 while (length >= kMaxDigitPerIteration) {
IanBenzMaxim 0:33d4e66780c0 46 AppendDecimal64(decimals + i, decimals + i + kMaxDigitPerIteration);
IanBenzMaxim 0:33d4e66780c0 47 length -= kMaxDigitPerIteration;
IanBenzMaxim 0:33d4e66780c0 48 i += kMaxDigitPerIteration;
IanBenzMaxim 0:33d4e66780c0 49 }
IanBenzMaxim 0:33d4e66780c0 50
IanBenzMaxim 0:33d4e66780c0 51 if (length > 0)
IanBenzMaxim 0:33d4e66780c0 52 AppendDecimal64(decimals + i, decimals + i + length);
IanBenzMaxim 0:33d4e66780c0 53 }
IanBenzMaxim 0:33d4e66780c0 54
IanBenzMaxim 0:33d4e66780c0 55 BigInteger& operator=(const BigInteger &rhs)
IanBenzMaxim 0:33d4e66780c0 56 {
IanBenzMaxim 0:33d4e66780c0 57 if (this != &rhs) {
IanBenzMaxim 0:33d4e66780c0 58 count_ = rhs.count_;
IanBenzMaxim 0:33d4e66780c0 59 std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type));
IanBenzMaxim 0:33d4e66780c0 60 }
IanBenzMaxim 0:33d4e66780c0 61 return *this;
IanBenzMaxim 0:33d4e66780c0 62 }
IanBenzMaxim 0:33d4e66780c0 63
IanBenzMaxim 0:33d4e66780c0 64 BigInteger& operator=(uint64_t u) {
IanBenzMaxim 0:33d4e66780c0 65 digits_[0] = u;
IanBenzMaxim 0:33d4e66780c0 66 count_ = 1;
IanBenzMaxim 0:33d4e66780c0 67 return *this;
IanBenzMaxim 0:33d4e66780c0 68 }
IanBenzMaxim 0:33d4e66780c0 69
IanBenzMaxim 0:33d4e66780c0 70 BigInteger& operator+=(uint64_t u) {
IanBenzMaxim 0:33d4e66780c0 71 Type backup = digits_[0];
IanBenzMaxim 0:33d4e66780c0 72 digits_[0] += u;
IanBenzMaxim 0:33d4e66780c0 73 for (size_t i = 0; i < count_ - 1; i++) {
IanBenzMaxim 0:33d4e66780c0 74 if (digits_[i] >= backup)
IanBenzMaxim 0:33d4e66780c0 75 return *this; // no carry
IanBenzMaxim 0:33d4e66780c0 76 backup = digits_[i + 1];
IanBenzMaxim 0:33d4e66780c0 77 digits_[i + 1] += 1;
IanBenzMaxim 0:33d4e66780c0 78 }
IanBenzMaxim 0:33d4e66780c0 79
IanBenzMaxim 0:33d4e66780c0 80 // Last carry
IanBenzMaxim 0:33d4e66780c0 81 if (digits_[count_ - 1] < backup)
IanBenzMaxim 0:33d4e66780c0 82 PushBack(1);
IanBenzMaxim 0:33d4e66780c0 83
IanBenzMaxim 0:33d4e66780c0 84 return *this;
IanBenzMaxim 0:33d4e66780c0 85 }
IanBenzMaxim 0:33d4e66780c0 86
IanBenzMaxim 0:33d4e66780c0 87 BigInteger& operator*=(uint64_t u) {
IanBenzMaxim 0:33d4e66780c0 88 if (u == 0) return *this = 0;
IanBenzMaxim 0:33d4e66780c0 89 if (u == 1) return *this;
IanBenzMaxim 0:33d4e66780c0 90 if (*this == 1) return *this = u;
IanBenzMaxim 0:33d4e66780c0 91
IanBenzMaxim 0:33d4e66780c0 92 uint64_t k = 0;
IanBenzMaxim 0:33d4e66780c0 93 for (size_t i = 0; i < count_; i++) {
IanBenzMaxim 0:33d4e66780c0 94 uint64_t hi;
IanBenzMaxim 0:33d4e66780c0 95 digits_[i] = MulAdd64(digits_[i], u, k, &hi);
IanBenzMaxim 0:33d4e66780c0 96 k = hi;
IanBenzMaxim 0:33d4e66780c0 97 }
IanBenzMaxim 0:33d4e66780c0 98
IanBenzMaxim 0:33d4e66780c0 99 if (k > 0)
IanBenzMaxim 0:33d4e66780c0 100 PushBack(k);
IanBenzMaxim 0:33d4e66780c0 101
IanBenzMaxim 0:33d4e66780c0 102 return *this;
IanBenzMaxim 0:33d4e66780c0 103 }
IanBenzMaxim 0:33d4e66780c0 104
IanBenzMaxim 0:33d4e66780c0 105 BigInteger& operator*=(uint32_t u) {
IanBenzMaxim 0:33d4e66780c0 106 if (u == 0) return *this = 0;
IanBenzMaxim 0:33d4e66780c0 107 if (u == 1) return *this;
IanBenzMaxim 0:33d4e66780c0 108 if (*this == 1) return *this = u;
IanBenzMaxim 0:33d4e66780c0 109
IanBenzMaxim 0:33d4e66780c0 110 uint64_t k = 0;
IanBenzMaxim 0:33d4e66780c0 111 for (size_t i = 0; i < count_; i++) {
IanBenzMaxim 0:33d4e66780c0 112 const uint64_t c = digits_[i] >> 32;
IanBenzMaxim 0:33d4e66780c0 113 const uint64_t d = digits_[i] & 0xFFFFFFFF;
IanBenzMaxim 0:33d4e66780c0 114 const uint64_t uc = u * c;
IanBenzMaxim 0:33d4e66780c0 115 const uint64_t ud = u * d;
IanBenzMaxim 0:33d4e66780c0 116 const uint64_t p0 = ud + k;
IanBenzMaxim 0:33d4e66780c0 117 const uint64_t p1 = uc + (p0 >> 32);
IanBenzMaxim 0:33d4e66780c0 118 digits_[i] = (p0 & 0xFFFFFFFF) | (p1 << 32);
IanBenzMaxim 0:33d4e66780c0 119 k = p1 >> 32;
IanBenzMaxim 0:33d4e66780c0 120 }
IanBenzMaxim 0:33d4e66780c0 121
IanBenzMaxim 0:33d4e66780c0 122 if (k > 0)
IanBenzMaxim 0:33d4e66780c0 123 PushBack(k);
IanBenzMaxim 0:33d4e66780c0 124
IanBenzMaxim 0:33d4e66780c0 125 return *this;
IanBenzMaxim 0:33d4e66780c0 126 }
IanBenzMaxim 0:33d4e66780c0 127
IanBenzMaxim 0:33d4e66780c0 128 BigInteger& operator<<=(size_t shift) {
IanBenzMaxim 0:33d4e66780c0 129 if (IsZero() || shift == 0) return *this;
IanBenzMaxim 0:33d4e66780c0 130
IanBenzMaxim 0:33d4e66780c0 131 size_t offset = shift / kTypeBit;
IanBenzMaxim 0:33d4e66780c0 132 size_t interShift = shift % kTypeBit;
IanBenzMaxim 0:33d4e66780c0 133 RAPIDJSON_ASSERT(count_ + offset <= kCapacity);
IanBenzMaxim 0:33d4e66780c0 134
IanBenzMaxim 0:33d4e66780c0 135 if (interShift == 0) {
IanBenzMaxim 0:33d4e66780c0 136 std::memmove(&digits_[count_ - 1 + offset], &digits_[count_ - 1], count_ * sizeof(Type));
IanBenzMaxim 0:33d4e66780c0 137 count_ += offset;
IanBenzMaxim 0:33d4e66780c0 138 }
IanBenzMaxim 0:33d4e66780c0 139 else {
IanBenzMaxim 0:33d4e66780c0 140 digits_[count_] = 0;
IanBenzMaxim 0:33d4e66780c0 141 for (size_t i = count_; i > 0; i--)
IanBenzMaxim 0:33d4e66780c0 142 digits_[i + offset] = (digits_[i] << interShift) | (digits_[i - 1] >> (kTypeBit - interShift));
IanBenzMaxim 0:33d4e66780c0 143 digits_[offset] = digits_[0] << interShift;
IanBenzMaxim 0:33d4e66780c0 144 count_ += offset;
IanBenzMaxim 0:33d4e66780c0 145 if (digits_[count_])
IanBenzMaxim 0:33d4e66780c0 146 count_++;
IanBenzMaxim 0:33d4e66780c0 147 }
IanBenzMaxim 0:33d4e66780c0 148
IanBenzMaxim 0:33d4e66780c0 149 std::memset(digits_, 0, offset * sizeof(Type));
IanBenzMaxim 0:33d4e66780c0 150
IanBenzMaxim 0:33d4e66780c0 151 return *this;
IanBenzMaxim 0:33d4e66780c0 152 }
IanBenzMaxim 0:33d4e66780c0 153
IanBenzMaxim 0:33d4e66780c0 154 bool operator==(const BigInteger& rhs) const {
IanBenzMaxim 0:33d4e66780c0 155 return count_ == rhs.count_ && std::memcmp(digits_, rhs.digits_, count_ * sizeof(Type)) == 0;
IanBenzMaxim 0:33d4e66780c0 156 }
IanBenzMaxim 0:33d4e66780c0 157
IanBenzMaxim 0:33d4e66780c0 158 bool operator==(const Type rhs) const {
IanBenzMaxim 0:33d4e66780c0 159 return count_ == 1 && digits_[0] == rhs;
IanBenzMaxim 0:33d4e66780c0 160 }
IanBenzMaxim 0:33d4e66780c0 161
IanBenzMaxim 0:33d4e66780c0 162 BigInteger& MultiplyPow5(unsigned exp) {
IanBenzMaxim 0:33d4e66780c0 163 static const uint32_t kPow5[12] = {
IanBenzMaxim 0:33d4e66780c0 164 5,
IanBenzMaxim 0:33d4e66780c0 165 5 * 5,
IanBenzMaxim 0:33d4e66780c0 166 5 * 5 * 5,
IanBenzMaxim 0:33d4e66780c0 167 5 * 5 * 5 * 5,
IanBenzMaxim 0:33d4e66780c0 168 5 * 5 * 5 * 5 * 5,
IanBenzMaxim 0:33d4e66780c0 169 5 * 5 * 5 * 5 * 5 * 5,
IanBenzMaxim 0:33d4e66780c0 170 5 * 5 * 5 * 5 * 5 * 5 * 5,
IanBenzMaxim 0:33d4e66780c0 171 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
IanBenzMaxim 0:33d4e66780c0 172 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
IanBenzMaxim 0:33d4e66780c0 173 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
IanBenzMaxim 0:33d4e66780c0 174 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
IanBenzMaxim 0:33d4e66780c0 175 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5
IanBenzMaxim 0:33d4e66780c0 176 };
IanBenzMaxim 0:33d4e66780c0 177 if (exp == 0) return *this;
IanBenzMaxim 0:33d4e66780c0 178 for (; exp >= 27; exp -= 27) *this *= RAPIDJSON_UINT64_C2(0X6765C793, 0XFA10079D); // 5^27
IanBenzMaxim 0:33d4e66780c0 179 for (; exp >= 13; exp -= 13) *this *= static_cast<uint32_t>(1220703125u); // 5^13
IanBenzMaxim 0:33d4e66780c0 180 if (exp > 0) *this *= kPow5[exp - 1];
IanBenzMaxim 0:33d4e66780c0 181 return *this;
IanBenzMaxim 0:33d4e66780c0 182 }
IanBenzMaxim 0:33d4e66780c0 183
IanBenzMaxim 0:33d4e66780c0 184 // Compute absolute difference of this and rhs.
IanBenzMaxim 0:33d4e66780c0 185 // Assume this != rhs
IanBenzMaxim 0:33d4e66780c0 186 bool Difference(const BigInteger& rhs, BigInteger* out) const {
IanBenzMaxim 0:33d4e66780c0 187 int cmp = Compare(rhs);
IanBenzMaxim 0:33d4e66780c0 188 RAPIDJSON_ASSERT(cmp != 0);
IanBenzMaxim 0:33d4e66780c0 189 const BigInteger *a, *b; // Makes a > b
IanBenzMaxim 0:33d4e66780c0 190 bool ret;
IanBenzMaxim 0:33d4e66780c0 191 if (cmp < 0) { a = &rhs; b = this; ret = true; }
IanBenzMaxim 0:33d4e66780c0 192 else { a = this; b = &rhs; ret = false; }
IanBenzMaxim 0:33d4e66780c0 193
IanBenzMaxim 0:33d4e66780c0 194 Type borrow = 0;
IanBenzMaxim 0:33d4e66780c0 195 for (size_t i = 0; i < a->count_; i++) {
IanBenzMaxim 0:33d4e66780c0 196 Type d = a->digits_[i] - borrow;
IanBenzMaxim 0:33d4e66780c0 197 if (i < b->count_)
IanBenzMaxim 0:33d4e66780c0 198 d -= b->digits_[i];
IanBenzMaxim 0:33d4e66780c0 199 borrow = (d > a->digits_[i]) ? 1 : 0;
IanBenzMaxim 0:33d4e66780c0 200 out->digits_[i] = d;
IanBenzMaxim 0:33d4e66780c0 201 if (d != 0)
IanBenzMaxim 0:33d4e66780c0 202 out->count_ = i + 1;
IanBenzMaxim 0:33d4e66780c0 203 }
IanBenzMaxim 0:33d4e66780c0 204
IanBenzMaxim 0:33d4e66780c0 205 return ret;
IanBenzMaxim 0:33d4e66780c0 206 }
IanBenzMaxim 0:33d4e66780c0 207
IanBenzMaxim 0:33d4e66780c0 208 int Compare(const BigInteger& rhs) const {
IanBenzMaxim 0:33d4e66780c0 209 if (count_ != rhs.count_)
IanBenzMaxim 0:33d4e66780c0 210 return count_ < rhs.count_ ? -1 : 1;
IanBenzMaxim 0:33d4e66780c0 211
IanBenzMaxim 0:33d4e66780c0 212 for (size_t i = count_; i-- > 0;)
IanBenzMaxim 0:33d4e66780c0 213 if (digits_[i] != rhs.digits_[i])
IanBenzMaxim 0:33d4e66780c0 214 return digits_[i] < rhs.digits_[i] ? -1 : 1;
IanBenzMaxim 0:33d4e66780c0 215
IanBenzMaxim 0:33d4e66780c0 216 return 0;
IanBenzMaxim 0:33d4e66780c0 217 }
IanBenzMaxim 0:33d4e66780c0 218
IanBenzMaxim 0:33d4e66780c0 219 size_t GetCount() const { return count_; }
IanBenzMaxim 0:33d4e66780c0 220 Type GetDigit(size_t index) const { RAPIDJSON_ASSERT(index < count_); return digits_[index]; }
IanBenzMaxim 0:33d4e66780c0 221 bool IsZero() const { return count_ == 1 && digits_[0] == 0; }
IanBenzMaxim 0:33d4e66780c0 222
IanBenzMaxim 0:33d4e66780c0 223 private:
IanBenzMaxim 0:33d4e66780c0 224 void AppendDecimal64(const char* begin, const char* end) {
IanBenzMaxim 0:33d4e66780c0 225 uint64_t u = ParseUint64(begin, end);
IanBenzMaxim 0:33d4e66780c0 226 if (IsZero())
IanBenzMaxim 0:33d4e66780c0 227 *this = u;
IanBenzMaxim 0:33d4e66780c0 228 else {
IanBenzMaxim 0:33d4e66780c0 229 unsigned exp = static_cast<unsigned>(end - begin);
IanBenzMaxim 0:33d4e66780c0 230 (MultiplyPow5(exp) <<= exp) += u; // *this = *this * 10^exp + u
IanBenzMaxim 0:33d4e66780c0 231 }
IanBenzMaxim 0:33d4e66780c0 232 }
IanBenzMaxim 0:33d4e66780c0 233
IanBenzMaxim 0:33d4e66780c0 234 void PushBack(Type digit) {
IanBenzMaxim 0:33d4e66780c0 235 RAPIDJSON_ASSERT(count_ < kCapacity);
IanBenzMaxim 0:33d4e66780c0 236 digits_[count_++] = digit;
IanBenzMaxim 0:33d4e66780c0 237 }
IanBenzMaxim 0:33d4e66780c0 238
IanBenzMaxim 0:33d4e66780c0 239 static uint64_t ParseUint64(const char* begin, const char* end) {
IanBenzMaxim 0:33d4e66780c0 240 uint64_t r = 0;
IanBenzMaxim 0:33d4e66780c0 241 for (const char* p = begin; p != end; ++p) {
IanBenzMaxim 0:33d4e66780c0 242 RAPIDJSON_ASSERT(*p >= '0' && *p <= '9');
IanBenzMaxim 0:33d4e66780c0 243 r = r * 10u + static_cast<unsigned>(*p - '0');
IanBenzMaxim 0:33d4e66780c0 244 }
IanBenzMaxim 0:33d4e66780c0 245 return r;
IanBenzMaxim 0:33d4e66780c0 246 }
IanBenzMaxim 0:33d4e66780c0 247
IanBenzMaxim 0:33d4e66780c0 248 // Assume a * b + k < 2^128
IanBenzMaxim 0:33d4e66780c0 249 static uint64_t MulAdd64(uint64_t a, uint64_t b, uint64_t k, uint64_t* outHigh) {
IanBenzMaxim 0:33d4e66780c0 250 #if defined(_MSC_VER) && defined(_M_AMD64)
IanBenzMaxim 0:33d4e66780c0 251 uint64_t low = _umul128(a, b, outHigh) + k;
IanBenzMaxim 0:33d4e66780c0 252 if (low < k)
IanBenzMaxim 0:33d4e66780c0 253 (*outHigh)++;
IanBenzMaxim 0:33d4e66780c0 254 return low;
IanBenzMaxim 0:33d4e66780c0 255 #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__)
IanBenzMaxim 0:33d4e66780c0 256 __extension__ typedef unsigned __int128 uint128;
IanBenzMaxim 0:33d4e66780c0 257 uint128 p = static_cast<uint128>(a) * static_cast<uint128>(b);
IanBenzMaxim 0:33d4e66780c0 258 p += k;
IanBenzMaxim 0:33d4e66780c0 259 *outHigh = static_cast<uint64_t>(p >> 64);
IanBenzMaxim 0:33d4e66780c0 260 return static_cast<uint64_t>(p);
IanBenzMaxim 0:33d4e66780c0 261 #else
IanBenzMaxim 0:33d4e66780c0 262 const uint64_t a0 = a & 0xFFFFFFFF, a1 = a >> 32, b0 = b & 0xFFFFFFFF, b1 = b >> 32;
IanBenzMaxim 0:33d4e66780c0 263 uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1;
IanBenzMaxim 0:33d4e66780c0 264 x1 += (x0 >> 32); // can't give carry
IanBenzMaxim 0:33d4e66780c0 265 x1 += x2;
IanBenzMaxim 0:33d4e66780c0 266 if (x1 < x2)
IanBenzMaxim 0:33d4e66780c0 267 x3 += (static_cast<uint64_t>(1) << 32);
IanBenzMaxim 0:33d4e66780c0 268 uint64_t lo = (x1 << 32) + (x0 & 0xFFFFFFFF);
IanBenzMaxim 0:33d4e66780c0 269 uint64_t hi = x3 + (x1 >> 32);
IanBenzMaxim 0:33d4e66780c0 270
IanBenzMaxim 0:33d4e66780c0 271 lo += k;
IanBenzMaxim 0:33d4e66780c0 272 if (lo < k)
IanBenzMaxim 0:33d4e66780c0 273 hi++;
IanBenzMaxim 0:33d4e66780c0 274 *outHigh = hi;
IanBenzMaxim 0:33d4e66780c0 275 return lo;
IanBenzMaxim 0:33d4e66780c0 276 #endif
IanBenzMaxim 0:33d4e66780c0 277 }
IanBenzMaxim 0:33d4e66780c0 278
IanBenzMaxim 0:33d4e66780c0 279 static const size_t kBitCount = 3328; // 64bit * 54 > 10^1000
IanBenzMaxim 0:33d4e66780c0 280 static const size_t kCapacity = kBitCount / sizeof(Type);
IanBenzMaxim 0:33d4e66780c0 281 static const size_t kTypeBit = sizeof(Type) * 8;
IanBenzMaxim 0:33d4e66780c0 282
IanBenzMaxim 0:33d4e66780c0 283 Type digits_[kCapacity];
IanBenzMaxim 0:33d4e66780c0 284 size_t count_;
IanBenzMaxim 0:33d4e66780c0 285 };
IanBenzMaxim 0:33d4e66780c0 286
IanBenzMaxim 0:33d4e66780c0 287 } // namespace internal
IanBenzMaxim 0:33d4e66780c0 288 RAPIDJSON_NAMESPACE_END
IanBenzMaxim 0:33d4e66780c0 289
IanBenzMaxim 0:33d4e66780c0 290 #endif // RAPIDJSON_BIGINTEGER_H_