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     final ImmutableIntegerModuloP x = residueField.getElement(refx);
 57     final ImmutableIntegerModuloP X = montField.getElement(refx);
 58     final ImmutableIntegerModuloP one = montField.get1();
 59 
 60     @Param({"true", "false"})
 61     private boolean isMontBench;
 62 
 63     @Benchmark
 64     public MutableIntegerModuloP benchMultiply() {
 65         MutableIntegerModuloP test;
 66         if (isMontBench) {
 67             test = X.mutable();
 68         } else {
 69             test = x.mutable();
 70         }
 71 
 72         for (int i = 0; i< 10000; i++) {
 73             test = test.setProduct(test);
 74         }
 75         return test;
 76     }
 77 
 78     @Benchmark
 79     public MutableIntegerModuloP benchSquare() {
 80         MutableIntegerModuloP test;
 81         if (isMontBench) {
 82             test = X.mutable();
 83         } else {
 84             test = x.mutable();
 85         }
 86 
 87         for (int i = 0; i< 10000; i++) {
 88             test = test.setSquare();
 89         }
 90         return test;
 91     }
 92 
 93     @Benchmark
 94     public MutableIntegerModuloP benchAssign() {
 95         MutableIntegerModuloP test1 = X.mutable();
 96         MutableIntegerModuloP test2 = one.mutable();
 97         for (int i = 0; i< 10000; i++) {
 98             test1.conditionalSet(test2, 0);
 99             test1.conditionalSet(test2, 1);
100             test2.conditionalSet(test1, 0);
101             test2.conditionalSet(test1, 1);
102         }
103         return test2;
104     }
105 }