xref: /freebsd/crypto/libecc/src/fp/fp_montgomery.c (revision f0865ec9906d5a18fa2a3b61381f22ce16e606ad)
1*f0865ec9SKyle Evans /*
2*f0865ec9SKyle Evans  *  Copyright (C) 2017 - This file is part of libecc project
3*f0865ec9SKyle Evans  *
4*f0865ec9SKyle Evans  *  Authors:
5*f0865ec9SKyle Evans  *      Ryad BENADJILA <ryadbenadjila@gmail.com>
6*f0865ec9SKyle Evans  *      Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
7*f0865ec9SKyle Evans  *      Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
8*f0865ec9SKyle Evans  *
9*f0865ec9SKyle Evans  *  Contributors:
10*f0865ec9SKyle Evans  *      Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
11*f0865ec9SKyle Evans  *      Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
12*f0865ec9SKyle Evans  *
13*f0865ec9SKyle Evans  *  This software is licensed under a dual BSD and GPL v2 license.
14*f0865ec9SKyle Evans  *  See LICENSE file at the root folder of the project.
15*f0865ec9SKyle Evans  */
16*f0865ec9SKyle Evans #include <libecc/fp/fp.h>
17*f0865ec9SKyle Evans #include <libecc/fp/fp_add.h>
18*f0865ec9SKyle Evans #include <libecc/fp/fp_mul.h>
19*f0865ec9SKyle Evans #include <libecc/fp/fp_mul_redc1.h>
20*f0865ec9SKyle Evans #include <libecc/fp/fp_montgomery.h>
21*f0865ec9SKyle Evans 
22*f0865ec9SKyle Evans /* Compute out = in1 + in2 mod p in the Montgomery form.
23*f0865ec9SKyle Evans  * Inputs and outputs are in their Montgomery form.
24*f0865ec9SKyle Evans  * Returns 0 on success, -1 on error.
25*f0865ec9SKyle Evans  *
26*f0865ec9SKyle Evans  * Aliasing is supported.
27*f0865ec9SKyle Evans  */
fp_add_monty(fp_t out,fp_src_t in1,fp_src_t in2)28*f0865ec9SKyle Evans int fp_add_monty(fp_t out, fp_src_t in1, fp_src_t in2)
29*f0865ec9SKyle Evans {
30*f0865ec9SKyle Evans 	return fp_add(out, in1, in2);
31*f0865ec9SKyle Evans }
32*f0865ec9SKyle Evans 
33*f0865ec9SKyle Evans /* Compute out = in1 - in2 mod p in the Montgomery form.
34*f0865ec9SKyle Evans  * Inputs and outputs are in their Montgomery form.
35*f0865ec9SKyle Evans  * Returns 0 on success, -1 on error.
36*f0865ec9SKyle Evans  *
37*f0865ec9SKyle Evans  * Aliasing is supported.
38*f0865ec9SKyle Evans  */
fp_sub_monty(fp_t out,fp_src_t in1,fp_src_t in2)39*f0865ec9SKyle Evans int fp_sub_monty(fp_t out, fp_src_t in1, fp_src_t in2)
40*f0865ec9SKyle Evans {
41*f0865ec9SKyle Evans 	return fp_sub(out, in1, in2);
42*f0865ec9SKyle Evans }
43*f0865ec9SKyle Evans 
44*f0865ec9SKyle Evans /* Compute out = in1 * in2 mod p in the Montgomery form.
45*f0865ec9SKyle Evans  * Inputs and outputs are in their Montgomery form.
46*f0865ec9SKyle Evans  * Returns 0 on success, -1 on error.
47*f0865ec9SKyle Evans  *
48*f0865ec9SKyle Evans  * Aliasing is supported.
49*f0865ec9SKyle Evans  */
fp_mul_monty(fp_t out,fp_src_t in1,fp_src_t in2)50*f0865ec9SKyle Evans int fp_mul_monty(fp_t out, fp_src_t in1, fp_src_t in2)
51*f0865ec9SKyle Evans {
52*f0865ec9SKyle Evans 	return fp_mul_redc1(out, in1, in2);
53*f0865ec9SKyle Evans }
54*f0865ec9SKyle Evans 
55*f0865ec9SKyle Evans /* Compute out = in * in mod p in the Montgomery form.
56*f0865ec9SKyle Evans  * Inputs and outputs are in their Montgomery form.
57*f0865ec9SKyle Evans  * Returns 0 on success, -1 on error.
58*f0865ec9SKyle Evans  *
59*f0865ec9SKyle Evans  * Aliasing is supported.
60*f0865ec9SKyle Evans  */
fp_sqr_monty(fp_t out,fp_src_t in)61*f0865ec9SKyle Evans int fp_sqr_monty(fp_t out, fp_src_t in)
62*f0865ec9SKyle Evans {
63*f0865ec9SKyle Evans 	return fp_sqr_redc1(out, in);
64*f0865ec9SKyle Evans }
65*f0865ec9SKyle Evans 
66*f0865ec9SKyle Evans /*
67*f0865ec9SKyle Evans  * Compute out such that in1 = out * in2 mod p in the Montgomery form.
68*f0865ec9SKyle Evans  * Inputs and outputs are in their Montgomery form.
69*f0865ec9SKyle Evans  * Returns 0 on success, -1 on error. out must be initialized by the caller.
70*f0865ec9SKyle Evans  *
71*f0865ec9SKyle Evans  * Aliasing is supported.
72*f0865ec9SKyle Evans  */
fp_div_monty(fp_t out,fp_src_t in1,fp_src_t in2)73*f0865ec9SKyle Evans int fp_div_monty(fp_t out, fp_src_t in1, fp_src_t in2)
74*f0865ec9SKyle Evans {
75*f0865ec9SKyle Evans 	int ret, iszero;
76*f0865ec9SKyle Evans 
77*f0865ec9SKyle Evans 	ret = fp_check_initialized(in1); EG(ret, err);
78*f0865ec9SKyle Evans 	ret = fp_check_initialized(in2); EG(ret, err);
79*f0865ec9SKyle Evans 	ret = fp_check_initialized(out); EG(ret, err);
80*f0865ec9SKyle Evans 
81*f0865ec9SKyle Evans 	MUST_HAVE((out->ctx == in1->ctx), ret, err);
82*f0865ec9SKyle Evans 	MUST_HAVE((out->ctx == in2->ctx), ret, err);
83*f0865ec9SKyle Evans         FORCE_USED_VAR(iszero); /* silence warning when macro results in nothing */
84*f0865ec9SKyle Evans 	MUST_HAVE(!fp_iszero(in2, &iszero) && (!iszero), ret, err);
85*f0865ec9SKyle Evans 
86*f0865ec9SKyle Evans 	ret = fp_div(out, in1, in2); EG(ret, err);
87*f0865ec9SKyle Evans 	ret = fp_redcify(out, out);
88*f0865ec9SKyle Evans 
89*f0865ec9SKyle Evans err:
90*f0865ec9SKyle Evans 	return ret;
91*f0865ec9SKyle Evans }
92