Skip to content

Commit

Permalink
Merge branch 'prepare_CF' into IPECC
Browse files Browse the repository at this point in the history
  • Loading branch information
rben-dev committed Jun 4, 2023
2 parents 080cd68 + 2479434 commit bb994f8
Show file tree
Hide file tree
Showing 14 changed files with 250 additions and 11 deletions.
7 changes: 7 additions & 0 deletions src/fp/fp.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ int fp_iszero(fp_src_t in, int *iszero)
* output is already initialized, check that the Fp contexts are consistent.
* Else, output is initialized with the same field context as input. Returns 0
* on success, -1 on error.
*
* Aliasing of input and output is supported.
*/
int fp_copy(fp_t out, fp_src_t in)
{
Expand Down Expand Up @@ -355,6 +357,9 @@ int fp_copy(fp_t out, fp_src_t in)
* Fp elements in 'tab'
*
* Returns 0 on success, -1 on error.
*
* Aliasing of out and the selected element inside the tab is NOT supported.
*
*/
int fp_tabselect(fp_t out, u8 idx, fp_src_t *tab, u8 tabsize)
{
Expand Down Expand Up @@ -400,6 +405,8 @@ int fp_tabselect(fp_t out, u8 idx, fp_src_t *tab, u8 tabsize)
* in2 are not equal or opposite, 'eq_or_opp' is set to 0. The function
* returns 0 on success and -1 on error. 'eq_or_opp' is only meaningful
* on success, i.e. if the return value is 0.
*
* Aliasing of inputs is supported.
*/
int fp_eq_or_opp(fp_src_t in1, fp_src_t in2, int *eq_or_opp)
{
Expand Down
10 changes: 10 additions & 0 deletions src/fp/fp_add.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
/*
* Compute out = in1 + in2 mod p. 'out' parameter must have been initialized
* by the caller. Returns 0 on success, -1 on error.
*
* Aliasing is supported.
*/
int fp_add(fp_t out, fp_src_t in1, fp_src_t in2)
{
Expand All @@ -44,6 +46,8 @@ int fp_add(fp_t out, fp_src_t in1, fp_src_t in2)
/*
* Compute out = in + 1 mod p. 'out' parameter must have been initialized
* by the caller. Returns 0 on success, -1 on error.
*
* Aliasing is supported.
*/
int fp_inc(fp_t out, fp_src_t in)
{
Expand All @@ -65,6 +69,8 @@ int fp_inc(fp_t out, fp_src_t in)
/*
* Compute out = in1 - in2 mod p. 'out' parameter must have been initialized
* by the caller. Returns 0 on success, -1 on error.
*
* Aliasing is supported.
*/
int fp_sub(fp_t out, fp_src_t in1, fp_src_t in2)
{
Expand All @@ -90,6 +96,8 @@ int fp_sub(fp_t out, fp_src_t in1, fp_src_t in2)
/*
* Compute out = in - 1 mod p. 'out' parameter must have been initialized
* by the caller. Returns 0 on success, -1 on error.
*
* Aliasing is supported.
*/
int fp_dec(fp_t out, fp_src_t in)
{
Expand All @@ -111,6 +119,8 @@ int fp_dec(fp_t out, fp_src_t in)
/*
* Compute out = -in mod p = (p - in) mod p. 'out' parameter must have been
* initialized by the caller. Returns 0 on success, -1 on error.
*
* Aliasing is supported.
*/
int fp_neg(fp_t out, fp_src_t in)
{
Expand Down
9 changes: 9 additions & 0 deletions src/fp/fp_montgomery.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
/* Compute out = in1 + in2 mod p in the Montgomery form.
* Inputs and outputs are in their Montgomery form.
* Returns 0 on success, -1 on error.
*
* Aliasing is supported.
*/
int fp_add_monty(fp_t out, fp_src_t in1, fp_src_t in2)
{
Expand All @@ -31,6 +33,8 @@ int fp_add_monty(fp_t out, fp_src_t in1, fp_src_t in2)
/* Compute out = in1 - in2 mod p in the Montgomery form.
* Inputs and outputs are in their Montgomery form.
* Returns 0 on success, -1 on error.
*
* Aliasing is supported.
*/
int fp_sub_monty(fp_t out, fp_src_t in1, fp_src_t in2)
{
Expand All @@ -40,6 +44,8 @@ int fp_sub_monty(fp_t out, fp_src_t in1, fp_src_t in2)
/* Compute out = in1 * in2 mod p in the Montgomery form.
* Inputs and outputs are in their Montgomery form.
* Returns 0 on success, -1 on error.
*
* Aliasing is supported.
*/
int fp_mul_monty(fp_t out, fp_src_t in1, fp_src_t in2)
{
Expand All @@ -49,6 +55,8 @@ int fp_mul_monty(fp_t out, fp_src_t in1, fp_src_t in2)
/* Compute out = in * in mod p in the Montgomery form.
* Inputs and outputs are in their Montgomery form.
* Returns 0 on success, -1 on error.
*
* Aliasing is supported.
*/
int fp_sqr_monty(fp_t out, fp_src_t in)
{
Expand All @@ -60,6 +68,7 @@ int fp_sqr_monty(fp_t out, fp_src_t in)
* Inputs and outputs are in their Montgomery form.
* Returns 0 on success, -1 on error. out must be initialized by the caler.
*
* Aliasing is supported.
*/
int fp_div_monty(fp_t out, fp_src_t in1, fp_src_t in2)
{
Expand Down
15 changes: 15 additions & 0 deletions src/fp/fp_mul.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include "../nn/nn_div.h"
#include "../nn/nn_modinv.h"

/*
* Aliasing is supported.
*/
int fp_mul(fp_t out, fp_src_t in1, fp_src_t in2)
{
int ret;
Expand All @@ -39,6 +42,9 @@ int fp_mul(fp_t out, fp_src_t in1, fp_src_t in2)
return ret;
}

/*
* Aliasing is supported.
*/
int fp_sqr(fp_t out, fp_src_t in)
{
return fp_mul(out, in, in);
Expand All @@ -47,6 +53,8 @@ int fp_sqr(fp_t out, fp_src_t in)
/* We use Fermat's little theorem for our inversion in Fp:
* x^(p-1) = 1 mod (p) means that x^(p-2) mod(p) is the modular
* inverse of x mod (p)
*
* Aliasing is supported.
*/
int fp_inv(fp_t out, fp_src_t in)
{
Expand Down Expand Up @@ -79,6 +87,10 @@ int fp_inv_word(fp_t out, word_t w)
return ret;
}

/*
* Aliasing of out and num is NOT supported.
* Aliasing of out and den is supported.
*/
int fp_div(fp_t out, fp_src_t num, fp_src_t den)
{
int ret;
Expand All @@ -87,6 +99,9 @@ int fp_div(fp_t out, fp_src_t num, fp_src_t den)
ret = fp_check_initialized(den); EG(ret, err);
ret = fp_check_initialized(out); EG(ret, err);

/* Unsupported multi-aliasing */
MUST_HAVE((out != num), ret, err);

MUST_HAVE(out->ctx == num->ctx, ret, err);
MUST_HAVE(out->ctx == den->ctx, ret, err);

Expand Down
13 changes: 12 additions & 1 deletion src/fp/fp_mul_redc1.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ ATTRIBUTE_WARN_UNUSED_RET static inline int _fp_mul_redc1(nn_t out, nn_src_t in1
/*
* Exported version based on previous one, that sanity checks input parameters.
* The function returns 0 on success, -1 on error.
*
* Aliasing is supported.
*/
int fp_mul_redc1(fp_t out, fp_src_t in1, fp_src_t in2)
{
Expand All @@ -50,6 +52,9 @@ int fp_mul_redc1(fp_t out, fp_src_t in1, fp_src_t in2)
return ret;
}

/*
* Aliasing is supported.
*/
int fp_sqr_redc1(fp_t out, fp_src_t in)
{
return fp_mul_redc1(out, in, in);
Expand All @@ -58,6 +63,8 @@ int fp_sqr_redc1(fp_t out, fp_src_t in)
/*
* redcify could be done by shifting and division by p. The function returns 0
* on success, -1 on error.
*
* Aliasing is supported.
*/
int fp_redcify(fp_t out, fp_src_t in)
{
Expand All @@ -75,7 +82,11 @@ int fp_redcify(fp_t out, fp_src_t in)
return ret;
}

/* The function returns 0 on success, -1 on error. */
/*
* The function returns 0 on success, -1 on error.
*
* Aliasing is supported.
*/
int fp_unredcify(fp_t out, fp_src_t in)
{
int ret;
Expand Down
5 changes: 4 additions & 1 deletion src/fp/fp_pow.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ ATTRIBUTE_WARN_UNUSED_RET static int _fp_pow_aliased(fp_t out, nn_src_t exp)
return ret;
}

/* Aliased version of previous one. */
/* Aliased version of previous one.
*
* Aliasing is supported.
*/
int fp_pow(fp_t out, fp_src_t base, nn_src_t exp)
{
int ret;
Expand Down
5 changes: 3 additions & 2 deletions src/fp/fp_sqrt.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ ATTRIBUTE_WARN_UNUSED_RET static int legendre(fp_src_t a)
* - Otherwise find, by repeated squaring, the lowest i , 0 < i < m , such as t^(2^i) ≡ 1
* - Let b ≡ c^(2^(m-i-1)), and set r ≡ r*b, t ≡ t*b^2 , c ≡ b^2 and m = i.
*
* Input aliasing is supported.
*
* NOTE: the algorithm is NOT constant time.
*
* Aliasing is supported.
*
*/
int fp_sqrt(fp_t sqrt1, fp_t sqrt2, fp_src_t n)
{
Expand Down
8 changes: 8 additions & 0 deletions src/nn/nn.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ int nn_one(nn_t A)
* Conditionally swap two nn's content *in constant time*. Swapping is done
* if 'cnd' is not zero. Nothing is done otherwise. Returns 0 on success, -1
* on error.
*
* Aliasing of inputs is supported.
*/
int nn_cnd_swap(int cnd, nn_t in1, nn_t in2)
{
Expand Down Expand Up @@ -361,6 +363,8 @@ int nn_cmp_word(nn_src_t in, word_t w, int *cmp)
* function returns 0 on success and provides the comparison value in
* 'cmp' parameter (0 if A == B, -1 if A < B, +1 if A > B). -1 is returned
* on error, in which case 'cmp' is not meaningful.
*
* Aliasing of inputs is supported.
*/
int nn_cmp(nn_src_t A, nn_src_t B, int *cmp)
{
Expand Down Expand Up @@ -390,6 +394,8 @@ int nn_cmp(nn_src_t A, nn_src_t B, int *cmp)
* 'dst_nn' must point to a declared nn, but *need not be initialized*; it will
* be (manually) initialized by the function. 'src_nn' must have been
* initialized prior to the call. The function returns 0 on success, -1 on error.
*
* Alising of input and output is supported.
*/
int nn_copy(nn_t dst_nn, nn_src_t src_nn)
{
Expand Down Expand Up @@ -565,6 +571,8 @@ int nn_export_to_buf(u8 *buf, u16 buflen, nn_src_t in_nn)
* NN elements in 'tab'
*
* Returns 0 on success, -1 on error.
*
* Aliasing of out and the selected element inside the tab is NOT supported.
*/
int nn_tabselect(nn_t out, u8 idx, nn_src_t *tab, u8 tabsize)
{
Expand Down

0 comments on commit bb994f8

Please sign in to comment.