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;
}
}
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();
}
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();
}
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();
}
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);
}
Aggregations