2323// Minimal required bytes for BN storing a GF(256) value
2424#define GF2_8_MPI_BYTES 1
2525
26- #define memzero (...) explicit_bzero(__VA_ARGS__)
27-
2826#ifdef TARGET_NANOS
2927/**
3028 * @brief Performs a multiplication over GF(2^n).
@@ -135,81 +133,6 @@ cx_err_t cx_bn_gf2_n_mul(cx_bn_t bn_r,
135133}
136134#endif
137135
138- /**
139- * @brief Performs an invert operation over GF(2^8).
140- *
141- * @param[out] bn_r BN index for the result.
142- *
143- * @param[in] bn_a BN index of the first operand.
144- *
145- * @param[in] bn_n BN index of the modulus.
146- * The modulus must be an irreducible polynomial over GF(2)
147- * of degree n.
148- *
149- * @param[in] bn_h BN index of the second montgomery constant.
150- *
151- * @return Error code:
152- * - CX_OK on success
153- * - CX_NOT_LOCKED
154- * - CX_INVALID_PARAMETER
155- * - CX_MEMORY_FULL
156- */
157- cx_err_t bn_gf2_8_inv (cx_bn_t bn_r , const cx_bn_t bn_a , const cx_bn_t bn_n , const cx_bn_t bn_h ) {
158- cx_err_t error = CX_OK ; // By default, until some error occurs
159- cx_bn_t bn_x , bn_y , bn_z ;
160-
161- CX_CHECK (cx_bn_alloc (& bn_x , GF2_8_MPI_BYTES ));
162- CX_CHECK (cx_bn_alloc (& bn_y , GF2_8_MPI_BYTES ));
163- CX_CHECK (cx_bn_alloc (& bn_z , GF2_8_MPI_BYTES ));
164- CX_CHECK (cx_bn_copy (bn_x , bn_a ));
165-
166- CX_CHECK (cx_bn_gf2_n_mul (bn_y , bn_x , bn_x , bn_n , bn_h )); // bn_y = bn_x^2
167- CX_CHECK (cx_bn_gf2_n_mul (bn_y , bn_y , bn_y , bn_n , bn_h )); // bn_y = bn_x^4
168- CX_CHECK (cx_bn_gf2_n_mul (bn_r , bn_y , bn_y , bn_n , bn_h )); // bn_r = bn_x^8
169- CX_CHECK (cx_bn_gf2_n_mul (bn_z , bn_r , bn_x , bn_n , bn_h )); // bn_z = bn_x^9
170- CX_CHECK (cx_bn_gf2_n_mul (bn_r , bn_r , bn_r , bn_n , bn_h )); // bn_r = bn_x^16
171- CX_CHECK (cx_bn_gf2_n_mul (bn_r , bn_r , bn_z , bn_n , bn_h )); // bn_r = bn_x^25
172- CX_CHECK (cx_bn_gf2_n_mul (bn_r , bn_r , bn_r , bn_n , bn_h )); // bn_r = bn_x^50
173- CX_CHECK (cx_bn_gf2_n_mul (bn_z , bn_r , bn_r , bn_n , bn_h )); // bn_z = bn_x^100
174- CX_CHECK (cx_bn_gf2_n_mul (bn_z , bn_z , bn_z , bn_n , bn_h )); // bn_z = bn_x^200
175- CX_CHECK (cx_bn_gf2_n_mul (bn_r , bn_r , bn_z , bn_n , bn_h )); // bn_r = bn_x^250
176- CX_CHECK (cx_bn_gf2_n_mul (bn_r , bn_r , bn_y , bn_n , bn_h )); // bn_r = bn_x^254
177-
178- CX_CHECK (cx_bn_destroy (& bn_x ));
179- CX_CHECK (cx_bn_destroy (& bn_y ));
180- CX_CHECK (cx_bn_destroy (& bn_z ));
181-
182- end :
183- return error ;
184- }
185-
186- /**
187- * @brief Performs polynomial interpolation on SSS shares.
188- *
189- * @details This function interpolates a polynomial that passes through the provided points
190- * represented by `xi` (x-coordinates) and `yij` (y-coordinate arrays) i.e.
191- * where:
192- * xi points to [x0 x1 ... xn-1 ]
193- * y contains an array of pointers to 32-bit arrays of y values
194- * y contains [y0 y1 y2 ... yn-1]
195- * and each of the yi arrays contain [yi_0 yi_i ... yi_31].
196- *
197- * This interpolation is used in Shamir's Secret Sharing (SSS) to recover
198- * the secret from a set of shares.
199- *
200- * @param[in] n Number of points to interpolate (length of `xi` and `yij`).
201- * @param[in] xi Pointer to an array containing the x-coordinates of the points (length `n`).
202- * @param[in] yl Length of each y-coordinate array in bytes.
203- * @param[in] yij Pointer to an array of `n` pointers, each pointing to a y-coordinate array of
204- * length `yl`.
205- * @param[in] x X-coordinate at which to perform the interpolation.
206- * @param[out] result Pointer to a buffer where the interpolated value will be stored (must be `yl`
207- * bytes long).
208- *
209- * @return - CX_OK on success
210- * - A negative error code on failure (specific error codes not defined here,
211- * consult implementation details for specific error handling)
212- */
213136cx_err_t interpolate (uint8_t n ,
214137 const uint8_t * xi ,
215138 uint8_t yl ,
@@ -220,21 +143,20 @@ cx_err_t interpolate(uint8_t n,
220143 const uint8_t R2 [1 ] = MONTGOMERY_CONSTANT_R2 ;
221144
222145 cx_err_t error = CX_OK ; // By default, until some error occurs
223- cx_bn_t bn_x , bn_xc_i , bn_xc_j ;
224- cx_bn_t bn_numerator , bn_denominator ;
225- cx_bn_t bn_lagrange , bn_y , bn_result , bn_temp , bn_n , bn_r2 ;
146+ cx_bn_t bn_x , bn_xc_i ;
147+ cx_bn_t bn_numerator , bn_denominator , bn_lagrange ;
148+ cx_bn_t bn_result , bn_tempa , bn_tempb , bn_n , bn_r2 ;
226149 uint32_t result_u32 ;
227150
228151 CX_CHECK (cx_bn_lock (GF2_8_MPI_BYTES , 0 ));
229152 CX_CHECK (cx_bn_alloc (& bn_x , GF2_8_MPI_BYTES ));
230153 CX_CHECK (cx_bn_alloc (& bn_xc_i , GF2_8_MPI_BYTES ));
231- CX_CHECK (cx_bn_alloc (& bn_xc_j , GF2_8_MPI_BYTES ));
232154 CX_CHECK (cx_bn_alloc (& bn_numerator , GF2_8_MPI_BYTES ));
233155 CX_CHECK (cx_bn_alloc (& bn_denominator , GF2_8_MPI_BYTES ));
234156 CX_CHECK (cx_bn_alloc (& bn_lagrange , GF2_8_MPI_BYTES ));
235- CX_CHECK (cx_bn_alloc (& bn_y , GF2_8_MPI_BYTES ));
236157 CX_CHECK (cx_bn_alloc (& bn_result , GF2_8_MPI_BYTES ));
237- CX_CHECK (cx_bn_alloc (& bn_temp , GF2_8_MPI_BYTES ));
158+ CX_CHECK (cx_bn_alloc (& bn_tempa , GF2_8_MPI_BYTES ));
159+ CX_CHECK (cx_bn_alloc (& bn_tempb , GF2_8_MPI_BYTES ));
238160 CX_CHECK (cx_bn_alloc_init (& bn_n , GF2_8_MPI_BYTES , N , sizeof (N )));
239161 CX_CHECK (cx_bn_alloc_init (& bn_r2 , GF2_8_MPI_BYTES , R2 , sizeof (R2 )));
240162
@@ -255,16 +177,38 @@ cx_err_t interpolate(uint8_t n,
255177 // j != i (xi[i]-xi[j])
256178 for (uint8_t j = 0 ; j < n ; j ++ ) {
257179 if (j != i ) {
258- CX_CHECK (cx_bn_set_u32 (bn_xc_j , (uint32_t ) xi [j ]));
180+ CX_CHECK (cx_bn_set_u32 (bn_tempa , (uint32_t ) xi [j ]));
259181
260182 // Calculate the numerator (x - xc[j])
261- CX_CHECK (cx_bn_xor (bn_numerator , bn_x , bn_xc_j ));
183+ CX_CHECK (cx_bn_xor (bn_numerator , bn_x , bn_tempa ));
262184
263185 // Calculate the denominator (xc[i] - xc[j])
264- CX_CHECK (cx_bn_xor (bn_denominator , bn_xc_i , bn_xc_j ));
186+ CX_CHECK (cx_bn_xor (bn_denominator , bn_xc_i , bn_tempa ));
265187
266188 // Calculate the inverse of the denominator
267- CX_CHECK (bn_gf2_8_inv (bn_denominator , bn_denominator , bn_n , bn_r2 ));
189+ // In GF(2^8) the inverse of x = x^254
190+ // bn_result = bn_denominator^2
191+ CX_CHECK (cx_bn_gf2_n_mul (bn_result , bn_denominator , bn_denominator , bn_n , bn_r2 ));
192+ // bn_result = bn_denominator^4
193+ CX_CHECK (cx_bn_gf2_n_mul (bn_result , bn_result , bn_result , bn_n , bn_r2 ));
194+ // bn_tempa = bn_denominator^8
195+ CX_CHECK (cx_bn_gf2_n_mul (bn_tempa , bn_result , bn_result , bn_n , bn_r2 ));
196+ // bn_tempb = bn_denominator^9
197+ CX_CHECK (cx_bn_gf2_n_mul (bn_tempb , bn_tempa , bn_denominator , bn_n , bn_r2 ));
198+ // bn_tempa = bn_denominator^16
199+ CX_CHECK (cx_bn_gf2_n_mul (bn_tempa , bn_tempa , bn_tempa , bn_n , bn_r2 ));
200+ // bn_tempa = bn_denominator^25
201+ CX_CHECK (cx_bn_gf2_n_mul (bn_tempa , bn_tempa , bn_tempb , bn_n , bn_r2 ));
202+ // bn_tempa = bn_denominator^50
203+ CX_CHECK (cx_bn_gf2_n_mul (bn_tempa , bn_tempa , bn_tempa , bn_n , bn_r2 ));
204+ // bn_tempb = bn_denominator^100
205+ CX_CHECK (cx_bn_gf2_n_mul (bn_tempb , bn_tempa , bn_tempa , bn_n , bn_r2 ));
206+ // bn_tempb = bn_denominator^200
207+ CX_CHECK (cx_bn_gf2_n_mul (bn_tempb , bn_tempb , bn_tempb , bn_n , bn_r2 ));
208+ // bn_tempa = bn_denominator^250
209+ CX_CHECK (cx_bn_gf2_n_mul (bn_tempa , bn_tempa , bn_tempb , bn_n , bn_r2 ));
210+ // bn_denominator = bn_denominator^254
211+ CX_CHECK (cx_bn_gf2_n_mul (bn_denominator , bn_result , bn_tempa , bn_n , bn_r2 ));
268212
269213 // Calculate the lagrange basis coefficient
270214 CX_CHECK (cx_bn_gf2_n_mul (bn_lagrange , bn_numerator , bn_lagrange , bn_n , bn_r2 ));
@@ -273,12 +217,12 @@ cx_err_t interpolate(uint8_t n,
273217 }
274218
275219 for (uint8_t j = 0 ; j < yl ; j ++ ) {
276- CX_CHECK (cx_bn_set_u32 (bn_y , (uint32_t ) yij [i ][j ]));
220+ CX_CHECK (cx_bn_set_u32 (bn_tempa , (uint32_t ) yij [i ][j ]));
277221 CX_CHECK (cx_bn_set_u32 (bn_result , (uint32_t ) result [j ]));
278222
279- CX_CHECK (cx_bn_gf2_n_mul (bn_y , bn_lagrange , bn_y , bn_n , bn_r2 ));
280- CX_CHECK (cx_bn_copy (bn_temp , bn_result ));
281- CX_CHECK (cx_bn_xor (bn_result , bn_temp , bn_y ));
223+ CX_CHECK (cx_bn_gf2_n_mul (bn_tempa , bn_lagrange , bn_tempa , bn_n , bn_r2 ));
224+ CX_CHECK (cx_bn_copy (bn_tempb , bn_result ));
225+ CX_CHECK (cx_bn_xor (bn_result , bn_tempa , bn_tempb ));
282226 CX_CHECK (cx_bn_get_u32 (bn_result , & result_u32 ));
283227 result [j ] = (uint8_t ) result_u32 ;
284228 result_u32 = 0 ;
@@ -288,13 +232,12 @@ cx_err_t interpolate(uint8_t n,
288232 // clean up stack
289233 CX_CHECK (cx_bn_destroy (& bn_x ));
290234 CX_CHECK (cx_bn_destroy (& bn_xc_i ));
291- CX_CHECK (cx_bn_destroy (& bn_xc_j ));
292235 CX_CHECK (cx_bn_destroy (& bn_numerator ));
293236 CX_CHECK (cx_bn_destroy (& bn_denominator ));
294237 CX_CHECK (cx_bn_destroy (& bn_lagrange ));
295- CX_CHECK (cx_bn_destroy (& bn_y ));
296238 CX_CHECK (cx_bn_destroy (& bn_result ));
297- CX_CHECK (cx_bn_destroy (& bn_temp ));
239+ CX_CHECK (cx_bn_destroy (& bn_tempa ));
240+ CX_CHECK (cx_bn_destroy (& bn_tempb ));
298241 CX_CHECK (cx_bn_destroy (& bn_n ));
299242 CX_CHECK (cx_bn_destroy (& bn_r2 ));
300243
0 commit comments