micro-ECC for mbed, ported from GCC version from Github,
Dependents: mbed_microECC Wallet_v1
uECC_vli.h
00001 /* Copyright 2015, Kenneth MacKay. Licensed under the BSD 2-clause license. */ 00002 00003 #ifndef _UECC_VLI_H_ 00004 #define _UECC_VLI_H_ 00005 00006 #include "uECC.h" 00007 #include "types.h" 00008 00009 /* Functions for raw large-integer manipulation. These are only available 00010 if uECC.c is compiled with uECC_ENABLE_VLI_API defined to 1. */ 00011 #ifndef uECC_ENABLE_VLI_API 00012 #define uECC_ENABLE_VLI_API 0 00013 #endif 00014 00015 #ifdef __cplusplus 00016 extern "C" 00017 { 00018 #endif 00019 00020 #if uECC_ENABLE_VLI_API 00021 00022 void uECC_vli_clear(uECC_word_t *vli, wordcount_t num_words); 00023 00024 /* Constant-time comparison to zero - secure way to compare long integers */ 00025 /* Returns 1 if vli == 0, 0 otherwise. */ 00026 uECC_word_t uECC_vli_isZero(const uECC_word_t *vli, wordcount_t num_words); 00027 00028 /* Returns nonzero if bit 'bit' of vli is set. */ 00029 uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit); 00030 00031 /* Counts the number of bits required to represent vli. */ 00032 bitcount_t uECC_vli_numBits(const uECC_word_t *vli, const wordcount_t max_words); 00033 00034 /* Sets dest = src. */ 00035 void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src, wordcount_t num_words); 00036 00037 /* Constant-time comparison function - secure way to compare long integers */ 00038 /* Returns one if left == right, zero otherwise */ 00039 uECC_word_t uECC_vli_equal(const uECC_word_t *left, 00040 const uECC_word_t *right, 00041 wordcount_t num_words); 00042 00043 /* Constant-time comparison function - secure way to compare long integers */ 00044 /* Returns sign of left - right, in constant time. */ 00045 cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right, wordcount_t num_words); 00046 00047 /* Computes vli = vli >> 1. */ 00048 void uECC_vli_rshift1(uECC_word_t *vli, wordcount_t num_words); 00049 00050 /* Computes result = left + right, returning carry. Can modify in place. */ 00051 uECC_word_t uECC_vli_add(uECC_word_t *result, 00052 const uECC_word_t *left, 00053 const uECC_word_t *right, 00054 wordcount_t num_words); 00055 00056 /* Computes result = left - right, returning borrow. Can modify in place. */ 00057 uECC_word_t uECC_vli_sub(uECC_word_t *result, 00058 const uECC_word_t *left, 00059 const uECC_word_t *right, 00060 wordcount_t num_words); 00061 00062 /* Computes result = left * right. Result must be 2 * num_words long. */ 00063 void uECC_vli_mult(uECC_word_t *result, 00064 const uECC_word_t *left, 00065 const uECC_word_t *right, 00066 wordcount_t num_words); 00067 00068 /* Computes result = left^2. Result must be 2 * num_words long. */ 00069 void uECC_vli_square(uECC_word_t *result, const uECC_word_t *left, wordcount_t num_words); 00070 00071 /* Computes result = (left + right) % mod. 00072 Assumes that left < mod and right < mod, and that result does not overlap mod. */ 00073 void uECC_vli_modAdd(uECC_word_t *result, 00074 const uECC_word_t *left, 00075 const uECC_word_t *right, 00076 const uECC_word_t *mod, 00077 wordcount_t num_words); 00078 00079 /* Computes result = (left - right) % mod. 00080 Assumes that left < mod and right < mod, and that result does not overlap mod. */ 00081 void uECC_vli_modSub(uECC_word_t *result, 00082 const uECC_word_t *left, 00083 const uECC_word_t *right, 00084 const uECC_word_t *mod, 00085 wordcount_t num_words); 00086 00087 /* Computes result = product % mod, where product is 2N words long. 00088 Currently only designed to work for mod == curve->p or curve_n. */ 00089 void uECC_vli_mmod(uECC_word_t *result, 00090 uECC_word_t *product, 00091 const uECC_word_t *mod, 00092 wordcount_t num_words); 00093 00094 /* Calculates result = product (mod curve->p), where product is up to 00095 2 * curve->num_words long. */ 00096 void uECC_vli_mmod_fast(uECC_word_t *result, uECC_word_t *product, uECC_Curve curve); 00097 00098 /* Computes result = (left * right) % mod. 00099 Currently only designed to work for mod == curve->p or curve_n. */ 00100 void uECC_vli_modMult(uECC_word_t *result, 00101 const uECC_word_t *left, 00102 const uECC_word_t *right, 00103 const uECC_word_t *mod, 00104 wordcount_t num_words); 00105 00106 /* Computes result = (left * right) % curve->p. */ 00107 void uECC_vli_modMult_fast(uECC_word_t *result, 00108 const uECC_word_t *left, 00109 const uECC_word_t *right, 00110 uECC_Curve curve); 00111 00112 /* Computes result = left^2 % mod. 00113 Currently only designed to work for mod == curve->p or curve_n. */ 00114 void uECC_vli_modSquare(uECC_word_t *result, 00115 const uECC_word_t *left, 00116 const uECC_word_t *mod, 00117 wordcount_t num_words); 00118 00119 /* Computes result = left^2 % curve->p. */ 00120 void uECC_vli_modSquare_fast(uECC_word_t *result, const uECC_word_t *left, uECC_Curve curve); 00121 00122 /* Computes result = (1 / input) % mod.*/ 00123 void uECC_vli_modInv(uECC_word_t *result, 00124 const uECC_word_t *input, 00125 const uECC_word_t *mod, 00126 wordcount_t num_words); 00127 00128 #if uECC_SUPPORT_COMPRESSED_POINT 00129 /* Calculates a = sqrt(a) (mod curve->p) */ 00130 void uECC_vli_mod_sqrt(uECC_word_t *a, uECC_Curve curve); 00131 #endif 00132 00133 /* Converts an integer in uECC native format to big-endian bytes. */ 00134 void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes, const uECC_word_t *native); 00135 /* Converts big-endian bytes to an integer in uECC native format. */ 00136 void uECC_vli_bytesToNative(uECC_word_t *native, const uint8_t *bytes, int num_bytes); 00137 00138 unsigned uECC_curve_num_words(uECC_Curve curve); 00139 unsigned uECC_curve_num_bytes(uECC_Curve curve); 00140 unsigned uECC_curve_num_bits(uECC_Curve curve); 00141 unsigned uECC_curve_num_n_words(uECC_Curve curve); 00142 unsigned uECC_curve_num_n_bytes(uECC_Curve curve); 00143 unsigned uECC_curve_num_n_bits(uECC_Curve curve); 00144 00145 const uECC_word_t *uECC_curve_p(uECC_Curve curve); 00146 const uECC_word_t *uECC_curve_n(uECC_Curve curve); 00147 const uECC_word_t *uECC_curve_G(uECC_Curve curve); 00148 const uECC_word_t *uECC_curve_b(uECC_Curve curve); 00149 00150 int uECC_valid_point(const uECC_word_t *point, uECC_Curve curve); 00151 00152 /* Multiplies a point by a scalar. Points are represented by the X coordinate followed by 00153 the Y coordinate in the same array, both coordinates are curve->num_words long. Note 00154 that scalar must be curve->num_n_words long (NOT curve->num_words). */ 00155 void uECC_point_mult(uECC_word_t *result, 00156 const uECC_word_t *point, 00157 const uECC_word_t *scalar, 00158 uECC_Curve curve); 00159 00160 /* Generates a random integer in the range 0 < random < top. 00161 Both random and top have num_words words. */ 00162 int uECC_generate_random_int(uECC_word_t *random, 00163 const uECC_word_t *top, 00164 wordcount_t num_words); 00165 00166 #endif /* uECC_ENABLE_VLI_API */ 00167 00168 #ifdef __cplusplus 00169 } /* end of extern "C" */ 00170 #endif 00171 00172 #endif /* _UECC_VLI_H_ */
Generated on Wed Jul 13 2022 03:48:20 by
1.7.2