1 /*
2 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package org.openjdk.bench.javax.crypto.full;
24
25 import org.openjdk.jmh.annotations.BenchmarkMode;
26 import org.openjdk.jmh.annotations.Fork;
27 import org.openjdk.jmh.annotations.Measurement;
28 import org.openjdk.jmh.annotations.Mode;
29 import org.openjdk.jmh.annotations.OutputTimeUnit;
30 import org.openjdk.jmh.annotations.Param;
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.Warmup;
35 import org.openjdk.jmh.annotations.Benchmark;
36 import java.math.BigInteger;
37 import java.util.concurrent.TimeUnit;
38 import sun.security.util.math.intpoly.MontgomeryIntegerPolynomialP256;
39 import sun.security.util.math.intpoly.IntegerPolynomialP256;
40 import sun.security.util.math.MutableIntegerModuloP;
41 import sun.security.util.math.ImmutableIntegerModuloP;
42
43 @Fork(jvmArgs = {"-XX:+AlwaysPreTouch",
44 "--add-exports", "java.base/sun.security.util.math.intpoly=ALL-UNNAMED",
45 "--add-exports", "java.base/sun.security.util.math=ALL-UNNAMED"}, value = 1)
46 @Warmup(iterations = 3, time = 3)
47 @Measurement(iterations = 8, time = 2)
48 @OutputTimeUnit(TimeUnit.SECONDS)
49 @State(Scope.Thread)
50 @BenchmarkMode(Mode.Throughput)
51 public class PolynomialP256Bench {
52 final MontgomeryIntegerPolynomialP256 montField = MontgomeryIntegerPolynomialP256.ONE;
53 final IntegerPolynomialP256 residueField = IntegerPolynomialP256.ONE;
54 final BigInteger refx =
55 new BigInteger("6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296", 16);
56 @SuppressWarnings("initialization")
57 final ImmutableIntegerModuloP x = residueField.getElement(refx);
58 @SuppressWarnings("initialization")
59 final ImmutableIntegerModuloP X = montField.getElement(refx);
60 final ImmutableIntegerModuloP one = montField.get1();
61
62 @Param({"true", "false"})
63 private boolean isMontBench;
64
65 @Benchmark
66 public MutableIntegerModuloP benchMultiply() {
67 MutableIntegerModuloP test;
68 if (isMontBench) {
69 test = X.mutable();
70 } else {
71 test = x.mutable();
72 }
73
74 for (int i = 0; i< 10000; i++) {
75 test = test.setProduct(test);
76 }
77 return test;
78 }
79
80 @Benchmark
81 public MutableIntegerModuloP benchSquare() {
82 MutableIntegerModuloP test;
83 if (isMontBench) {
84 test = X.mutable();
85 } else {
86 test = x.mutable();
87 }
88
89 for (int i = 0; i< 10000; i++) {
90 test = test.setSquare();
91 }
92 return test;
93 }
94
95 @Benchmark
96 public MutableIntegerModuloP benchAssign() {
97 MutableIntegerModuloP test1 = X.mutable();
98 MutableIntegerModuloP test2 = one.mutable();
99 for (int i = 0; i< 10000; i++) {
100 test1.conditionalSet(test2, 0);
101 test1.conditionalSet(test2, 1);
102 test2.conditionalSet(test1, 0);
103 test2.conditionalSet(test1, 1);
104 }
105 return test2;
106 }
107 }