Search in sources :

Example 31 with KeyPair

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);
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) SignatureAlgorithm(org.hyperledger.besu.crypto.SignatureAlgorithm)

Example 32 with KeyPair

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);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) KeyPair(org.hyperledger.besu.crypto.KeyPair) SECPSignature(org.hyperledger.besu.crypto.SECPSignature) SECPPrivateKey(org.hyperledger.besu.crypto.SECPPrivateKey) Stopwatch(com.google.common.base.Stopwatch) BigInteger(java.math.BigInteger) SignatureAlgorithm(org.hyperledger.besu.crypto.SignatureAlgorithm) Bytes32(org.apache.tuweni.bytes.Bytes32)

Example 33 with KeyPair

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);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) KeyPair(org.hyperledger.besu.crypto.KeyPair) Test(org.junit.Test)

Example 34 with KeyPair

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);
    }
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) TransactionTestFixture(org.hyperledger.besu.ethereum.core.TransactionTestFixture) Transaction(org.hyperledger.besu.ethereum.core.Transaction) SignatureAlgorithm(org.hyperledger.besu.crypto.SignatureAlgorithm) Test(org.junit.jupiter.api.Test)

Aggregations

KeyPair (org.hyperledger.besu.crypto.KeyPair)34 Test (org.junit.Test)21 Address (org.hyperledger.besu.datatypes.Address)14 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)13 BlockHeaderTestFixture (org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture)10 IbftExtraData (org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData)9 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)9 Transaction (org.hyperledger.besu.ethereum.core.Transaction)7 SignatureAlgorithm (org.hyperledger.besu.crypto.SignatureAlgorithm)5 ArrayList (java.util.ArrayList)4 SECPPrivateKey (org.hyperledger.besu.crypto.SECPPrivateKey)4 BigInteger (java.math.BigInteger)3 Bytes (org.apache.tuweni.bytes.Bytes)3 Path (java.nio.file.Path)2 Collection (java.util.Collection)2 Bytes32 (org.apache.tuweni.bytes.Bytes32)2 NodeRecord (org.ethereum.beacon.discovery.schema.NodeRecord)2 SECPSignature (org.hyperledger.besu.crypto.SECPSignature)2 Hash (org.hyperledger.besu.datatypes.Hash)2 BlockWithMetadata (org.hyperledger.besu.ethereum.api.query.BlockWithMetadata)2