This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathfp_add.c
130 lines (105 loc) · 3.86 KB
/
fp_add.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright (C) 2017 - This file is part of libecc project
*
* Authors:
* Ryad BENADJILA <ryadbenadjila@gmail.com>
* Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
* Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
*
* Contributors:
* Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
* Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
*
* This software is licensed under a dual BSD and GPL v2 license.
* See LICENSE file at the root folder of the project.
*/
#include "fp_add.h"
#include "../nn/nn_add.h"
/*
* Compute out = in1 + in2 mod p. 'out' parameter must have been initialized
* by the caller. Returns 0 on success, -1 on error.
*/
int fp_add(fp_t out, fp_src_t in1, fp_src_t in2)
{
int ret, cmp;
ret = fp_check_initialized(out); EG(ret, err);
ret = fp_check_initialized(in1); EG(ret, err);
ret = fp_check_initialized(in2); EG(ret, err);
MUST_HAVE(((&(in1->ctx->p)) == (&(in2->ctx->p))), ret, err);
MUST_HAVE(((&(in1->ctx->p)) == (&(out->ctx->p))), ret, err);
FORCE_USED_VAR(cmp); /* silence warning when macro results in nothing */
SHOULD_HAVE(!nn_cmp(&in1->fp_val, &(in1->ctx->p), &cmp) && (cmp < 0), ret, err);
SHOULD_HAVE(!nn_cmp(&in2->fp_val, &(in2->ctx->p), &cmp) && (cmp < 0), ret, err);
ret = nn_mod_add(&(out->fp_val), &(in1->fp_val),
&(in2->fp_val), &(in1->ctx->p));
err:
return ret;
}
/*
* Compute out = in + 1 mod p. 'out' parameter must have been initialized
* by the caller. Returns 0 on success, -1 on error.
*/
int fp_inc(fp_t out, fp_src_t in)
{
int ret, cmp;
ret = fp_check_initialized(in); EG(ret, err);
ret = fp_check_initialized(out); EG(ret, err);
MUST_HAVE(((&(in->ctx->p)) == (&(out->ctx->p))), ret, err);
FORCE_USED_VAR(cmp); /* silence warning when macro results in nothing */
SHOULD_HAVE(!nn_cmp(&in->fp_val, &(in->ctx->p), &cmp) && (cmp < 0), ret, err);
ret = nn_mod_inc(&(out->fp_val), &(in->fp_val), &(in->ctx->p));
err:
return ret;
}
/*
* Compute out = in1 - in2 mod p. 'out' parameter must have been initialized
* by the caller. Returns 0 on success, -1 on error.
*/
int fp_sub(fp_t out, fp_src_t in1, fp_src_t in2)
{
int ret, cmp;
ret = fp_check_initialized(out); EG(ret, err);
ret = fp_check_initialized(in1); EG(ret, err);
ret = fp_check_initialized(in2); EG(ret, err);
MUST_HAVE(((&(in1->ctx->p)) == (&(in2->ctx->p))), ret, err);
MUST_HAVE(((&(in1->ctx->p)) == (&(out->ctx->p))), ret, err);
FORCE_USED_VAR(cmp); /* silence warning when macro results in nothing */
SHOULD_HAVE(!nn_cmp(&in1->fp_val, &(in1->ctx->p), &cmp) && (cmp < 0), ret, err);
SHOULD_HAVE(!nn_cmp(&in2->fp_val, &(in2->ctx->p), &cmp) && (cmp < 0), ret, err);
ret = nn_mod_sub(&(out->fp_val), &(in1->fp_val),
&(in2->fp_val), &(in1->ctx->p));
err:
return ret;
}
/*
* Compute out = in - 1 mod p. 'out' parameter must have been initialized
* by the caller. Returns 0 on success, -1 on error.
*/
int fp_dec(fp_t out, fp_src_t in)
{
int ret, cmp;
ret = fp_check_initialized(out); EG(ret, err);
ret = fp_check_initialized(in); EG(ret, err);
MUST_HAVE(((&(in->ctx->p)) == (&(out->ctx->p))), ret, err);
FORCE_USED_VAR(cmp); /* silence warning when macro results in nothing */
SHOULD_HAVE(!nn_cmp(&in->fp_val, &(in->ctx->p), &cmp) && (cmp < 0), ret, err);
ret = nn_mod_dec(&(out->fp_val), &(in->fp_val), &(in->ctx->p));
err:
return ret;
}
/*
* Compute out = -in mod p = (p - in) mod p. 'out' parameter must have been
* initialized by the caller. Returns 0 on success, -1 on error.
*/
int fp_neg(fp_t out, fp_src_t in)
{
int ret, cmp;
ret = fp_check_initialized(in); EG(ret, err);
ret = fp_check_initialized(out); EG(ret, err);
MUST_HAVE(((&(in->ctx->p)) == (&(out->ctx->p))), ret, err);
FORCE_USED_VAR(cmp); /* silence warning when macro results in nothing */
SHOULD_HAVE(!nn_cmp(&in->fp_val, &(in->ctx->p), &cmp) && (cmp < 0), ret, err);
ret = nn_sub(&(out->fp_val), &(in->ctx->p), &(in->fp_val));
err:
return ret;
}