Skip to content

Commit

Permalink
Some cleanups in Fp functions comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
rben-dev committed Jun 6, 2023
1 parent 2479434 commit 390c6c6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/fp/fp_montgomery.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ int fp_sqr_monty(fp_t out, fp_src_t in)
/*
* Compute out such that in1 = out * in2 mod p in the Montgomery form.
* Inputs and outputs are in their Montgomery form.
* Returns 0 on success, -1 on error. out must be initialized by the caler.
* Returns 0 on success, -1 on error. out must be initialized by the caller.
*
* Aliasing is supported.
*/
Expand Down
38 changes: 31 additions & 7 deletions src/fp/fp_mul.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#include "../nn/nn_modinv.h"

/*
* 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_mul(fp_t out, fp_src_t in1, fp_src_t in2)
Expand All @@ -43,6 +46,9 @@ int fp_mul(fp_t out, fp_src_t in1, fp_src_t in2)
}

/*
* Compute out = in * in mod p. 'out' parameter must have been initialized
* by the caller. Returns 0 on success, -1 on error.
*
* Aliasing is supported.
*/
int fp_sqr(fp_t out, fp_src_t in)
Expand Down Expand Up @@ -75,6 +81,10 @@ int fp_inv(fp_t out, fp_src_t in)
return ret;
}

/*
* Compute out = w^-1 mod p. 'out' parameter must have been initialized
* by the caller. Returns 0 on success, -1 on error.
*/
int fp_inv_word(fp_t out, word_t w)
{
int ret;
Expand All @@ -88,8 +98,10 @@ int fp_inv_word(fp_t out, word_t w)
}

/*
* Aliasing of out and num is NOT supported.
* Aliasing of out and den is supported.
* Compute out such that num = out * den mod p. 'out' parameter must have been initialized
* by the caller. Returns 0 on success, -1 on error.
*
* Aliasing is supported.
*/
int fp_div(fp_t out, fp_src_t num, fp_src_t den)
{
Expand All @@ -99,14 +111,26 @@ 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);

ret = fp_inv(out, den); EG(ret, err);
ret = fp_mul(out, num, out);
if(out == num){
/* Handle aliasing of out and num */
fp _num;
_num.magic = WORD(0);

ret = fp_copy(&_num, num); EG(ret, err1);
ret = fp_inv(out, den); EG(ret, err1);
ret = fp_mul(out, &_num, out);

err1:
fp_uninit(&_num);
EG(ret, err);
}
else{
ret = fp_inv(out, den); EG(ret, err);
ret = fp_mul(out, num, out);
}

err:
return ret;
Expand Down
6 changes: 6 additions & 0 deletions src/fp/fp_mul_redc1.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ ATTRIBUTE_WARN_UNUSED_RET static inline int _fp_mul_redc1(nn_t out, nn_src_t in1
}

/*
* Compute out = in1 * in2 mod (p) in redcified form.
*
* Exported version based on previous one, that sanity checks input parameters.
* The function returns 0 on success, -1 on error.
*
Expand All @@ -53,6 +55,8 @@ int fp_mul_redc1(fp_t out, fp_src_t in1, fp_src_t in2)
}

/*
* Compute out = in * in mod (p) in redcified form.
*
* Aliasing is supported.
*/
int fp_sqr_redc1(fp_t out, fp_src_t in)
Expand All @@ -61,6 +65,7 @@ int fp_sqr_redc1(fp_t out, fp_src_t in)
}

/*
* Compute out = redcified form of in.
* redcify could be done by shifting and division by p. The function returns 0
* on success, -1 on error.
*
Expand All @@ -83,6 +88,7 @@ int fp_redcify(fp_t out, fp_src_t in)
}

/*
* Compute out = unredcified form of in.
* The function returns 0 on success, -1 on error.
*
* Aliasing is supported.
Expand Down
7 changes: 5 additions & 2 deletions src/fp/fp_pow.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ ATTRIBUTE_WARN_UNUSED_RET static int _fp_pow_aliased(fp_t out, nn_src_t exp)
return ret;
}

/* Aliased version of previous one.
/*
* Compute out = base^exp (p). 'base', 'exp' and 'out' are supposed to be initialized.
* Aliased version of previous one.
*
* Aliasing is supported.
*/
Expand All @@ -62,12 +64,13 @@ int fp_pow(fp_t out, fp_src_t base, nn_src_t exp)

ret = fp_check_initialized(base); EG(ret, err);
ret = nn_check_initialized(exp); EG(ret, err);
ret = fp_check_initialized(out); EG(ret, err);
MUST_HAVE(((&(out->ctx->p)) == (&(base->ctx->p))), ret, err);

/* Handle output aliasing */
if (out == base) {
ret = _fp_pow_aliased(out, exp);
} else {
ret = fp_init(out, base->ctx); EG(ret, err);
ret = _fp_pow(out, base, exp);
}

Expand Down
4 changes: 4 additions & 0 deletions src/fp/fp_sqrt.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ ATTRIBUTE_WARN_UNUSED_RET static int legendre(fp_src_t a)
*
* NOTE: the algorithm is NOT constant time.
*
* The outputs, sqrt1 and sqrt2 ARE initialized by the function.
* The function returns 0 on success, -1 on error (in which case values of sqrt1 and sqrt2
* must not be considered).
*
* Aliasing is supported.
*
*/
Expand Down

0 comments on commit 390c6c6

Please sign in to comment.