Embedded SDK
Embedded SDK
lcx_math.h
Go to the documentation of this file.
1 
2 /*******************************************************************************
3  * Ledger Nano S - Secure firmware
4  * (c) 2022 Ledger
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  ********************************************************************************/
18 
24 #ifndef LCX_MATH_H
25 #define LCX_MATH_H
26 
27 #ifdef HAVE_MATH
28 
29 #include "lcx_wrappers.h"
30 #include "ox_bn.h"
31 
54 WARN_UNUSED_RESULT cx_err_t cx_math_cmp_no_throw(const uint8_t *a,
55  const uint8_t *b,
56  size_t length,
57  int *diff);
58 
63 DEPRECATED static inline int32_t cx_math_cmp(const uint8_t *a, const uint8_t *b, size_t length)
64 {
65  int diff;
66  CX_THROW(cx_math_cmp_no_throw(a, b, length, &diff));
67  return diff;
68 }
69 
89 WARN_UNUSED_RESULT cx_err_t cx_math_add_no_throw(uint8_t *r,
90  const uint8_t *a,
91  const uint8_t *b,
92  size_t len);
93 
119 static inline uint32_t cx_math_add(uint8_t *r, const uint8_t *a, const uint8_t *b, size_t len)
120 {
121  cx_err_t error = cx_math_add_no_throw(r, a, b, len);
122  if (error && error != CX_CARRY) {
123  THROW(error);
124  }
125  return (error == CX_CARRY);
126 }
127 
147 WARN_UNUSED_RESULT cx_err_t cx_math_sub_no_throw(uint8_t *r,
148  const uint8_t *a,
149  const uint8_t *b,
150  size_t len);
151 
177 static inline uint32_t cx_math_sub(uint8_t *r, const uint8_t *a, const uint8_t *b, size_t len)
178 {
179  cx_err_t error = cx_math_sub_no_throw(r, a, b, len);
180  if (error && error != CX_CARRY) {
181  THROW(error);
182  }
183  return (error == CX_CARRY);
184 }
185 
205 WARN_UNUSED_RESULT cx_err_t cx_math_mult_no_throw(uint8_t *r,
206  const uint8_t *a,
207  const uint8_t *b,
208  size_t len);
209 
214 DEPRECATED static inline void cx_math_mult(uint8_t *r,
215  const uint8_t *a,
216  const uint8_t *b,
217  size_t len)
218 {
219  CX_THROW(cx_math_mult_no_throw(r, a, b, len));
220 }
221 
246 WARN_UNUSED_RESULT cx_err_t
247 cx_math_addm_no_throw(uint8_t *r, const uint8_t *a, const uint8_t *b, const uint8_t *m, size_t len);
248 
253 DEPRECATED static inline void cx_math_addm(uint8_t *r,
254  const uint8_t *a,
255  const uint8_t *b,
256  const uint8_t *m,
257  size_t len)
258 {
259  CX_THROW(cx_math_addm_no_throw(r, a, b, m, len));
260 }
261 
286 WARN_UNUSED_RESULT cx_err_t
287 cx_math_subm_no_throw(uint8_t *r, const uint8_t *a, const uint8_t *b, const uint8_t *m, size_t len);
288 
293 DEPRECATED static inline void cx_math_subm(uint8_t *r,
294  const uint8_t *a,
295  const uint8_t *b,
296  const uint8_t *m,
297  size_t len)
298 {
299  CX_THROW(cx_math_subm_no_throw(r, a, b, m, len));
300 }
301 
326 WARN_UNUSED_RESULT cx_err_t cx_math_multm_no_throw(uint8_t *r,
327  const uint8_t *a,
328  const uint8_t *b,
329  const uint8_t *m,
330  size_t len);
331 
336 DEPRECATED static inline void cx_math_multm(uint8_t *r,
337  const uint8_t *a,
338  const uint8_t *b,
339  const uint8_t *m,
340  size_t len)
341 {
342  CX_THROW(cx_math_multm_no_throw(r, a, b, m, len));
343 }
344 
366 WARN_UNUSED_RESULT cx_err_t cx_math_modm_no_throw(uint8_t *v,
367  size_t len_v,
368  const uint8_t *m,
369  size_t len_m);
370 
375 DEPRECATED static inline void cx_math_modm(uint8_t *v, size_t len_v, const uint8_t *m, size_t len_m)
376 {
377  CX_THROW(cx_math_modm_no_throw(v, len_v, m, len_m));
378 }
379 
405 WARN_UNUSED_RESULT cx_err_t cx_math_powm_no_throw(uint8_t *r,
406  const uint8_t *a,
407  const uint8_t *e,
408  size_t len_e,
409  const uint8_t *m,
410  size_t len);
411 
416 DEPRECATED static inline void cx_math_powm(uint8_t *r,
417  const uint8_t *a,
418  const uint8_t *e,
419  size_t len_e,
420  const uint8_t *m,
421  size_t len)
422 {
423  CX_THROW(cx_math_powm_no_throw(r, a, e, len_e, m, len));
424 }
425 
447 WARN_UNUSED_RESULT cx_err_t cx_math_invprimem_no_throw(uint8_t *r,
448  const uint8_t *a,
449  const uint8_t *m,
450  size_t len);
451 
456 DEPRECATED static inline void cx_math_invprimem(uint8_t *r,
457  const uint8_t *a,
458  const uint8_t *m,
459  size_t len)
460 {
461  CX_THROW(cx_math_invprimem_no_throw(r, a, m, len));
462 }
463 
486 WARN_UNUSED_RESULT cx_err_t cx_math_invintm_no_throw(uint8_t *r,
487  uint32_t a,
488  const uint8_t *m,
489  size_t len);
490 
495 DEPRECATED static inline void cx_math_invintm(uint8_t *r, uint32_t a, const uint8_t *m, size_t len)
496 {
497  CX_THROW(cx_math_invintm_no_throw(r, a, m, len));
498 }
499 
519 WARN_UNUSED_RESULT cx_err_t cx_math_is_prime_no_throw(const uint8_t *r, size_t len, bool *prime);
520 
525 DEPRECATED static inline bool cx_math_is_prime(const uint8_t *r, size_t len)
526 {
527  bool prime;
528  CX_THROW(cx_math_is_prime_no_throw(r, len, &prime));
529  return prime;
530 }
531 
549 WARN_UNUSED_RESULT cx_err_t cx_math_next_prime_no_throw(uint8_t *r, uint32_t len);
550 
555 DEPRECATED static inline void cx_math_next_prime(uint8_t *r, uint32_t len)
556 {
557  CX_THROW(cx_math_next_prime_no_throw(r, len));
558 }
559 
569 static inline bool cx_math_is_zero(const uint8_t *a, size_t len)
570 {
571  uint32_t i;
572  for (i = 0; i < len; i++) {
573  if (a[i] != 0) {
574  return 0;
575  }
576  }
577  return 1;
578 }
579 
580 #endif // HAVE_MATH
581 
582 #endif // LCX_MATH_H
#define CX_THROW(call)
Definition: lcx_wrappers.h:15
unsigned char uint8_t
Definition: usbd_conf.h:53