• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015, Kenneth MacKay. Licensed under the BSD 2-clause license. */
2 
3 #ifndef _UECC_VLI_H_
4 #define _UECC_VLI_H_
5 
6 #include "uECC.h"
7 #include "types.h"
8 
9 /* Functions for raw large-integer manipulation. These are only available
10    if uECC.c is compiled with uECC_ENABLE_VLI_API defined to 1. */
11 #ifndef uECC_ENABLE_VLI_API
12     #define uECC_ENABLE_VLI_API 0
13 #endif
14 
15 #ifdef __cplusplus
16 extern "C"
17 {
18 #endif
19 
20 #if uECC_ENABLE_VLI_API
21 
22 void uECC_vli_clear(uECC_word_t *vli, wordcount_t num_words);
23 
24 /* Constant-time comparison to zero - secure way to compare long integers */
25 /* Returns 1 if vli == 0, 0 otherwise. */
26 uECC_word_t uECC_vli_isZero(const uECC_word_t *vli, wordcount_t num_words);
27 
28 /* Returns nonzero if bit 'bit' of vli is set. */
29 uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit);
30 
31 /* Counts the number of bits required to represent vli. */
32 bitcount_t uECC_vli_numBits(const uECC_word_t *vli, const wordcount_t max_words);
33 
34 /* Sets dest = src. */
35 void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src, wordcount_t num_words);
36 
37 /* Constant-time comparison function - secure way to compare long integers */
38 /* Returns one if left == right, zero otherwise */
39 uECC_word_t uECC_vli_equal(const uECC_word_t *left,
40                            const uECC_word_t *right,
41                            wordcount_t num_words);
42 
43 /* Constant-time comparison function - secure way to compare long integers */
44 /* Returns sign of left - right, in constant time. */
45 cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right, wordcount_t num_words);
46 
47 /* Computes vli = vli >> 1. */
48 void uECC_vli_rshift1(uECC_word_t *vli, wordcount_t num_words);
49 
50 /* Computes result = left + right, returning carry. Can modify in place. */
51 uECC_word_t uECC_vli_add(uECC_word_t *result,
52                          const uECC_word_t *left,
53                          const uECC_word_t *right,
54                          wordcount_t num_words);
55 
56 /* Computes result = left - right, returning borrow. Can modify in place. */
57 uECC_word_t uECC_vli_sub(uECC_word_t *result,
58                          const uECC_word_t *left,
59                          const uECC_word_t *right,
60                          wordcount_t num_words);
61 
62 /* Computes result = left * right. Result must be 2 * num_words long. */
63 void uECC_vli_mult(uECC_word_t *result,
64                    const uECC_word_t *left,
65                    const uECC_word_t *right,
66                    wordcount_t num_words);
67 
68 /* Computes result = left^2. Result must be 2 * num_words long. */
69 void uECC_vli_square(uECC_word_t *result, const uECC_word_t *left, wordcount_t num_words);
70 
71 /* Computes result = (left + right) % mod.
72    Assumes that left < mod and right < mod, and that result does not overlap mod. */
73 void uECC_vli_modAdd(uECC_word_t *result,
74                      const uECC_word_t *left,
75                      const uECC_word_t *right,
76                      const uECC_word_t *mod,
77                      wordcount_t num_words);
78 
79 /* Computes result = (left - right) % mod.
80    Assumes that left < mod and right < mod, and that result does not overlap mod. */
81 void uECC_vli_modSub(uECC_word_t *result,
82                      const uECC_word_t *left,
83                      const uECC_word_t *right,
84                      const uECC_word_t *mod,
85                      wordcount_t num_words);
86 
87 /* Computes result = product % mod, where product is 2N words long.
88    Currently only designed to work for mod == curve->p or curve_n. */
89 void uECC_vli_mmod(uECC_word_t *result,
90                    uECC_word_t *product,
91                    const uECC_word_t *mod,
92                    wordcount_t num_words);
93 
94 /* Calculates result = product (mod curve->p), where product is up to
95    2 * curve->num_words long. */
96 void uECC_vli_mmod_fast(uECC_word_t *result, uECC_word_t *product, uECC_Curve curve);
97 
98 /* Computes result = (left * right) % mod.
99    Currently only designed to work for mod == curve->p or curve_n. */
100 void uECC_vli_modMult(uECC_word_t *result,
101                       const uECC_word_t *left,
102                       const uECC_word_t *right,
103                       const uECC_word_t *mod,
104                       wordcount_t num_words);
105 
106 /* Computes result = (left * right) % curve->p. */
107 void uECC_vli_modMult_fast(uECC_word_t *result,
108                            const uECC_word_t *left,
109                            const uECC_word_t *right,
110                            uECC_Curve curve);
111 
112 /* Computes result = left^2 % mod.
113    Currently only designed to work for mod == curve->p or curve_n. */
114 void uECC_vli_modSquare(uECC_word_t *result,
115                         const uECC_word_t *left,
116                         const uECC_word_t *mod,
117                         wordcount_t num_words);
118 
119 /* Computes result = left^2 % curve->p. */
120 void uECC_vli_modSquare_fast(uECC_word_t *result, const uECC_word_t *left, uECC_Curve curve);
121 
122 /* Computes result = (1 / input) % mod.*/
123 void uECC_vli_modInv(uECC_word_t *result,
124                      const uECC_word_t *input,
125                      const uECC_word_t *mod,
126                      wordcount_t num_words);
127 
128 #if uECC_SUPPORT_COMPRESSED_POINT
129 /* Calculates a = sqrt(a) (mod curve->p) */
130 void uECC_vli_mod_sqrt(uECC_word_t *a, uECC_Curve curve);
131 #endif
132 
133 /* Converts an integer in uECC native format to big-endian bytes. */
134 void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes, const uECC_word_t *native);
135 /* Converts big-endian bytes to an integer in uECC native format. */
136 void uECC_vli_bytesToNative(uECC_word_t *native, const uint8_t *bytes, int num_bytes);
137 
138 unsigned uECC_curve_num_words(uECC_Curve curve);
139 unsigned uECC_curve_num_bytes(uECC_Curve curve);
140 unsigned uECC_curve_num_bits(uECC_Curve curve);
141 unsigned uECC_curve_num_n_words(uECC_Curve curve);
142 unsigned uECC_curve_num_n_bytes(uECC_Curve curve);
143 unsigned uECC_curve_num_n_bits(uECC_Curve curve);
144 
145 const uECC_word_t *uECC_curve_p(uECC_Curve curve);
146 const uECC_word_t *uECC_curve_n(uECC_Curve curve);
147 const uECC_word_t *uECC_curve_G(uECC_Curve curve);
148 const uECC_word_t *uECC_curve_b(uECC_Curve curve);
149 
150 int uECC_valid_point(const uECC_word_t *point, uECC_Curve curve);
151 
152 /* Multiplies a point by a scalar. Points are represented by the X coordinate followed by
153    the Y coordinate in the same array, both coordinates are curve->num_words long. Note
154    that scalar must be curve->num_n_words long (NOT curve->num_words). */
155 void uECC_point_mult(uECC_word_t *result,
156                      const uECC_word_t *point,
157                      const uECC_word_t *scalar,
158                      uECC_Curve curve);
159 
160 /* Generates a random integer in the range 0 < random < top.
161    Both random and top have num_words words. */
162 int uECC_generate_random_int(uECC_word_t *random,
163                              const uECC_word_t *top,
164                              wordcount_t num_words);
165 
166 #endif /* uECC_ENABLE_VLI_API */
167 
168 #ifdef __cplusplus
169 } /* end of extern "C" */
170 #endif
171 
172 #endif /* _UECC_VLI_H_ */
173