source: other-projects/trunk/gs3-release-maker/tasks/sshtaskdef/src/mindbright/util/Math.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 3.5 KB
Line 
1/******************************************************************************
2 *
3 * Copyright (c) 1998,99 by Mindbright Technology AB, Stockholm, Sweden.
4 * www.mindbright.se, [email protected]
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 *****************************************************************************
17 * $Author: mats $
18 * $Date: 2000/04/04 12:31:49 $
19 * $Name: rel1-2-1 $
20 *****************************************************************************/
21package mindbright.util;
22
23import java.math.BigInteger;
24
25import mindbright.security.SecureRandom;
26
27public final class Math {
28
29 public static BigInteger findRandomGenerator(BigInteger order,
30 BigInteger modulo,
31 SecureRandom random) {
32 BigInteger one = BigInteger.valueOf(1);
33 BigInteger aux = modulo.subtract(BigInteger.valueOf(1));
34 BigInteger t = aux.mod(order);
35 BigInteger generator;
36
37 if(t.longValue() != 0) {
38 return null;
39 }
40
41 t = aux.divide(order);
42
43 while(true) {
44 generator = new BigInteger(modulo.bitLength(), random);
45 generator = generator.mod(modulo);
46 generator = generator.modPow(t, modulo);
47 if(generator.compareTo(one) != 0)
48 break;
49 }
50
51 aux = generator.modPow(order, modulo);
52
53 if(aux.compareTo(one) != 0) {
54 return null;
55 }
56
57 return generator;
58 }
59
60 public static BigInteger[] findRandomStrongPrime(int primeBits,
61 int orderBits,
62 SecureRandom random) {
63 BigInteger one = BigInteger.valueOf(1);
64 BigInteger u, aux, aux2, t;
65 long[] table_q, table_u, prime_table;
66 PrimeSieve sieve = new PrimeSieve(16000);
67 int table_count = sieve.availablePrimes() - 1;
68 int i, j;
69 boolean flag;
70 BigInteger prime = null, order = null;
71
72 order = new BigInteger(orderBits, 20, random);
73
74 prime_table = new long[table_count];
75 table_q = new long[table_count];
76 table_u = new long[table_count];
77
78 i = 0;
79 for(int pN = 2; pN != 0; pN = sieve.getNextPrime(pN), i++) {
80 prime_table[i] = (long)pN;
81 }
82
83 for(i = 0; i < table_count; i++) {
84 table_q[i] =
85 (order.mod(BigInteger.valueOf(prime_table[i])).longValue() *
86 (long)2) % prime_table[i];
87 }
88
89 while(true) {
90 u = new BigInteger(primeBits, random);
91 u.setBit(primeBits - 1);
92 aux = order.shiftLeft(1);
93 aux2 = u.mod(aux);
94 u = u.subtract(aux2);
95 u = u.add(one);
96
97 if(u.bitLength() <= (primeBits - 1))
98 continue;
99
100 for(j = 0; j < table_count; j++) {
101 table_u[j] =
102 u.mod(BigInteger.valueOf(prime_table[j])).longValue();
103 }
104
105 aux2 = order.shiftLeft(1);
106
107 for(i = 0; i < (1 << 24); i++) {
108 long cur_p;
109 long value;
110
111 flag = true;
112 for(j = 1; j < table_count; j++) {
113 cur_p = prime_table[j];
114 value = table_u[j];
115 if(value >= cur_p)
116 value -= cur_p;
117 if(value == 0)
118 flag = false;
119 table_u[j] = value + table_q[j];
120 }
121 if(!flag)
122 continue;
123
124 aux = aux2.multiply(BigInteger.valueOf(i));
125 prime = u.add(aux);
126
127 if(prime.bitLength() > primeBits)
128 continue;
129
130 if(prime.isProbablePrime(20))
131 break;
132 }
133
134 if(i < (1 << 24))
135 break;
136 }
137
138 return new BigInteger[] { prime, order };
139 }
140
141}
142
Note: See TracBrowser for help on using the repository browser.