ta/wallet_ta.c
/**
  * Compute the modular inverse of a 32 byte multi-precision number under
  * the current prime finite field (i.e. find r such that
  * (r x op1) modulo #n = 1). It is to be used
  * internally (only by the functions of the TA itself).
  * \param session_data A data pointer to a session context.
  * \param r The 32 byte result will be written into here.
  * \param op1 The 32 byte operand to find the inverse of. This may alias r.
  */
Compute the modular inverse of a 32 byte multi-precision number under
the current prime finite field  modulo #n = 1). It is to be used
internally (only by the functions of the TA itself).
\param session_data A data pointer to a session context.
\param r The 32 byte result will be written into here.
\param op1 The 32 byte operand to find the inverse of. This may alias r.
Compute the modular inverse of a 32 byte multi - precision number under the current prime finite field modulo #n = 1 ) . It is to be used internally ( only by the functions of the TA itself ) . \ param session_data A data pointer to a session context . \ param r The 32 byte result will be written into here . \ param op1 The 32 byte operand to find the inverse of . This may alias r .
static void big_invert_internal(Session_data * session_data, BigNum256 r, BigNum256 op1)
{
	uint8_t temp[32];
	uint8_t i;
	uint8_t j;
	uint8_t byte_of_n_minus_2;
	uint8_t bit_of_n_minus_2;
	uint8_t *lookup[2];
	big_assign_internal(temp, op1);
	big_set_zero_internal(r);
	r[0] = 1;
	lookup[0] = r;
	lookup[1] = temp;
	for (i = 31; i < 32; i--)
	{
		byte_of_n_minus_2 = (session_data->n)[i];
		if (i == 0)
			byte_of_n_minus_2 = (uint8_t)(byte_of_n_minus_2 - 2);
		for (j = 0; j < 8; j++)
		{
			bit_of_n_minus_2 = (uint8_t)((byte_of_n_minus_2 & 0x80) >> 7);
			byte_of_n_minus_2 = (uint8_t)(byte_of_n_minus_2 << 1);
			big_multiply_internal(session_data,
								lookup[1 - bit_of_n_minus_2],
								r,
								temp);
			big_multiply_internal(session_data,
								lookup[bit_of_n_minus_2],
								lookup[bit_of_n_minus_2],
								lookup[bit_of_n_minus_2]);
		}
	}
}
static void big_invert_internal ( Session_data * session_data , BigNum256 r , BigNum256 op1 ) { uint8_t temp [ 32 ] ; uint8_t i ; uint8_t j ; uint8_t byte_of_n_minus_2 ; uint8_t bit_of_n_minus_2 ; uint8_t * lookup [ 2 ] ; big_assign_internal ( temp , op1 ) ; big_set_zero_internal ( r ) ; r [ 0 ] = 1 ; lookup [ 0 ] = r ; lookup [ 1 ] = temp ; for ( i = 31 ; i < 32 ; i -- ) { byte_of_n_minus_2 = ( session_data -> n ) [ i ] ; if ( i == 0 ) byte_of_n_minus_2 = ( uint8_t ) ( byte_of_n_minus_2 - 2 ) ; for ( j = 0 ; j < 8 ; j ++ ) { bit_of_n_minus_2 = ( uint8_t ) ( ( byte_of_n_minus_2 & 0x80 ) >> 7 ) ; byte_of_n_minus_2 = ( uint8_t ) ( byte_of_n_minus_2 << 1 ) ; big_multiply_internal ( session_data , lookup [ 1 - bit_of_n_minus_2 ] , r , temp ) ; big_multiply_internal ( session_data , lookup [ bit_of_n_minus_2 ] , lookup [ bit_of_n_minus_2 ] , lookup [ bit_of_n_minus_2 ] ) ; } } }
Compute the modular inverse of a 32 byte multi-precision number under
 the current prime finite field (i.e.
Compute the modular inverse of a 32 byte multi - precision number under the current prime finite field ( i . e .
/*
	 * This uses Fermat's Little Theorem, of which an immediate corollary is:
	 * a ^ (p - 2) = a ^ (-1) modulo n.
	 * The Montgomery ladder method is used to perform the exponentiation.
	 */ /*
			 * The next two lines do the following:
			 * if (bit_of_n_minus_2)
			 * {
			 *     bigMultiply(r, r, temp);
			 *     bigMultiply(temp, temp, temp);
			 * }
			 * else
			 * {
			 *     bigMultiply(temp, r, temp);
			 *     bigMultiply(r, r, r);
			 * }
			 */