Skip to content

Commit 48b1bf3

Browse files
committed
Bin型変更
1 parent 5b6f423 commit 48b1bf3

File tree

6 files changed

+152
-23
lines changed

6 files changed

+152
-23
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Java Module System JDK 11用
3030
<dependency>
3131
<groupId>net.siisise</groupId>
3232
<artifactId>softlib.module</artifactId>
33-
<version>1.1.16</version>
33+
<version>1.1.17</version>
3434
<type>jar</type>
3535
</dependency>
3636
~~~
@@ -45,8 +45,8 @@ JDK 8用
4545
~~~
4646
時々変わることがあるので特定バージョンを指定するか、SoftLibJSONなど使用したい機能経由で指定するのがおすすめです。
4747

48-
リリース版 1.1.16 ぐらい。
49-
次版 1.1.17-SNAPSHOT
48+
リリース版 1.1.17 ぐらい。
49+
次版 1.1.18-SNAPSHOT
5050

5151
~~~
5252
<version>[1.1.8,)</version>

pom.xml

Lines changed: 13 additions & 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.module</artifactId>
6-
<version>1.1.17-SNAPSHOT</version>
6+
<version>1.1.17</version>
77
<packaging>jar</packaging>
88
<name>SoftLib</name>
99
<description>Java simple API Packet io, base64, etc...</description>
@@ -107,6 +107,18 @@
107107
<version>[5.12.0,)</version>
108108
<scope>test</scope>
109109
</dependency>
110+
<dependency>
111+
<groupId>junit</groupId>
112+
<artifactId>junit</artifactId>
113+
<version>4.13.2</version>
114+
<scope>test</scope>
115+
</dependency>
116+
<dependency>
117+
<groupId>org.hamcrest</groupId>
118+
<artifactId>hamcrest-core</artifactId>
119+
<version>1.3</version>
120+
<scope>test</scope>
121+
</dependency>
110122
</dependencies>
111123
<distributionManagement>
112124
<snapshotRepository>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import net.siisise.math.Matics;
2121

2222
/**
23-
* 名前候補 BinかHex
23+
* 名前候補 BinかHex.
2424
* Hexというよりバイト列変換の主なもの.
2525
* 配列をintやlongとbyteで変換してみたり、ちょっとしたbit演算をまとめて実行するだけのまとまり.
2626
* HexとBASE64は統合したいかもしれない
@@ -788,7 +788,7 @@ public static byte[] litob(final int[] src) {
788788
* @param src long列
789789
* @return byte列
790790
*/
791-
public static byte[] ltob(final long[] src) {
791+
public static byte[] ltob(final long... src) {
792792
byte[] ds = new byte[src.length * 8];
793793
for (int i = 0; i < src.length; i++) {
794794
long s = src[i];
@@ -811,7 +811,7 @@ public static byte[] ltob(final long[] src) {
811811
* @param src long列
812812
* @return byte列
813813
*/
814-
public static byte[] lltob(final long[] src) {
814+
public static byte[] lltob(final long... src) {
815815
byte[] ds = new byte[src.length * 8];
816816
for (int i = 0; i < src.length; i++) {
817817
long s = src[i];

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

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

33
import java.math.BigInteger;
4+
import java.util.Arrays;
45
import net.siisise.lang.Bin;
56

67
/**
@@ -21,7 +22,7 @@ public class GF {
2122
// ガロア 有限体 原始多項式?
2223
// final byte FF4 = 0x3; // 0x13 10011
2324
public static final byte FF8 = 0x1b; // 0x11b 100011011 AES
24-
public static final byte FF64 = 0x1b; // 0x1000000000000001b x11011
25+
public static final byte FF64 = 0x1b; // 0x1000000000000001b x11011
2526
public static final byte FF128 = (byte) 0x87; // 0x100000000000000000000000000000087 x10000111 // GMAC
2627
public static final byte[] GF8 = {FF8}; // 0x11b
2728
public static final byte[] GF64 = {0, 0, 0, 0, 0, 0, 0, FF64}; // 0x1000000000000001b
@@ -191,14 +192,40 @@ public int inv(int a) {
191192
* @param a
192193
* @return aの逆数 60bit程度まで
193194
*/
194-
public byte[] inv(byte[] a) {
195+
public byte[] inv1(byte[] a) {
195196
return pow(a, TWO.shiftLeft(N).subtract(TWO));
196197
}
197198

198-
public long[] inv(long[] a) {
199+
public long[] inv1(long[] a) {
199200
return pow(a, TWO.shiftLeft(N).subtract(TWO));
200201
}
201202

203+
/**
204+
* 逆数演算.
205+
* 速い?
206+
* @param a
207+
* @return
208+
*/
209+
public byte[] inv(byte[] a) {
210+
byte[] p = new byte[a.length];
211+
Arrays.fill(p, (byte) -1);
212+
p[p.length - 1]--;
213+
return pow(a, p);
214+
}
215+
216+
/**
217+
* 逆数演算.
218+
* 速い?
219+
* @param a
220+
* @return
221+
*/
222+
public long[] inv(long[] a) {
223+
long[] p = new long[a.length];
224+
Arrays.fill(p, -1l);
225+
p[p.length - 1]--;
226+
return pow(a, p);
227+
}
228+
202229
/**
203230
* 累乗.
204231
*
@@ -256,6 +283,34 @@ public long[] pow(long[] a, long p) {
256283
}
257284
}
258285

286+
public byte[] pow(byte[] n, byte[] p) {
287+
byte[] x = new byte[n.length];
288+
x[x.length - 1] = 1;
289+
for (int i = 0; i < p.length; i++) {
290+
for (int j = 24; j < 32; j++) {
291+
x = mul(x, x);
292+
if ((p[i] << j) < 0) {
293+
x = mul(x, n);
294+
}
295+
}
296+
}
297+
return x;
298+
}
299+
300+
public long[] pow(long[] n, long[] p) {
301+
long[] x = new long[n.length];
302+
x[n.length - 1] = 1;
303+
for (int i = 0; i < p.length; i++) {
304+
for (int j = 0; j < 64; j++) {
305+
x = mul(x, x);
306+
if ((p[i] << j) < 0) {
307+
x = mul(x, n);
308+
}
309+
}
310+
}
311+
return x;
312+
}
313+
259314
static final BigInteger SEVEN = BigInteger.valueOf(7);
260315
static final BigInteger eSEVEN = BigInteger.ONE.shiftLeft(128).subtract(TWO);
261316

@@ -373,7 +428,14 @@ private static boolean isZero(long[] a) {
373428
* @return a・b
374429
*/
375430
public byte[] mul(byte[] a, byte[] b) {
431+
if (a.length % 8 == 0) {
432+
return Bin.ltob(mul(Bin.btol(a), Bin.btol(b)));
433+
}
376434
byte[] r = new byte[a.length];
435+
if (isZero(b)) {
436+
return r;
437+
}
438+
/*
377439
int last = a.length - 1;
378440
while (!isZero(a)) {
379441
if ((a[last] & 0x01) != 0) {
@@ -382,6 +444,23 @@ public byte[] mul(byte[] a, byte[] b) {
382444
a = Bin.shr(a);
383445
b = x(b);
384446
}
447+
*/
448+
for (int i = a.length - 1; i >= 0; i--) {
449+
byte ai = a[i];
450+
if (ai == 0) {
451+
for (int j = 0; j < 8; j++) {
452+
b = x(b);
453+
}
454+
} else {
455+
for (int j = 7; j >= 0; j--) {
456+
if (((byte) (ai << j)) < 0) {
457+
Bin.xorl(r, b);
458+
}
459+
b = x(b);
460+
}
461+
}
462+
}
463+
385464
return r;
386465
}
387466

@@ -444,13 +523,4 @@ public byte[] div(byte[] a, byte[] b) {
444523
public long[] div(long[] a, long[] b) {
445524
return mul(a, inv(b));
446525
}
447-
448-
public static String toHexString(long[] s) {
449-
StringBuilder sb = new StringBuilder(32);
450-
for (long v : s) {
451-
String h = "000000000000000" + Long.toHexString(v);
452-
sb.append(h.substring(h.length() - 16));
453-
}
454-
return sb.toString();
455-
}
456526
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class GFL {
2727
static final long FF128 = 0x87;
2828

2929
long constRb = FF128;
30+
long[] constRbl;
3031

3132
private final long[][] shL;
3233

@@ -49,6 +50,15 @@ public GFL(long[] a) {
4950
this(FF128, a);
5051
}
5152

53+
public GFL(long[] rb, long[] a) {
54+
constRbl = rb.clone();
55+
shL = new long[a.length * 64][];
56+
shL[0] = a.clone();
57+
for ( int i = 1; i < shL.length; i++) {
58+
shL[i] = shll(shL[i-1]);
59+
}
60+
}
61+
5262
/**
5363
* 左シフト演算っぽい動作 1つ.
5464
* s・2.
@@ -62,6 +72,15 @@ public final long[] shl(long[] s) {
6272
return v;
6373
}
6474

75+
76+
public final long[] shll(long[] s) {
77+
long[] v = Bin.shl(s);
78+
if (s[0] < 0) {
79+
Bin.xorl(v, constRbl);
80+
}
81+
return v;
82+
}
83+
6584
/**
6685
* 右シフト演算っぽい動作.
6786
* s/2.

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

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ public long[] add(long[] a, long[] b) {
114114
static final BigInteger TWO = BigInteger.valueOf(2);
115115
static final BigInteger THREE = BigInteger.valueOf(3);
116116

117-
static final BigInteger INV_POW = BigInteger.ONE.shiftLeft(128).subtract(TWO);
117+
// static final BigInteger INV_POW = BigInteger.ONE.shiftLeft(128).subtract(TWO);
118+
static final long[] INV_LONG_POW = {0xffffffffffffffffl, 0xfffffffffffffffdl};
118119

119120
/**
120121
* 逆数計算.
@@ -123,18 +124,18 @@ public long[] add(long[] a, long[] b) {
123124
* @return 逆数
124125
*/
125126
public long[] inv(long[] a) {
126-
return new GFRev(a).pow(INV_POW);
127+
return new GFRev(a).pow(INV_LONG_POW);
127128
}
128129

129130
public long[] inv() {
130-
return pow(INV_POW);
131+
return pow(INV_LONG_POW);
131132
}
132133

133134
/**
134135
* 累乗.
135136
* @param a 底
136137
* @param p exponent 1以上
137-
* @return
138+
* @return a^p
138139
*/
139140
public long[] pow(long[] a, BigInteger p) {
140141
return new GFRev(a).pow(p);
@@ -156,12 +157,39 @@ public long[] pow(long[] a, BigInteger p) {
156157
}
157158
*/
158159
}
160+
161+
public long[] pow(long[] p) {
162+
long[] n = shL[0];
163+
long[] x = {0,1};
164+
for ( int i = 0; i < p.length; i++) {
165+
for ( int j = 0; j < 64; j++) {
166+
if ( (p[i] << j) < 0) {
167+
x = mul(x, n);
168+
}
169+
x = mul(x, x);
170+
}
171+
}
172+
return x;
173+
}
174+
175+
BigInteger OCT = BigInteger.valueOf(256);
159176

160177
public long[] pow(BigInteger p) {
161178
if ( p.equals(BigInteger.ONE)) {
162179
return shL[0];
163180
} else {
164181
long[] n;
182+
if ( p.compareTo(OCT) > 0 ) {
183+
BigInteger m = p.mod(OCT);
184+
n = pow(p.divide(OCT));
185+
for ( int i = 0; i < 8; i++ ) {
186+
n = mul(n,n);
187+
}
188+
if ( !m.equals(BigInteger.ZERO)) {
189+
n = mul( n, pow(p));
190+
}
191+
return n;
192+
}
165193
if ( p.mod(THREE).equals(BigInteger.ZERO)) {
166194
n = pow(p.divide(THREE));
167195
GFRev gfn = new GFRev(n);

0 commit comments

Comments
 (0)