summary refs log tree commit diff
path: root/crypto/src/math/ec/IntArray.cs
blob: 1089966a870b4159b5feb6103c396102315919d9 (plain) (blame)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
using System;
using System.Text;

namespace Org.BouncyCastle.Math.EC
{
	internal class IntArray
    {
        // TODO make m fixed for the IntArray, and hence compute T once and for all

		// TODO Use uint's internally?
		private int[] m_ints;

		public IntArray(int intLen)
		{
			m_ints = new int[intLen];
		}

		private IntArray(int[] ints)
		{
			m_ints = ints;
		}

		public IntArray(BigInteger bigInt)
			: this(bigInt, 0)
		{
		}

		public IntArray(BigInteger bigInt, int minIntLen)
		{
			if (bigInt.SignValue == -1)
				throw new ArgumentException("Only positive Integers allowed", "bigint");

			if (bigInt.SignValue == 0)
			{
				m_ints = new int[] { 0 };
				return;
			}

			byte[] barr = bigInt.ToByteArrayUnsigned();
			int barrLen = barr.Length;

			int intLen = (barrLen + 3) / 4;
			m_ints = new int[System.Math.Max(intLen, minIntLen)];

			int rem = barrLen % 4;
			int barrI = 0;

			if (0 < rem)
			{
				int temp = (int) barr[barrI++];
				while (barrI < rem)
				{
					temp = temp << 8 | (int) barr[barrI++];
				}
				m_ints[--intLen] = temp;
			}

			while (intLen > 0)
			{
				int temp = (int) barr[barrI++];
				for (int i = 1; i < 4; i++)
				{
					temp = temp << 8 | (int) barr[barrI++];
				}
				m_ints[--intLen] = temp;
			}
		}

		public int GetUsedLength()
		{
			int highestIntPos = m_ints.Length;

			if (highestIntPos < 1)
				return 0;

			// Check if first element will act as sentinel
			if (m_ints[0] != 0)
			{
				while (m_ints[--highestIntPos] == 0)
				{
				}
				return highestIntPos + 1;
			}

			do
			{
				if (m_ints[--highestIntPos] != 0)
				{
					return highestIntPos + 1;
				}
			}
			while (highestIntPos > 0);

			return 0;
		}

		public int BitLength
		{
			get
			{
				// JDK 1.5: see Integer.numberOfLeadingZeros()
				int intLen = GetUsedLength();
				if (intLen == 0)
					return 0;

				int last = intLen - 1;
				uint highest = (uint) m_ints[last];
				int bits = (last << 5) + 1;

				// A couple of binary search steps
				if (highest > 0x0000ffff)
				{
					if (highest > 0x00ffffff)
					{
						bits += 24;
						highest >>= 24;
					}
					else
					{
						bits += 16;
						highest >>= 16;
					}
				}
				else if (highest > 0x000000ff)
				{
					bits += 8;
					highest >>= 8;
				}

				while (highest > 1)
				{
					++bits;
					highest >>= 1;
				}

				return bits;
			}
		}

		private int[] resizedInts(int newLen)
		{
			int[] newInts = new int[newLen];
			int oldLen = m_ints.Length;
			int copyLen = oldLen < newLen ? oldLen : newLen;
			Array.Copy(m_ints, 0, newInts, 0, copyLen);
			return newInts;
		}

		public BigInteger ToBigInteger()
		{
			int usedLen = GetUsedLength();
			if (usedLen == 0)
			{
				return BigInteger.Zero;
			}

			int highestInt = m_ints[usedLen - 1];
			byte[] temp = new byte[4];
			int barrI = 0;
			bool trailingZeroBytesDone = false;
			for (int j = 3; j >= 0; j--)
			{
				byte thisByte = (byte)((int)((uint) highestInt >> (8 * j)));
				if (trailingZeroBytesDone || (thisByte != 0))
				{
					trailingZeroBytesDone = true;
					temp[barrI++] = thisByte;
				}
			}

			int barrLen = 4 * (usedLen - 1) + barrI;
			byte[] barr = new byte[barrLen];
			for (int j = 0; j < barrI; j++)
			{
				barr[j] = temp[j];
			}
			// Highest value int is done now

			for (int iarrJ = usedLen - 2; iarrJ >= 0; iarrJ--)
			{
				for (int j = 3; j >= 0; j--)
				{
					barr[barrI++] = (byte)((int)((uint)m_ints[iarrJ] >> (8 * j)));
				}
			}
			return new BigInteger(1, barr);
		}

		public void ShiftLeft()
		{
			int usedLen = GetUsedLength();
			if (usedLen == 0)
			{
				return;
			}
			if (m_ints[usedLen - 1] < 0)
			{
				// highest bit of highest used byte is set, so shifting left will
				// make the IntArray one byte longer
				usedLen++;
				if (usedLen > m_ints.Length)
				{
					// make the m_ints one byte longer, because we need one more
					// byte which is not available in m_ints
					m_ints = resizedInts(m_ints.Length + 1);
				}
			}

			bool carry = false;
			for (int i = 0; i < usedLen; i++)
			{
				// nextCarry is true if highest bit is set
				bool nextCarry = m_ints[i] < 0;
				m_ints[i] <<= 1;
				if (carry)
				{
					// set lowest bit
					m_ints[i] |= 1;
				}
				carry = nextCarry;
			}
		}

		public IntArray ShiftLeft(int n)
		{
			int usedLen = GetUsedLength();
			if (usedLen == 0)
			{
				return this;
			}

			if (n == 0)
			{
				return this;
			}

			if (n > 31)
			{
				throw new ArgumentException("shiftLeft() for max 31 bits "
					+ ", " + n + "bit shift is not possible", "n");
			}

			int[] newInts = new int[usedLen + 1];

			int nm32 = 32 - n;
			newInts[0] = m_ints[0] << n;
			for (int i = 1; i < usedLen; i++)
			{
				newInts[i] = (m_ints[i] << n) | (int)((uint)m_ints[i - 1] >> nm32);
			}
			newInts[usedLen] = (int)((uint)m_ints[usedLen - 1] >> nm32);

			return new IntArray(newInts);
		}

		public void AddShifted(IntArray other, int shift)
		{
			int usedLenOther = other.GetUsedLength();
			int newMinUsedLen = usedLenOther + shift;
			if (newMinUsedLen > m_ints.Length)
			{
				m_ints = resizedInts(newMinUsedLen);
				//Console.WriteLine("Resize required");
			}

			for (int i = 0; i < usedLenOther; i++)
			{
				m_ints[i + shift] ^= other.m_ints[i];
			}
		}

		public int Length
		{
			get { return m_ints.Length; }
		}

		public bool TestBit(int n)
		{
			// theInt = n / 32
			int theInt = n >> 5;
			// theBit = n % 32
			int theBit = n & 0x1F;
			int tester = 1 << theBit;
			return ((m_ints[theInt] & tester) != 0);
		}

		public void FlipBit(int n)
		{
			// theInt = n / 32
			int theInt = n >> 5;
			// theBit = n % 32
			int theBit = n & 0x1F;
			int flipper = 1 << theBit;
			m_ints[theInt] ^= flipper;
		}

		public void SetBit(int n)
		{
			// theInt = n / 32
			int theInt = n >> 5;
			// theBit = n % 32
			int theBit = n & 0x1F;
			int setter = 1 << theBit;
			m_ints[theInt] |= setter;
		}

		public IntArray Multiply(IntArray other, int m)
		{
			// Lenght of c is 2m bits rounded up to the next int (32 bit)
			int t = (m + 31) >> 5;
			if (m_ints.Length < t)
			{
				m_ints = resizedInts(t);
			}

			IntArray b = new IntArray(other.resizedInts(other.Length + 1));
			IntArray c = new IntArray((m + m + 31) >> 5);
			// IntArray c = new IntArray(t + t);
			int testBit = 1;
			for (int k = 0; k < 32; k++)
			{
				for (int j = 0; j < t; j++)
				{
					if ((m_ints[j] & testBit) != 0)
					{
						// The kth bit of m_ints[j] is set
						c.AddShifted(b, j);
					}
				}
				testBit <<= 1;
				b.ShiftLeft();
			}
			return c;
		}

		// public IntArray multiplyLeftToRight(IntArray other, int m) {
		// // Lenght of c is 2m bits rounded up to the next int (32 bit)
		// int t = (m + 31) / 32;
		// if (m_ints.Length < t) {
		// m_ints = resizedInts(t);
		// }
		//
		// IntArray b = new IntArray(other.resizedInts(other.getLength() + 1));
		// IntArray c = new IntArray((m + m + 31) / 32);
		// // IntArray c = new IntArray(t + t);
		// int testBit = 1 << 31;
		// for (int k = 31; k >= 0; k--) {
		// for (int j = 0; j < t; j++) {
		// if ((m_ints[j] & testBit) != 0) {
		// // The kth bit of m_ints[j] is set
		// c.addShifted(b, j);
		// }
		// }
		// testBit >>>= 1;
		// if (k > 0) {
		// c.shiftLeft();
		// }
		// }
		// return c;
		// }

		// TODO note, redPol.Length must be 3 for TPB and 5 for PPB
		public void Reduce(int m, int[] redPol)
		{
			for (int i = m + m - 2; i >= m; i--)
			{
				if (TestBit(i))
				{
					int bit = i - m;
					FlipBit(bit);
					FlipBit(i);
					int l = redPol.Length;
					while (--l >= 0)
					{
						FlipBit(redPol[l] + bit);
					}
				}
			}
			m_ints = resizedInts((m + 31) >> 5);
		}

		public IntArray Square(int m)
		{
			// TODO make the table static readonly
			int[] table = { 0x0, 0x1, 0x4, 0x5, 0x10, 0x11, 0x14, 0x15, 0x40,
									0x41, 0x44, 0x45, 0x50, 0x51, 0x54, 0x55 };

			int t = (m + 31) >> 5;
			if (m_ints.Length < t)
			{
				m_ints = resizedInts(t);
			}

			IntArray c = new IntArray(t + t);

			// TODO twice the same code, put in separate private method
			for (int i = 0; i < t; i++)
			{
				int v0 = 0;
				for (int j = 0; j < 4; j++)
				{
					v0 = (int)((uint) v0 >> 8);
					int u = (int)((uint)m_ints[i] >> (j * 4)) & 0xF;
					int w = table[u] << 24;
					v0 |= w;
				}
				c.m_ints[i + i] = v0;

				v0 = 0;
				int upper = (int)((uint) m_ints[i] >> 16);
				for (int j = 0; j < 4; j++)
				{
					v0 = (int)((uint) v0 >> 8);
					int u = (int)((uint)upper >> (j * 4)) & 0xF;
					int w = table[u] << 24;
					v0 |= w;
				}
				c.m_ints[i + i + 1] = v0;
			}
			return c;
		}

		public override bool Equals(object o)
		{
			if (!(o is IntArray))
			{
				return false;
			}
			IntArray other = (IntArray) o;
			int usedLen = GetUsedLength();
			if (other.GetUsedLength() != usedLen)
			{
				return false;
			}
			for (int i = 0; i < usedLen; i++)
			{
				if (m_ints[i] != other.m_ints[i])
				{
					return false;
				}
			}
			return true;
		}

		public override int GetHashCode()
		{
			int i = GetUsedLength();
			int hc = i;
			while (--i >= 0)
			{
				hc *= 17;
				hc ^= m_ints[i];
			}
			return hc;
		}

		internal IntArray Copy()
		{
			return new IntArray((int[]) m_ints.Clone());
		}

		public override string ToString()
		{
			int usedLen = GetUsedLength();
			if (usedLen == 0)
			{
				return "0";
			}

			StringBuilder sb = new StringBuilder(Convert.ToString(m_ints[usedLen - 1], 2));
			for (int iarrJ = usedLen - 2; iarrJ >= 0; iarrJ--)
			{
				string hexString = Convert.ToString(m_ints[iarrJ], 2);

				// Add leading zeroes, except for highest significant int
				for (int i = hexString.Length; i < 8; i++)
				{
					hexString = "0" + hexString;
				}
				sb.Append(hexString);
			}
			return sb.ToString();
		}
	}
}