[crypto] Expose shifted out bit from big integer shifts

Expose the bit shifted out as a result of shifting a big integer left
or right.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2025-02-13 14:18:15 +00:00
parent bd90abf487
commit 5056e8ad93
7 changed files with 146 additions and 85 deletions

View File

@@ -123,16 +123,18 @@ bigint_subtract_raw ( const uint32_t *subtrahend0, uint32_t *value0,
* *
* @v value0 Element 0 of big integer * @v value0 Element 0 of big integer
* @v size Number of elements * @v size Number of elements
* @ret out Bit shifted out
*/ */
static inline __attribute__ (( always_inline )) void static inline __attribute__ (( always_inline )) int
bigint_shl_raw ( uint32_t *value0, unsigned int size ) { bigint_shl_raw ( uint32_t *value0, unsigned int size ) {
bigint_t ( size ) __attribute__ (( may_alias )) *value = bigint_t ( size ) __attribute__ (( may_alias )) *value =
( ( void * ) value0 ); ( ( void * ) value0 );
uint32_t *discard_value; uint32_t *discard_value;
uint32_t *discard_end; uint32_t *discard_end;
uint32_t discard_value_i; uint32_t discard_value_i;
int carry;
__asm__ __volatile__ ( "adds %1, %0, %5, lsl #2\n\t" /* clear CF */ __asm__ __volatile__ ( "adds %1, %0, %1, lsl #2\n\t" /* clear CF */
"\n1:\n\t" "\n1:\n\t"
"ldr %2, [%0]\n\t" "ldr %2, [%0]\n\t"
"adcs %2, %2\n\t" "adcs %2, %2\n\t"
@@ -142,9 +144,10 @@ bigint_shl_raw ( uint32_t *value0, unsigned int size ) {
: "=l" ( discard_value ), : "=l" ( discard_value ),
"=l" ( discard_end ), "=l" ( discard_end ),
"=l" ( discard_value_i ), "=l" ( discard_value_i ),
"=@cccs" ( carry ),
"+m" ( *value ) "+m" ( *value )
: "0" ( value0 ), "1" ( size ) : "0" ( value0 ), "1" ( size ) );
: "cc" ); return carry;
} }
/** /**
@@ -152,16 +155,18 @@ bigint_shl_raw ( uint32_t *value0, unsigned int size ) {
* *
* @v value0 Element 0 of big integer * @v value0 Element 0 of big integer
* @v size Number of elements * @v size Number of elements
* @ret out Bit shifted out
*/ */
static inline __attribute__ (( always_inline )) void static inline __attribute__ (( always_inline )) int
bigint_shr_raw ( uint32_t *value0, unsigned int size ) { bigint_shr_raw ( uint32_t *value0, unsigned int size ) {
bigint_t ( size ) __attribute__ (( may_alias )) *value = bigint_t ( size ) __attribute__ (( may_alias )) *value =
( ( void * ) value0 ); ( ( void * ) value0 );
uint32_t *discard_value; uint32_t *discard_value;
uint32_t *discard_end; uint32_t *discard_end;
uint32_t discard_value_i; uint32_t discard_value_i;
int carry;
__asm__ __volatile__ ( "adds %1, %0, %5, lsl #2\n\t" /* clear CF */ __asm__ __volatile__ ( "adds %1, %0, %1, lsl #2\n\t" /* clear CF */
"\n1:\n\t" "\n1:\n\t"
"ldmdb %1!, {%2}\n\t" "ldmdb %1!, {%2}\n\t"
"rrxs %2, %2\n\t" "rrxs %2, %2\n\t"
@@ -171,9 +176,10 @@ bigint_shr_raw ( uint32_t *value0, unsigned int size ) {
: "=l" ( discard_value ), : "=l" ( discard_value ),
"=l" ( discard_end ), "=l" ( discard_end ),
"=l" ( discard_value_i ), "=l" ( discard_value_i ),
"=@cccs" ( carry ),
"+m" ( *value ) "+m" ( *value )
: "0" ( value0 ), "1" ( size ) : "0" ( value0 ), "1" ( size ) );
: "cc" ); return carry;
} }
/** /**

View File

@@ -122,14 +122,16 @@ bigint_subtract_raw ( const uint64_t *subtrahend0, uint64_t *value0,
* *
* @v value0 Element 0 of big integer * @v value0 Element 0 of big integer
* @v size Number of elements * @v size Number of elements
* @ret out Bit shifted out
*/ */
static inline __attribute__ (( always_inline )) void static inline __attribute__ (( always_inline )) int
bigint_shl_raw ( uint64_t *value0, unsigned int size ) { bigint_shl_raw ( uint64_t *value0, unsigned int size ) {
bigint_t ( size ) __attribute__ (( may_alias )) *value = bigint_t ( size ) __attribute__ (( may_alias )) *value =
( ( void * ) value0 ); ( ( void * ) value0 );
uint64_t *discard_value; uint64_t *discard_value;
uint64_t discard_value_i; uint64_t discard_value_i;
unsigned int discard_size; unsigned int discard_size;
int carry;
__asm__ __volatile__ ( "cmn xzr, xzr\n\t" /* clear CF */ __asm__ __volatile__ ( "cmn xzr, xzr\n\t" /* clear CF */
"\n1:\n\t" "\n1:\n\t"
@@ -141,9 +143,10 @@ bigint_shl_raw ( uint64_t *value0, unsigned int size ) {
: "=r" ( discard_value ), : "=r" ( discard_value ),
"=r" ( discard_size ), "=r" ( discard_size ),
"=r" ( discard_value_i ), "=r" ( discard_value_i ),
"=@cccs" ( carry ),
"+m" ( *value ) "+m" ( *value )
: "0" ( value0 ), "1" ( size ) : "0" ( value0 ), "1" ( size ) );
: "cc" ); return carry;
} }
/** /**
@@ -151,30 +154,32 @@ bigint_shl_raw ( uint64_t *value0, unsigned int size ) {
* *
* @v value0 Element 0 of big integer * @v value0 Element 0 of big integer
* @v size Number of elements * @v size Number of elements
* @ret out Bit shifted out
*/ */
static inline __attribute__ (( always_inline )) void static inline __attribute__ (( always_inline )) int
bigint_shr_raw ( uint64_t *value0, unsigned int size ) { bigint_shr_raw ( uint64_t *value0, unsigned int size ) {
bigint_t ( size ) __attribute__ (( may_alias )) *value = bigint_t ( size ) __attribute__ (( may_alias )) *value =
( ( void * ) value0 ); ( ( void * ) value0 );
uint64_t *discard_value; uint64_t *discard_value;
uint64_t discard_value_i; uint64_t discard_high;
uint64_t discard_value_j;
unsigned int discard_size; unsigned int discard_size;
uint64_t low;
__asm__ __volatile__ ( "mov %3, #0\n\t" __asm__ __volatile__ ( "mov %2, #0\n\t"
"\n1:\n\t" "\n1:\n\t"
"sub %w1, %w1, #1\n\t" "sub %w1, %w1, #1\n\t"
"ldr %2, [%0, %1, lsl #3]\n\t" "ldr %3, [%0, %1, lsl #3]\n\t"
"extr %3, %3, %2, #1\n\t" "extr %2, %2, %3, #1\n\t"
"str %3, [%0, %1, lsl #3]\n\t" "str %2, [%0, %1, lsl #3]\n\t"
"mov %3, %2\n\t" "mov %2, %3\n\t"
"cbnz %w1, 1b\n\t" "cbnz %w1, 1b\n\t"
: "=r" ( discard_value ), : "=r" ( discard_value ),
"=r" ( discard_size ), "=r" ( discard_size ),
"=r" ( discard_value_i ), "=r" ( discard_high ),
"=r" ( discard_value_j ), "=r" ( low ),
"+m" ( *value ) "+m" ( *value )
: "0" ( value0 ), "1" ( size ) ); : "0" ( value0 ), "1" ( size ) );
return ( low & 1 );
} }
/** /**

View File

@@ -144,26 +144,27 @@ bigint_subtract_raw ( const uint64_t *subtrahend0, uint64_t *value0,
* *
* @v value0 Element 0 of big integer * @v value0 Element 0 of big integer
* @v size Number of elements * @v size Number of elements
* @ret out Bit shifted out
*/ */
static inline __attribute__ (( always_inline )) void static inline __attribute__ (( always_inline )) int
bigint_shl_raw ( uint64_t *value0, unsigned int size ) { bigint_shl_raw ( uint64_t *value0, unsigned int size ) {
bigint_t ( size ) __attribute__ (( may_alias )) *value = bigint_t ( size ) __attribute__ (( may_alias )) *value =
( ( void * ) value0 ); ( ( void * ) value0 );
uint64_t *discard_value; uint64_t *discard_value;
uint64_t discard_value_i; uint64_t discard_value_i;
uint64_t discard_carry;
uint64_t discard_temp; uint64_t discard_temp;
unsigned int discard_size; unsigned int discard_size;
uint64_t carry;
__asm__ __volatile__ ( "\n1:\n\t" __asm__ __volatile__ ( "\n1:\n\t"
/* Load value[i] */ /* Load value[i] */
"ld.d %2, %0, 0\n\t" "ld.d %2, %0, 0\n\t"
/* Shift left */ /* Shift left */
"rotri.d %2, %2, 63\n\t" "rotri.d %2, %2, 63\n\t"
"andi %4, %2, 1\n\t" "andi %3, %2, 1\n\t"
"xor %2, %2, %4\n\t" "xor %2, %2, %3\n\t"
"or %2, %2, %3\n\t" "or %2, %2, %4\n\t"
"move %3, %4\n\t" "move %4, %3\n\t"
/* Store value[i] */ /* Store value[i] */
"st.d %2, %0, 0\n\t" "st.d %2, %0, 0\n\t"
/* Loop */ /* Loop */
@@ -173,11 +174,12 @@ bigint_shl_raw ( uint64_t *value0, unsigned int size ) {
: "=r" ( discard_value ), : "=r" ( discard_value ),
"=r" ( discard_size ), "=r" ( discard_size ),
"=r" ( discard_value_i ), "=r" ( discard_value_i ),
"=r" ( discard_carry ),
"=r" ( discard_temp ), "=r" ( discard_temp ),
"=r" ( carry ),
"+m" ( *value ) "+m" ( *value )
: "0" ( value0 ), "1" ( size ), "3" ( 0 ) : "0" ( value0 ), "1" ( size ), "4" ( 0 )
: "cc" ); : "cc" );
return ( carry & 1 );
} }
/** /**
@@ -185,25 +187,26 @@ bigint_shl_raw ( uint64_t *value0, unsigned int size ) {
* *
* @v value0 Element 0 of big integer * @v value0 Element 0 of big integer
* @v size Number of elements * @v size Number of elements
* @ret out Bit shifted out
*/ */
static inline __attribute__ (( always_inline )) void static inline __attribute__ (( always_inline )) int
bigint_shr_raw ( uint64_t *value0, unsigned int size ) { bigint_shr_raw ( uint64_t *value0, unsigned int size ) {
bigint_t ( size ) __attribute__ (( may_alias )) *value = bigint_t ( size ) __attribute__ (( may_alias )) *value =
( ( void * ) value0 ); ( ( void * ) value0 );
uint64_t *discard_value; uint64_t *discard_value;
uint64_t discard_value_i; uint64_t discard_value_i;
uint64_t discard_carry;
uint64_t discard_temp; uint64_t discard_temp;
unsigned int discard_size; unsigned int discard_size;
uint64_t carry;
__asm__ __volatile__ ( "\n1:\n\t" __asm__ __volatile__ ( "\n1:\n\t"
/* Load value[i] */ /* Load value[i] */
"ld.d %2, %0, -8\n\t" "ld.d %2, %0, -8\n\t"
/* Shift right */ /* Shift right */
"andi %4, %2, 1\n\t" "andi %3, %2, 1\n\t"
"xor %2, %2, %4\n\t" "xor %2, %2, %3\n\t"
"or %2, %2, %3\n\t" "or %2, %2, %4\n\t"
"move %3, %4\n\t" "move %4, %3\n\t"
"rotri.d %2, %2, 1\n\t" "rotri.d %2, %2, 1\n\t"
/* Store value[i] */ /* Store value[i] */
"st.d %2, %0, -8\n\t" "st.d %2, %0, -8\n\t"
@@ -214,11 +217,12 @@ bigint_shr_raw ( uint64_t *value0, unsigned int size ) {
: "=r" ( discard_value ), : "=r" ( discard_value ),
"=r" ( discard_size ), "=r" ( discard_size ),
"=r" ( discard_value_i ), "=r" ( discard_value_i ),
"=r" ( discard_carry ),
"=r" ( discard_temp ), "=r" ( discard_temp ),
"=r" ( carry ),
"+m" ( *value ) "+m" ( *value )
: "0" ( value0 + size ), "1" ( size ), "3" ( 0 ) : "0" ( value0 + size ), "1" ( size ), "4" ( 0 )
: "cc" ); : "cc" );
return ( carry & 1 );
} }
/** /**

View File

@@ -143,38 +143,40 @@ bigint_subtract_raw ( const unsigned long *subtrahend0, unsigned long *value0,
* *
* @v value0 Element 0 of big integer * @v value0 Element 0 of big integer
* @v size Number of elements * @v size Number of elements
* @ret out Bit shifted out
*/ */
static inline __attribute__ (( always_inline )) void static inline __attribute__ (( always_inline )) int
bigint_shl_raw ( unsigned long *value0, unsigned int size ) { bigint_shl_raw ( unsigned long *value0, unsigned int size ) {
bigint_t ( size ) __attribute__ (( may_alias )) *value = bigint_t ( size ) __attribute__ (( may_alias )) *value =
( ( void * ) value0 ); ( ( void * ) value0 );
unsigned long *valueN = ( value0 + size ); unsigned long *valueN = ( value0 + size );
unsigned long *discard_value; unsigned long *discard_value;
unsigned long discard_value_i; unsigned long discard_value_i;
unsigned long discard_carry;
unsigned long discard_temp; unsigned long discard_temp;
unsigned long carry;
__asm__ __volatile__ ( "\n1:\n\t" __asm__ __volatile__ ( "\n1:\n\t"
/* Load value[i] */ /* Load value[i] */
LOADN " %1, (%0)\n\t" LOADN " %1, (%0)\n\t"
/* Shift left */ /* Shift left */
"slli %3, %1, 1\n\t" "slli %2, %1, 1\n\t"
"or %3, %3, %2\n\t" "or %2, %2, %3\n\t"
"srli %2, %1, %7\n\t" "srli %3, %1, %7\n\t"
/* Store value[i] */ /* Store value[i] */
STOREN " %3, (%0)\n\t" STOREN " %2, (%0)\n\t"
/* Loop */ /* Loop */
"addi %0, %0, %6\n\t" "addi %0, %0, %6\n\t"
"bne %0, %5, 1b\n\t" "bne %0, %5, 1b\n\t"
: "=&r" ( discard_value ), : "=&r" ( discard_value ),
"=&r" ( discard_value_i ), "=&r" ( discard_value_i ),
"=&r" ( discard_carry ),
"=&r" ( discard_temp ), "=&r" ( discard_temp ),
"=&r" ( carry ),
"+m" ( *value ) "+m" ( *value )
: "r" ( valueN ), : "r" ( valueN ),
"i" ( sizeof ( unsigned long ) ), "i" ( sizeof ( unsigned long ) ),
"i" ( ( 8 * sizeof ( unsigned long ) - 1 ) ), "i" ( ( 8 * sizeof ( unsigned long ) - 1 ) ),
"0" ( value0 ), "2" ( 0 ) ); "0" ( value0 ), "3" ( 0 ) );
return carry;
} }
/** /**
@@ -182,38 +184,40 @@ bigint_shl_raw ( unsigned long *value0, unsigned int size ) {
* *
* @v value0 Element 0 of big integer * @v value0 Element 0 of big integer
* @v size Number of elements * @v size Number of elements
* @ret out Bit shifted out
*/ */
static inline __attribute__ (( always_inline )) void static inline __attribute__ (( always_inline )) int
bigint_shr_raw ( unsigned long *value0, unsigned int size ) { bigint_shr_raw ( unsigned long *value0, unsigned int size ) {
bigint_t ( size ) __attribute__ (( may_alias )) *value = bigint_t ( size ) __attribute__ (( may_alias )) *value =
( ( void * ) value0 ); ( ( void * ) value0 );
unsigned long *valueN = ( value0 + size ); unsigned long *valueN = ( value0 + size );
unsigned long *discard_value; unsigned long *discard_value;
unsigned long discard_value_i; unsigned long discard_value_i;
unsigned long discard_carry;
unsigned long discard_temp; unsigned long discard_temp;
unsigned long carry;
__asm__ __volatile__ ( "\n1:\n\t" __asm__ __volatile__ ( "\n1:\n\t"
/* Load value[i] */ /* Load value[i] */
LOADN " %1, %6(%0)\n\t" LOADN " %1, %6(%0)\n\t"
/* Shift right */ /* Shift right */
"srli %3, %1, 1\n\t" "srli %2, %1, 1\n\t"
"or %3, %3, %2\n\t" "or %2, %2, %3\n\t"
"slli %2, %1, %7\n\t" "slli %3, %1, %7\n\t"
/* Store value[i] */ /* Store value[i] */
STOREN " %3, %6(%0)\n\t" STOREN " %2, %6(%0)\n\t"
/* Loop */ /* Loop */
"addi %0, %0, %6\n\t" "addi %0, %0, %6\n\t"
"bne %0, %5, 1b\n\t" "bne %0, %5, 1b\n\t"
: "=&r" ( discard_value ), : "=&r" ( discard_value ),
"=&r" ( discard_value_i ), "=&r" ( discard_value_i ),
"=&r" ( discard_carry ),
"=&r" ( discard_temp ), "=&r" ( discard_temp ),
"=&r" ( carry ),
"+m" ( *value ) "+m" ( *value )
: "r" ( value0 ), : "r" ( value0 ),
"i" ( -( sizeof ( unsigned long ) ) ), "i" ( -( sizeof ( unsigned long ) ) ),
"i" ( ( 8 * sizeof ( unsigned long ) - 1 ) ), "i" ( ( 8 * sizeof ( unsigned long ) - 1 ) ),
"0" ( valueN ), "2" ( 0 ) ); "0" ( valueN ), "3" ( 0 ) );
return ( !! carry );
} }
/** /**

View File

@@ -116,22 +116,25 @@ bigint_subtract_raw ( const uint32_t *subtrahend0, uint32_t *value0,
* *
* @v value0 Element 0 of big integer * @v value0 Element 0 of big integer
* @v size Number of elements * @v size Number of elements
* @ret out Bit shifted out
*/ */
static inline __attribute__ (( always_inline )) void static inline __attribute__ (( always_inline )) int
bigint_shl_raw ( uint32_t *value0, unsigned int size ) { bigint_shl_raw ( uint32_t *value0, unsigned int size ) {
bigint_t ( size ) __attribute__ (( may_alias )) *value = bigint_t ( size ) __attribute__ (( may_alias )) *value =
( ( void * ) value0 ); ( ( void * ) value0 );
long index; long index;
long discard_c; long discard_c;
int out;
__asm__ __volatile__ ( "xor %0, %0\n\t" /* Zero %0 and clear CF */ __asm__ __volatile__ ( "xor %0, %0\n\t" /* Zero %0 and clear CF */
"\n1:\n\t" "\n1:\n\t"
"rcll $1, (%3,%0,4)\n\t" "rcll $1, (%4,%0,4)\n\t"
"inc %0\n\t" /* Does not affect CF */ "inc %0\n\t" /* Does not affect CF */
"loop 1b\n\t" "loop 1b\n\t"
: "=&r" ( index ), "=&c" ( discard_c ), : "=&r" ( index ), "=&c" ( discard_c ),
"+m" ( *value ) "=@ccc" ( out ), "+m" ( *value )
: "r" ( value0 ), "1" ( size ) ); : "r" ( value0 ), "1" ( size ) );
return out;
} }
/** /**
@@ -139,19 +142,23 @@ bigint_shl_raw ( uint32_t *value0, unsigned int size ) {
* *
* @v value0 Element 0 of big integer * @v value0 Element 0 of big integer
* @v size Number of elements * @v size Number of elements
* @ret out Bit shifted out
*/ */
static inline __attribute__ (( always_inline )) void static inline __attribute__ (( always_inline )) int
bigint_shr_raw ( uint32_t *value0, unsigned int size ) { bigint_shr_raw ( uint32_t *value0, unsigned int size ) {
bigint_t ( size ) __attribute__ (( may_alias )) *value = bigint_t ( size ) __attribute__ (( may_alias )) *value =
( ( void * ) value0 ); ( ( void * ) value0 );
long discard_c; long discard_c;
int out;
__asm__ __volatile__ ( "clc\n\t" __asm__ __volatile__ ( "clc\n\t"
"\n1:\n\t" "\n1:\n\t"
"rcrl $1, -4(%2,%0,4)\n\t" "rcrl $1, -4(%3,%0,4)\n\t"
"loop 1b\n\t" "loop 1b\n\t"
: "=&c" ( discard_c ), "+m" ( *value ) : "=&c" ( discard_c ), "=@ccc" ( out ),
"+m" ( *value )
: "r" ( value0 ), "0" ( size ) ); : "r" ( value0 ), "0" ( size ) );
return out;
} }
/** /**

View File

@@ -105,21 +105,23 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
* Shift big integer left * Shift big integer left
* *
* @v value Big integer * @v value Big integer
* @ret out Bit shifted out
*/ */
#define bigint_shl( value ) do { \ #define bigint_shl( value ) ( { \
unsigned int size = bigint_size (value); \ unsigned int size = bigint_size (value); \
bigint_shl_raw ( (value)->element, size ); \ bigint_shl_raw ( (value)->element, size ); \
} while ( 0 ) } )
/** /**
* Shift big integer right * Shift big integer right
* *
* @v value Big integer * @v value Big integer
* @ret out Bit shifted out
*/ */
#define bigint_shr( value ) do { \ #define bigint_shr( value ) ( { \
unsigned int size = bigint_size (value); \ unsigned int size = bigint_size (value); \
bigint_shr_raw ( (value)->element, size ); \ bigint_shr_raw ( (value)->element, size ); \
} while ( 0 ) } )
/** /**
* Test if big integer is equal to zero * Test if big integer is equal to zero
@@ -413,8 +415,8 @@ int bigint_add_raw ( const bigint_element_t *addend0,
bigint_element_t *value0, unsigned int size ); bigint_element_t *value0, unsigned int size );
int bigint_subtract_raw ( const bigint_element_t *subtrahend0, int bigint_subtract_raw ( const bigint_element_t *subtrahend0,
bigint_element_t *value0, unsigned int size ); bigint_element_t *value0, unsigned int size );
void bigint_shl_raw ( bigint_element_t *value0, unsigned int size ); int bigint_shl_raw ( bigint_element_t *value0, unsigned int size );
void bigint_shr_raw ( bigint_element_t *value0, unsigned int size ); int bigint_shr_raw ( bigint_element_t *value0, unsigned int size );
int bigint_is_zero_raw ( const bigint_element_t *value0, unsigned int size ); int bigint_is_zero_raw ( const bigint_element_t *value0, unsigned int size );
int bigint_is_geq_raw ( const bigint_element_t *value0, int bigint_is_geq_raw ( const bigint_element_t *value0,
const bigint_element_t *reference0, const bigint_element_t *reference0,

View File

@@ -78,18 +78,18 @@ int bigint_subtract_sample ( const bigint_element_t *subtrahend0,
return bigint_subtract ( subtrahend, value ); return bigint_subtract ( subtrahend, value );
} }
void bigint_shl_sample ( bigint_element_t *value0, unsigned int size ) { int bigint_shl_sample ( bigint_element_t *value0, unsigned int size ) {
bigint_t ( size ) *value __attribute__ (( may_alias )) bigint_t ( size ) *value __attribute__ (( may_alias ))
= ( ( void * ) value0 ); = ( ( void * ) value0 );
bigint_shl ( value ); return bigint_shl ( value );
} }
void bigint_shr_sample ( bigint_element_t *value0, unsigned int size ) { int bigint_shr_sample ( bigint_element_t *value0, unsigned int size ) {
bigint_t ( size ) *value __attribute__ (( may_alias )) bigint_t ( size ) *value __attribute__ (( may_alias ))
= ( ( void * ) value0 ); = ( ( void * ) value0 );
bigint_shr ( value ); return bigint_shr ( value );
} }
int bigint_is_zero_sample ( const bigint_element_t *value0, int bigint_is_zero_sample ( const bigint_element_t *value0,
@@ -321,25 +321,31 @@ void bigint_mod_exp_sample ( const bigint_element_t *base0,
* *
* @v value Big integer * @v value Big integer
* @v expected Big integer expected result * @v expected Big integer expected result
* @v bit Expected bit shifted out
*/ */
#define bigint_shl_ok( value, expected ) do { \ #define bigint_shl_ok( value, expected, bit ) do { \
static const uint8_t value_raw[] = value; \ static const uint8_t value_raw[] = value; \
static const uint8_t expected_raw[] = expected; \ static const uint8_t expected_raw[] = expected; \
uint8_t result_raw[ sizeof ( expected_raw ) ]; \ uint8_t result_raw[ sizeof ( expected_raw ) ]; \
unsigned int size = \ unsigned int size = \
bigint_required_size ( sizeof ( value_raw ) ); \ bigint_required_size ( sizeof ( value_raw ) ); \
unsigned int msb = ( 8 * sizeof ( value_raw ) ); \
bigint_t ( size ) value_temp; \ bigint_t ( size ) value_temp; \
int out; \
{} /* Fix emacs alignment */ \ {} /* Fix emacs alignment */ \
\ \
bigint_init ( &value_temp, value_raw, sizeof ( value_raw ) ); \ bigint_init ( &value_temp, value_raw, sizeof ( value_raw ) ); \
DBG ( "Shift left:\n" ); \ DBG ( "Shift left:\n" ); \
DBG_HDA ( 0, &value_temp, sizeof ( value_temp ) ); \ DBG_HDA ( 0, &value_temp, sizeof ( value_temp ) ); \
bigint_shl ( &value_temp ); \ out = bigint_shl ( &value_temp ); \
DBG_HDA ( 0, &value_temp, sizeof ( value_temp ) ); \ DBG_HDA ( 0, &value_temp, sizeof ( value_temp ) ); \
bigint_done ( &value_temp, result_raw, sizeof ( result_raw ) ); \ bigint_done ( &value_temp, result_raw, sizeof ( result_raw ) ); \
\ \
ok ( memcmp ( result_raw, expected_raw, \ ok ( memcmp ( result_raw, expected_raw, \
sizeof ( result_raw ) ) == 0 ); \ sizeof ( result_raw ) ) == 0 ); \
if ( sizeof ( result_raw ) < sizeof ( value_temp ) ) \
out += bigint_bit_is_set ( &value_temp, msb ); \
ok ( out == bit ); \
} while ( 0 ) } while ( 0 )
/** /**
@@ -347,25 +353,28 @@ void bigint_mod_exp_sample ( const bigint_element_t *base0,
* *
* @v value Big integer * @v value Big integer
* @v expected Big integer expected result * @v expected Big integer expected result
* @v bit Expected bit shifted out
*/ */
#define bigint_shr_ok( value, expected ) do { \ #define bigint_shr_ok( value, expected, bit ) do { \
static const uint8_t value_raw[] = value; \ static const uint8_t value_raw[] = value; \
static const uint8_t expected_raw[] = expected; \ static const uint8_t expected_raw[] = expected; \
uint8_t result_raw[ sizeof ( expected_raw ) ]; \ uint8_t result_raw[ sizeof ( expected_raw ) ]; \
unsigned int size = \ unsigned int size = \
bigint_required_size ( sizeof ( value_raw ) ); \ bigint_required_size ( sizeof ( value_raw ) ); \
bigint_t ( size ) value_temp; \ bigint_t ( size ) value_temp; \
int out; \
{} /* Fix emacs alignment */ \ {} /* Fix emacs alignment */ \
\ \
bigint_init ( &value_temp, value_raw, sizeof ( value_raw ) ); \ bigint_init ( &value_temp, value_raw, sizeof ( value_raw ) ); \
DBG ( "Shift right:\n" ); \ DBG ( "Shift right:\n" ); \
DBG_HDA ( 0, &value_temp, sizeof ( value_temp ) ); \ DBG_HDA ( 0, &value_temp, sizeof ( value_temp ) ); \
bigint_shr ( &value_temp ); \ out = bigint_shr ( &value_temp ); \
DBG_HDA ( 0, &value_temp, sizeof ( value_temp ) ); \ DBG_HDA ( 0, &value_temp, sizeof ( value_temp ) ); \
bigint_done ( &value_temp, result_raw, sizeof ( result_raw ) ); \ bigint_done ( &value_temp, result_raw, sizeof ( result_raw ) ); \
\ \
ok ( memcmp ( result_raw, expected_raw, \ ok ( memcmp ( result_raw, expected_raw, \
sizeof ( result_raw ) ) == 0 ); \ sizeof ( result_raw ) ) == 0 ); \
ok ( out == bit ); \
} while ( 0 ) } while ( 0 )
/** /**
@@ -896,13 +905,13 @@ static void bigint_test_exec ( void ) {
0x7f, 0xcb, 0x94, 0x31, 0x1d, 0xbc, 0x7f, 0xcb, 0x94, 0x31, 0x1d, 0xbc,
0x44, 0x1a ), 0 ); 0x44, 0x1a ), 0 );
bigint_shl_ok ( BIGINT ( 0xe0 ), bigint_shl_ok ( BIGINT ( 0xe0 ),
BIGINT ( 0xc0 ) ); BIGINT ( 0xc0 ), 1 );
bigint_shl_ok ( BIGINT ( 0x43, 0x1d ), bigint_shl_ok ( BIGINT ( 0x43, 0x1d ),
BIGINT ( 0x86, 0x3a ) ); BIGINT ( 0x86, 0x3a ), 0 );
bigint_shl_ok ( BIGINT ( 0xac, 0xed, 0x9b ), bigint_shl_ok ( BIGINT ( 0xac, 0xed, 0x9b ),
BIGINT ( 0x59, 0xdb, 0x36 ) ); BIGINT ( 0x59, 0xdb, 0x36 ), 1 );
bigint_shl_ok ( BIGINT ( 0x2c, 0xe8, 0x3a, 0x22 ), bigint_shl_ok ( BIGINT ( 0x2c, 0xe8, 0x3a, 0x22 ),
BIGINT ( 0x59, 0xd0, 0x74, 0x44 ) ); BIGINT ( 0x59, 0xd0, 0x74, 0x44 ), 0 );
bigint_shl_ok ( BIGINT ( 0x4e, 0x88, 0x4a, 0x05, 0x5e, 0x10, 0xee, bigint_shl_ok ( BIGINT ( 0x4e, 0x88, 0x4a, 0x05, 0x5e, 0x10, 0xee,
0x5b, 0xc6, 0x40, 0x0e, 0x03, 0xd7, 0x0d, 0x5b, 0xc6, 0x40, 0x0e, 0x03, 0xd7, 0x0d,
0x28, 0xa5, 0x55, 0xb2, 0x50, 0xef, 0x69, 0x28, 0xa5, 0x55, 0xb2, 0x50, 0xef, 0x69,
@@ -910,7 +919,19 @@ static void bigint_test_exec ( void ) {
BIGINT ( 0x9d, 0x10, 0x94, 0x0a, 0xbc, 0x21, 0xdc, BIGINT ( 0x9d, 0x10, 0x94, 0x0a, 0xbc, 0x21, 0xdc,
0xb7, 0x8c, 0x80, 0x1c, 0x07, 0xae, 0x1a, 0xb7, 0x8c, 0x80, 0x1c, 0x07, 0xae, 0x1a,
0x51, 0x4a, 0xab, 0x64, 0xa1, 0xde, 0xd3, 0x51, 0x4a, 0xab, 0x64, 0xa1, 0xde, 0xd3,
0xa2, 0x3a ) ); 0xa2, 0x3a ), 0 );
bigint_shl_ok ( BIGINT ( 0x84, 0x56, 0xaa, 0xb4, 0x23, 0xd4, 0x4e,
0xea, 0x92, 0x34, 0x61, 0x11, 0x3e, 0x38,
0x31, 0x8b ),
BIGINT ( 0x08, 0xad, 0x55, 0x68, 0x47, 0xa8, 0x9d,
0xd5, 0x24, 0x68, 0xc2, 0x22, 0x7c, 0x70,
0x63, 0x16 ), 1 );
bigint_shl_ok ( BIGINT ( 0x4a, 0x2b, 0x6b, 0x7c, 0xbf, 0x8a, 0x43,
0x71, 0x96, 0xd6, 0x2f, 0x14, 0xa0, 0x2a,
0xf8, 0x15 ),
BIGINT ( 0x94, 0x56, 0xd6, 0xf9, 0x7f, 0x14, 0x86,
0xe3, 0x2d, 0xac, 0x5e, 0x29, 0x40, 0x55,
0xf0, 0x2a ), 0 );
bigint_shl_ok ( BIGINT ( 0x07, 0x62, 0x78, 0x70, 0x2e, 0xd4, 0x41, bigint_shl_ok ( BIGINT ( 0x07, 0x62, 0x78, 0x70, 0x2e, 0xd4, 0x41,
0xaa, 0x9b, 0x50, 0xb1, 0x9a, 0x71, 0xf5, 0xaa, 0x9b, 0x50, 0xb1, 0x9a, 0x71, 0xf5,
0x1c, 0x2f, 0xe7, 0x0d, 0xf1, 0x46, 0x57, 0x1c, 0x2f, 0xe7, 0x0d, 0xf1, 0x46, 0x57,
@@ -948,15 +969,15 @@ static void bigint_test_exec ( void ) {
0x10, 0xee, 0x09, 0xea, 0x09, 0x9a, 0xd6, 0x10, 0xee, 0x09, 0xea, 0x09, 0x9a, 0xd6,
0x49, 0x7c, 0x1e, 0xdb, 0xc7, 0x65, 0xa6, 0x49, 0x7c, 0x1e, 0xdb, 0xc7, 0x65, 0xa6,
0x0e, 0xd1, 0xd2, 0x00, 0xb3, 0x41, 0xc9, 0x0e, 0xd1, 0xd2, 0x00, 0xb3, 0x41, 0xc9,
0x3c, 0xbc ) ); 0x3c, 0xbc ), 0 );
bigint_shr_ok ( BIGINT ( 0x8f ), bigint_shr_ok ( BIGINT ( 0x8f ),
BIGINT ( 0x47 ) ); BIGINT ( 0x47 ), 1 );
bigint_shr_ok ( BIGINT ( 0xaa, 0x1d ), bigint_shr_ok ( BIGINT ( 0xaa, 0x1d ),
BIGINT ( 0x55, 0x0e ) ); BIGINT ( 0x55, 0x0e ), 1 );
bigint_shr_ok ( BIGINT ( 0xf0, 0xbd, 0x68 ), bigint_shr_ok ( BIGINT ( 0xf0, 0xbd, 0x68 ),
BIGINT ( 0x78, 0x5e, 0xb4 ) ); BIGINT ( 0x78, 0x5e, 0xb4 ), 0 );
bigint_shr_ok ( BIGINT ( 0x33, 0xa0, 0x3d, 0x95 ), bigint_shr_ok ( BIGINT ( 0x33, 0xa0, 0x3d, 0x95 ),
BIGINT ( 0x19, 0xd0, 0x1e, 0xca ) ); BIGINT ( 0x19, 0xd0, 0x1e, 0xca ), 1 );
bigint_shr_ok ( BIGINT ( 0xa1, 0xf4, 0xb9, 0x64, 0x91, 0x99, 0xa1, bigint_shr_ok ( BIGINT ( 0xa1, 0xf4, 0xb9, 0x64, 0x91, 0x99, 0xa1,
0xf4, 0xae, 0xeb, 0x71, 0x97, 0x1b, 0x71, 0xf4, 0xae, 0xeb, 0x71, 0x97, 0x1b, 0x71,
0x09, 0x38, 0x3f, 0x8f, 0xc5, 0x3a, 0xb9, 0x09, 0x38, 0x3f, 0x8f, 0xc5, 0x3a, 0xb9,
@@ -964,7 +985,19 @@ static void bigint_test_exec ( void ) {
BIGINT ( 0x50, 0xfa, 0x5c, 0xb2, 0x48, 0xcc, 0xd0, BIGINT ( 0x50, 0xfa, 0x5c, 0xb2, 0x48, 0xcc, 0xd0,
0xfa, 0x57, 0x75, 0xb8, 0xcb, 0x8d, 0xb8, 0xfa, 0x57, 0x75, 0xb8, 0xcb, 0x8d, 0xb8,
0x84, 0x9c, 0x1f, 0xc7, 0xe2, 0x9d, 0x5c, 0x84, 0x9c, 0x1f, 0xc7, 0xe2, 0x9d, 0x5c,
0xba, 0xca ) ); 0xba, 0xca ), 0 );
bigint_shr_ok ( BIGINT ( 0xa4, 0x19, 0xbb, 0x93, 0x0b, 0x3f, 0x47,
0x5b, 0xb4, 0xb5, 0x7d, 0x75, 0x42, 0x22,
0xcc, 0xdf ),
BIGINT ( 0x52, 0x0c, 0xdd, 0xc9, 0x85, 0x9f, 0xa3,
0xad, 0xda, 0x5a, 0xbe, 0xba, 0xa1, 0x11,
0x66, 0x6f ), 1 );
bigint_shr_ok ( BIGINT ( 0x27, 0x7e, 0x8f, 0x60, 0x40, 0x93, 0x43,
0xd6, 0x89, 0x3e, 0x40, 0x61, 0x9a, 0x04,
0x4c, 0x02 ),
BIGINT ( 0x13, 0xbf, 0x47, 0xb0, 0x20, 0x49, 0xa1,
0xeb, 0x44, 0x9f, 0x20, 0x30, 0xcd, 0x02,
0x26, 0x01 ), 0 );
bigint_shr_ok ( BIGINT ( 0xc0, 0xb3, 0x78, 0x46, 0x69, 0x6e, 0x35, bigint_shr_ok ( BIGINT ( 0xc0, 0xb3, 0x78, 0x46, 0x69, 0x6e, 0x35,
0x94, 0xed, 0x28, 0xdc, 0xfd, 0xf6, 0xdb, 0x94, 0xed, 0x28, 0xdc, 0xfd, 0xf6, 0xdb,
0x2d, 0x24, 0xcb, 0xa4, 0x6f, 0x0e, 0x58, 0x2d, 0x24, 0xcb, 0xa4, 0x6f, 0x0e, 0x58,
@@ -1002,7 +1035,7 @@ static void bigint_test_exec ( void ) {
0x10, 0x64, 0x55, 0x07, 0xec, 0xa8, 0xc7, 0x10, 0x64, 0x55, 0x07, 0xec, 0xa8, 0xc7,
0x46, 0xa8, 0x94, 0xb0, 0xf7, 0xa4, 0x57, 0x46, 0xa8, 0x94, 0xb0, 0xf7, 0xa4, 0x57,
0x1f, 0x72, 0x88, 0x5f, 0xed, 0x4d, 0xc9, 0x1f, 0x72, 0x88, 0x5f, 0xed, 0x4d, 0xc9,
0x59, 0xbb ) ); 0x59, 0xbb ), 1 );
bigint_is_zero_ok ( BIGINT ( 0x9b ), bigint_is_zero_ok ( BIGINT ( 0x9b ),
0 ); 0 );
bigint_is_zero_ok ( BIGINT ( 0x5a, 0x9d ), bigint_is_zero_ok ( BIGINT ( 0x5a, 0x9d ),