ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
BigNumberUtil.h
1 /*
2  * Copyright (C) 2015 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef CRYPTO_BIGNUMBERUTIL_h
24 #define CRYPTO_BIGNUMBERUTIL_h
25 
26 #include <inttypes.h>
27 #include <stddef.h>
28 
29 // Define exactly one of these to 1 to set the size of the basic limb type.
30 #if defined(__AVR__)
31 // 16-bit limbs seem to give the best performance on 8-bit AVR micros.
32 #define BIGNUMBER_LIMB_8BIT 0
33 #define BIGNUMBER_LIMB_16BIT 1
34 #define BIGNUMBER_LIMB_32BIT 0
35 #define BIGNUMBER_LIMB_64BIT 0
36 #elif defined(__GNUC__) && __WORDSIZE == 64
37 // 64-bit system with 128-bit double limbs.
38 #define BIGNUMBER_LIMB_8BIT 0
39 #define BIGNUMBER_LIMB_16BIT 0
40 #define BIGNUMBER_LIMB_32BIT 0
41 #define BIGNUMBER_LIMB_64BIT 1
42 #else
43 // On all other platforms, assume 32-bit is best.
44 #define BIGNUMBER_LIMB_8BIT 0
45 #define BIGNUMBER_LIMB_16BIT 0
46 #define BIGNUMBER_LIMB_32BIT 1
47 #define BIGNUMBER_LIMB_64BIT 0
48 #endif
49 
50 // Define the limb types to use on this platform.
51 #if BIGNUMBER_LIMB_8BIT
52 typedef uint8_t limb_t;
53 typedef int8_t slimb_t;
54 typedef uint16_t dlimb_t;
55 #elif BIGNUMBER_LIMB_16BIT
56 typedef uint16_t limb_t;
57 typedef int16_t slimb_t;
58 typedef uint32_t dlimb_t;
59 #elif BIGNUMBER_LIMB_32BIT
60 typedef uint32_t limb_t;
61 typedef int32_t slimb_t;
62 typedef uint64_t dlimb_t;
63 #elif BIGNUMBER_LIMB_64BIT
64 typedef uint64_t limb_t;
65 typedef int64_t slimb_t;
66 typedef unsigned __int128 dlimb_t;
67 #else
68 #error "limb_t must be 8, 16, 32, or 64 bits in size"
69 #endif
70 
72 {
73 public:
74  static void unpackLE(limb_t *limbs, size_t count,
75  const uint8_t *bytes, size_t len);
76  static void unpackBE(limb_t *limbs, size_t count,
77  const uint8_t *bytes, size_t len);
78  static void packLE(uint8_t *bytes, size_t len,
79  const limb_t *limbs, size_t count);
80  static void packBE(uint8_t *bytes, size_t len,
81  const limb_t *limbs, size_t count);
82 
83  static limb_t add(limb_t *result, const limb_t *x,
84  const limb_t *y, size_t size);
85  static limb_t sub(limb_t *result, const limb_t *x,
86  const limb_t *y, size_t size);
87  static void mul(limb_t *result, const limb_t *x, size_t xcount,
88  const limb_t *y, size_t ycount);
89  static void reduceQuick(limb_t *result, const limb_t *x,
90  const limb_t *y, size_t size);
91 
92  static limb_t add_P(limb_t *result, const limb_t *x,
93  const limb_t *y, size_t size);
94  static limb_t sub_P(limb_t *result, const limb_t *x,
95  const limb_t *y, size_t size);
96  static void mul_P(limb_t *result, const limb_t *x, size_t xcount,
97  const limb_t *y, size_t ycount);
98  static void reduceQuick_P(limb_t *result, const limb_t *x,
99  const limb_t *y, size_t size);
100 
101  static limb_t isZero(const limb_t *x, size_t size);
102 
103 private:
104  // Constructor and destructor are private - cannot instantiate this class.
105  BigNumberUtil() {}
106  ~BigNumberUtil() {}
107 };
108 
109 #endif
static void reduceQuick_P(limb_t *result, const limb_t *x, const limb_t *y, size_t size)
Reduces x modulo y using subtraction where y is in program memory.
static limb_t add(limb_t *result, const limb_t *x, const limb_t *y, size_t size)
Adds two big numbers.
static limb_t sub_P(limb_t *result, const limb_t *x, const limb_t *y, size_t size)
Subtracts one big number from another where one is in program memory.
static void reduceQuick(limb_t *result, const limb_t *x, const limb_t *y, size_t size)
Reduces x modulo y using subtraction.
static limb_t sub(limb_t *result, const limb_t *x, const limb_t *y, size_t size)
Subtracts one big number from another.
Utilities to assist with implementing big number arithmetic.
Definition: BigNumberUtil.h:71
static void mul_P(limb_t *result, const limb_t *x, size_t xcount, const limb_t *y, size_t ycount)
Multiplies two big numbers where one is in program memory.
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 mul(limb_t *result, const limb_t *x, size_t xcount, const limb_t *y, size_t ycount)
Multiplies two big numbers.
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.
static limb_t isZero(const limb_t *x, size_t size)
Determine if a big number is zero.
static limb_t add_P(limb_t *result, const limb_t *x, const limb_t *y, size_t size)
Adds two big numbers where one of them is in program memory.