use of org.hyperledger.besu.crypto.KeyPair in project besu by hyperledger.
the class ECIESEncryptionEngine method forEncryption.
/**
* Creates a new engine for encryption.
*
* <p>The generated IV and ephemeral public key are available via getters {@link #getIv()} and
* {@link #getEphPubKey()}.
*
* @param pubKey The public key of the receiver.
* @return An engine prepared for encryption.
*/
public static ECIESEncryptionEngine forEncryption(final SECPPublicKey pubKey) {
final SignatureAlgorithm signatureAlgorithm = SignatureAlgorithmFactory.getInstance();
// Create an ephemeral key pair for IES whose public key we can later append in the message.
final KeyPair ephKeyPair = signatureAlgorithm.generateKeyPair();
// Create random iv.
final byte[] ivb = ECIESHandshaker.random(CIPHER_BLOCK_SIZE).toArray();
return new ECIESEncryptionEngine(signatureAlgorithm.calculateECDHKeyAgreement(ephKeyPair.getPrivateKey(), pubKey), ephKeyPair.getPublicKey(), ivb);
}
use of org.hyperledger.besu.crypto.KeyPair in project besu by hyperledger.
the class Benchmarks method benchSecp256k1Recover.
public static void benchSecp256k1Recover() {
final SignatureAlgorithm signatureAlgorithm = SignatureAlgorithmFactory.getInstance();
final SECPPrivateKey privateKey = signatureAlgorithm.createPrivateKey(new BigInteger("c85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aaf4", 16));
final KeyPair keyPair = signatureAlgorithm.createKeyPair(privateKey);
final Bytes data = Bytes.wrap("This is an example of a signed message.".getBytes(UTF_8));
final Bytes32 dataHash = keccak256(data);
final SECPSignature signature = signatureAlgorithm.sign(dataHash, keyPair);
for (int i = 0; i < MATH_WARMUP; i++) {
signatureAlgorithm.recoverPublicKeyFromSignature(dataHash, signature);
}
final Stopwatch timer = Stopwatch.createStarted();
for (int i = 0; i < MATH_ITERATIONS; i++) {
signatureAlgorithm.recoverPublicKeyFromSignature(dataHash, signature);
}
timer.stop();
final double elapsed = timer.elapsed(TimeUnit.NANOSECONDS) / 1.0e9D;
final double perCall = elapsed / MATH_ITERATIONS;
final double gasSpent = perCall * GAS_PER_SECOND_STANDARD;
System.out.printf("secp256k1 signature recovery for %,d gas.%n", (int) gasSpent);
}
use of org.hyperledger.besu.crypto.KeyPair in project besu by hyperledger.
the class EncryptedMessageTest method eip8RoundTrip.
@Test
public void eip8RoundTrip() throws InvalidCipherTextException {
final KeyPair keyPair = SignatureAlgorithmFactory.getInstance().generateKeyPair();
final byte[] message = new byte[288];
ThreadLocalRandom.current().nextBytes(message);
final Bytes initial = Bytes.wrap(message);
final Bytes encrypted = EncryptedMessage.encryptMsgEip8(initial, keyPair.getPublicKey());
final Bytes decrypted = EncryptedMessage.decryptMsgEIP8(encrypted, NodeKeyUtils.createFrom(keyPair));
Assertions.assertThat(decrypted.slice(0, 288)).isEqualTo(initial);
}
use of org.hyperledger.besu.crypto.KeyPair in project besu by hyperledger.
the class TransactionPoolPropagationTest method shouldPropagateLocalAndRemoteTransactions.
/**
* Simulate a 4-node cluster. Send at least 1 Tx to each node, and multiple Tx to at least one
* node. Verify that all nodes get the correct number of pending transactions.
*/
@Test
void shouldPropagateLocalAndRemoteTransactions() throws Exception {
try (final TestNodeList nodes = new TestNodeList()) {
// Create & Start Nodes
final TestNode node1 = nodes.create(vertx, null, null, noDiscovery);
final TestNode node2 = nodes.create(vertx, null, null, noDiscovery);
final TestNode node3 = nodes.create(vertx, null, null, noDiscovery);
final TestNode node4 = nodes.create(vertx, null, null, noDiscovery);
final SignatureAlgorithm signatureAlgorithm = SignatureAlgorithmFactory.getInstance();
final KeyPair keyPair = signatureAlgorithm.createKeyPair(signatureAlgorithm.createPrivateKey(Bytes32.fromHexString("8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63")));
final TransactionTestFixture transactionBuilder = new TransactionTestFixture();
transactionBuilder.gasLimit(1_000_000);
final Transaction transaction1 = transactionBuilder.nonce(0).createTransaction(keyPair);
final Transaction transaction2 = transactionBuilder.nonce(1).createTransaction(keyPair);
final Transaction transaction3 = transactionBuilder.nonce(2).createTransaction(keyPair);
final Transaction transaction4 = transactionBuilder.nonce(3).createTransaction(keyPair);
final Transaction transaction5 = transactionBuilder.nonce(4).createTransaction(keyPair);
initTest(nodes);
node1.receiveRemoteTransaction(transaction1);
waitForPendingTransactionCounts(nodes, 1);
node2.receiveRemoteTransaction(transaction2);
waitForPendingTransactionCounts(nodes, 2);
node3.receiveRemoteTransaction(transaction3);
waitForPendingTransactionCounts(nodes, 3);
node4.receiveRemoteTransaction(transaction4);
waitForPendingTransactionCounts(nodes, 4);
node3.receiveLocalTransaction(transaction5);
waitForPendingTransactionCounts(nodes, 5);
}
}
Aggregations