Skip to content

Commit 1fdc823

Browse files
gefeilidghgit
authored andcommitted
Remove unused functions. Format the code
1 parent 50c84da commit 1fdc823

File tree

5 files changed

+14
-90
lines changed

5 files changed

+14
-90
lines changed

core/src/main/java/org/bouncycastle/crypto/kems/SAKKEKEMExtractor.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
import org.bouncycastle.crypto.params.SAKKEPrivateKeyParameters;
77
import org.bouncycastle.crypto.params.SAKKEPublicKeyParameters;
88
import org.bouncycastle.math.ec.ECCurve;
9-
import org.bouncycastle.math.ec.ECFieldElement;
109
import org.bouncycastle.math.ec.ECPoint;
1110
import org.bouncycastle.util.Arrays;
1211
import org.bouncycastle.util.BigIntegers;
13-
import org.bouncycastle.util.encoders.Hex;
1412

1513

1614
public class SAKKEKEMExtractor
@@ -49,7 +47,7 @@ public byte[] extractSecret(byte[] encapsulation)
4947

5048
// Step 2: Compute w = <R_bS, K_bS> using pairing
5149
BigInteger w = computePairing(R_bS, K_bS, p, q);
52-
System.out.println(new String(Hex.encode(w.toByteArray())));
50+
//System.out.println(new String(Hex.encode(w.toByteArray())));
5351
//BigInteger w = tatePairing(R_bS.getXCoord().toBigInteger(), R_bS.getYCoord().toBigInteger(), K_bS.getXCoord().toBigInteger(), K_bS.getYCoord().toBigInteger(), q, p);
5452
// Step 3: Compute SSV = H XOR HashToIntegerRange(w, 2^n)
5553
BigInteger twoToN = BigInteger.ONE.shiftLeft(n);

core/src/main/java/org/bouncycastle/crypto/kems/SAKKEKEMSGenerator.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public SAKKEKEMSGenerator(SecureRandom random)
6464
public SecretWithEncapsulation generateEncapsulated(AsymmetricKeyParameter recipientKey)
6565
{
6666
// 1. Generate random SSV in range [0, 2^n - 1]
67-
BigInteger ssv = new BigInteger("123456789ABCDEF0123456789ABCDEF0", 16);//new BigInteger(n, random);
67+
BigInteger ssv = new BigInteger(n, random);
6868

6969
// 2. Compute r = HashToIntegerRange(SSV || b, q)
7070
BigInteger b = new BigInteger("323031312D30320074656C3A2B34343737303039303031323300", 16); //getRecipientId((SAKKEPublicKey)recipientKey);
@@ -127,25 +127,13 @@ public SecretWithEncapsulation generateEncapsulated(AsymmetricKeyParameter recip
127127
// Helper method for F_p² exponentiation
128128
public static BigInteger[] fp2Exponentiate(
129129
BigInteger p,
130-
BigInteger x,
131-
BigInteger y,
132-
BigInteger exponent,
133-
ECCurve.Fp curve
134-
)
135-
{
136-
BigInteger[] result = new BigInteger[2];
137-
sakkePointExponent(p, result, x, y, exponent, curve);
138-
return result;
139-
}
140-
141-
public static boolean sakkePointExponent(
142-
BigInteger p,
143-
BigInteger[] result,
144130
BigInteger pointX,
145131
BigInteger pointY,
146132
BigInteger n,
147-
ECCurve.Fp curve)
133+
ECCurve.Fp curve
134+
)
148135
{
136+
BigInteger[] result = new BigInteger[2];
149137

150138
// Initialize result with the original point
151139
BigInteger currentX = pointX;
@@ -174,6 +162,6 @@ public static boolean sakkePointExponent(
174162

175163
result[0] = currentX;
176164
result[1] = currentY;
177-
return true;
165+
return result;
178166
}
179167
}

core/src/main/java/org/bouncycastle/crypto/kems/SAKKEUtils.java

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,11 @@
22

33
import java.math.BigInteger;
44

5-
import org.bouncycastle.crypto.Digest;
65
import org.bouncycastle.crypto.digests.SHA256Digest;
7-
import org.bouncycastle.math.ec.ECPoint;
8-
import org.bouncycastle.util.encoders.Hex;
96

107
public class SAKKEUtils
118
{
12-
public static ECPoint sakkePointExponent(ECPoint point, BigInteger n) {
13-
if (n.equals(BigInteger.ZERO)) {
14-
throw new IllegalArgumentException("Exponent cannot be zero.");
15-
}
16-
17-
ECPoint result = point;
18-
int N = n.bitLength() - 1;
19-
20-
for (; N != 0; --N) {
21-
result = sakkePointSquare(result);
22-
if (n.testBit(N - 1)) {
23-
result = sakkePointsMultiply(result, point);
24-
}
25-
}
26-
return result;
27-
}
28-
29-
public static ECPoint sakkePointSquare(ECPoint point) {
30-
BigInteger x = point.getAffineXCoord().toBigInteger();
31-
BigInteger y = point.getAffineYCoord().toBigInteger();
32-
33-
BigInteger bx1 = x.add(y);
34-
BigInteger bx2 = x.subtract(y);
35-
BigInteger newX = bx1.multiply(bx2).mod(point.getCurve().getField().getCharacteristic());
36-
BigInteger newY = x.multiply(y).multiply(BigInteger.valueOf(2)).mod(point.getCurve().getField().getCharacteristic());
37-
38-
return point.getCurve().createPoint(newX, newY);
39-
}
409

41-
public static ECPoint sakkePointsMultiply(ECPoint p1, ECPoint p2) {
42-
return p1.add(p2).normalize();
43-
}
4410
public static BigInteger hashToIntegerRange(byte[] input, BigInteger q)
4511
{
4612
// RFC 6508 Section 5.1: Hashing to an Integer Range
@@ -66,32 +32,15 @@ public static BigInteger hashToIntegerRange(byte[] input, BigInteger q)
6632
// h_i = hashfn(h_{i-1})
6733
digest.update(h, 0, h.length);
6834
digest.doFinal(h, 0);
69-
//System.out.println("h_"+i+":" +new String(Hex.encode(h)));
7035
// v_i = hashfn(h_i || A)
7136
digest.update(h, 0, h.length);
7237
digest.update(A, 0, A.length);
7338
byte[] v_i = new byte[digest.getDigestSize()];
7439
digest.doFinal(v_i, 0);
75-
//System.out.println("v_"+i+":" +new String(Hex.encode(v_i)));
7640
// Append v_i to v'
7741
v = v.shiftLeft(v_i.length * 8).add(new BigInteger(1, v_i));
7842
}
79-
//System.out.println("v:" +new String(Hex.encode(v.toByteArray())));
8043
// Step 6: v = v' mod n
8144
return v.mod(q);
8245
}
83-
84-
public static byte[] hash(byte[] data)
85-
{
86-
Digest digest = new SHA256Digest();
87-
byte[] rlt = new byte[digest.getDigestSize()];
88-
digest.update(data, 0, data.length);
89-
digest.doFinal(rlt, 0);
90-
return rlt;
91-
}
92-
93-
public static byte[] hash(ECPoint point)
94-
{
95-
return hash(point.getEncoded(false)); // Use uncompressed encoding
96-
}
9746
}

core/src/test/java/org/bouncycastle/crypto/kems/test/SAKKEKEMSTest.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
import org.bouncycastle.crypto.SecretWithEncapsulation;
88
import org.bouncycastle.crypto.kems.SAKKEKEMExtractor;
99
import org.bouncycastle.crypto.kems.SAKKEKEMSGenerator;
10-
import org.bouncycastle.crypto.kems.SAKKEUtils;
1110
import org.bouncycastle.crypto.params.SAKKEPrivateKeyParameters;
1211
import org.bouncycastle.crypto.params.SAKKEPublicKeyParameters;
1312
import org.bouncycastle.math.ec.ECCurve;
1413
import org.bouncycastle.math.ec.ECPoint;
1514
import org.bouncycastle.util.Arrays;
16-
import org.bouncycastle.util.Strings;
1715
import org.bouncycastle.util.encoders.Hex;
1816
import org.bouncycastle.util.test.FixedSecureRandom;
1917
import org.bouncycastle.util.test.SimpleTest;
@@ -27,18 +25,6 @@ public static void main(String[] args)
2725
{
2826
SAKKEKEMSTest test = new SAKKEKEMSTest();
2927
test.performTest();
30-
// Expected Rb values
31-
// BigInteger expectedRbx = new BigInteger("44E8AD44AB8592A6A5A3DDCA5CF896C718043606A01D650DEF37A01F37C228C332FC317354E2C274D4DAF8AD001054C7...
32-
// BigInteger expectedRby = new BigInteger("557E134AD85BB1D4B9CE4F8BE4B08A12BABF55B1D6F1D7A638019EA28E15AB1C9F76375FDD1210D4F4351B9A009486B7...
33-
//
34-
// // Instantiate SAKKE KEM Generator
35-
// SAKKEKEMSGenerator kem = new SAKKEKEMSGenerator();
36-
// EncapsulatedData encapsulatedData = kem.encapsulate(SSV);
37-
//
38-
// // Validate results
39-
// boolean testPassed = expectedRbx.equals(encapsulatedData.getRbx()) && expectedRby.equals(encapsulatedData.getRby());
40-
41-
//System.out.println("SAKKE KEM Test " + (testPassed ? "PASSED" : "FAILED"));
4228
}
4329

4430

@@ -78,7 +64,7 @@ public void performTest()
7864
//
7965
byte[] b = Hex.decode("323031312D30320074656C3A2B34343737303039303031323300");
8066

81-
byte[] SSV = Hex.decode("123456789ABCDEF0123456789ABCDEF0");
67+
byte[] ssv = Hex.decode("123456789ABCDEF0123456789ABCDEF0");
8268
byte[] expectedR = Hex.decode("13EE3E1B8DAC5DB168B1CEB32F0566A4C273693F78BAFFA2A2EE6A686E6BD90F8206CCAB84E7F"
8369
+ "42ED39BD4FB131012ECCA2ECD2119414560C17CAB46B956A80F58A3302EB3E2C9A228FBA7ED34D8ACA2392DA1FFB0B17B2320AE09AAEDF"
8470
+ "D0235F6FE0EB65337A63F9CC97728B8E5AD0460FADE144369AA5B2166213247712096");
@@ -137,17 +123,18 @@ public void performTest()
137123
g,// Order of the subgroup (from RFC 6509)
138124
BigInteger.ONE // Cofactor = 1
139125
);
140-
SAKKEKEMSGenerator generator = new SAKKEKEMSGenerator(new SecureRandom());
141-
SecretWithEncapsulation rlt = generator.generateEncapsulated(null);
142126

127+
SecureRandom random = new FixedSecureRandom(new FixedSecureRandom.Source[]{new FixedSecureRandom.Data(ssv),
128+
new FixedSecureRandom.Data(b)});
129+
SAKKEKEMSGenerator generator = new SAKKEKEMSGenerator(random);
130+
SecretWithEncapsulation rlt = generator.generateEncapsulated(null);
143131

144132
ECPoint K_bS = curve.createPoint(kbx, kby);
145133

146-
147134
SAKKEKEMExtractor extractor = new SAKKEKEMExtractor(new SAKKEPrivateKeyParameters(new BigInteger(b), K_bS,
148135
new SAKKEPublicKeyParameters(curve.createPoint(Zx, Zy))));
149136
byte[] test = extractor.extractSecret(rlt.getEncapsulation());
150-
Assert.assertTrue(Arrays.areEqual(test, SSV));
137+
Assert.assertTrue(Arrays.areEqual(test, ssv));
151138

152139
}
153140
}

core/src/test/java/org/bouncycastle/crypto/test/RegressionTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.bouncycastle.crypto.test;
22

3+
import org.bouncycastle.crypto.kems.test.SAKKEKEMSTest;
34
import org.bouncycastle.util.test.SimpleTest;
45
import org.bouncycastle.util.test.Test;
56

@@ -195,6 +196,7 @@ public class RegressionTest
195196
new SparkleTest(),
196197
new ISAPTest(),
197198
new ConcatenationKDFTest(),
199+
new SAKKEKEMSTest(),
198200
};
199201

200202
public static void main(String[] args)

0 commit comments

Comments
 (0)