Skip to content

Commit 552e55c

Browse files
author
gefeili
committed
Change the int to byte for Polynomial classes
1 parent 84f29d4 commit 552e55c

File tree

4 files changed

+563
-573
lines changed

4 files changed

+563
-573
lines changed

core/src/main/java/org/bouncycastle/crypto/split/Polynomial.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
public abstract class Polynomial
44
{
5-
public static final int AES = 0;
6-
public static final int RSA = 1;
5+
public static final byte AES = 0;
6+
public static final byte RSA = 1;
77
/**
88
* <summary>
99
* Length of the secret
@@ -23,7 +23,7 @@ public abstract class Polynomial
2323
* </summary>
2424
*/
2525
protected int n;
26-
protected int[][] p;
26+
protected byte[][] p;
2727

2828
protected Polynomial(int l, int m, int n)
2929
{
@@ -35,47 +35,47 @@ protected Polynomial(int l, int m, int n)
3535

3636
protected void init()
3737
{
38-
p = new int[n][m];
38+
p = new byte[n][m];
3939
for (int i = 0; i < n; i++)
4040
{
4141
for (int j = 0; j < m; j++)
4242
{
43-
p[i][j] = gfPow(i + 1, j);
43+
p[i][j] = gfPow((byte)(i + 1), (byte)j);
4444
}
4545
}
4646
}
4747

48-
public int[][] createShares(int[][] sr)
48+
public byte[][] createShares(byte[][] sr)
4949
{
50-
int[][] result = new int[p.length][sr[0].length];
50+
byte[][] result = new byte[p.length][sr[0].length];
5151
for (int i = 0; i < p.length; i++)
5252
{
5353
result[i] = gfVecMul(p[i], sr);
5454
}
5555
return result;
5656
}
5757

58-
public int[] recombine(int[] rr, int[][] splits)
58+
public byte[] recombine(byte[] rr, byte[][] splits)
5959
{
6060
int n = rr.length;
61-
int[] r = new int[n];
62-
int tmp;
63-
int[] products = new int[n - 1];
61+
byte[] r = new byte[n];
62+
byte tmp;
63+
byte[] products = new byte[n - 1];
6464
for (int i = 0; i < n; i++)
6565
{
6666
tmp = 0;
6767
for (int j = 0; j < n; j++)
6868
{
6969
if (j != i)
7070
{
71-
products[tmp++] = gfDiv(rr[j], rr[i] ^ rr[j]);
71+
products[tmp++] = gfDiv(rr[j] & 0xff, (rr[i] ^ rr[j]) & 0xff);
7272
}
7373
}
7474

7575
tmp = 1;
76-
for (int p : products)
76+
for (byte p : products)
7777
{
78-
tmp = gfMul(tmp, p);
78+
tmp = (byte)gfMul(tmp & 0xff, p & 0xff);
7979
}
8080
r[i] = tmp;
8181
}
@@ -85,34 +85,34 @@ public int[] recombine(int[] rr, int[][] splits)
8585

8686
protected abstract int gfMul(int x, int y);
8787

88-
protected abstract int gfDiv(int x, int y);
88+
protected abstract byte gfDiv(int x, int y);
8989

90-
protected int gfPow(int n, int k)
90+
protected byte gfPow(int n, byte k)
9191
{
9292
int result = 1;
9393
for (int i = 0; i < 8; i++)
9494
{
9595
if ((k & (1 << i)) != 0)
9696
{
97-
result = gfMul(result, n);
97+
result = (byte) gfMul(result & 0xff, n & 0xff);
9898
}
99-
n = gfMul(n, n);
99+
n = gfMul(n & 0xff, n & 0xff);
100100
}
101-
return result;
101+
return (byte) result;
102102
}
103103

104-
private int[] gfVecMul(int[] xs, int[][] yss)
104+
private byte[] gfVecMul(byte[] xs, byte[][] yss)
105105
{
106-
int[] result = new int[yss[0].length];
106+
byte[] result = new byte[yss[0].length];
107107
int sum;
108108
for (int j = 0; j < yss[0].length; j++)
109109
{
110110
sum = 0;
111111
for (int k = 0; k < xs.length; k++)
112112
{
113-
sum = sum ^ gfMul(xs[k], yss[k][j]);
113+
sum ^= gfMul(xs[k] & 0xff, yss[k][j] & 0xff);
114114
}
115-
result[j] = sum;
115+
result[j] = (byte) sum;
116116
}
117117
return result;
118118
}

core/src/main/java/org/bouncycastle/crypto/split/PolynomialNative.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ protected int gfMul(int x, int y)
5151
return result & 0xFF;
5252
}
5353

54-
protected int gfDiv(int x, int y)
54+
protected byte gfDiv(int x, int y)
5555
{
56-
return gfMul(x, gfPow(y, 254));
56+
return (byte)gfMul(x, gfPow((byte)y, (byte)254) & 0xff);
5757
}
5858
}

0 commit comments

Comments
 (0)