diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2018-09-19 15:00:11 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-19 15:00:11 +0800 |
commit | 54fa224dbbf1b1c0f8d54a3f10a81adb321ce1e4 (patch) | |
tree | b0e503e08cc52dae2536ebef3dcd0110edd1b333 /core/crypto.go | |
parent | 8c33027b943e08de21b7bddb82fecc2b2a5664a2 (diff) | |
download | tangerine-consensus-54fa224dbbf1b1c0f8d54a3f10a81adb321ce1e4.tar tangerine-consensus-54fa224dbbf1b1c0f8d54a3f10a81adb321ce1e4.tar.gz tangerine-consensus-54fa224dbbf1b1c0f8d54a3f10a81adb321ce1e4.tar.bz2 tangerine-consensus-54fa224dbbf1b1c0f8d54a3f10a81adb321ce1e4.tar.lz tangerine-consensus-54fa224dbbf1b1c0f8d54a3f10a81adb321ce1e4.tar.xz tangerine-consensus-54fa224dbbf1b1c0f8d54a3f10a81adb321ce1e4.tar.zst tangerine-consensus-54fa224dbbf1b1c0f8d54a3f10a81adb321ce1e4.zip |
core:DKG and TSIG protocol (#115)
Diffstat (limited to 'core/crypto.go')
-rw-r--r-- | core/crypto.go | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/core/crypto.go b/core/crypto.go index 402fd2e..8fcb118 100644 --- a/core/crypto.go +++ b/core/crypto.go @@ -143,3 +143,102 @@ func hashPosition(position types.Position) common.Hash { binaryHeight, ) } + +func hashDKGPrivateShare(prvShare *types.DKGPrivateShare) common.Hash { + binaryRound := make([]byte, 8) + binary.LittleEndian.PutUint64(binaryRound, prvShare.Round) + + return crypto.Keccak256Hash( + prvShare.ProposerID.Hash[:], + binaryRound, + prvShare.PrivateShare.Bytes(), + ) +} + +func verifyDKGPrivateShareSignature( + prvShare *types.DKGPrivateShare, sigToPub SigToPubFn) (bool, error) { + hash := hashDKGPrivateShare(prvShare) + pubKey, err := sigToPub(hash, prvShare.Signature) + if err != nil { + return false, err + } + if prvShare.ProposerID != types.NewValidatorID(pubKey) { + return false, nil + } + return true, nil +} + +func hashDKGMasterPublicKey(mpk *types.DKGMasterPublicKey) common.Hash { + binaryRound := make([]byte, 8) + binary.LittleEndian.PutUint64(binaryRound, mpk.Round) + + return crypto.Keccak256Hash( + mpk.ProposerID.Hash[:], + mpk.DKGID.GetLittleEndian(), + mpk.PublicKeyShares.MasterKeyBytes(), + binaryRound, + ) +} + +func verifyDKGMasterPublicKeySignature( + mpk *types.DKGMasterPublicKey, sigToPub SigToPubFn) (bool, error) { + hash := hashDKGMasterPublicKey(mpk) + pubKey, err := sigToPub(hash, mpk.Signature) + if err != nil { + return false, err + } + if mpk.ProposerID != types.NewValidatorID(pubKey) { + return false, nil + } + return true, nil +} + +func hashDKGComplaint(complaint *types.DKGComplaint) common.Hash { + binaryRound := make([]byte, 8) + binary.LittleEndian.PutUint64(binaryRound, complaint.Round) + + hashPrvShare := hashDKGPrivateShare(&complaint.PrivateShare) + + return crypto.Keccak256Hash( + complaint.ProposerID.Hash[:], + binaryRound, + hashPrvShare[:], + ) +} + +func verifyDKGComplaintSignature( + complaint *types.DKGComplaint, sigToPub SigToPubFn) (bool, error) { + hash := hashDKGComplaint(complaint) + pubKey, err := sigToPub(hash, complaint.Signature) + if err != nil { + return false, err + } + if complaint.ProposerID != types.NewValidatorID(pubKey) { + return false, nil + } + return true, nil +} + +func hashDKGPartialSignature(psig *types.DKGPartialSignature) common.Hash { + binaryRound := make([]byte, 8) + binary.LittleEndian.PutUint64(binaryRound, psig.Round) + + return crypto.Keccak256Hash( + psig.ProposerID.Hash[:], + binaryRound, + psig.PartialSignature[:], + ) +} + +func verifyDKGPartialSignatureSignature( + psig *types.DKGPartialSignature, sigToPub SigToPubFn) (bool, error) { + hash := hashDKGPartialSignature(psig) + pubKey, err := sigToPub(hash, psig.Signature) + if err != nil { + return false, err + } + if psig.ProposerID != types.NewValidatorID(pubKey) { + return false, nil + } + return true, nil +} |