forked from ANSSI-FR/libecc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fp_mul_redc1.c
71 lines (59 loc) · 1.57 KB
/
fp_mul_redc1.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
/*
* 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_mul_redc1.h"
/*
* Perform Montgomery multiplication.
*/
static inline void _fp_mul_redc1(nn_t out, nn_src_t in1, nn_src_t in2,
fp_ctx_src_t ctx)
{
nn_mul_redc1(out, in1, in2, &(ctx->p), ctx->mpinv);
}
void fp_mul_redc1(fp_t out, fp_src_t in1, fp_src_t in2)
{
fp_check_initialized(in1);
fp_check_initialized(in2);
fp_check_initialized(out);
MUST_HAVE(out->ctx == in1->ctx);
MUST_HAVE(out->ctx == in2->ctx);
_fp_mul_redc1(&(out->fp_val), &(in1->fp_val), &(in2->fp_val),
out->ctx);
}
void fp_sqr_redc1(fp_t out, fp_src_t in)
{
fp_mul_redc1(out, in, in);
}
/*
* redcify could be done by shifting and division by p.
*/
void fp_redcify(fp_t out, fp_src_t in)
{
fp_check_initialized(in);
fp_check_initialized(out);
MUST_HAVE(out->ctx == in->ctx);
_fp_mul_redc1(&(out->fp_val), &(in->fp_val), &(out->ctx->r_square),
out->ctx);
}
void fp_unredcify(fp_t out, fp_src_t in)
{
nn one;
fp_check_initialized(in);
fp_check_initialized(out);
nn_init(&one, 0);
nn_one(&one);
_fp_mul_redc1(&(out->fp_val), &(in->fp_val), &one, out->ctx);
nn_uninit(&one);
}