Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- Add blockTimestamp to transaction RPC results [#9887](https://github.com/hyperledger/besu/pull/9887)
- Plugin API: Allow the registration of multiple PluginTransactionPoolValidatorFactory [#9964](https://github.com/hyperledger/besu/pull/9964)
- Add `-Pcases` case name filtering to JMH benchmark suite [#9982](https://github.com/hyperledger/besu/pull/9982)
- Use JDK SHA-256 provider to leverage hardware SHA-NI instructions instead of BouncyCastle [#9924](https://github.com/hyperledger/besu/pull/9924)

## 26.2.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ private Hash() {}

private static final Supplier<MessageDigest> KECCAK256_SUPPLIER =
Suppliers.memoize(() -> messageDigest(KECCAK256_ALG));
private static final Supplier<MessageDigest> SHA256_SUPPLIER =
Suppliers.memoize(() -> messageDigest(SHA256_ALG));
private static final ThreadLocal<MessageDigest> SHA256_DIGEST =
ThreadLocal.withInitial(() -> messageDigest(SHA256_ALG));
private static final Supplier<MessageDigest> RIPEMD160_SUPPLIER =
Suppliers.memoize(() -> messageDigest(RIPEMD160_ALG));
private static final Supplier<MessageDigest> BLAKE2BF_SUPPLIER =
Expand Down Expand Up @@ -73,7 +73,9 @@ private static byte[] digestUsingAlgorithm(
* @return A digest.
*/
public static Bytes32 sha256(final Bytes input) {
return Bytes32.wrap(digestUsingAlgorithm(input, SHA256_SUPPLIER));
final MessageDigest digest = SHA256_DIGEST.get();
input.update(digest);
return Bytes32.wrap(digest.digest());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.bouncycastle.jcajce.provider.digest.Keccak;
import org.bouncycastle.jcajce.provider.digest.RIPEMD160;
import org.bouncycastle.jcajce.provider.digest.SHA256;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

/** The Message digest factory. */
Expand Down Expand Up @@ -56,9 +55,10 @@ private MessageDigestFactory() {}
public static MessageDigest create(final String algorithm) throws NoSuchAlgorithmException {
return switch (algorithm) {
case KECCAK256_ALG -> new Keccak.Digest256();
case SHA256_ALG -> new SHA256.Digest();
case RIPEMD160_ALG -> new RIPEMD160.Digest();
case BLAKE2BF_ALG -> new Blake2bfMessageDigest();
// SHA-256 (and other standard algorithms) resolved via JCA provider priority order
// enabling SHA-NI hardware acceleration if available
default -> MessageDigest.getInstance(algorithm);
Comment thread
daniellehrner marked this conversation as resolved.
};
}
Expand Down
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: there's also ./ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/benchmarks/SHA256Benchmark.java
which is nearly testing the same thing, just with the thin precompile wrapper around it. Think it's OK to have both though since we use evmtool benchmark for the precompiles and JMH for the opcodes.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright contributors to Besu.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.vm.operations;

import org.hyperledger.besu.crypto.Hash;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

@State(Scope.Thread)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@OutputTimeUnit(value = TimeUnit.NANOSECONDS)
@BenchmarkMode(Mode.AverageTime)
public class SHA256Benchmark {

@Param({"32", "64", "128", "256", "512", "1024", "2048", "4096"})
private String inputSize;

public Bytes bytes;

@Setup
public void setUp() {
final Random random = new Random(42);
final byte[] byteArray = new byte[Integer.parseInt(inputSize)];
random.nextBytes(byteArray);
bytes = Bytes.wrap(byteArray);
}

@Benchmark
public Bytes32 sha256() {
return Hash.sha256(bytes);
}
}