|
| 1 | +// Copyright (c) 2018-2025 The Dash Core developers |
| 2 | +// Distributed under the MIT/X11 software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#ifndef BITCOIN_LLMQ_DKGTYPES_H |
| 6 | +#define BITCOIN_LLMQ_DKGTYPES_H |
| 7 | + |
| 8 | +#include <bls/bls.h> |
| 9 | +#include <bls/bls_ies.h> |
| 10 | +#include <consensus/params.h> |
| 11 | +#include <hash.h> |
| 12 | +#include <serialize.h> |
| 13 | +#include <util/underlying.h> |
| 14 | + |
| 15 | +#include <llmq/commitment.h> |
| 16 | + |
| 17 | +#include <algorithm> |
| 18 | +#include <vector> |
| 19 | + |
| 20 | +namespace llmq |
| 21 | +{ |
| 22 | + |
| 23 | +class CDKGContribution |
| 24 | +{ |
| 25 | +public: |
| 26 | + Consensus::LLMQType llmqType; |
| 27 | + uint256 quorumHash; |
| 28 | + uint256 proTxHash; |
| 29 | + BLSVerificationVectorPtr vvec; |
| 30 | + std::shared_ptr<CBLSIESMultiRecipientObjects<CBLSSecretKey>> contributions; |
| 31 | + CBLSSignature sig; |
| 32 | + |
| 33 | +public: |
| 34 | + template<typename Stream> |
| 35 | + inline void SerializeWithoutSig(Stream& s) const |
| 36 | + { |
| 37 | + s << ToUnderlying(llmqType); |
| 38 | + s << quorumHash; |
| 39 | + s << proTxHash; |
| 40 | + s << *vvec; |
| 41 | + s << *contributions; |
| 42 | + } |
| 43 | + template<typename Stream> |
| 44 | + inline void Serialize(Stream& s) const |
| 45 | + { |
| 46 | + SerializeWithoutSig(s); |
| 47 | + s << sig; |
| 48 | + } |
| 49 | + template<typename Stream> |
| 50 | + inline void Unserialize(Stream& s) |
| 51 | + { |
| 52 | + std::vector<CBLSPublicKey> tmp1; |
| 53 | + CBLSIESMultiRecipientObjects<CBLSSecretKey> tmp2; |
| 54 | + |
| 55 | + s >> llmqType; |
| 56 | + s >> quorumHash; |
| 57 | + s >> proTxHash; |
| 58 | + s >> tmp1; |
| 59 | + s >> tmp2; |
| 60 | + s >> sig; |
| 61 | + |
| 62 | + vvec = std::make_shared<std::vector<CBLSPublicKey>>(std::move(tmp1)); |
| 63 | + contributions = std::make_shared<CBLSIESMultiRecipientObjects<CBLSSecretKey>>(std::move(tmp2)); |
| 64 | + } |
| 65 | + |
| 66 | + [[nodiscard]] uint256 GetSignHash() const |
| 67 | + { |
| 68 | + CHashWriter hw(SER_GETHASH, 0); |
| 69 | + SerializeWithoutSig(hw); |
| 70 | + hw << CBLSSignature(); |
| 71 | + return hw.GetHash(); |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +class CDKGComplaint |
| 76 | +{ |
| 77 | +public: |
| 78 | + Consensus::LLMQType llmqType{Consensus::LLMQType::LLMQ_NONE}; |
| 79 | + uint256 quorumHash; |
| 80 | + uint256 proTxHash; |
| 81 | + std::vector<bool> badMembers; |
| 82 | + std::vector<bool> complainForMembers; |
| 83 | + CBLSSignature sig; |
| 84 | + |
| 85 | +public: |
| 86 | + CDKGComplaint() = default; |
| 87 | + explicit CDKGComplaint(const Consensus::LLMQParams& params) : |
| 88 | + badMembers((size_t)params.size), complainForMembers((size_t)params.size) {}; |
| 89 | + |
| 90 | + SERIALIZE_METHODS(CDKGComplaint, obj) |
| 91 | + { |
| 92 | + READWRITE( |
| 93 | + obj.llmqType, |
| 94 | + obj.quorumHash, |
| 95 | + obj.proTxHash, |
| 96 | + DYNBITSET(obj.badMembers), |
| 97 | + DYNBITSET(obj.complainForMembers), |
| 98 | + obj.sig |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + [[nodiscard]] uint256 GetSignHash() const |
| 103 | + { |
| 104 | + CDKGComplaint tmp(*this); |
| 105 | + tmp.sig = CBLSSignature(); |
| 106 | + return ::SerializeHash(tmp); |
| 107 | + } |
| 108 | +}; |
| 109 | + |
| 110 | +class CDKGJustification |
| 111 | +{ |
| 112 | +public: |
| 113 | + Consensus::LLMQType llmqType; |
| 114 | + uint256 quorumHash; |
| 115 | + uint256 proTxHash; |
| 116 | + struct Contribution { |
| 117 | + uint32_t index; |
| 118 | + CBLSSecretKey key; |
| 119 | + SERIALIZE_METHODS(Contribution, obj) |
| 120 | + { |
| 121 | + READWRITE(obj.index, obj.key); |
| 122 | + } |
| 123 | + }; |
| 124 | + std::vector<Contribution> contributions; |
| 125 | + CBLSSignature sig; |
| 126 | + |
| 127 | +public: |
| 128 | + SERIALIZE_METHODS(CDKGJustification, obj) |
| 129 | + { |
| 130 | + READWRITE(obj.llmqType, obj.quorumHash, obj.proTxHash, obj.contributions, obj.sig); |
| 131 | + } |
| 132 | + |
| 133 | + [[nodiscard]] uint256 GetSignHash() const |
| 134 | + { |
| 135 | + CDKGJustification tmp(*this); |
| 136 | + tmp.sig = CBLSSignature(); |
| 137 | + return ::SerializeHash(tmp); |
| 138 | + } |
| 139 | +}; |
| 140 | + |
| 141 | +// each member commits to a single set of valid members with this message |
| 142 | +// then each node aggregate all received premature commitments |
| 143 | +// into a single CFinalCommitment, which is only valid if |
| 144 | +// enough (>=minSize) premature commitments were aggregated |
| 145 | +class CDKGPrematureCommitment |
| 146 | +{ |
| 147 | +public: |
| 148 | + Consensus::LLMQType llmqType{Consensus::LLMQType::LLMQ_NONE}; |
| 149 | + uint256 quorumHash; |
| 150 | + uint256 proTxHash; |
| 151 | + std::vector<bool> validMembers; |
| 152 | + |
| 153 | + CBLSPublicKey quorumPublicKey; |
| 154 | + uint256 quorumVvecHash; |
| 155 | + |
| 156 | + CBLSSignature quorumSig; // threshold sig share of quorumHash+validMembers+pubKeyHash+vvecHash |
| 157 | + CBLSSignature sig; // single member sig of quorumHash+validMembers+pubKeyHash+vvecHash |
| 158 | + |
| 159 | +public: |
| 160 | + CDKGPrematureCommitment() = default; |
| 161 | + explicit CDKGPrematureCommitment(const Consensus::LLMQParams& params) : |
| 162 | + validMembers((size_t)params.size) {}; |
| 163 | + |
| 164 | + [[nodiscard]] int CountValidMembers() const |
| 165 | + { |
| 166 | + return int(std::count(validMembers.begin(), validMembers.end(), true)); |
| 167 | + } |
| 168 | + |
| 169 | +public: |
| 170 | + SERIALIZE_METHODS(CDKGPrematureCommitment, obj) |
| 171 | + { |
| 172 | + READWRITE( |
| 173 | + obj.llmqType, |
| 174 | + obj.quorumHash, |
| 175 | + obj.proTxHash, |
| 176 | + DYNBITSET(obj.validMembers), |
| 177 | + obj.quorumPublicKey, |
| 178 | + obj.quorumVvecHash, |
| 179 | + obj.quorumSig, |
| 180 | + obj.sig |
| 181 | + ); |
| 182 | + } |
| 183 | + |
| 184 | + [[nodiscard]] uint256 GetSignHash() const |
| 185 | + { |
| 186 | + return BuildCommitmentHash(llmqType, quorumHash, validMembers, quorumPublicKey, quorumVvecHash); |
| 187 | + } |
| 188 | +}; |
| 189 | + |
| 190 | +} // namespace llmq |
| 191 | + |
| 192 | +#endif // BITCOIN_LLMQ_DKGTYPES_H |
| 193 | + |
0 commit comments