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