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
185
186
187
188
189
190
191
192
193
194
195
|
using System;
using System.Diagnostics;
using Org.BouncyCastle.Crypto.Utilities;
using Org.BouncyCastle.Math.Raw;
using Org.BouncyCastle.Security;
namespace Org.BouncyCastle.Math.EC.Custom.Sec
{
internal class SecP521R1Field
{
// 2^521 - 1
internal static readonly uint[] P = new uint[]{ 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
0xFFFFFFFF, 0xFFFFFFFF, 0x1FF };
private const uint P16 = 0x1FFU;
public static void Add(uint[] x, uint[] y, uint[] z)
{
uint c = Nat.Add(16, x, y, z) + x[16] + y[16];
if (c > P16 || (c == P16 && Nat.Eq(16, z, P)))
{
c += Nat.Inc(16, z);
c &= P16;
}
z[16] = c;
}
public static void AddOne(uint[] x, uint[] z)
{
uint c = Nat.Inc(16, x, z) + x[16];
if (c > P16 || (c == P16 && Nat.Eq(16, z, P)))
{
c += Nat.Inc(16, z);
c &= P16;
}
z[16] = c;
}
public static uint[] FromBigInteger(BigInteger x)
{
uint[] z = Nat.FromBigInteger(521, x);
if (Nat.Eq(17, z, P))
{
Nat.Zero(17, z);
}
return z;
}
public static void Half(uint[] x, uint[] z)
{
uint x16 = x[16];
uint c = Nat.ShiftDownBit(16, x, x16, z);
z[16] = (x16 >> 1) | (c >> 23);
}
public static void Inv(uint[] x, uint[] z)
{
Mod.CheckedModOddInverse(P, x, z);
}
public static int IsZero(uint[] x)
{
uint d = 0;
for (int i = 0; i < 17; ++i)
{
d |= x[i];
}
d = (d >> 1) | (d & 1);
return ((int)d - 1) >> 31;
}
public static void Multiply(uint[] x, uint[] y, uint[] z)
{
uint[] tt = Nat.Create(33);
ImplMultiply(x, y, tt);
Reduce(tt, z);
}
public static void Negate(uint[] x, uint[] z)
{
if (0 != IsZero(x))
{
Nat.Sub(17, P, P, z);
}
else
{
Nat.Sub(17, P, x, z);
}
}
public static void Random(SecureRandom r, uint[] z)
{
byte[] bb = new byte[17 * 4];
do
{
r.NextBytes(bb);
Pack.LE_To_UInt32(bb, 0, z, 0, 17);
z[16] &= P16;
}
while (0 == Nat.LessThan(17, z, P));
}
public static void RandomMult(SecureRandom r, uint[] z)
{
do
{
Random(r, z);
}
while (0 != IsZero(z));
}
public static void Reduce(uint[] xx, uint[] z)
{
Debug.Assert(xx[32] >> 18 == 0);
uint xx32 = xx[32];
uint c = Nat.ShiftDownBits(16, xx, 16, 9, xx32, z, 0) >> 23;
c += xx32 >> 9;
c += Nat.AddTo(16, xx, z);
if (c > P16 || (c == P16 && Nat.Eq(16, z, P)))
{
c += Nat.Inc(16, z);
c &= P16;
}
z[16] = c;
}
public static void Reduce23(uint[] z)
{
uint z16 = z[16];
uint c = Nat.AddWordTo(16, z16 >> 9, z) + (z16 & P16);
if (c > P16 || (c == P16 && Nat.Eq(16, z, P)))
{
c += Nat.Inc(16, z);
c &= P16;
}
z[16] = c;
}
public static void Square(uint[] x, uint[] z)
{
uint[] tt = Nat.Create(33);
ImplSquare(x, tt);
Reduce(tt, z);
}
public static void SquareN(uint[] x, int n, uint[] z)
{
Debug.Assert(n > 0);
uint[] tt = Nat.Create(33);
ImplSquare(x, tt);
Reduce(tt, z);
while (--n > 0)
{
ImplSquare(z, tt);
Reduce(tt, z);
}
}
public static void Subtract(uint[] x, uint[] y, uint[] z)
{
int c = Nat.Sub(16, x, y, z) + (int)(x[16] - y[16]);
if (c < 0)
{
c += Nat.Dec(16, z);
c &= (int)P16;
}
z[16] = (uint)c;
}
public static void Twice(uint[] x, uint[] z)
{
uint x16 = x[16];
uint c = Nat.ShiftUpBit(16, x, x16 << 23, z) | (x16 << 1);
z[16] = c & P16;
}
protected static void ImplMultiply(uint[] x, uint[] y, uint[] zz)
{
Nat512.Mul(x, y, zz);
uint x16 = x[16], y16 = y[16];
zz[32] = Nat.Mul31BothAdd(16, x16, y, y16, x, zz, 16) + (x16 * y16);
}
protected static void ImplSquare(uint[] x, uint[] zz)
{
Nat512.Square(x, zz);
uint x16 = x[16];
zz[32] = Nat.MulWordAddTo(16, x16 << 1, x, 0, zz, 16) + (x16 * x16);
}
}
}
|