aboutsummaryrefslogtreecommitdiff
path: root/lib/libmpa/mpa_modulus.c
blob: 55c8776556d87c4509bb80cd30723bc595c1312b (plain)
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
// SPDX-License-Identifier: BSD-2-Clause
/*
 * Copyright (c) 2014, STMicroelectronics International N.V.
 */
#include "mpa.h"

/*************************************************************
 *
 *   LIB FUNCTIONS
 *
 *************************************************************/

/*------------------------------------------------------------
 *
 *  mpa_mod
 *
 */
void mpa_mod(mpanum dest, const mpanum op, const mpanum n, mpa_scratch_mem pool)
{
	mpa_div(NULL, dest, op, n, pool);
}

/*------------------------------------------------------------
 *
 *  mpa_add_mod
 *
 */
void mpa_add_mod(mpanum dest,
		const mpanum op1,
		const mpanum op2, const mpanum n, mpa_scratch_mem pool)
{
	mpanum tmp_dest;

	mpa_alloc_static_temp_var(&tmp_dest, pool);

	mpa_add(tmp_dest, op1, op2, pool);
	mpa_div(NULL, dest, tmp_dest, n, pool);

	mpa_free_static_temp_var(&tmp_dest, pool);
}

/*------------------------------------------------------------
 *
 *  mpa_sub_mod
 *
 */
void mpa_sub_mod(mpanum dest,
		const mpanum op1,
		const mpanum op2, const mpanum n, mpa_scratch_mem pool)
{
	mpanum tmp_dest;

	mpa_alloc_static_temp_var(&tmp_dest, pool);

	mpa_sub(tmp_dest, op1, op2, pool);
	mpa_div(NULL, dest, tmp_dest, n, pool);

	mpa_free_static_temp_var(&tmp_dest, pool);
}

/*------------------------------------------------------------
 *
 *  mpa_mul_mod
 *
 */
void mpa_mul_mod(mpanum dest,
		const mpanum op1,
		const mpanum op2, const mpanum n, mpa_scratch_mem pool)
{
	mpanum tmp_dest;

	mpa_alloc_static_temp_var(&tmp_dest, pool);

	mpa_mul(tmp_dest, op1, op2, pool);
	mpa_div(NULL, dest, tmp_dest, n, pool);

	mpa_free_static_temp_var(&tmp_dest, pool);
}

/*------------------------------------------------------------
 *
 *  mpa_inv_mod
 *
 */
int mpa_inv_mod(mpanum dest,
	       const mpanum op, const mpanum n, mpa_scratch_mem pool)
{
	mpanum gcd;
	mpanum tmp_dest;
	int mem_marker;
	int res;

	if (mpa_cmp_short(op, 1) == 0) {
		mpa_set_S32(dest, 1);
		return 0;
	}

	mem_marker = (dest == op);
	if (mem_marker)
		mpa_alloc_static_temp_var(&tmp_dest, pool);
	else
		tmp_dest = dest;

	mpa_alloc_static_temp_var(&gcd, pool);
	/* The function mpa_extended_gcd behaves badly if tmp_dest = op */
	mpa_extended_gcd(gcd, tmp_dest, NULL, op, n, pool);
	res = mpa_cmp_short(gcd, 1);

	if (mem_marker) {
		mpa_copy(dest, tmp_dest);
		mpa_free_static_temp_var(&tmp_dest, pool);
	}

	mpa_free_static_temp_var(&gcd, pool);
	if (res == 0) {
		while (mpa_cmp_short(dest, 0) < 0)
			mpa_add(dest, dest, n, pool);
		return 0;
	} else {
		return -1;
	}
}