24 cx_err_t cx_math_cmp_no_throw(
const uint8_t *a,
const uint8_t *b,
size_t length,
int *diff)
29 CX_CHECK(cx_bn_lock(length, 0));
30 CX_CHECK(cx_bn_alloc_init(&bn_a, length, a, length));
31 CX_CHECK(cx_bn_alloc_init(&bn_b, length, b, length));
32 CX_CHECK(cx_bn_cmp(bn_a, bn_b, diff));
41 cx_bn_t bn_a, bn_b, bn_r;
44 CX_CHECK(cx_bn_lock(len, 0));
45 CX_CHECK(cx_bn_alloc(&bn_r, len));
46 CX_CHECK(cx_bn_alloc_init(&bn_a, len, a, len));
47 CX_CHECK(cx_bn_alloc_init(&bn_b, len, b, len));
48 CX_CHECK_IGNORE_CARRY(cx_bn_add(bn_r, bn_a, bn_b));
49 CX_CHECK(cx_bn_export(bn_r, r, len));
58 cx_bn_t bn_a, bn_b, bn_r;
61 CX_CHECK(cx_bn_lock(len, 0));
62 CX_CHECK(cx_bn_alloc(&bn_r, len));
63 CX_CHECK(cx_bn_alloc_init(&bn_a, len, a, len));
64 CX_CHECK(cx_bn_alloc_init(&bn_b, len, b, len));
65 CX_CHECK_IGNORE_CARRY(cx_bn_sub(bn_r, bn_a, bn_b));
66 CX_CHECK(cx_bn_export(bn_r, r, len));
75 cx_bn_t bn_a, bn_b, bn_r;
78 CX_CHECK(cx_bn_lock(len, 0));
79 CX_CHECK(cx_bn_alloc(&bn_r, 2 * len));
80 CX_CHECK(cx_bn_alloc_init(&bn_a, len, a, len));
81 CX_CHECK(cx_bn_alloc_init(&bn_b, len, b, len));
82 CX_CHECK(cx_bn_mul(bn_r, bn_a, bn_b));
83 CX_CHECK(cx_bn_export(bn_r, r, 2 * len));
90 cx_err_t cx_math_addm_no_throw(
uint8_t *r,
96 cx_bn_t bn_a, bn_b, bn_m, bn_r;
99 CX_CHECK(cx_bn_lock(len, 0));
100 CX_CHECK(cx_bn_alloc(&bn_r, len));
101 CX_CHECK(cx_bn_alloc_init(&bn_a, len, a, len));
102 CX_CHECK(cx_bn_alloc_init(&bn_b, len, b, len));
103 CX_CHECK(cx_bn_alloc_init(&bn_m, len, m, len));
104 CX_CHECK(cx_bn_mod_add(bn_r, bn_a, bn_b, bn_m));
106 CX_CHECK(cx_bn_set_u32(bn_a, 0));
107 CX_CHECK(cx_bn_mod_sub(bn_r, bn_r, bn_a, bn_m));
109 CX_CHECK(cx_bn_export(bn_r, r, len));
116 cx_err_t cx_math_subm_no_throw(
uint8_t *r,
122 cx_bn_t bn_a, bn_b, bn_m, bn_r;
125 CX_CHECK(cx_bn_lock(len, 0));
126 CX_CHECK(cx_bn_alloc(&bn_r, len));
127 CX_CHECK(cx_bn_alloc_init(&bn_a, len, a, len));
128 CX_CHECK(cx_bn_alloc_init(&bn_b, len, b, len));
129 CX_CHECK(cx_bn_alloc_init(&bn_m, len, m, len));
130 CX_CHECK(cx_bn_mod_sub(bn_r, bn_a, bn_b, bn_m));
131 CX_CHECK(cx_bn_export(bn_r, r, len));
138 cx_err_t cx_math_multm_no_throw(
uint8_t *r,
144 cx_bn_t bn_a, bn_b, bn_m, bn_r;
147 CX_CHECK(cx_bn_lock(len, 0));
148 CX_CHECK(cx_bn_alloc(&bn_r, len));
149 CX_CHECK(cx_bn_alloc_init(&bn_a, len, a, len));
150 CX_CHECK(cx_bn_alloc_init(&bn_b, len, b, len));
151 CX_CHECK(cx_bn_alloc_init(&bn_m, len, m, len));
152 CX_CHECK(cx_bn_mod_mul(bn_r, bn_a, bn_b, bn_m));
153 CX_CHECK(cx_bn_export(bn_r, r, len));
160 cx_err_t cx_math_modm_no_throw(
uint8_t *v,
size_t len_v,
const uint8_t *m,
size_t len_m)
162 cx_bn_t bn_v, bn_m, bn_r;
165 CX_CHECK(cx_bn_lock(len_v, 0));
166 CX_CHECK(cx_bn_alloc(&bn_r, len_v));
167 CX_CHECK(cx_bn_alloc_init(&bn_v, len_v, v, len_v));
168 CX_CHECK(cx_bn_alloc_init(&bn_m, len_m, m, len_m));
169 CX_CHECK(cx_bn_reduce(bn_r, bn_v, bn_m));
170 CX_CHECK(cx_bn_export(bn_r, v, len_v));
177 cx_err_t cx_math_powm_no_throw(
uint8_t *r,
184 cx_bn_t bn_a, bn_m, bn_r;
187 CX_CHECK(cx_bn_lock(len, 0));
188 CX_CHECK(cx_bn_alloc_init(&bn_a, len, a, len));
189 CX_CHECK(cx_bn_alloc_init(&bn_m, len, m, len));
190 CX_CHECK(cx_bn_alloc(&bn_r, len));
191 CX_CHECK(cx_bn_mod_pow2(bn_r, bn_a, e, len_e, bn_m));
192 CX_CHECK(cx_bn_export(bn_r, r, len));
201 cx_bn_t bn_a, bn_r, bn_m;
204 CX_CHECK(cx_bn_lock(len, 0));
205 CX_CHECK(cx_bn_alloc(&bn_r, len));
206 CX_CHECK(cx_bn_alloc_init(&bn_a, len, a, len));
207 CX_CHECK(cx_bn_alloc_init(&bn_m, len, m, len));
208 CX_CHECK(cx_bn_mod_invert_nprime(bn_r, bn_a, bn_m));
209 CX_CHECK(cx_bn_export(bn_r, r, len));
216 cx_err_t cx_math_invintm_no_throw(
uint8_t *r, uint32_t a,
const uint8_t *m,
size_t len)
221 CX_CHECK(cx_bn_lock(len, 0));
222 CX_CHECK(cx_bn_alloc(&bn_r, len));
223 CX_CHECK(cx_bn_alloc_init(&bn_m, len, m, len));
224 CX_CHECK(cx_bn_mod_u32_invert(bn_r, a, bn_m));
225 CX_CHECK(cx_bn_export(bn_r, r, len));
232 cx_err_t cx_math_is_prime_no_throw(
const uint8_t *r,
size_t len,
bool *prime)
237 CX_CHECK(cx_bn_lock(len, 0));
238 CX_CHECK(cx_bn_alloc_init(&bn_r, len, r, len));
239 CX_CHECK(cx_bn_is_prime(bn_r, prime));
246 cx_err_t cx_math_next_prime_no_throw(
uint8_t *r, uint32_t len)
251 CX_CHECK(cx_bn_lock(len, 0));
252 CX_CHECK(cx_bn_alloc_init(&bn_r, len, r, len));
253 CX_CHECK(cx_bn_next_prime(bn_r));
254 CX_CHECK(cx_bn_export(bn_r, r, len));