Skip to content

Commit 8160be2

Browse files
committed
シフト系追加
GFのmodPowっぽいもの
1 parent 0c949cf commit 8160be2

File tree

3 files changed

+195
-11
lines changed

3 files changed

+195
-11
lines changed

src/main/java/net/siisise/lang/Bin.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,14 @@ public static byte[] xorl(byte[] a, byte[] b) {
383383
return a;
384384
}
385385

386+
public static long[] xorl(long[] a, long[] b) {
387+
int len = Math.min(a.length, b.length);
388+
for ( int i = 0; i < len; i++ ) {
389+
a[i] ^= b[i];
390+
}
391+
return a;
392+
}
393+
386394
/**
387395
* a と b の offset からの xor.
388396
*
@@ -488,6 +496,7 @@ public static byte[] left(byte[] a, int shift) {
488496
}
489497

490498
/**
499+
* 右1bitシフト
491500
* @param a 元列
492501
* @return 右シフト
493502
*/
@@ -500,6 +509,25 @@ public static byte[] shr(byte[] a) {
500509
return n;
501510
}
502511

512+
/**
513+
* 右1bitシフト
514+
* @param a 非破壊
515+
* @return 右シフト
516+
*/
517+
public static int[] shr(int[] a) {
518+
int[] n = new int[a.length];
519+
for (int i = a.length - 1; i > 0; i--) {
520+
n[i] = (a[i] >>> 1) | ( a[i - 1] << 31);
521+
}
522+
n[0] = a[0] >>> 1;
523+
return n;
524+
}
525+
526+
/**
527+
* 右1bitシフト
528+
* @param a 非破壊
529+
* @return 右シフト
530+
*/
503531
public static long[] shr(long[] a) {
504532
long[] n = new long[a.length];
505533
for (int i = a.length - 1; i > 0; i--) {
@@ -509,13 +537,35 @@ public static long[] shr(long[] a) {
509537
return n;
510538
}
511539

540+
/**
541+
* 右1bit rotate
542+
* @param a 非破壊
543+
* @return
544+
*/
512545
public static byte[] ror(byte[] a) {
513546
byte b = (byte)(a[a.length - 1] << 7);
514547
byte[] n = shr(a);
515548
n[0] |= b;
516549
return n;
517550
}
518551

552+
/**
553+
* 右1bit rotate
554+
* @param a 非破壊
555+
* @return
556+
*/
557+
public static int[] ror(int[] a) {
558+
int b = a[a.length - 1] << 31;
559+
int[] n = shr(a);
560+
n[0] |= b;
561+
return n;
562+
}
563+
564+
/**
565+
*
566+
* @param a 非破壊
567+
* @return
568+
*/
519569
public static long[] ror(long[] a) {
520570
long b = a[a.length - 1] << 63;
521571
long[] n = shr(a);

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

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,22 @@ public GF() {
3939
public GF(int n, int m) {
4040
N = n - 1;
4141
root = m;
42-
constRb = 0; // root側を使う
42+
constRb = (byte) root; // root側を使う
4343
size = (1 << n) - 1;
4444
x = new int[size + 1];
4545
log = new int[size + 1];
4646
exp = new int[size + 1];
4747

48-
for (int a = 0; a <= size; a++) {
49-
x[a] = (a << 1) ^ ((a >>> N) * root);
50-
}
48+
// for (int a = 0; a <= size; a++) {
49+
// x[a] = (a << 1) ^ ((a >>> N) * root);
50+
// }
5151

5252
int a = 1;
5353
for (int e = 0; e < size; e++) {
5454
log[a] = e;
5555
exp[e] = a;
5656

57-
a ^= x(a);
57+
a ^= x(a); // a・3 // ・5 ・17でもいい
5858
}
5959
log[0] = 0;
6060
exp[size] = exp[0];
@@ -82,7 +82,7 @@ public GF(int n, byte rb) {
8282
log = null;
8383
exp = null;
8484
}
85-
85+
8686
/**
8787
* ふつうのGF s・2
8888
* バイト数は未検証. てきとう.
@@ -92,7 +92,7 @@ public GF(int n, byte rb) {
9292
public byte[] x(byte[] s) {
9393
byte[] v = Bin.shl(s); // constRb に 1bit 持っているのでこっちは消す
9494
if (s[0] < 0) {
95-
v[v.length - 1] ^= constRb;
95+
v[v.length - 1] ^= constRb; // 長い用
9696
}
9797
return v;
9898
}
@@ -123,6 +123,25 @@ public byte[] r(byte[] s) {
123123
return r;
124124
}
125125

126+
/**
127+
* GF s・2の逆 /2
128+
* @param s・2
129+
* @return s
130+
*/
131+
public int[] r(int[] s) {
132+
int[] r = Bin.ror(s); // constRb の 1bit が消えるのでこっちでつける
133+
if (r[0] < 0) {
134+
r[r.length - 1] ^= (constRb & 0xff) >>> 1;
135+
}
136+
return r;
137+
}
138+
139+
/**
140+
* s / 2.
141+
* s・2ができるならs/2も比較的単純な計算でできる 2以外はできない
142+
* @param s
143+
* @return
144+
*/
126145
public long[] r(long[] s) {
127146
long[] r = Bin.ror(s); // constRb の 1bit が消えるのでこっちでつける
128147
if (r[0] < 0) {
@@ -137,13 +156,57 @@ public long[] r(long[] s) {
137156
* @return a・2
138157
*/
139158
public final int x(int a) {
140-
// return (a << 1) ^ ((a >>> N) * root);
141-
return x[a];
159+
return (a << 1) ^ ((a >>> N) * root);
160+
// return x[a];
161+
}
162+
163+
public int r(int s) {
164+
return (s >>> 1) ^ ((s & 1) * root);
142165
}
143166

167+
/**
168+
* 8bit 逆数計算的なもの(高速版)
169+
* @param a
170+
* @return aの逆数
171+
*/
144172
public int inv(int a) {
145173
return a == 0 ? 0 : exp[size - log[a]];
146174
}
175+
176+
/**
177+
* 逆数計算的なもの(簡易版)
178+
* @param a
179+
* @return aの逆数
180+
* @deprecated いろいろ間違っている
181+
*/
182+
public byte[] inv(byte[] a) {
183+
return modPow(a, (2 << (N+1)) - 2);
184+
}
185+
186+
/**
187+
* あれ
188+
* @param a
189+
* @param p 1以上
190+
* @return a^p mod xx
191+
*/
192+
public byte[] modPow(byte[] a, int p) {
193+
byte[] x;
194+
if ( p == 1 ) {
195+
return a;
196+
} else {
197+
if ( p % 3 == 0 ) {
198+
x = modPow(a, p / 3 );
199+
return mul(mul(x,x),x);
200+
}
201+
x = modPow(a, p / 2);
202+
x = mul(x, x);
203+
if ( p % 2 != 0 ) {
204+
x = mul(x, a);
205+
// Bin.xorl(x, a);
206+
}
207+
}
208+
return x;
209+
}
147210
//*
148211

149212
public int mul(int a, int b) {
@@ -160,6 +223,15 @@ public int mul(int a, int b) {
160223
return exp[e];
161224
}
162225

226+
public byte[] add(byte[] a, byte[] b) {
227+
return Bin.xor(a, b);
228+
}
229+
230+
public long[] add(long[] a, long[] b) {
231+
return Bin.xor(a, b);
232+
}
233+
234+
163235
/*
164236
public int mul(int x, int y) {
165237
if (x == 0 || y == 0) {
@@ -225,7 +297,7 @@ public long[] mul(long[] a, long[] b) {
225297
int last = a.length - 1;
226298
while ( !isZero(a) ) {
227299
if ( (a[last] & 0x01) != 0 ) {
228-
r = Bin.xor(r, b);
300+
Bin.xorl(r, b);
229301
}
230302
a = Bin.shr(a);
231303
b = x(b);

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

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package net.siisise.math;
1717

18+
import java.math.BigInteger;
1819
import java.security.NoSuchAlgorithmException;
1920
import java.security.SecureRandom;
2021
import net.siisise.lang.Bin;
@@ -23,7 +24,6 @@
2324

2425
/**
2526
*
26-
* @author okome
2727
*/
2828
public class GFTest {
2929

@@ -73,6 +73,11 @@ public void testX_byteArr() {
7373
byte[] expResult = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,(byte)0x85};
7474
byte[] result = gf.x(s);
7575
assertArrayEquals(expResult, result);
76+
s = new byte[] {(byte)0x80};
77+
gf = new GF();
78+
expResult = new byte[] {0x1b};
79+
result = gf.x(s);
80+
assertArrayEquals(expResult, result);
7681
}
7782

7883
/**
@@ -100,4 +105,61 @@ public void testX_int() {
100105
int result = instance.x(a);
101106
assertEquals(expResult, result);
102107
}
108+
109+
/**
110+
* 3 0b00000011
111+
* 5 0b00000101
112+
* 17 0b00010001
113+
* 0x11b 0b100011011 283 256 + 16 + 8 + 2 + 1
114+
*/
115+
@Test
116+
public void testXXX() {
117+
System.out.println();
118+
BigInteger ff = BigInteger.valueOf(0x11b);
119+
GF gf = new GF(8, 0x11b);
120+
// 100011011
121+
// 279
122+
int x = 3;
123+
int x5 = 5;
124+
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));
131+
byte[] n = new byte[1];
132+
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)));
139+
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)));
142+
x5 = gf.mul(x5,5);
143+
System.out.print(" mod3 " + i % 3);
144+
System.out.print(" mod5 " + i % 5);
145+
System.out.print(" mod17 " + i % 17);
146+
System.out.print(" mod3 " + gf.inv(i) % 3);
147+
System.out.print(" mod5 " + gf.inv(i) % 5);
148+
System.out.println(" mod17 " + gf.inv(i) % 17);
149+
}
150+
151+
152+
System.out.println("e 6 inv " + gf.mul(0x8d, 0xf6));
153+
System.out.println("e 6 inv " + gf.inv(gf.mul(0x8d, 0xf6)));
154+
}
155+
156+
public void testLongGF() {
157+
System.out.println("longGF");
158+
GF gf = new GF(8,GF.FF8);
159+
byte[] a = new byte[1];
160+
byte[] b = new byte[1];
161+
byte[] c;
162+
c = gf.mul(a, b);
163+
System.out.println(Bin.toHex(c));
164+
}
103165
}

0 commit comments

Comments
 (0)