Path: blob/main/crypto/libecc/src/fp/fp_mul_redc1.c
34869 views
/*1* Copyright (C) 2017 - This file is part of libecc project2*3* Authors:4* Ryad BENADJILA <[email protected]>5* Arnaud EBALARD <[email protected]>6* Jean-Pierre FLORI <[email protected]>7*8* Contributors:9* Nicolas VIVET <[email protected]>10* Karim KHALFALLAH <[email protected]>11*12* This software is licensed under a dual BSD and GPL v2 license.13* See LICENSE file at the root folder of the project.14*/15#include <libecc/fp/fp_mul_redc1.h>1617/*18* Internal helper performing Montgomery multiplication. The function returns19* 0 on success, -1 on error.20*21* CAUTION: the function does not check input parameters. Those checks MUST be22* performed by the caller.23*/24ATTRIBUTE_WARN_UNUSED_RET static inline int _fp_mul_redc1(nn_t out, nn_src_t in1, nn_src_t in2,25fp_ctx_src_t ctx)26{27return nn_mul_redc1(out, in1, in2, &(ctx->p), ctx->mpinv);28}2930/*31* Compute out = in1 * in2 mod (p) in redcified form.32*33* Exported version based on previous one, that sanity checks input parameters.34* The function returns 0 on success, -1 on error.35*36* Aliasing is supported.37*/38int fp_mul_redc1(fp_t out, fp_src_t in1, fp_src_t in2)39{40int ret;4142ret = fp_check_initialized(in1); EG(ret, err);43ret = fp_check_initialized(in2); EG(ret, err);44ret = fp_check_initialized(out); EG(ret, err);4546MUST_HAVE((out->ctx == in1->ctx), ret, err);47MUST_HAVE((out->ctx == in2->ctx), ret, err);4849ret = _fp_mul_redc1(&(out->fp_val), &(in1->fp_val), &(in2->fp_val),50out->ctx);5152err:53return ret;54}5556/*57* Compute out = in * in mod (p) in redcified form.58*59* Aliasing is supported.60*/61int fp_sqr_redc1(fp_t out, fp_src_t in)62{63return fp_mul_redc1(out, in, in);64}6566/*67* Compute out = redcified form of in.68* redcify could be done by shifting and division by p. The function returns 069* on success, -1 on error.70*71* Aliasing is supported.72*/73int fp_redcify(fp_t out, fp_src_t in)74{75int ret;7677ret = fp_check_initialized(in); EG(ret, err);78ret = fp_check_initialized(out); EG(ret, err);7980MUST_HAVE((out->ctx == in->ctx), ret, err);8182ret = _fp_mul_redc1(&(out->fp_val), &(in->fp_val), &(out->ctx->r_square),83out->ctx);8485err:86return ret;87}8889/*90* Compute out = unredcified form of in.91* The function returns 0 on success, -1 on error.92*93* Aliasing is supported.94*/95int fp_unredcify(fp_t out, fp_src_t in)96{97int ret;98nn one;99one.magic = WORD(0);100101ret = fp_check_initialized(in); EG(ret, err);102ret = fp_check_initialized(out); EG(ret, err);103ret = nn_init(&one, 0); EG(ret, err);104ret = nn_one(&one); EG(ret, err);105ret = _fp_mul_redc1(&(out->fp_val), &(in->fp_val), &one, out->ctx);106107err:108nn_uninit(&one);109110return ret;111}112113114