Coverage Report

Created: 2024-06-28 06:19

/src/libecc/src/fp/fp_mul_redc1.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *  Copyright (C) 2017 - This file is part of libecc project
3
 *
4
 *  Authors:
5
 *      Ryad BENADJILA <ryadbenadjila@gmail.com>
6
 *      Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
7
 *      Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
8
 *
9
 *  Contributors:
10
 *      Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
11
 *      Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
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_mul_redc1.h>
17
18
/*
19
 * Internal helper performing Montgomery multiplication. The function returns
20
 * 0 on success, -1 on error.
21
 *
22
 * CAUTION: the function does not check input parameters. Those checks MUST be
23
 * performed by the caller.
24
 */
25
ATTRIBUTE_WARN_UNUSED_RET static inline int _fp_mul_redc1(nn_t out, nn_src_t in1, nn_src_t in2,
26
         fp_ctx_src_t ctx)
27
68.4M
{
28
68.4M
  return nn_mul_redc1(out, in1, in2, &(ctx->p), ctx->mpinv);
29
68.4M
}
30
31
/*
32
 * Compute out = in1 * in2 mod (p) in redcified form.
33
 *
34
 * Exported version based on previous one, that sanity checks input parameters.
35
 * The function returns 0 on success, -1 on error.
36
 *
37
 * Aliasing is supported.
38
 */
39
int fp_mul_redc1(fp_t out, fp_src_t in1, fp_src_t in2)
40
68.4M
{
41
68.4M
  int ret;
42
43
68.4M
  ret = fp_check_initialized(in1); EG(ret, err);
44
68.4M
  ret = fp_check_initialized(in2); EG(ret, err);
45
68.4M
  ret = fp_check_initialized(out); EG(ret, err);
46
47
68.4M
  MUST_HAVE((out->ctx == in1->ctx), ret, err);
48
68.4M
  MUST_HAVE((out->ctx == in2->ctx), ret, err);
49
50
68.4M
  ret = _fp_mul_redc1(&(out->fp_val), &(in1->fp_val), &(in2->fp_val),
51
68.4M
          out->ctx);
52
53
68.4M
err:
54
68.4M
  return ret;
55
68.4M
}
56
57
/*
58
 * Compute out = in * in mod (p) in redcified form.
59
 *
60
 * Aliasing is supported.
61
 */
62
int fp_sqr_redc1(fp_t out, fp_src_t in)
63
0
{
64
0
  return fp_mul_redc1(out, in, in);
65
0
}
66
67
/*
68
 * Compute out = redcified form of in.
69
 * redcify could be done by shifting and division by p. The function returns 0
70
 * on success, -1 on error.
71
 *
72
 * Aliasing is supported.
73
 */
74
int fp_redcify(fp_t out, fp_src_t in)
75
15.6k
{
76
15.6k
  int ret;
77
78
15.6k
  ret = fp_check_initialized(in); EG(ret, err);
79
15.6k
  ret = fp_check_initialized(out); EG(ret, err);
80
81
15.6k
  MUST_HAVE((out->ctx == in->ctx), ret, err);
82
83
15.6k
  ret = _fp_mul_redc1(&(out->fp_val), &(in->fp_val), &(out->ctx->r_square),
84
15.6k
          out->ctx);
85
86
15.6k
err:
87
15.6k
  return ret;
88
15.6k
}
89
90
/*
91
 * Compute out = unredcified form of in.
92
 * The function returns 0 on success, -1 on error.
93
 *
94
 * Aliasing is supported.
95
 */
96
int fp_unredcify(fp_t out, fp_src_t in)
97
0
{
98
0
  int ret;
99
0
  nn one;
100
0
  one.magic = WORD(0);
101
102
0
  ret = fp_check_initialized(in); EG(ret, err);
103
0
  ret = fp_check_initialized(out); EG(ret, err);
104
0
  ret = nn_init(&one, 0);  EG(ret, err);
105
0
  ret = nn_one(&one); EG(ret, err);
106
0
  ret = _fp_mul_redc1(&(out->fp_val), &(in->fp_val), &one, out->ctx);
107
108
0
err:
109
0
  nn_uninit(&one);
110
111
0
  return ret;
112
0
}