Search in sources :

Example 6 with ECDSASignature

use of com.radixdlt.crypto.ECDSASignature in project radixdlt by radixdlt.

the class SafetyRules method createVote.

public Vote createVote(VerifiedVertex proposedVertex, BFTHeader proposedHeader, long timestamp, HighQC highQC) {
    final VoteData voteData = constructVoteData(proposedVertex, proposedHeader);
    final var voteHash = Vote.getHashOfData(hasher, voteData, timestamp);
    // TODO make signing more robust by including author in signed hash
    final ECDSASignature signature = this.signer.sign(voteHash);
    return new Vote(this.self, voteData, timestamp, signature, highQC, Optional.empty());
}
Also used : Vote(com.radixdlt.hotstuff.Vote) ECDSASignature(com.radixdlt.crypto.ECDSASignature) VoteData(com.radixdlt.hotstuff.VoteData)

Example 7 with ECDSASignature

use of com.radixdlt.crypto.ECDSASignature in project radixdlt by radixdlt.

the class REParser method parse.

public ParsedTxn parse(Txn txn) throws TxnParseException {
    UInt256 feePaid = null;
    ECDSASignature sig = null;
    int sigPosition = 0;
    var parserState = new ParserState(txn);
    var buf = ByteBuffer.wrap(txn.getPayload());
    while (buf.hasRemaining()) {
        if (sig != null) {
            throw new TxnParseException(parserState, "Signature must be last");
        }
        var curPos = buf.position();
        parserState.pos(curPos);
        final var inst = readInstruction(parserState, buf);
        parserState.nextInstruction(inst);
        if (inst.isStateUpdate()) {
            parserState.substateUpdate(inst.getMicroOp().getOp());
        } else if (inst.getMicroOp().getOp() == REOp.READ || inst.getMicroOp().getOp() == REOp.READINDEX) {
            parserState.read();
        } else if (inst.getMicroOp() == REInstruction.REMicroOp.HEADER) {
            parserState.header(inst.getData());
        } else if (inst.getMicroOp() == REInstruction.REMicroOp.SYSCALL) {
            try {
                var callData = inst.<CallData>getData();
                byte id = callData.get(0);
                var syscall = Syscall.of(id).orElseThrow(() -> new TxnParseException(parserState, "Invalid call data type: " + id));
                switch(syscall) {
                    case FEE_RESERVE_PUT:
                        if (feePaid != null) {
                            throw new TxnParseException(parserState, "Should only pay fees once.");
                        }
                        feePaid = callData.getUInt256(1);
                        break;
                    case FEE_RESERVE_TAKE:
                        if (feePaid == null) {
                            throw new TxnParseException(parserState, "No fees paid");
                        }
                        var takeAmount = callData.getUInt256(1);
                        if (takeAmount.compareTo(feePaid) > 0) {
                            throw new TxnParseException(parserState, "Trying to take more fees than paid");
                        }
                        feePaid = feePaid.subtract(takeAmount);
                        break;
                    case READDR_CLAIM:
                        break;
                    // TODO: Along with FeeConstraintScrypt.java
                    default:
                        throw new TxnParseException(parserState, "Invalid call data type: " + id);
                }
            } catch (CallDataAccessException | TrailingBytesException e) {
                throw new TxnParseException(parserState, e);
            }
        } else if (inst.getMicroOp() == REInstruction.REMicroOp.MSG) {
            parserState.msg(inst.getData());
        } else if (inst.getMicroOp() == REInstruction.REMicroOp.END) {
            parserState.end();
        } else if (inst.getMicroOp() == REInstruction.REMicroOp.SIG) {
            sigPosition = curPos;
            sig = inst.getData();
        } else {
            throw new TxnParseException(parserState, "Unknown CM Op " + inst.getMicroOp());
        }
    }
    parserState.finish();
    return new ParsedTxn(txn, feePaid, parserState.instructions, parserState.msg, sig == null ? null : Pair.of(calculatePayloadHash(txn, sigPosition), sig), parserState.disableResourceAllocAndDestroy);
}
Also used : ECDSASignature(com.radixdlt.crypto.ECDSASignature) TxnParseException(com.radixdlt.engine.parser.exceptions.TxnParseException) UInt256(com.radixdlt.utils.UInt256)

Example 8 with ECDSASignature

use of com.radixdlt.crypto.ECDSASignature in project radixdlt by radixdlt.

the class KeyVerifyTestScenarioRunner method doRunTestVector.

@Override
public void doRunTestVector(KeyVerifyTestVector testVector) throws AssertionError {
    ECPublicKey publicKey = null;
    try {
        publicKey = ECPublicKey.fromBytes(fromHexString(testVector.input.publicKeyUncompressed));
    } catch (Exception e) {
        throw new AssertionError("Failed to construct public key from hex", e);
    }
    ECDSASignature signature = ECDSASignature.decodeFromDER(fromHexString(testVector.input.signatureDerEncoded));
    byte[] hashedMessageToVerify = sha256Hash(fromHexString(testVector.input.msg));
    assertEquals(testVector.expected.isValid, publicKey.verify(hashedMessageToVerify, signature));
}
Also used : ECPublicKey(com.radixdlt.crypto.ECPublicKey) ECDSASignature(com.radixdlt.crypto.ECDSASignature)

Example 9 with ECDSASignature

use of com.radixdlt.crypto.ECDSASignature in project radixdlt by radixdlt.

the class KeySignTestScenarioRunner method doRunTestVector.

@Override
public void doRunTestVector(KeySignTestVector testVector) throws AssertionError {
    ECKeyPair keyPair = null;
    try {
        keyPair = ECKeyPair.fromPrivateKey(Bytes.fromHexString(testVector.input.privateKey));
    } catch (Exception e) {
        throw new AssertionError("Failed to construct private key from hex", e);
    }
    byte[] unhashedEncodedMessage = testVector.input.messageToSign.getBytes(StandardCharsets.UTF_8);
    byte[] hashedMessageToSign = sha256Hash(unhashedEncodedMessage);
    ECDSASignature signature = keyPair.sign(hashedMessageToSign, true, true);
    assertEquals(testVector.expected.signature.r, signature.getR().toString(16));
    assertEquals(testVector.expected.signature.s, signature.getS().toString(16));
}
Also used : ECKeyPair(com.radixdlt.crypto.ECKeyPair) ECDSASignature(com.radixdlt.crypto.ECDSASignature)

Example 10 with ECDSASignature

use of com.radixdlt.crypto.ECDSASignature in project radixdlt by radixdlt.

the class SafetyRules method signProposal.

/**
 * Create a signed proposal from a vertex
 *
 * @param proposedVertex vertex to sign
 * @param highestCommittedQC highest known committed QC
 * @param highestTC highest known TC
 * @return signed proposal object for consensus
 */
public Optional<Proposal> signProposal(VerifiedVertex proposedVertex, QuorumCertificate highestCommittedQC, Optional<TimeoutCertificate> highestTC) {
    final Builder safetyStateBuilder = this.state.toBuilder();
    if (!checkLocked(proposedVertex, safetyStateBuilder)) {
        return Optional.empty();
    }
    this.state = safetyStateBuilder.build();
    final ECDSASignature signature = this.signer.sign(proposedVertex.getId());
    return Optional.of(new Proposal(proposedVertex.toSerializable(), highestCommittedQC, signature, highestTC));
}
Also used : Builder(com.radixdlt.hotstuff.safety.SafetyState.Builder) ECDSASignature(com.radixdlt.crypto.ECDSASignature) Proposal(com.radixdlt.hotstuff.Proposal)

Aggregations

ECDSASignature (com.radixdlt.crypto.ECDSASignature)17 HashCode (com.google.common.hash.HashCode)4 Vote (com.radixdlt.consensus.Vote)4 Vote (com.radixdlt.hotstuff.Vote)4 Test (org.junit.Test)4 VoteTimeout (com.radixdlt.consensus.liveness.VoteTimeout)2 Proposal (com.radixdlt.hotstuff.Proposal)2 TimestampedECDSASignature (com.radixdlt.hotstuff.TimestampedECDSASignature)2 VoteTimeout (com.radixdlt.hotstuff.liveness.VoteTimeout)2 Proposal (com.radixdlt.consensus.Proposal)1 VoteData (com.radixdlt.consensus.VoteData)1 BFTNode (com.radixdlt.consensus.bft.BFTNode)1 ValidationState (com.radixdlt.consensus.bft.ValidationState)1 Builder (com.radixdlt.consensus.safety.SafetyState.Builder)1 ECKeyPair (com.radixdlt.crypto.ECKeyPair)1 ECPublicKey (com.radixdlt.crypto.ECPublicKey)1 TxnParseException (com.radixdlt.engine.parser.exceptions.TxnParseException)1 QuorumCertificate (com.radixdlt.hotstuff.QuorumCertificate)1 UnverifiedVertex (com.radixdlt.hotstuff.UnverifiedVertex)1 VoteData (com.radixdlt.hotstuff.VoteData)1