forked from ANSSI-FR/libecc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fp_add.c
75 lines (70 loc) · 2.3 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
/*
* Copyright (C) 2017 - This file is part of libecc project
*
* Authors:
* Ryad BENADJILA <[email protected]>
* Arnaud EBALARD <[email protected]>
* Jean-Pierre FLORI <[email protected]>
*
* Contributors:
* Nicolas VIVET <[email protected]>
* Karim KHALFALLAH <[email protected]>
*
* 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 */
void fp_add(fp_t out, fp_src_t in1, fp_src_t in2)
{
fp_check_initialized(out);
fp_check_initialized(in1);
fp_check_initialized(in2);
MUST_HAVE((&(in1->ctx->p)) == (&(in2->ctx->p)));
MUST_HAVE((&(in1->ctx->p)) == (&(out->ctx->p)));
SHOULD_HAVE(nn_cmp(&in1->fp_val, &(in1->ctx->p)) < 0);
SHOULD_HAVE(nn_cmp(&in2->fp_val, &(in2->ctx->p)) < 0);
nn_mod_add(&(out->fp_val), &(in1->fp_val),
&(in2->fp_val), &(in1->ctx->p));
}
/* Compute out = in + 1 mod p */
void fp_inc(fp_t out, fp_src_t in)
{
fp_check_initialized(in);
fp_check_initialized(out);
MUST_HAVE((&(in->ctx->p)) == (&(out->ctx->p)));
SHOULD_HAVE(nn_cmp(&in->fp_val, &(in->ctx->p)) < 0);
nn_mod_inc(&(out->fp_val), &(in->fp_val), &(in->ctx->p));
}
/* Compute out = in1 - in2 mod p */
void fp_sub(fp_t out, fp_src_t in1, fp_src_t in2)
{
fp_check_initialized(out);
fp_check_initialized(in1);
fp_check_initialized(in2);
MUST_HAVE((&(in1->ctx->p)) == (&(in2->ctx->p)));
MUST_HAVE((&(in1->ctx->p)) == (&(out->ctx->p)));
SHOULD_HAVE(nn_cmp(&in1->fp_val, &(in1->ctx->p)) < 0);
SHOULD_HAVE(nn_cmp(&in2->fp_val, &(in2->ctx->p)) < 0);
nn_mod_sub(&(out->fp_val), &(in1->fp_val),
&(in2->fp_val), &(in1->ctx->p));
}
/* Compute out = in - 1 mod p */
void fp_dec(fp_t out, fp_src_t in)
{
fp_check_initialized(out);
fp_check_initialized(in);
MUST_HAVE((&(in->ctx->p)) == (&(out->ctx->p)));
SHOULD_HAVE(nn_cmp(&in->fp_val, &(in->ctx->p)) < 0);
nn_mod_dec(&(out->fp_val), &(in->fp_val), &(in->ctx->p));
}
/* Compute out = -in mod p = (p - in) mod p */
void fp_neg(fp_t out, fp_src_t in)
{
fp_check_initialized(in);
fp_check_initialized(out);
MUST_HAVE((&(in->ctx->p)) == (&(out->ctx->p)));
SHOULD_HAVE(nn_cmp(&in->fp_val, &(in->ctx->p)) < 0);
nn_sub(&(out->fp_val), &(in->ctx->p), &(in->fp_val));
}