Search in sources :

Example 6 with ECDSASignature

use of org.ethereum.crypto.signature.ECDSASignature in project rskj by rsksmart.

the class ECKeyTest method testEthereumSign.

@Test
public void testEthereumSign() throws IOException {
    // TODO: Understand why key must be decompressed for this to work
    ECKey key = ECKey.fromPrivate(privateKey).decompress();
    System.out.println("Secret\t: " + ByteUtil.toHexString(key.getPrivKeyBytes()));
    System.out.println("Pubkey\t: " + ByteUtil.toHexString(key.getPubKey()));
    System.out.println("Data\t: " + exampleMessage);
    byte[] messageHash = HashUtil.keccak256(exampleMessage.getBytes());
    ECDSASignature signature = ECDSASignature.fromSignature(key.sign(messageHash));
    assertEquals(r, signature.getR());
    assertEquals(s, signature.getS());
    assertEquals(v, signature.getV());
}
Also used : ECDSASignature(org.ethereum.crypto.signature.ECDSASignature) Test(org.junit.Test)

Example 7 with ECDSASignature

use of org.ethereum.crypto.signature.ECDSASignature in project rskj by rsksmart.

the class ProofOfWorkRule method validFallbackBlockSignature.

private static boolean validFallbackBlockSignature(Constants constants, BlockHeader header, byte[] signatureBytesRLP) {
    byte[] fallbackMiningPubKeyBytes;
    boolean isEvenBlockNumber = header.getNumber() % 2 == 0;
    if (isEvenBlockNumber) {
        fallbackMiningPubKeyBytes = constants.getFallbackMiningPubKey0();
    } else {
        fallbackMiningPubKeyBytes = constants.getFallbackMiningPubKey1();
    }
    ECKey fallbackMiningPubKey = ECKey.fromPublicOnly(fallbackMiningPubKeyBytes);
    List<RLPElement> signatureRlpElements = RLP.decode2(signatureBytesRLP);
    if (signatureRlpElements.size() != 1) {
        return false;
    }
    RLPList signatureRLP = (RLPList) signatureRlpElements.get(0);
    if (signatureRLP.size() != 3) {
        return false;
    }
    byte[] v = signatureRLP.get(0).getRLPData();
    byte[] r = signatureRLP.get(1).getRLPData();
    byte[] s = signatureRLP.get(2).getRLPData();
    if (v == null || v.length != 1) {
        return false;
    }
    ECDSASignature signature = ECDSASignature.fromComponents(r, s, v[0]);
    if (!Arrays.equals(r, signature.getR().toByteArray())) {
        return false;
    }
    if (!Arrays.equals(s, signature.getS().toByteArray())) {
        return false;
    }
    if (signature.getV() > 31 || signature.getV() < 27) {
        return false;
    }
    if (signature.getS().compareTo(SECP256K1N_HALF) >= 0) {
        return false;
    }
    ECKey pub = Secp256k1.getInstance().recoverFromSignature(signature.getV() - 27, signature, header.getHashForMergedMining(), false);
    return pub.getPubKeyPoint().equals(fallbackMiningPubKey.getPubKeyPoint());
}
Also used : RLPElement(org.ethereum.util.RLPElement) ECDSASignature(org.ethereum.crypto.signature.ECDSASignature) ECKey(org.ethereum.crypto.ECKey) RLPList(org.ethereum.util.RLPList)

Example 8 with ECDSASignature

use of org.ethereum.crypto.signature.ECDSASignature in project rskj by rsksmart.

the class BootstrapDataVerifier method verifyEntries.

public int verifyEntries(Map<String, BootstrapDataEntry> selectedEntries) {
    int verifications = 0;
    if (selectedEntries.isEmpty()) {
        return 0;
    }
    String hashToVerify = selectedEntries.values().iterator().next().getHash();
    byte[] dbHash = Hex.decode(hashToVerify);
    for (Map.Entry<String, BootstrapDataEntry> entry : selectedEntries.entrySet()) {
        BootstrapDataEntry bde = entry.getValue();
        String currentHash = bde.getHash();
        if (!hashToVerify.equals(currentHash)) {
            throw new BootstrapImportException(String.format("Error trying to verify different hashes: %s vs %s", hashToVerify, currentHash));
        }
        BootstrapDataSignature bds = bde.getSig();
        // to use the public key we need to have an extra byte according to x9.62 declaring
        // which format is using. The current format from signer is uncompressed
        byte[] publicKey = Hex.decode(entry.getKey());
        // 1 is for forcing to interpret the values as unsigned integers
        BigInteger r = new BigInteger(1, Hex.decode(bds.getR()));
        BigInteger s = new BigInteger(1, Hex.decode(bds.getS()));
        ECDSASignature signature = new ECDSASignature(r, s);
        if (Secp256k1.getInstance().verify(dbHash, signature, publicKey)) {
            verifications++;
        }
    }
    return verifications;
}
Also used : BootstrapDataSignature(co.rsk.db.importer.provider.index.data.BootstrapDataSignature) BootstrapDataEntry(co.rsk.db.importer.provider.index.data.BootstrapDataEntry) BootstrapImportException(co.rsk.db.importer.BootstrapImportException) BigInteger(java.math.BigInteger) ECDSASignature(org.ethereum.crypto.signature.ECDSASignature) Map(java.util.Map)

Aggregations

ECDSASignature (org.ethereum.crypto.signature.ECDSASignature)8 Test (org.junit.Test)4 BigInteger (java.math.BigInteger)2 ECKey (org.ethereum.crypto.ECKey)2 BootstrapImportException (co.rsk.db.importer.BootstrapImportException)1 BootstrapDataEntry (co.rsk.db.importer.provider.index.data.BootstrapDataEntry)1 BootstrapDataSignature (co.rsk.db.importer.provider.index.data.BootstrapDataSignature)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)1 Map (java.util.Map)1 Callable (java.util.concurrent.Callable)1 RLPElement (org.ethereum.util.RLPElement)1 RLPList (org.ethereum.util.RLPList)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1