Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TKSS-1008: Remove BC from JMH tests #1009

Merged
merged 1 commit into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.tencent.kona.crypto.spec.SM2ParameterSpec;
import com.tencent.kona.crypto.util.Constants;
import com.tencent.kona.sun.security.ec.ECOperator;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
Expand All @@ -43,14 +42,13 @@
import javax.crypto.Cipher;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.Security;
import java.util.concurrent.TimeUnit;

/**
* The JMH-based performance test for checking constant-time issue.
*/
@Warmup(iterations = 3, time = 5)
@Measurement(iterations = 3, time = 5)
@Measurement(iterations = 5, time = 5)
@Fork(value = 2, jvmArgsAppend = {"-server", "-Xms2048M", "-Xmx2048M", "-XX:+UseG1GC"})
@Threads(1)
@BenchmarkMode(Mode.Throughput)
Expand All @@ -69,7 +67,6 @@ public class SM2CipherConstantTimeTest {

static {
TestUtils.addProviders();
Security.addProvider(new BouncyCastleProvider());
}

private static KeyPair keyPair(BigInteger priKeyValue) {
Expand All @@ -82,7 +79,7 @@ private static KeyPair keyPair(BigInteger priKeyValue) {
@State(Scope.Thread)
public static class CipherHolder {

@Param({"KonaCrypto", "KonaCrypto-Native", "BC"})
@Param({"KonaCrypto", "KonaCrypto-Native"})
String provider;

@Param({"Small", "Mid", "Big"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,17 @@
package com.tencent.kona.crypto.perf;

import com.tencent.kona.crypto.TestUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.openjdk.jmh.annotations.*;

import javax.crypto.Cipher;
import java.security.KeyPair;
import java.security.Security;
import java.util.concurrent.TimeUnit;

/**
* The JMH-based performance test for SM2 decryption.
*/
@Warmup(iterations = 5, time = 5)
@Measurement(iterations = 5, time = 10)
@Measurement(iterations = 5, time = 5)
@Fork(value = 2, jvmArgsAppend = {"-server", "-Xms2048M", "-Xmx2048M", "-XX:+UseG1GC"})
@Threads(1)
@BenchmarkMode(Mode.Throughput)
Expand All @@ -41,139 +39,82 @@ public class SM2CipherPerfTest {

static {
TestUtils.addProviders();
Security.addProvider(new BouncyCastleProvider());
}

private final static String PUB_KEY
= "041D9E2952A06C913BAD21CCC358905ADB3A8097DB6F2F87EB5F393284EC2B7208C30B4D9834D0120216D6F1A73164FDA11A87B0A053F63D992BFB0E4FC1C5D9AD";
private final static String PRI_KEY
= "3B03B35C2F26DBC56F6D33677F1B28AF15E45FE9B594A6426BDCAD4A69FF976B";
private final static KeyPair KEY_PAIR = TestUtils.keyPair(PUB_KEY, PRI_KEY);
private static final byte[] MESSAGE = TestUtils.dataKB(1);

private final static byte[] SMALL_DATA = TestUtils.data(128);
private final static byte[] MEDIUM_DATA = TestUtils.dataKB(1);
private final static byte[] BIG_DATA = TestUtils.dataMB(1);

@State(Scope.Benchmark)
public static class EncrypterHolder {

Cipher encrypter;
@Param({"KonaCrypto", "KonaCrypto-Native"})
String provider;

@Setup(Level.Trial)
public void setup() throws Exception {
encrypter = Cipher.getInstance("SM2", "KonaCrypto");
encrypter.init(Cipher.ENCRYPT_MODE, KEY_PAIR.getPublic());
}
}

@State(Scope.Benchmark)
public static class EncrypterHolderNative {
@Param({"Small", "Mid", "Big"})
String dataType;

byte[] data;
Cipher encrypter;

@Setup(Level.Trial)
public void setup() throws Exception {
encrypter = Cipher.getInstance("SM2", "KonaCrypto-Native");
encrypter.init(Cipher.ENCRYPT_MODE, KEY_PAIR.getPublic());
}
}

@State(Scope.Benchmark)
public static class EncrypterHolderBC {

Cipher encrypter;
data = data(dataType);

@Setup(Level.Trial)
public void setup() throws Exception {
encrypter = Cipher.getInstance("SM2", "BC");
encrypter = Cipher.getInstance("SM2", provider);
encrypter.init(Cipher.ENCRYPT_MODE, KEY_PAIR.getPublic());
}
}

@State(Scope.Benchmark)
public static class DecrypterHolder {

byte[] ciphertext;
Cipher decrypter;
@Param({"KonaCrypto", "KonaCrypto-Native"})
String provider;

@Setup(Level.Trial)
public void setup() throws Exception {
ciphertext = ciphertext();
decrypter = Cipher.getInstance("SM2", "KonaCrypto");
decrypter.init(Cipher.DECRYPT_MODE, KEY_PAIR.getPrivate());
}

private byte[] ciphertext() throws Exception {
Cipher cipher = Cipher.getInstance("SM2", "KonaCrypto");
cipher.init(Cipher.ENCRYPT_MODE, KEY_PAIR.getPublic());
return cipher.doFinal(MESSAGE);
}
}

@State(Scope.Benchmark)
public static class DecrypterHolderNative {
@Param({"Small", "Mid", "Big"})
String dataType;

byte[] ciphertext;
Cipher decrypter;

@Setup(Level.Trial)
public void setup() throws Exception {
ciphertext = ciphertext();
decrypter = Cipher.getInstance("SM2", "KonaCrypto-Native");
decrypter = Cipher.getInstance("SM2", provider);
decrypter.init(Cipher.DECRYPT_MODE, KEY_PAIR.getPrivate());
}

private byte[] ciphertext() throws Exception {
Cipher cipher = Cipher.getInstance("SM2", "KonaCrypto-Native");
Cipher cipher = Cipher.getInstance("SM2", provider);
cipher.init(Cipher.ENCRYPT_MODE, KEY_PAIR.getPublic());
return cipher.doFinal(MESSAGE);
return cipher.doFinal(data(dataType));
}
}

@State(Scope.Benchmark)
public static class DecrypterHolderBC {

byte[] ciphertext;
Cipher decrypter;

@Setup(Level.Trial)
public void setup() throws Exception {
ciphertext = ciphertext();
decrypter = Cipher.getInstance("SM2", "BC");
decrypter.init(Cipher.DECRYPT_MODE, KEY_PAIR.getPrivate());
}

private byte[] ciphertext() throws Exception {
Cipher cipher = Cipher.getInstance("SM2", "BC");
cipher.init(Cipher.ENCRYPT_MODE, KEY_PAIR.getPublic());
return cipher.doFinal(MESSAGE);
private static byte[] data(String dataType) {
switch (dataType) {
case "Small": return SMALL_DATA;
case "Mid": return MEDIUM_DATA;
case "Big": return BIG_DATA;
default: throw new IllegalArgumentException(
"Unsupported data type: " + dataType);
}
}

@Benchmark
public byte[] encrypt(EncrypterHolder holder) throws Exception {
return holder.encrypter.doFinal(MESSAGE);
}

@Benchmark
public byte[] encryptNative(EncrypterHolderNative holder) throws Exception {
return holder.encrypter.doFinal(MESSAGE);
}

@Benchmark
public byte[] encryptBC(EncrypterHolderBC holder) throws Exception {
return holder.encrypter.doFinal(MESSAGE);
return holder.encrypter.doFinal(holder.data);
}

@Benchmark
public byte[] decrypt(DecrypterHolder holder) throws Exception {
return holder.decrypter.doFinal(holder.ciphertext);
}

@Benchmark
public byte[] decryptNative(DecrypterHolderNative holder) throws Exception {
return holder.decrypter.doFinal(holder.ciphertext);
}

@Benchmark
public byte[] decryptBC(DecrypterHolderBC holder) throws Exception {
return holder.decrypter.doFinal(holder.ciphertext);
}
}
Loading
Loading