21
21
22
22
import com .tencent .kona .crypto .TestUtils ;
23
23
import org .bouncycastle .jce .provider .BouncyCastleProvider ;
24
- import org .openjdk .jmh .annotations .Benchmark ;
25
- import org .openjdk .jmh .annotations .BenchmarkMode ;
26
- import org .openjdk .jmh .annotations .Fork ;
27
- import org .openjdk .jmh .annotations .Level ;
28
- import org .openjdk .jmh .annotations .Measurement ;
29
- import org .openjdk .jmh .annotations .Mode ;
30
- import org .openjdk .jmh .annotations .OutputTimeUnit ;
31
- import org .openjdk .jmh .annotations .Scope ;
32
- import org .openjdk .jmh .annotations .Setup ;
33
- import org .openjdk .jmh .annotations .State ;
34
- import org .openjdk .jmh .annotations .Threads ;
35
- import org .openjdk .jmh .annotations .Warmup ;
24
+ import org .openjdk .jmh .annotations .*;
36
25
37
26
import javax .crypto .Cipher ;
38
27
import java .security .KeyPair ;
39
28
import java .security .Security ;
40
29
import java .util .concurrent .TimeUnit ;
41
30
42
- import static com .tencent .kona .crypto .TestUtils .PROVIDER ;
43
-
44
31
/**
45
32
* The JMH-based performance test for SM2 decryption.
46
33
*/
@@ -71,7 +58,19 @@ public static class EncrypterHolder {
71
58
72
59
@ Setup (Level .Trial )
73
60
public void setup () throws Exception {
74
- encrypter = Cipher .getInstance ("SM2" , PROVIDER );
61
+ encrypter = Cipher .getInstance ("SM2" , "KonaCrypto" );
62
+ encrypter .init (Cipher .ENCRYPT_MODE , KEY_PAIR .getPublic ());
63
+ }
64
+ }
65
+
66
+ @ State (Scope .Benchmark )
67
+ public static class EncrypterHolderNative {
68
+
69
+ Cipher encrypter ;
70
+
71
+ @ Setup (Level .Trial )
72
+ public void setup () throws Exception {
73
+ encrypter = Cipher .getInstance ("SM2" , "KonaCrypto-Native" );
75
74
encrypter .init (Cipher .ENCRYPT_MODE , KEY_PAIR .getPublic ());
76
75
}
77
76
}
@@ -97,12 +96,32 @@ public static class DecrypterHolder {
97
96
@ Setup (Level .Trial )
98
97
public void setup () throws Exception {
99
98
ciphertext = ciphertext ();
100
- decrypter = Cipher .getInstance ("SM2" , PROVIDER );
99
+ decrypter = Cipher .getInstance ("SM2" , "KonaCrypto" );
100
+ decrypter .init (Cipher .DECRYPT_MODE , KEY_PAIR .getPrivate ());
101
+ }
102
+
103
+ private byte [] ciphertext () throws Exception {
104
+ Cipher cipher = Cipher .getInstance ("SM2" , "KonaCrypto" );
105
+ cipher .init (Cipher .ENCRYPT_MODE , KEY_PAIR .getPublic ());
106
+ return cipher .doFinal (MESSAGE );
107
+ }
108
+ }
109
+
110
+ @ State (Scope .Benchmark )
111
+ public static class DecrypterHolderNative {
112
+
113
+ byte [] ciphertext ;
114
+ Cipher decrypter ;
115
+
116
+ @ Setup (Level .Trial )
117
+ public void setup () throws Exception {
118
+ ciphertext = ciphertext ();
119
+ decrypter = Cipher .getInstance ("SM2" , "KonaCrypto-Native" );
101
120
decrypter .init (Cipher .DECRYPT_MODE , KEY_PAIR .getPrivate ());
102
121
}
103
122
104
123
private byte [] ciphertext () throws Exception {
105
- Cipher cipher = Cipher .getInstance ("SM2" , PROVIDER );
124
+ Cipher cipher = Cipher .getInstance ("SM2" , "KonaCrypto-Native" );
106
125
cipher .init (Cipher .ENCRYPT_MODE , KEY_PAIR .getPublic ());
107
126
return cipher .doFinal (MESSAGE );
108
127
}
@@ -133,6 +152,11 @@ public byte[] encrypt(EncrypterHolder holder) throws Exception {
133
152
return holder .encrypter .doFinal (MESSAGE );
134
153
}
135
154
155
+ @ Benchmark
156
+ public byte [] encryptNative (EncrypterHolderNative holder ) throws Exception {
157
+ return holder .encrypter .doFinal (MESSAGE );
158
+ }
159
+
136
160
@ Benchmark
137
161
public byte [] encryptBC (EncrypterHolderBC holder ) throws Exception {
138
162
return holder .encrypter .doFinal (MESSAGE );
@@ -143,6 +167,11 @@ public byte[] decrypt(DecrypterHolder holder) throws Exception {
143
167
return holder .decrypter .doFinal (holder .ciphertext );
144
168
}
145
169
170
+ @ Benchmark
171
+ public byte [] decryptNative (DecrypterHolderNative holder ) throws Exception {
172
+ return holder .decrypter .doFinal (holder .ciphertext );
173
+ }
174
+
146
175
@ Benchmark
147
176
public byte [] decryptBC (DecrypterHolderBC holder ) throws Exception {
148
177
return holder .decrypter .doFinal (holder .ciphertext );
0 commit comments