|
@@ -35,12 +35,14 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
35
|
35
|
* @v multiplier0 Element 0 of big integer to be multiplied
|
36
|
36
|
* @v modulus0 Element 0 of big integer modulus
|
37
|
37
|
* @v result0 Element 0 of big integer to hold result
|
|
38
|
+ * @v size Number of elements in base, modulus, and result
|
|
39
|
+ * @v tmp Temporary working space
|
38
|
40
|
*/
|
39
|
41
|
void bigint_mod_multiply_raw ( const bigint_element_t *multiplicand0,
|
40
|
42
|
const bigint_element_t *multiplier0,
|
41
|
43
|
const bigint_element_t *modulus0,
|
42
|
44
|
bigint_element_t *result0,
|
43
|
|
- unsigned int size ) {
|
|
45
|
+ unsigned int size, void *tmp ) {
|
44
|
46
|
const bigint_t ( size ) __attribute__ (( may_alias )) *multiplicand =
|
45
|
47
|
( ( const void * ) multiplicand0 );
|
46
|
48
|
const bigint_t ( size ) __attribute__ (( may_alias )) *multiplier =
|
|
@@ -49,30 +51,35 @@ void bigint_mod_multiply_raw ( const bigint_element_t *multiplicand0,
|
49
|
51
|
( ( const void * ) modulus0 );
|
50
|
52
|
bigint_t ( size ) __attribute__ (( may_alias )) *result =
|
51
|
53
|
( ( void * ) result0 );
|
52
|
|
- bigint_t ( size * 2 ) temp_result;
|
53
|
|
- bigint_t ( size * 2 ) temp_modulus;
|
|
54
|
+ struct {
|
|
55
|
+ bigint_t ( size * 2 ) result;
|
|
56
|
+ bigint_t ( size * 2 ) modulus;
|
|
57
|
+ } *temp = tmp;
|
54
|
58
|
int rotation;
|
55
|
59
|
int i;
|
56
|
60
|
|
|
61
|
+ /* Sanity check */
|
|
62
|
+ assert ( sizeof ( *temp ) == bigint_mod_multiply_tmp_len ( modulus ) );
|
|
63
|
+
|
57
|
64
|
/* Perform multiplication */
|
58
|
|
- bigint_multiply ( multiplicand, multiplier, &temp_result );
|
|
65
|
+ bigint_multiply ( multiplicand, multiplier, &temp->result );
|
59
|
66
|
|
60
|
67
|
/* Rescale modulus to match result */
|
61
|
|
- bigint_grow ( modulus, &temp_modulus );
|
62
|
|
- rotation = ( bigint_max_set_bit ( &temp_result ) -
|
63
|
|
- bigint_max_set_bit ( &temp_modulus ) );
|
|
68
|
+ bigint_grow ( modulus, &temp->modulus );
|
|
69
|
+ rotation = ( bigint_max_set_bit ( &temp->result ) -
|
|
70
|
+ bigint_max_set_bit ( &temp->modulus ) );
|
64
|
71
|
for ( i = 0 ; i < rotation ; i++ )
|
65
|
|
- bigint_rol ( &temp_modulus );
|
|
72
|
+ bigint_rol ( &temp->modulus );
|
66
|
73
|
|
67
|
74
|
/* Subtract multiples of modulus */
|
68
|
75
|
for ( i = 0 ; i <= rotation ; i++ ) {
|
69
|
|
- if ( bigint_is_geq ( &temp_result, &temp_modulus ) )
|
70
|
|
- bigint_subtract ( &temp_modulus, &temp_result );
|
71
|
|
- bigint_ror ( &temp_modulus );
|
|
76
|
+ if ( bigint_is_geq ( &temp->result, &temp->modulus ) )
|
|
77
|
+ bigint_subtract ( &temp->modulus, &temp->result );
|
|
78
|
+ bigint_ror ( &temp->modulus );
|
72
|
79
|
}
|
73
|
80
|
|
74
|
81
|
/* Resize result */
|
75
|
|
- bigint_shrink ( &temp_result, result );
|
|
82
|
+ bigint_shrink ( &temp->result, result );
|
76
|
83
|
|
77
|
84
|
/* Sanity check */
|
78
|
85
|
assert ( bigint_is_geq ( modulus, result ) );
|
|
@@ -87,13 +94,14 @@ void bigint_mod_multiply_raw ( const bigint_element_t *multiplicand0,
|
87
|
94
|
* @v result0 Element 0 of big integer to hold result
|
88
|
95
|
* @v size Number of elements in base, modulus, and result
|
89
|
96
|
* @v exponent_size Number of elements in exponent
|
|
97
|
+ * @v tmp Temporary working space
|
90
|
98
|
*/
|
91
|
99
|
void bigint_mod_exp_raw ( const bigint_element_t *base0,
|
92
|
100
|
const bigint_element_t *modulus0,
|
93
|
101
|
const bigint_element_t *exponent0,
|
94
|
102
|
bigint_element_t *result0,
|
95
|
|
- unsigned int size,
|
96
|
|
- unsigned int exponent_size ) {
|
|
103
|
+ unsigned int size, unsigned int exponent_size,
|
|
104
|
+ void *tmp ) {
|
97
|
105
|
const bigint_t ( size ) __attribute__ (( may_alias )) *base =
|
98
|
106
|
( ( const void * ) base0 );
|
99
|
107
|
const bigint_t ( size ) __attribute__ (( may_alias )) *modulus =
|
|
@@ -102,21 +110,25 @@ void bigint_mod_exp_raw ( const bigint_element_t *base0,
|
102
|
110
|
*exponent = ( ( const void * ) exponent0 );
|
103
|
111
|
bigint_t ( size ) __attribute__ (( may_alias )) *result =
|
104
|
112
|
( ( void * ) result0 );
|
105
|
|
- bigint_t ( size ) temp_base;
|
106
|
|
- bigint_t ( exponent_size ) temp_exponent;
|
|
113
|
+ size_t mod_multiply_len = bigint_mod_multiply_tmp_len ( modulus );
|
|
114
|
+ struct {
|
|
115
|
+ bigint_t ( size ) base;
|
|
116
|
+ bigint_t ( exponent_size ) exponent;
|
|
117
|
+ uint8_t mod_multiply[mod_multiply_len];
|
|
118
|
+ } *temp = tmp;
|
107
|
119
|
static const uint8_t start[1] = { 0x01 };
|
108
|
120
|
|
109
|
|
- memcpy ( &temp_base, base, sizeof ( temp_base ) );
|
110
|
|
- memcpy ( &temp_exponent, exponent, sizeof ( temp_exponent ) );
|
|
121
|
+ memcpy ( &temp->base, base, sizeof ( temp->base ) );
|
|
122
|
+ memcpy ( &temp->exponent, exponent, sizeof ( temp->exponent ) );
|
111
|
123
|
bigint_init ( result, start, sizeof ( start ) );
|
112
|
124
|
|
113
|
|
- while ( ! bigint_is_zero ( &temp_exponent ) ) {
|
114
|
|
- if ( bigint_bit_is_set ( &temp_exponent, 0 ) ) {
|
115
|
|
- bigint_mod_multiply ( result, &temp_base,
|
116
|
|
- modulus, result );
|
|
125
|
+ while ( ! bigint_is_zero ( &temp->exponent ) ) {
|
|
126
|
+ if ( bigint_bit_is_set ( &temp->exponent, 0 ) ) {
|
|
127
|
+ bigint_mod_multiply ( result, &temp->base, modulus,
|
|
128
|
+ result, temp->mod_multiply );
|
117
|
129
|
}
|
118
|
|
- bigint_ror ( &temp_exponent );
|
119
|
|
- bigint_mod_multiply ( &temp_base, &temp_base, modulus,
|
120
|
|
- &temp_base );
|
|
130
|
+ bigint_ror ( &temp->exponent );
|
|
131
|
+ bigint_mod_multiply ( &temp->base, &temp->base, modulus,
|
|
132
|
+ &temp->base, temp->mod_multiply );
|
121
|
133
|
}
|
122
|
134
|
}
|