-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulmod.c
185 lines (141 loc) · 3.47 KB
/
mulmod.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//
// mulmod.c - perform 64x64 multiply mod 64
//
// $RCSfile: mulmod.c,v $
//
// $Date: 2013/12/02 19:16:57 $
//
// $Author: chuck $
//
// $Id: mulmod.c,v 1.3 2013/12/02 19:16:57 chuck Exp $
//
//
// ChangeLog:
//
// $Log: mulmod.c,v $
// Revision 1.3 2013/12/02 19:16:57 chuck
// added rcs_file_version identification string
//
// Revision 1.2 2013/11/29 21:36:54 chuck
// improved performance
// added pthread support
//
// Revision 1.1 2013/11/21 04:23:25 chuck
// Initial revision
//
//
static char *rcs_file_verson = "$Id: mulmod.c,v 1.3 2013/12/02 19:16:57 chuck Exp $";
//
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
//
// local struct/union to facilite multiply
//
typedef struct {
uint32_t lo;
uint32_t hi;
} mont;
typedef union {
mont hilo;
uint64_t whole;
} montgomery;
// mulmod32 relies on the underlying 64 bit os and simply does a * %. the overhead
// of the function call cannot be avoided at this time since the selection of routines
// is made based on Pmax.
uint64_t mulmod32 (uint64_t a, uint64_t b, uint64_t mod)
{
return (a * b) % mod;
}
uint64_t mulmod64 (uint64_t a, uint64_t b, uint64_t mod)
{
register uint64_t ah, al, bh, bl, ch, cl, chl, clh;
uint64_t mask = 0xFFFFFFFF;
a %= mod;
b %= mod;
#define WANT_ORIGINAL
#ifdef WANT_ORIGINAL
ah = a >>32;
bh = b >>32;
al = a & mask;
bl = b & mask;
cl = (al * bl) % mod;
ch = (ah * bh) % mod;
ch = (ch << 32) % mod;
chl = (ah * bl) % mod;
ch += chl;
ch %= mod;
clh = (al * bh) % mod;
ch += clh;
ch %= mod;
ch = (ch << 32) % mod;
ch += cl;
ch %= mod;
return ch;
#else // simple folded lines together to help the compiler
a.whole = A;
b.whole = B;
c.whole = (a.hilo.lo * b.hilo.lo) & mod;
t1.whole = (a.hilo.hi & b*.hilo.hi) & mod;
#endif
}
#ifdef MAYBE_FUTURE
uint64_t mulmod64 (uint64_t A, uint64_t B, uint64_t MOD)
{
// register montgomery a,b,c,t1,t2;
register uint64_t a = A % MOD;
register uint64_t b = B % MOD;
register uint64_t mod = MOD;
// load A and B from the stack
// a.whole = A;
// b.whole = B;
//
// c.hilo.low = (a.hilo.lo * b.hilo.lo) % mod;
// t1.whole = (a.hili.hi * b.hilo.hi) % mod;
// t1.whole = (t1.whole << 32)
register uint64_t ah, al, bh, bl, ch, cl, chl, clh;
// uint64_t ah, al, bh, bl, ch, cl, chl, clh;
uint64_t mask = 0xFFFFFFFF;
// done at initialization :)
// a %= mod;
// b %= mod;
ah = a >>32;
bh = b >>32;
al = a & mask;
bl = b & mask;
// 1 - cl = (al * bl) % mod;
// ch = (ah * bh) % mod;
// ch = (ch << 32) % mod;
// folded together
ch = (((ah * bh) % mod) << 32) % mod;
ch += (ah * bl) % mod;
ch %= mod;
ch += (al * bh) % mod;
ch %= mod;
ch = ((ch << 32) % mod) + ((al * bl) % mod); // 1 - +=cl;
ch %= mod;
return ch;
}
#endif // MAYBE_FUTURE (mixed 32-64 bit register ops via structures/unions)
#ifdef UNIT_TEST
main()
{
uint64_t a,b,c;
uint64_t C;
uint64_t P;
// exhaustively regression test for 64 bits on a, b, and P (yes it will take a while)
for (a = 1; a< 0x7FFFFFFFFFFFFFFF; a+=83) {
printf(".") ; fflush(stdout);
for (b = 1; b< 0x7FFFFFFFFFFFFFFF; b+=90) {
for (P = 1; P< 0x7FFFFFFFFFFFFFFF; P+=51) {
c = mulmod(a,b,P);
C = mulmod64(a,b,P);
if (C != c) {
printf("ERROR: %ul != %ul @ (%ul, %ul, %ul (%x))\n",c,C,a,b,P);
exit(1);
}
}
}
}
}
#endif // UNIT_TEST