Search in sources :

Example 1 with SignatureAlgorithm

use of org.hyperledger.besu.crypto.SignatureAlgorithm in project besu by hyperledger.

the class ECRECPrecompiledContract method compute.

@Override
public Bytes compute(final Bytes input, final MessageFrame messageFrame) {
    final int size = input.size();
    final Bytes d = size >= 128 ? input : Bytes.wrap(input, MutableBytes.create(128 - size));
    final Bytes32 h = Bytes32.wrap(d, 0);
    // to check the rest of the bytes are zero though.
    if (!d.slice(32, 31).isZero()) {
        return Bytes.EMPTY;
    }
    final int recId = d.get(63) - V_BASE;
    final BigInteger r = d.slice(64, 32).toUnsignedBigInteger();
    final BigInteger s = d.slice(96, 32).toUnsignedBigInteger();
    final SignatureAlgorithm signatureAlgorithm = SignatureAlgorithmFactory.getInstance();
    final SECPSignature signature;
    try {
        signature = signatureAlgorithm.createSignature(r, s, (byte) recId);
    } catch (final IllegalArgumentException e) {
        return Bytes.EMPTY;
    }
    // the library needs to be updated.
    try {
        final Optional<SECPPublicKey> recovered = signatureAlgorithm.recoverPublicKeyFromSignature(h, signature);
        if (!recovered.isPresent()) {
            return Bytes.EMPTY;
        }
        final Bytes32 hashed = Hash.keccak256(recovered.get().getEncodedBytes());
        final MutableBytes32 result = MutableBytes32.create();
        hashed.slice(12).copyTo(result, 12);
        return result;
    } catch (final IllegalArgumentException e) {
        return Bytes.EMPTY;
    }
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) MutableBytes(org.apache.tuweni.bytes.MutableBytes) SECPSignature(org.hyperledger.besu.crypto.SECPSignature) BigInteger(java.math.BigInteger) SignatureAlgorithm(org.hyperledger.besu.crypto.SignatureAlgorithm) MutableBytes32(org.apache.tuweni.bytes.MutableBytes32) Bytes32(org.apache.tuweni.bytes.Bytes32) SECPPublicKey(org.hyperledger.besu.crypto.SECPPublicKey) MutableBytes32(org.apache.tuweni.bytes.MutableBytes32)

Example 2 with SignatureAlgorithm

use of org.hyperledger.besu.crypto.SignatureAlgorithm in project besu by hyperledger.

the class TestSigningPrivateMarkerTransactionFactory method setSigningKeyEnbaled.

public void setSigningKeyEnbaled(final String privateMarkerTransactionSigningKey) {
    final SignatureAlgorithm algorithm = SignatureAlgorithmFactory.getInstance();
    final SECPPrivateKey privateKey = algorithm.createPrivateKey(Bytes32.fromHexString(privateMarkerTransactionSigningKey));
    aliceFixedSigningKey = algorithm.createKeyPair(privateKey);
    sender = extract(Hash.hash(aliceFixedSigningKey.getPublicKey().getEncodedBytes()));
}
Also used : SECPPrivateKey(org.hyperledger.besu.crypto.SECPPrivateKey) SignatureAlgorithm(org.hyperledger.besu.crypto.SignatureAlgorithm)

Example 3 with SignatureAlgorithm

use of org.hyperledger.besu.crypto.SignatureAlgorithm in project besu by hyperledger.

the class RoundStateTest method commitSealsAreExtractedFromReceivedMessages.

@Test
public void commitSealsAreExtractedFromReceivedMessages() {
    when(messageValidator.validateProposal(any())).thenReturn(true);
    when(messageValidator.validateCommit(any())).thenReturn(true);
    final RoundState roundState = new RoundState(roundIdentifier, 2, messageValidator);
    final SignatureAlgorithm signatureAlgorithm = SignatureAlgorithmFactory.getInstance();
    final Commit firstCommit = validatorMessageFactories.get(1).createCommit(roundIdentifier, block.getHash(), signatureAlgorithm.createSignature(BigInteger.ONE, BigInteger.TEN, (byte) 1));
    final Commit secondCommit = validatorMessageFactories.get(2).createCommit(roundIdentifier, block.getHash(), signatureAlgorithm.createSignature(BigInteger.TEN, BigInteger.TEN, (byte) 1));
    final Proposal proposal = validatorMessageFactories.get(0).createProposal(roundIdentifier, block, Optional.empty());
    roundState.setProposedBlock(proposal);
    roundState.addCommitMessage(firstCommit);
    assertThat(roundState.isCommitted()).isFalse();
    roundState.addCommitMessage(secondCommit);
    assertThat(roundState.isCommitted()).isTrue();
    assertThat(roundState.getCommitSeals()).containsOnly(firstCommit.getCommitSeal(), secondCommit.getCommitSeal());
}
Also used : Commit(org.hyperledger.besu.consensus.ibft.messagewrappers.Commit) SignatureAlgorithm(org.hyperledger.besu.crypto.SignatureAlgorithm) Proposal(org.hyperledger.besu.consensus.ibft.messagewrappers.Proposal) Test(org.junit.Test)

Example 4 with SignatureAlgorithm

use of org.hyperledger.besu.crypto.SignatureAlgorithm in project besu by hyperledger.

the class ECRECPrecompiledContract method computePrecompile.

@Nonnull
@Override
public PrecompileContractResult computePrecompile(final Bytes input, @Nonnull final MessageFrame messageFrame) {
    final int size = input.size();
    final Bytes d = size >= 128 ? input : Bytes.wrap(input, MutableBytes.create(128 - size));
    final Bytes32 h = Bytes32.wrap(d, 0);
    // to check the rest of the bytes are zero though.
    if (!d.slice(32, 31).isZero()) {
        return PrecompileContractResult.success(Bytes.EMPTY);
    }
    final int recId = d.get(63) - V_BASE;
    final BigInteger r = d.slice(64, 32).toUnsignedBigInteger();
    final BigInteger s = d.slice(96, 32).toUnsignedBigInteger();
    final SignatureAlgorithm signatureAlgorithm = SignatureAlgorithmFactory.getInstance();
    final SECPSignature signature;
    try {
        signature = signatureAlgorithm.createSignature(r, s, (byte) recId);
    } catch (final IllegalArgumentException e) {
        return PrecompileContractResult.success(Bytes.EMPTY);
    }
    // the library needs to be updated.
    try {
        final Optional<SECPPublicKey> recovered = signatureAlgorithm.recoverPublicKeyFromSignature(h, signature);
        if (recovered.isEmpty()) {
            return PrecompileContractResult.success(Bytes.EMPTY);
        }
        final Bytes32 hashed = Hash.keccak256(recovered.get().getEncodedBytes());
        final MutableBytes32 result = MutableBytes32.create();
        hashed.slice(12).copyTo(result, 12);
        return PrecompileContractResult.success(result);
    } catch (final IllegalArgumentException e) {
        return PrecompileContractResult.success(Bytes.EMPTY);
    }
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) MutableBytes(org.apache.tuweni.bytes.MutableBytes) SECPSignature(org.hyperledger.besu.crypto.SECPSignature) BigInteger(java.math.BigInteger) SignatureAlgorithm(org.hyperledger.besu.crypto.SignatureAlgorithm) MutableBytes32(org.apache.tuweni.bytes.MutableBytes32) Bytes32(org.apache.tuweni.bytes.Bytes32) SECPPublicKey(org.hyperledger.besu.crypto.SECPPublicKey) MutableBytes32(org.apache.tuweni.bytes.MutableBytes32) Nonnull(javax.annotation.Nonnull)

Example 5 with SignatureAlgorithm

use of org.hyperledger.besu.crypto.SignatureAlgorithm in project besu by hyperledger.

the class CommandTestAbstract method initMocks.

@Before
public void initMocks() throws Exception {
    // doReturn used because of generic BesuController
    doReturn(mockControllerBuilder).when(mockControllerBuilderFactory).fromEthNetworkConfig(any(), any());
    when(mockControllerBuilder.synchronizerConfiguration(any())).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.ethProtocolConfiguration(any())).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.transactionPoolConfiguration(any())).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.dataDirectory(any())).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.miningParameters(any())).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.nodeKey(any())).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.metricsSystem(any())).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.messagePermissioningProviders(any())).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.privacyParameters(any())).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.pkiBlockCreationConfiguration(any())).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.clock(any())).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.isRevertReasonEnabled(false)).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.storageProvider(any())).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.isPruningEnabled(anyBoolean())).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.pruningConfiguration(any())).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.genesisConfigOverrides(any())).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.gasLimitCalculator(any())).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.requiredBlocks(any())).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.reorgLoggingThreshold(anyLong())).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.dataStorageConfiguration(any())).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.evmConfiguration(any())).thenReturn(mockControllerBuilder);
    when(mockControllerBuilder.maxPeers(anyInt())).thenReturn(mockControllerBuilder);
    // doReturn used because of generic BesuController
    doReturn(mockController).when(mockControllerBuilder).build();
    lenient().when(mockController.getProtocolManager()).thenReturn(mockEthProtocolManager);
    lenient().when(mockController.getProtocolSchedule()).thenReturn(mockProtocolSchedule);
    lenient().when(mockController.getProtocolContext()).thenReturn(mockProtocolContext);
    lenient().when(mockController.getAdditionalPluginServices()).thenReturn(new NoopPluginServiceFactory());
    lenient().when(mockController.getNodeKey()).thenReturn(nodeKey);
    when(mockEthProtocolManager.getBlockBroadcaster()).thenReturn(mockBlockBroadcaster);
    when(mockProtocolContext.getBlockchain()).thenReturn(mockMutableBlockchain);
    lenient().when(mockProtocolContext.getWorldStateArchive()).thenReturn(mockWorldStateArchive);
    when(mockController.getTransactionPool()).thenReturn(mockTransactionPool);
    when(mockRunnerBuilder.vertx(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.besuController(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.discovery(anyBoolean())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.ethNetworkConfig(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.networkingConfiguration(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.p2pAdvertisedHost(anyString())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.p2pListenPort(anyInt())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.p2pListenInterface(anyString())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.permissioningConfiguration(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.maxPeers(anyInt())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.limitRemoteWireConnectionsEnabled(anyBoolean())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.fractionRemoteConnectionsAllowed(anyFloat())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.randomPeerPriority(anyBoolean())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.p2pEnabled(anyBoolean())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.natMethod(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.natManagerServiceName(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.natMethodFallbackEnabled(anyBoolean())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.jsonRpcConfiguration(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.engineJsonRpcConfiguration(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.graphQLConfiguration(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.webSocketConfiguration(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.jsonRpcIpcConfiguration(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.apiConfiguration(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.dataDir(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.bannedNodeIds(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.metricsSystem(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.permissioningService(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.metricsConfiguration(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.staticNodes(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.identityString(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.besuPluginContext(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.autoLogBloomCaching(anyBoolean())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.pidPath(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.ethstatsUrl(anyString())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.ethstatsContact(anyString())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.storageProvider(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.forkIdSupplier(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.rpcEndpointService(any())).thenReturn(mockRunnerBuilder);
    when(mockRunnerBuilder.build()).thenReturn(mockRunner);
    final SignatureAlgorithm signatureAlgorithm = SignatureAlgorithmFactory.getInstance();
    final Bytes32 keyPairPrvKey = Bytes32.fromHexString("0xf7a58d5e755d51fa2f6206e91dd574597c73248aaf946ec1964b8c6268d6207b");
    keyPair = signatureAlgorithm.createKeyPair(signatureAlgorithm.createPrivateKey(keyPairPrvKey));
    lenient().when(nodeKey.getPublicKey()).thenReturn(keyPair.getPublicKey());
    lenient().when(storageService.getByName(eq("rocksdb"))).thenReturn(Optional.of(rocksDBStorageFactory));
    lenient().when(storageService.getByName(eq("rocksdb-privacy"))).thenReturn(Optional.of(rocksDBSPrivacyStorageFactory));
    lenient().when(securityModuleService.getByName(eq("localfile"))).thenReturn(Optional.of(() -> securityModule));
    lenient().when(rocksDBSPrivacyStorageFactory.create(any(), any(), any())).thenReturn(new InMemoryKeyValueStorage());
    lenient().when(mockBesuPluginContext.getService(PicoCLIOptions.class)).thenReturn(Optional.of(cliOptions));
    lenient().when(mockBesuPluginContext.getService(StorageService.class)).thenReturn(Optional.of(storageService));
    lenient().doReturn(mockPkiBlockCreationConfiguration).when(mockPkiBlockCreationConfigProvider).load(pkiKeyStoreConfigurationArgumentCaptor.capture());
}
Also used : InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) SignatureAlgorithm(org.hyperledger.besu.crypto.SignatureAlgorithm) NoopPluginServiceFactory(org.hyperledger.besu.controller.NoopPluginServiceFactory) Bytes32(org.apache.tuweni.bytes.Bytes32) Before(org.junit.Before)

Aggregations

SignatureAlgorithm (org.hyperledger.besu.crypto.SignatureAlgorithm)9 Bytes32 (org.apache.tuweni.bytes.Bytes32)4 SECPSignature (org.hyperledger.besu.crypto.SECPSignature)4 BigInteger (java.math.BigInteger)3 Bytes (org.apache.tuweni.bytes.Bytes)3 KeyPair (org.hyperledger.besu.crypto.KeyPair)3 MutableBytes (org.apache.tuweni.bytes.MutableBytes)2 MutableBytes32 (org.apache.tuweni.bytes.MutableBytes32)2 SECPPrivateKey (org.hyperledger.besu.crypto.SECPPrivateKey)2 SECPPublicKey (org.hyperledger.besu.crypto.SECPPublicKey)2 Transaction (org.hyperledger.besu.ethereum.core.Transaction)2 Stopwatch (com.google.common.base.Stopwatch)1 Nonnull (javax.annotation.Nonnull)1 Commit (org.hyperledger.besu.consensus.ibft.messagewrappers.Commit)1 Proposal (org.hyperledger.besu.consensus.ibft.messagewrappers.Proposal)1 NoopPluginServiceFactory (org.hyperledger.besu.controller.NoopPluginServiceFactory)1 TransactionTestFixture (org.hyperledger.besu.ethereum.core.TransactionTestFixture)1 InMemoryKeyValueStorage (org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage)1 Before (org.junit.Before)1 Test (org.junit.Test)1