Skip to content

Commit a7ccd2b

Browse files
committed
GF inv計算など
1 parent 8160be2 commit a7ccd2b

File tree

4 files changed

+86
-44
lines changed

4 files changed

+86
-44
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ JSONまではほどほどに使えますが、他は実験感覚で作ってい
2828
<dependency>
2929
<groupId>net.siisise</groupId>
3030
<artifactId>softlib</artifactId>
31-
<version>1.1.11</version>
31+
<version>1.1.12</version>
3232
<type>jar</type>
3333
</dependency>
3434
~~~
3535
時々変わることがあるので特定バージョンを指定するか、SoftLibJSONなど使用したい機能経由で指定するのがおすすめです。
3636

37-
リリース版 1.1.11 ぐらい。
38-
次版 1.1.12-SNAPSHOT
37+
リリース版 1.1.12 ぐらい。
38+
次版 1.1.13-SNAPSHOT
3939

4040
~~~
4141
<version>[1.1.8,)</version>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>net.siisise</groupId>
55
<artifactId>softlib</artifactId>
6-
<version>1.1.12-SNAPSHOT</version>
6+
<version>1.1.12</version>
77
<packaging>jar</packaging>
88
<name>SoftLib</name>
99
<description>Java simple API Packet io, base64, etc...</description>

src/main/java/net/siisise/math/GF.java

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.siisise.math;
22

3+
import java.math.BigInteger;
34
import net.siisise.lang.Bin;
45

56
/**
@@ -18,7 +19,7 @@ public class GF {
1819
final int[] log;
1920
final int[] exp;
2021

21-
// ガロア 有限体
22+
// ガロア 有限体 原始多項式?
2223
// final byte FF4 = 0x3; // 0x13 10011
2324
public static final byte FF8 = 0x1b; // 0x11b 100011011 AES
2425
public static final byte FF64 = 0x1b; // 0x1000000000000001b x11011
@@ -45,10 +46,6 @@ public GF(int n, int m) {
4546
log = new int[size + 1];
4647
exp = new int[size + 1];
4748

48-
// for (int a = 0; a <= size; a++) {
49-
// x[a] = (a << 1) ^ ((a >>> N) * root);
50-
// }
51-
5249
int a = 1;
5350
for (int e = 0; e < size; e++) {
5451
log[a] = e;
@@ -157,7 +154,7 @@ public long[] r(long[] s) {
157154
*/
158155
public final int x(int a) {
159156
return (a << 1) ^ ((a >>> N) * root);
160-
// return x[a];
157+
// return n[a];
161158
}
162159

163160
public int r(int s) {
@@ -173,40 +170,71 @@ public int inv(int a) {
173170
return a == 0 ? 0 : exp[size - log[a]];
174171
}
175172

173+
static final BigInteger TWO = BigInteger.valueOf(2);
174+
static final BigInteger THREE = BigInteger.valueOf(3);
175+
176176
/**
177177
* 逆数計算的なもの(簡易版)
178+
* 256 - 2 で ^254 ぐらいの位置づけ
179+
* ビット長*2回掛けるぐらいで計算はできる
178180
* @param a
179-
* @return aの逆数
180-
* @deprecated いろいろ間違っている
181+
* @return aの逆数 60bit程度まで
181182
*/
182183
public byte[] inv(byte[] a) {
183-
return modPow(a, (2 << (N+1)) - 2);
184+
BigInteger p = TWO.shiftLeft(N).subtract(TWO);
185+
return pow(a, p);
186+
// return pow(a, (2l << N) - 2);
184187
}
185188

186189
/**
187190
* あれ
188191
* @param a
189-
* @param p 1以上
192+
* @param p exponent 1以上
190193
* @return a^p mod xx
191194
*/
192-
public byte[] modPow(byte[] a, int p) {
193-
byte[] x;
195+
public byte[] pow(byte[] a, long p) {
194196
if ( p == 1 ) {
195197
return a;
196198
} else {
199+
byte[] n;
197200
if ( p % 3 == 0 ) {
198-
x = modPow(a, p / 3 );
199-
return mul(mul(x,x),x);
201+
n = pow(a, p / 3 );
202+
return mul(mul(n,n),n);
200203
}
201-
x = modPow(a, p / 2);
202-
x = mul(x, x);
204+
n = pow(a, p / 2);
205+
n = mul(n, n);
203206
if ( p % 2 != 0 ) {
204-
x = mul(x, a);
205-
// Bin.xorl(x, a);
207+
n = mul(n, a);
208+
// Bin.xorl(n, a);
209+
}
210+
return n;
211+
}
212+
}
213+
214+
/**
215+
* 簡易版
216+
* @param a 元
217+
* @param p exponent 1以上
218+
* @return
219+
*/
220+
public byte[] pow(byte[] a, BigInteger p) {
221+
if ( p.equals(BigInteger.ONE)) {
222+
return a;
223+
} else {
224+
byte[] n;
225+
if ( p.mod(THREE).equals(BigInteger.ZERO)) {
226+
n = pow(a, p.divide(THREE));
227+
return mul(mul(n,n),n);
228+
}
229+
n = pow( a, p.divide(TWO));
230+
n = mul(n,n);
231+
if ( !p.mod(TWO).equals(BigInteger.ZERO)) {
232+
n = mul(n,a);
206233
}
234+
return n;
207235
}
208-
return x;
209236
}
237+
210238
//*
211239

212240
public int mul(int a, int b) {
@@ -233,21 +261,21 @@ public long[] add(long[] a, long[] b) {
233261

234262

235263
/*
236-
public int mul(int x, int y) {
237-
if (x == 0 || y == 0) {
264+
public int mul(int n, int y) {
265+
if (n == 0 || y == 0) {
238266
return 0;
239267
}
240268
int m = 0;
241269
242-
x &= size;
270+
n &= size;
243271
y &= size;
244272
245-
while (x > 0) {
246-
if ((x & 1) != 0) {
273+
while (n > 0) {
274+
if ((n & 1) != 0) {
247275
m ^= y;
248276
}
249-
y = x(y);
250-
x >>>= 1;
277+
y = n(y);
278+
n >>>= 1;
251279
}
252280
return m;
253281
}

src/test/java/net/siisise/math/GFTest.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,24 +121,25 @@ public void testXXX() {
121121
// 279
122122
int x = 3;
123123
int x5 = 5;
124+
int x7 = 7;
124125
for ( int i = 1; i < 0x100; i++ ) {
125-
System.out.print("x = 0x" + Integer.toHexString(i));
126-
System.out.print(" = " + i);
127-
System.out.print(" 2x = " + Integer.toHexString(gf.x(i)));
128-
System.out.print(" x^2 = " + Integer.toHexString(gf.mul(i,i)));
129-
System.out.print(" x^2 + x = 0x" + Integer.toHexString(gf.mul(i,i)));
130-
System.out.print(" = " + gf.mul(i,i));
126+
System.out.print("x = 0x" + hex(i,2));
127+
System.out.print("(" + sub(i,3));
131128
byte[] n = new byte[1];
132129
n[0] = (byte)i;
133-
n = gf.inv(n);
134-
System.out.print(" inv = 0x" + Integer.toHexString(n[0]));
135-
System.out.print(" = " + Integer.toHexString(gf.inv(i)));
136-
System.out.print(" = " + gf.inv(i));
137-
System.out.print(" 3^"+i+"= " + Integer.toHexString(x));
138-
System.out.print(" inv = 0x" + Integer.toHexString(gf.inv(x)));
130+
// n = gf.inv(n);
131+
System.out.print(") inv = 0x" + hex(gf.inv(n)[0] & 0xff,2));
132+
System.out.print(" = " + hex(gf.inv(i),2));
133+
System.out.print("(" + sub("" + gf.inv(i), 3));
134+
System.out.print(") 2x = " + hex(gf.x(i),2));
135+
System.out.print(" x^2 = " + hex(gf.mul(i,i),2)); // 意味のない計算?
136+
// System.out.print(" x^2 + x = 0x" + sub(Integer.toHexString(gf.mul(i,i)),2));
137+
System.out.print("(" + sub(gf.mul(i,i),3));
138+
System.out.print(") 3^"+sub(i,3)+"= " + hex(x,2));
139+
System.out.print(" inv = 0x" + hex(gf.inv(x),2));
139140
x = gf.mul(x,3);
140-
System.out.print(" 5^"+i+"= " + Integer.toHexString(x5));
141-
System.out.print(" inv = 0x" + Integer.toHexString(gf.inv(x5)));
141+
System.out.print(" 5^"+sub(i,3)+"= " + hex(x5,2));
142+
System.out.print(" inv = 0x" + hex(gf.inv(x5),2));
142143
x5 = gf.mul(x5,5);
143144
System.out.print(" mod3 " + i % 3);
144145
System.out.print(" mod5 " + i % 5);
@@ -153,6 +154,19 @@ public void testXXX() {
153154
System.out.println("e 6 inv " + gf.inv(gf.mul(0x8d, 0xf6)));
154155
}
155156

157+
String sub(int src, int len) {
158+
String t = "" + src;
159+
return " ".substring(0, len - t.length()) + t;
160+
}
161+
162+
String hex(int src, int len) {
163+
return sub(Integer.toHexString(src),len);
164+
}
165+
166+
String sub(String src, int len) {
167+
return "0000000".substring(0, len - src.length()) + src;
168+
}
169+
156170
public void testLongGF() {
157171
System.out.println("longGF");
158172
GF gf = new GF(8,GF.FF8);

0 commit comments

Comments
 (0)