lcx_math.h | Developers

lcx_math.h

Back to the files list

Basic arithmetic. More...

Functions

cx_err_t cx_math_cmp_no_throw (const uint8_t *a, const uint8_t *b, size_t length, int *diff)
 Compares two integers represented as byte arrays. More...
static int32_t cx_math_cmp (const uint8_t *a, const uint8_t *b, size_t length)
 Compares two integers represented as byte arrays. More...
cx_err_t cx_math_add_no_throw (uint8_t *r, const uint8_t *a, const uint8_t *b, size_t len)
 Adds two integers represented as byte arrays. More...
static uint32_t cx_math_add (uint8_t *r, const uint8_t *a, const uint8_t *b, size_t len)
 Adds two integers represented as byte arrays. More...
cx_err_t cx_math_sub_no_throw (uint8_t *r, const uint8_t *a, const uint8_t *b, size_t len)
 Subtracts two integers represented as byte arrays. More...
static uint32_t cx_math_sub (uint8_t *r, const uint8_t *a, const uint8_t *b, size_t len)
 Subtracts two integers represented as byte arrays. More...
cx_err_t cx_math_mult_no_throw (uint8_t *r, const uint8_t *a, const uint8_t *b, size_t len)
 Multiplies two integers represented as byte arrays. More...
static void cx_math_mult (uint8_t *r, const uint8_t *a, const uint8_t *b, size_t len)
 Multiplies two integers represented as byte arrays. More...
cx_err_t cx_math_addm_no_throw (uint8_t *r, const uint8_t *a, const uint8_t *b, const uint8_t *m, size_t len)
 Performs a modular addition of two integers represented as byte arrays. More...
static void cx_math_addm (uint8_t *r, const uint8_t *a, const uint8_t *b, const uint8_t *m, size_t len)
 Performs a modular addition of two integers represented as byte arrays. More...
cx_err_t cx_math_subm_no_throw (uint8_t *r, const uint8_t *a, const uint8_t *b, const uint8_t *m, size_t len)
 Performs a modular subtraction of two integers represented as byte arrays. More...
static void cx_math_subm (uint8_t *r, const uint8_t *a, const uint8_t *b, const uint8_t *m, size_t len)
 Performs a modular subtraction of two integers represented as byte arrays. More...
cx_err_t cx_math_multm_no_throw (uint8_t *r, const uint8_t *a, const uint8_t *b, const uint8_t *m, size_t len)
 Performs a modular multiplication of two integers represented as byte arrays. More...
static void cx_math_multm (uint8_t *r, const uint8_t *a, const uint8_t *b, const uint8_t *m, size_t len)
 Performs a modular multiplication of two integers represented as byte arrays. More...
cx_err_t cx_math_modm_no_throw (uint8_t *v, size_t len_v, const uint8_t *m, size_t len_m)
 Performs a modular reduction. More...
static void cx_math_modm (uint8_t *v, size_t len_v, const uint8_t *m, size_t len_m)
 Performs a modular reduction. More...
cx_err_t cx_math_powm_no_throw (uint8_t *r, const uint8_t *a, const uint8_t *e, size_t len_e, const uint8_t *m, size_t len)
 Performs a modular exponentiation. More...
static void cx_math_powm (uint8_t *r, const uint8_t *a, const uint8_t *e, size_t len_e, const uint8_t *m, size_t len)
 Performs a modular exponentiation. More...
cx_err_t cx_math_invprimem_no_throw (uint8_t *r, const uint8_t *a, const uint8_t *m, size_t len)
 Computes the modular inverse with a prime modulus. More...
static void cx_math_invprimem (uint8_t *r, const uint8_t *a, const uint8_t *m, size_t len)
 Computes the modular inverse with a prime modulus. More...
cx_err_t cx_math_invintm_no_throw (uint8_t *r, uint32_t a, const uint8_t *m, size_t len)
 Computes the modular inverse. More...
static void cx_math_invintm (uint8_t *r, uint32_t a, const uint8_t *m, size_t len)
 Computes the modular inverse. More...
cx_err_t cx_math_is_prime_no_throw (const uint8_t *r, size_t len, bool *prime)
 Checks whether a number is probably prime. More...
static bool cx_math_is_prime (const uint8_t *r, size_t len)
 Checks whether a number is probably prime. More...
cx_err_t cx_math_next_prime_no_throw (uint8_t *r, uint32_t len)
 Computes the next prime after a given number. More...
static void cx_math_next_prime (uint8_t *r, uint32_t len)
 Computes the next prime after a given number. More...
static bool cx_math_is_zero (const uint8_t *a, size_t len)
 Checks whether the byte array of an integer is all zero. More...

Detailed Description

Basic arithmetic.

Function Documentation

cx_math_add()

static uint32_t cx_math_add ( uint8_t *  r,
const uint8_t *  a,
const uint8_t *  b,
size_t  len 
)

Adds two integers represented as byte arrays.

This function throws an exception if the computation doesn't succeed.

Warning
It is recommended to use cx_math_add_no_throw rather than this function.
Parameters
[out]rBuffer for the result.
[in]aPointer to the first integer.
[in]bPointer to the second integer.
[in]lenNumber of bytes taken into account for the addition.
Returns
1 if there is a carry, 0 otherwise.
Exceptions
CX_NOT_UNLOCKED
CX_INVALID_PARAMETER_SIZE
CX_NOT_LOCKED
CX_MEMORY_FULL
CX_INVALID_PARAMETER

cx_math_add_no_throw()

cx_err_t cx_math_add_no_throw ( uint8_t *  r,
const uint8_t *  a,
const uint8_t *  b,
size_t  len 
)

Adds two integers represented as byte arrays.

Parameters
[out]rBuffer for the result.
[in]aPointer to the first integer.
[in]bPointer to the second integer.
[in]lenNumber of bytes taken into account for the addition.
Returns
Error code:
  • CX_OK on success
  • CX_NOT_UNLOCKED
  • CX_INVALID_PARAMETER_SIZE
  • CX_NOT_LOCKED
  • CX_MEMORY_FULL
  • CX_INVALID_PARAMETER

cx_math_addm()

static void cx_math_addm ( uint8_t *  r,
const uint8_t *  a,
const uint8_t *  b,
const uint8_t *  m,
size_t  len 
)

Performs a modular addition of two integers represented as byte arrays.

This function throws an exception if the computation doesn't succeed.

Warning
It is recommended to use cx_math_addm_no_throw rather than this function.
Parameters
[out]rBuffer for the result.
[in]aPointer to the first integer. This must be strictly smaller than the modulus.
[in]bPointer to the second integer. This must be strictly smaller than the modulus.
[in]mModulus
[in]lenNumber of bytes taken into account for the operation.
Exceptions
CX_NOT_UNLOCKED
CX_INVALID_PARAMETER_SIZE
CX_NOT_LOCKED
CX_MEMORY_FULL
CX_INVALID_PARAMETER

cx_math_addm_no_throw()

cx_err_t cx_math_addm_no_throw ( uint8_t *  r,
const uint8_t *  a,
const uint8_t *  b,
const uint8_t *  m,
size_t  len 
)

Performs a modular addition of two integers represented as byte arrays.

Parameters
[out]rBuffer for the result.
[in]aPointer to the first integer. This must be strictly smaller than the modulus.
[in]bPointer to the second integer. This must be strictly smaller than the modulus.
[in]mModulus
[in]lenNumber of bytes taken into account for the operation.
Returns
Error code:
  • CX_OK on success
  • CX_NOT_UNLOCKED
  • CX_INVALID_PARAMETER_SIZE
  • CX_NOT_LOCKED
  • CX_MEMORY_FULL
  • CX_INVALID_PARAMETER

cx_math_cmp()

static int32_t cx_math_cmp ( const uint8_t *  a,
const uint8_t *  b,
size_t  length 
)

Compares two integers represented as byte arrays.

This function throws an exception if the computation doesn't succeed.

Warning
It is recommended to use cx_math_cmp_no_throw rather than this function.
Parameters
[in]aPointer to the first integer.
[in]bPointer to the second integer.
[in]lengthNumber of bytes taken into account for the comparison.
Returns
Result of the comparison:
  • 0 if a and b are identical
  • < 0 if a is less than b
  • > 0 if a is greater than b
Exceptions
CX_NOT_UNLOCKED
CX_INVALID_PARAMETER_SIZE
CX_NOT_LOCKED
CX_MEMORY_FULL
CX_INVALID_PARAMETER

cx_math_cmp_no_throw()

cx_err_t cx_math_cmp_no_throw ( const uint8_t *  a,
const uint8_t *  b,
size_t  length,
int *  diff 
)

Compares two integers represented as byte arrays.

Parameters
[in]aPointer to the first integer.
[in]bPointer to the second integer.
[in]lengthNumber of bytes taken into account for the comparison.
[out]diffResult of the comparison:
  • 0 if a and b are identical
  • < 0 if a is less than b
  • > 0 if a is greater than b
Returns
Error code:
  • CX_OK on success
  • CX_NOT_UNLOCKED
  • CX_INVALID_PARAMETER_SIZE
  • CX_NOT_LOCKED
  • CX_MEMORY_FULL
  • CX_INVALID_PARAMETER

cx_math_invintm()

static void cx_math_invintm ( uint8_t *  r,
uint32_t  a,
const uint8_t *  m,
size_t  len 
)

Computes the modular inverse.

It computes the result of a^(-1) mod m. a must be invertible modulo m, i.e. the greatest common divisor of a and m is 1. This function throws an exception if the computation doesn't succeed.

Warning
It is recommended to use cx_math_invintm_no_throw rather than this function.
Parameters
[out]rBuffer for the result.
[in]aPointer to the integer.
[in]mModulus.
[in]lenNumber of bytes of the result.
Exceptions
CX_NOT_UNLOCKED
CX_INVALID_PARAMETER_SIZE
CX_NOT_LOCKED
CX_MEMORY_FULL
CX_INVALID_PARAMETER

cx_math_invintm_no_throw()

cx_err_t cx_math_invintm_no_throw ( uint8_t *  r,
uint32_t  a,
const uint8_t *  m,
size_t  len 
)

Computes the modular inverse.

It computes the result of a^(-1) mod m. a must be invertible modulo m, i.e. the greatest common divisor of a and m is 1.

Parameters
[out]rBuffer for the result.
[in]aPointer to the integer.
[in]mModulus.
[in]lenNumber of bytes of the result.
Returns
Error code:
  • CX_OK on success
  • CX_NOT_UNLOCKED
  • CX_INVALID_PARAMETER_SIZE
  • CX_NOT_LOCKED
  • CX_MEMORY_FULL
  • CX_INVALID_PARAMETER

cx_math_invprimem()

static void cx_math_invprimem ( uint8_t *  r,
const uint8_t *  a,
const uint8_t *  m,
size_t  len 
)

Computes the modular inverse with a prime modulus.

It computes the result of a^(-1) mod m, for a prime m. This function throws an exception if the computation doesn't succeed.

Warning
It is recommended to use cx_math_invprimem_no_throw rather than this function.
Parameters
[out]rBuffer for the result.
[in]aPointer to the integer.
[in]mModulus. Must be a prime number.
[in]lenNumber of bytes of the result.
Exceptions
CX_NOT_UNLOCKED
CX_INVALID_PARAMETER_SIZE
CX_NOT_LOCKED
CX_MEMORY_FULL
CX_INVALID_PARAMETER

cx_math_invprimem_no_throw()

cx_err_t cx_math_invprimem_no_throw ( uint8_t *  r,
const uint8_t *  a,
const uint8_t *  m,
size_t  len 
)

Computes the modular inverse with a prime modulus.

It computes the result of a^(-1) mod m, for a prime m.

Parameters
[out]rBuffer for the result.
[in]aPointer to the integer.
[in]mModulus. Must be a prime number.
[in]lenNumber of bytes of the result.
Returns
Error code:
  • CX_OK on success
  • CX_NOT_UNLOCKED
  • CX_INVALID_PARAMETER_SIZE
  • CX_NOT_LOCKED
  • CX_MEMORY_FULL
  • CX_INVALID_PARAMETER

cx_math_is_prime()

static bool cx_math_is_prime ( const uint8_t *  r,
size_t  len 
)

Checks whether a number is probably prime.

This function throws an exception if the computation doesn't succeed.

Warning
It is recommended to use cx_math_is_prime_no_throw rather than this function.
Parameters
[in]rPointer to an integer.
[in]lenNumber of bytes of the integer.
Returns
Bool indicating whether r is prime or not:
  • 0 : not prime
  • 1 : prime
Exceptions
CX_NOT_UNLOCKED
CX_INVALID_PARAMETER_SIZE
CX_NOT_LOCKED
CX_MEMORY_FULL
CX_INVALID_PARAMETER

cx_math_is_prime_no_throw()

cx_err_t cx_math_is_prime_no_throw ( const uint8_t *  r,
size_t  len,
bool *  prime 
)

Checks whether a number is probably prime.

Parameters
[in]rPointer to an integer.
[in]lenNumber of bytes of the integer.
[out]primeBool indicating whether r is prime or not:
  • 0 : not prime
  • 1 : prime
Returns
Error code:
  • CX_OK on success
  • CX_NOT_UNLOCKED
  • CX_INVALID_PARAMETER_SIZE
  • CX_NOT_LOCKED
  • CX_MEMORY_FULL
  • CX_INVALID_PARAMETER

cx_math_is_zero()

static bool cx_math_is_zero ( const uint8_t *  a,
size_t  len 
)

Checks whether the byte array of an integer is all zero.

Parameters
[in]aPointer to an integer.
[in]lenNumber of bytes of the integer.
Returns
1 if a is all zero, 0 otherwise.

cx_math_modm()

static void cx_math_modm ( uint8_t *  v,
size_t  len_v,
const uint8_t *  m,
size_t  len_m 
)

Performs a modular reduction.

This function throws an exception if the computation doesn't succeed.

Warning
It is recommended to use cx_math_modm_no_throw rather than this function.
Parameters
[in,out]vPointer to the dividend and buffer for the result.
[in]len_vNumber of bytes of the dividend.
[in]mModulus.
[in]len_mNumber of bytes of the modulus.
Exceptions
CX_NOT_UNLOCKED
CX_INVALID_PARAMETER_SIZE
CX_NOT_LOCKED
CX_MEMORY_FULL
CX_INVALID_PARAMETER

cx_math_modm_no_throw()

cx_err_t cx_math_modm_no_throw ( uint8_t *  v,
size_t  len_v,
const uint8_t *  m,
size_t  len_m 
)

Performs a modular reduction.

Computes the remainder of the division of v by m. Store the result in v.

Parameters
[in,out]vPointer to the dividend and buffer for the result.
[in]len_vNumber of bytes of the dividend.
[in]mModulus.
[in]len_mNumber of bytes of the modulus.
Returns
Error code:
  • CX_OK on success
  • CX_NOT_UNLOCKED
  • CX_INVALID_PARAMETER_SIZE
  • CX_NOT_LOCKED
  • CX_MEMORY_FULL
  • CX_INVALID_PARAMETER

cx_math_mult()

static void cx_math_mult ( uint8_t *  r,
const uint8_t *  a,
const uint8_t *  b,
size_t  len 
)

Multiplies two integers represented as byte arrays.

This function throws an exception if the computation doesn't succeed.

Warning
It is recommended to use cx_math_mult_no_throw rather than this function.
Parameters
[out]rBuffer for the result.
[in]aPointer to the first integer.
[in]bPointer to the second integer.
[in]lenNumber of bytes taken into account for the multiplication.
Exceptions
CX_NOT_UNLOCKED
CX_INVALID_PARAMETER_SIZE
CX_NOT_LOCKED
CX_MEMORY_FULL
CX_INVALID_PARAMETER

cx_math_mult_no_throw()

cx_err_t cx_math_mult_no_throw ( uint8_t *  r,
const uint8_t *  a,
const uint8_t *  b,
size_t  len 
)

Multiplies two integers represented as byte arrays.

Parameters
[out]rBuffer for the result.
[in]aPointer to the first integer.
[in]bPointer to the second integer.
[in]lenNumber of bytes taken into account for the multiplication.
Returns
Error code:
  • CX_OK on success
  • CX_NOT_UNLOCKED
  • CX_INVALID_PARAMETER_SIZE
  • CX_NOT_LOCKED
  • CX_MEMORY_FULL
  • CX_INVALID_PARAMETER

cx_math_multm()

static void cx_math_multm ( uint8_t *  r,
const uint8_t *  a,
const uint8_t *  b,
const uint8_t *  m,
size_t  len 
)

Performs a modular multiplication of two integers represented as byte arrays.

This function throws an exception if the computation doesn't succeed.

Warning
It is recommended to use cx_math_multm_no_throw rather than this function.
Parameters
[out]rBuffer for the result.
[in]aPointer to the first integer.
[in]bPointer to the second integer. This must be strictly smaller than the modulus.
[in]mModulus
[in]lenNumber of bytes taken into account for the operation.
Exceptions
CX_NOT_UNLOCKED
CX_INVALID_PARAMETER_SIZE
CX_NOT_LOCKED
CX_MEMORY_FULL
CX_INVALID_PARAMETER
CX_INVALID_PARAMETER_VALUE

cx_math_multm_no_throw()

cx_err_t cx_math_multm_no_throw ( uint8_t *  r,
const uint8_t *  a,
const uint8_t *  b,
const uint8_t *  m,
size_t  len 
)

Performs a modular multiplication of two integers represented as byte arrays.

Parameters
[out]rBuffer for the result.
[in]aPointer to the first integer.
[in]bPointer to the second integer. This must be strictly smaller than the modulus.
[in]mModulus
[in]lenNumber of bytes taken into account for the operation.
Returns
Error code:
  • CX_OK on success
  • CX_NOT_UNLOCKED
  • CX_INVALID_PARAMETER_SIZE
  • CX_NOT_LOCKED
  • CX_MEMORY_FULL
  • CX_INVALID_PARAMETER
  • CX_INVALID_PARAMETER_VALUE

cx_math_next_prime()

static void cx_math_next_prime ( uint8_t *  r,
uint32_t  len 
)

Computes the next prime after a given number.

This function throws an exception if the computation doesn't succeed.

Warning
It is recommended to use cx_math_next_prime_no_throw rather than this function.
Parameters
[in]rPointer to the integer and buffer for the result.
[in]lenNumber of bytes of the integer.
Exceptions
CX_NOT_UNLOCKED
CX_INVALID_PARAMETER_SIZE
CX_MEMORY_FULL
CX_NOT_LOCKED
CX_INVALID_PARAMETER
CX_INTERNAL_ERROR
CX_OVERFLOW

cx_math_next_prime_no_throw()

cx_err_t cx_math_next_prime_no_throw ( uint8_t *  r,
uint32_t  len 
)

Computes the next prime after a given number.

Parameters
[in,out]rPointer to the integer and buffer for the result.
[in]lenNumber of bytes of the integer.
Returns
Error code:
  • CX_OK on success
  • CX_NOT_UNLOCKED
  • CX_INVALID_PARAMETER_SIZE
  • CX_MEMORY_FULL
  • CX_NOT_LOCKED
  • CX_INVALID_PARAMETER
  • CX_INTERNAL_ERROR
  • CX_OVERFLOW

cx_math_powm()

static void cx_math_powm ( uint8_t *  r,
const uint8_t *  a,
const uint8_t *  e,
size_t  len_e,
const uint8_t *  m,
size_t  len 
)

Performs a modular exponentiation.

It computes the result of a^e mod m. This function throws an exception if the computation doesn't succeed.

Warning
It is recommended to use cx_math_powm_no_throw rather than this function.
Parameters
[out]rBuffer for the result.
[in]aPointer to an integer.
[in]ePointer to the exponent.
[in]len_eNumber of bytes of the exponent.
[in]mModulus
[in]lenNumber of bytes of the result.
Exceptions
CX_NOT_UNLOCKED
CX_INVALID_PARAMETER_SIZE
CX_NOT_LOCKED
CX_MEMORY_FULL
CX_INVALID_PARAMETER

cx_math_powm_no_throw()

cx_err_t cx_math_powm_no_throw ( uint8_t *  r,
const uint8_t *  a,
const uint8_t *  e,
size_t  len_e,
const uint8_t *  m,
size_t  len 
)

Performs a modular exponentiation.

Computes the result of a^e mod m.

Parameters
[out]rBuffer for the result.
[in]aPointer to an integer.
[in]ePointer to the exponent.
[in]len_eNumber of bytes of the exponent.
[in]mModulus
[in]lenNumber of bytes of the result.
Returns
Error code:
  • CX_OK on success
  • CX_NOT_UNLOCKED
  • CX_INVALID_PARAMETER_SIZE
  • CX_NOT_LOCKED
  • CX_MEMORY_FULL
  • CX_INVALID_PARAMETER

cx_math_sub()

static uint32_t cx_math_sub ( uint8_t *  r,
const uint8_t *  a,
const uint8_t *  b,
size_t  len 
)

Subtracts two integers represented as byte arrays.

This function throws an exception if the computation doesn't succeed.

Warning
It is recommended to use cx_math_sub_no_throw rather than this function.
Parameters
[out]rBuffer for the result.
[in]aPointer to the first integer.
[in]bPointer to the second integer.
[in]lenNumber of bytes taken into account for the subtraction.
Returns
1 if there is a carry, 0 otherwise.
Exceptions
CX_NOT_UNLOCKED
CX_INVALID_PARAMETER_SIZE
CX_NOT_LOCKED
CX_MEMORY_FULL
CX_INVALID_PARAMETER

cx_math_sub_no_throw()

cx_err_t cx_math_sub_no_throw ( uint8_t *  r,
const uint8_t *  a,
const uint8_t *  b,
size_t  len 
)

Subtracts two integers represented as byte arrays.

Parameters
[out]rBuffer for the result.
[in]aPointer to the first integer.
[in]bPointer to the second integer.
[in]lenNumber of bytes taken into account for the subtraction.
Returns
Error code:
  • CX_OK on success
  • CX_NOT_UNLOCKED
  • CX_INVALID_PARAMETER_SIZE
  • CX_NOT_LOCKED
  • CX_MEMORY_FULL
  • CX_INVALID_PARAMETER

cx_math_subm()

static void cx_math_subm ( uint8_t *  r,
const uint8_t *  a,
const uint8_t *  b,
const uint8_t *  m,
size_t  len 
)

Performs a modular subtraction of two integers represented as byte arrays.

This function throws an exception if the computation doesn't succeed.

Warning
It is recommended to use cx_math_subm_no_throw rather than this function.
Parameters
[out]rBuffer for the result.
[in]aPointer to the first integer. This must be strictly smaller than the modulus.
[in]bPointer to the second integer. This must be strictly smaller than the modulus.
[in]mModulus
[in]lenNumber of bytes taken into account for the operation.
Exceptions
CX_NOT_UNLOCKED
CX_INVALID_PARAMETER_SIZE
CX_NOT_LOCKED
CX_MEMORY_FULL
CX_INVALID_PARAMETER

cx_math_subm_no_throw()

cx_err_t cx_math_subm_no_throw ( uint8_t *  r,
const uint8_t *  a,
const uint8_t *  b,
const uint8_t *  m,
size_t  len 
)

Performs a modular subtraction of two integers represented as byte arrays.

Parameters
[out]rBuffer for the result.
[in]aPointer to the first integer. This must be strictly smaller than the modulus.
[in]bPointer to the second integer. This must be strictly smaller than the modulus.
[in]mModulus
[in]lenNumber of bytes taken into account for the operation.
Returns
Error code:
  • CX_OK on success
  • CX_NOT_UNLOCKED
  • CX_INVALID_PARAMETER_SIZE
  • CX_NOT_LOCKED
  • CX_MEMORY_FULL
  • CX_INVALID_PARAMETER

Back to the files list


Did you find this page helpful?


How would you improve this page for developers?



Getting Started
Theme Features
Customization

Nano Apps