23 #include "BigNumberUtil.h"
24 #include "utility/EndianUtil.h"
55 const uint8_t *bytes,
size_t len)
57 #if BIGNUMBER_LIMB_8BIT
59 memcpy(limbs, bytes, len);
60 memset(limbs + len, 0, count - len);
62 memcpy(limbs, bytes, count);
64 #elif CRYPTO_LITTLE_ENDIAN
65 count *=
sizeof(limb_t);
67 memcpy(limbs, bytes, len);
68 memset(((uint8_t *)limbs) + len, 0, count - len);
70 memcpy(limbs, bytes, count);
72 #elif BIGNUMBER_LIMB_16BIT
73 while (count > 0 && len >= 2) {
74 *limbs++ = ((limb_t)(bytes[0])) |
75 (((limb_t)(bytes[1])) << 8);
80 if (count > 0 && len == 1) {
81 *limbs++ = ((limb_t)(bytes[0]));
88 #elif BIGNUMBER_LIMB_32BIT
89 while (count > 0 && len >= 4) {
90 *limbs++ = ((limb_t)(bytes[0])) |
91 (((limb_t)(bytes[1])) << 8) |
92 (((limb_t)(bytes[2])) << 16) |
93 (((limb_t)(bytes[3])) << 24);
100 *limbs++ = ((limb_t)(bytes[0])) |
101 (((limb_t)(bytes[1])) << 8) |
102 (((limb_t)(bytes[2])) << 16);
103 }
else if (len == 2) {
104 *limbs++ = ((limb_t)(bytes[0])) |
105 (((limb_t)(bytes[1])) << 8);
106 }
else if (len == 1) {
107 *limbs++ = ((limb_t)(bytes[0]));
134 const uint8_t *bytes,
size_t len)
136 #if BIGNUMBER_LIMB_8BIT
137 while (count > 0 && len > 0) {
140 *limbs++ = bytes[len];
142 memset(limbs, 0, count);
143 #elif BIGNUMBER_LIMB_16BIT
145 while (count > 0 && len >= 2) {
149 *limbs++ = ((limb_t)(bytes[1])) |
150 (((limb_t)(bytes[0])) << 8);
152 if (count > 0 && len == 1) {
155 *limbs++ = (limb_t)(bytes[0]);
157 memset(limbs, 0, count *
sizeof(limb_t));
158 #elif BIGNUMBER_LIMB_32BIT
160 while (count > 0 && len >= 4) {
164 *limbs++ = ((limb_t)(bytes[3])) |
165 (((limb_t)(bytes[2])) << 8) |
166 (((limb_t)(bytes[1])) << 16) |
167 (((limb_t)(bytes[0])) << 24);
173 *limbs++ = ((limb_t)(bytes[2])) |
174 (((limb_t)(bytes[1])) << 8) |
175 (((limb_t)(bytes[0])) << 16);
176 }
else if (len == 2) {
179 *limbs++ = ((limb_t)(bytes[1])) |
180 (((limb_t)(bytes[0])) << 8);
181 }
else if (len == 1) {
184 *limbs++ = (limb_t)(bytes[0]);
187 memset(limbs, 0, count *
sizeof(limb_t));
208 const limb_t *limbs,
size_t count)
210 #if BIGNUMBER_LIMB_8BIT
212 memcpy(bytes, limbs, len);
214 memcpy(bytes, limbs, count);
215 memset(bytes + count, 0, len - count);
217 #elif CRYPTO_LITTLE_ENDIAN
218 count *=
sizeof(limb_t);
220 memcpy(bytes, limbs, len);
222 memcpy(bytes, limbs, count);
223 memset(bytes + count, 0, len - count);
225 #elif BIGNUMBER_LIMB_16BIT
227 while (count > 0 && len >= 2) {
229 bytes[0] = (uint8_t)word;
230 bytes[1] = (uint8_t)(word >> 8);
235 if (count > 0 && len == 1) {
236 bytes[0] = (uint8_t)(*limbs);
240 memset(bytes, 0, len);
241 #elif BIGNUMBER_LIMB_32BIT
243 while (count > 0 && len >= 4) {
245 bytes[0] = (uint8_t)word;
246 bytes[1] = (uint8_t)(word >> 8);
247 bytes[2] = (uint8_t)(word >> 16);
248 bytes[3] = (uint8_t)(word >> 24);
256 bytes[0] = (uint8_t)word;
257 bytes[1] = (uint8_t)(word >> 8);
258 bytes[2] = (uint8_t)(word >> 16);
261 }
else if (len == 2) {
263 bytes[0] = (uint8_t)word;
264 bytes[1] = (uint8_t)(word >> 8);
267 }
else if (len == 1) {
268 bytes[0] = (uint8_t)(*limbs);
273 memset(bytes, 0, len);
294 const limb_t *limbs,
size_t count)
296 #if BIGNUMBER_LIMB_8BIT
298 size_t size = len - count;
299 memset(bytes, 0, size);
302 }
else if (len < count) {
308 *bytes++ = *(--limbs);
310 #elif BIGNUMBER_LIMB_16BIT
311 size_t countBytes = count *
sizeof(limb_t);
313 if (len >= countBytes) {
314 size_t size = len - countBytes;
315 memset(bytes, 0, size);
320 count = len /
sizeof(limb_t);
323 *bytes++ = (uint8_t)(*limbs);
328 *bytes++ = (uint8_t)(word >> 8);
329 *bytes++ = (uint8_t)word;
331 #elif BIGNUMBER_LIMB_32BIT
332 size_t countBytes = count *
sizeof(limb_t);
334 if (len >= countBytes) {
335 size_t size = len - countBytes;
336 memset(bytes, 0, size);
341 count = len /
sizeof(limb_t);
343 if ((len & 3) == 3) {
345 *bytes++ = (uint8_t)(word >> 16);
346 *bytes++ = (uint8_t)(word >> 8);
347 *bytes++ = (uint8_t)word;
348 }
else if ((len & 3) == 2) {
350 *bytes++ = (uint8_t)(word >> 8);
351 *bytes++ = (uint8_t)word;
352 }
else if ((len & 3) == 1) {
353 *bytes++ = (uint8_t)(*limbs);
359 *bytes++ = (uint8_t)(word >> 24);
360 *bytes++ = (uint8_t)(word >> 16);
361 *bytes++ = (uint8_t)(word >> 8);
362 *bytes++ = (uint8_t)word;
static void packBE(uint8_t *bytes, size_t len, const limb_t *limbs, size_t count)
Packs the big-endian byte representation of a big number into a byte array.
static void unpackLE(limb_t *limbs, size_t count, const uint8_t *bytes, size_t len)
Unpacks the little-endian byte representation of a big number into a limb array.
static void unpackBE(limb_t *limbs, size_t count, const uint8_t *bytes, size_t len)
Unpacks the big-endian byte representation of a big number into a limb array.
static void packLE(uint8_t *bytes, size_t len, const limb_t *limbs, size_t count)
Packs the little-endian byte representation of a big number into a byte array.