Path: blob/main/crypto/libecc/src/fp/fp_montgomery.c
34914 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.h>16#include <libecc/fp/fp_add.h>17#include <libecc/fp/fp_mul.h>18#include <libecc/fp/fp_mul_redc1.h>19#include <libecc/fp/fp_montgomery.h>2021/* Compute out = in1 + in2 mod p in the Montgomery form.22* Inputs and outputs are in their Montgomery form.23* Returns 0 on success, -1 on error.24*25* Aliasing is supported.26*/27int fp_add_monty(fp_t out, fp_src_t in1, fp_src_t in2)28{29return fp_add(out, in1, in2);30}3132/* Compute out = in1 - in2 mod p in the Montgomery form.33* Inputs and outputs are in their Montgomery form.34* Returns 0 on success, -1 on error.35*36* Aliasing is supported.37*/38int fp_sub_monty(fp_t out, fp_src_t in1, fp_src_t in2)39{40return fp_sub(out, in1, in2);41}4243/* Compute out = in1 * in2 mod p in the Montgomery form.44* Inputs and outputs are in their Montgomery form.45* Returns 0 on success, -1 on error.46*47* Aliasing is supported.48*/49int fp_mul_monty(fp_t out, fp_src_t in1, fp_src_t in2)50{51return fp_mul_redc1(out, in1, in2);52}5354/* Compute out = in * in mod p in the Montgomery form.55* Inputs and outputs are in their Montgomery form.56* Returns 0 on success, -1 on error.57*58* Aliasing is supported.59*/60int fp_sqr_monty(fp_t out, fp_src_t in)61{62return fp_sqr_redc1(out, in);63}6465/*66* Compute out such that in1 = out * in2 mod p in the Montgomery form.67* Inputs and outputs are in their Montgomery form.68* Returns 0 on success, -1 on error. out must be initialized by the caller.69*70* Aliasing is supported.71*/72int fp_div_monty(fp_t out, fp_src_t in1, fp_src_t in2)73{74int ret, iszero;7576ret = fp_check_initialized(in1); EG(ret, err);77ret = fp_check_initialized(in2); EG(ret, err);78ret = fp_check_initialized(out); EG(ret, err);7980MUST_HAVE((out->ctx == in1->ctx), ret, err);81MUST_HAVE((out->ctx == in2->ctx), ret, err);82FORCE_USED_VAR(iszero); /* silence warning when macro results in nothing */83MUST_HAVE(!fp_iszero(in2, &iszero) && (!iszero), ret, err);8485ret = fp_div(out, in1, in2); EG(ret, err);86ret = fp_redcify(out, out);8788err:89return ret;90}919293