Search in sources :

Example 1 with SECPSignature

use of org.hyperledger.besu.crypto.SECPSignature 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 SECPSignature

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

the class CommitValidatorTest method commitForWrongSequenceFails.

@Test
public void commitForWrongSequenceFails() {
    final SECPSignature commitSeal = validators.getNode(0).getNodeKey().sign(expectedHash);
    final Commit msg = validators.getMessageFactory(0).createCommit(ConsensusRoundHelpers.createFrom(round, +1, 0), expectedHash, commitSeal);
    assertThat(validator.validate(msg)).isFalse();
}
Also used : SECPSignature(org.hyperledger.besu.crypto.SECPSignature) Commit(org.hyperledger.besu.consensus.qbft.messagewrappers.Commit) Test(org.junit.Test)

Example 3 with SECPSignature

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

the class CommitValidatorTest method commitSignedByANonValidatorFails.

@Test
public void commitSignedByANonValidatorFails() {
    final QbftNode nonValidator = QbftNode.create();
    final SECPSignature commitSeal = nonValidator.getNodeKey().sign(expectedHash);
    final Commit msg = nonValidator.getMessageFactory().createCommit(round, expectedHash, commitSeal);
    assertThat(validator.validate(msg)).isFalse();
}
Also used : SECPSignature(org.hyperledger.besu.crypto.SECPSignature) Commit(org.hyperledger.besu.consensus.qbft.messagewrappers.Commit) Test(org.junit.Test)

Example 4 with SECPSignature

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

the class CommitValidatorTest method commitWithWrongDigestFails.

@Test
public void commitWithWrongDigestFails() {
    final SECPSignature commitSeal = validators.getNode(0).getNodeKey().sign(expectedHash);
    final Commit msg = validators.getMessageFactory(0).createCommit(round, Hash.fromHexStringLenient("0x2"), commitSeal);
    assertThat(validator.validate(msg)).isFalse();
}
Also used : SECPSignature(org.hyperledger.besu.crypto.SECPSignature) Commit(org.hyperledger.besu.consensus.qbft.messagewrappers.Commit) Test(org.junit.Test)

Example 5 with SECPSignature

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

the class PkiQbftExtraDataCodecTest method getDecodedExtraData.

private static PkiQbftExtraData getDecodedExtraData(final Bytes cms) {
    final List<Address> validators = Arrays.asList(Address.fromHexString("1"), Address.fromHexString("2"));
    final Optional<Vote> vote = Optional.of(Vote.authVote(Address.fromHexString("1")));
    final int round = 0x00FEDCBA;
    final List<SECPSignature> committerSeals = Arrays.asList(SIGNATURE_ALGORITHM.get().createSignature(BigInteger.ONE, BigInteger.TEN, (byte) 0), SIGNATURE_ALGORITHM.get().createSignature(BigInteger.TEN, BigInteger.ONE, (byte) 0));
    // Create a byte buffer with no data.
    final byte[] vanity_bytes = createNonEmptyVanityData();
    final Bytes vanity_data = Bytes.wrap(vanity_bytes);
    return new PkiQbftExtraData(vanity_data, committerSeals, vote, round, validators, cms);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) Vote(org.hyperledger.besu.consensus.common.bft.Vote) SECPSignature(org.hyperledger.besu.crypto.SECPSignature) Address(org.hyperledger.besu.datatypes.Address)

Aggregations

SECPSignature (org.hyperledger.besu.crypto.SECPSignature)88 Test (org.junit.Test)48 Bytes (org.apache.tuweni.bytes.Bytes)47 Address (org.hyperledger.besu.datatypes.Address)44 BytesValueRLPOutput (org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput)30 BftExtraData (org.hyperledger.besu.consensus.common.bft.BftExtraData)26 Vote (org.hyperledger.besu.consensus.common.bft.Vote)22 Hash (org.hyperledger.besu.datatypes.Hash)14 Block (org.hyperledger.besu.ethereum.core.Block)11 SignatureAlgorithm (org.hyperledger.besu.crypto.SignatureAlgorithm)9 RLPInput (org.hyperledger.besu.ethereum.rlp.RLPInput)9 Commit (org.hyperledger.besu.consensus.qbft.messagewrappers.Commit)8 BigInteger (java.math.BigInteger)7 Bytes32 (org.apache.tuweni.bytes.Bytes32)7 BftBlockHashing (org.hyperledger.besu.consensus.common.bft.BftBlockHashing)7 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)6 SECPPublicKey (org.hyperledger.besu.crypto.SECPPublicKey)5 List (java.util.List)4 Optional (java.util.Optional)4 Random (java.util.Random)4