Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable fewer messages than nonces #32

Open
wants to merge 3 commits into
base: feature/multiple-nonces
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ external/libwally-core
# generated doc
doc/html*
doc/latex

.vscode
16 changes: 12 additions & 4 deletions src/cfddlc_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ AdaptorPair DlcManager::CreateCetAdaptorSignature(
std::vector<AdaptorPair> DlcManager::CreateCetAdaptorSignatures(
const std::vector<TransactionController>& cets,
const SchnorrPubkey& oracle_pubkey,
const std::vector<SchnorrPubkey>& oracle_r_value, const Privkey& funding_sk,
const std::vector<SchnorrPubkey>& oracle_r_values, const Privkey& funding_sk,
const Script& funding_script_pubkey, const Amount& total_collateral,
const std::vector<std::vector<ByteData256>>& msgs) {
size_t nb = cets.size();
Expand All @@ -217,8 +217,12 @@ std::vector<AdaptorPair> DlcManager::CreateCetAdaptorSignatures(

std::vector<AdaptorPair> sigs;
for (size_t i = 0; i < nb; i++) {
std::vector<SchnorrPubkey> r_values;
for (size_t j = 0; j < msgs[i].size(); j++) {
r_values.push_back(oracle_r_values[j]);
}
sigs.push_back(CreateCetAdaptorSignature(
cets[i], oracle_pubkey, oracle_r_value, funding_sk,
cets[i], oracle_pubkey, r_values, funding_sk,
funding_script_pubkey, total_collateral, msgs[i]));
}

Expand Down Expand Up @@ -257,9 +261,13 @@ bool DlcManager::VerifyCetAdaptorSignatures(
bool all_valid = true;

for (size_t i = 0; i < nb && all_valid; i++) {
std::vector<SchnorrPubkey> r_values;
for (size_t j = 0; j < msgs[i].size(); j++) {
r_values.push_back(oracle_r_values[j]);
}
all_valid &= VerifyCetAdaptorSignature(
signature_and_proofs[i], cets[i], pubkey, oracle_pubkey,
oracle_r_values, funding_script_pubkey, total_collateral, msgs[i]);
r_values, funding_script_pubkey, total_collateral, msgs[i]);
}

return all_valid;
Expand Down Expand Up @@ -304,7 +312,7 @@ void DlcManager::SignCet(TransactionController* cet,
throw new CfdException(CfdError::kCfdIllegalArgumentError,
"Public key not part of the multi sig script.");
}
}
} // namespace dlc

ByteData DlcManager::GetRawFundingTransactionInputSignature(
const TransactionController& funding_transaction, const Privkey& privkey,
Expand Down
37 changes: 37 additions & 0 deletions test/test_cfddlc_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ const std::vector<ByteData256> WIN_MESSAGES_HASH = {
HashUtil::Sha256(WIN_MESSAGES[0]), HashUtil::Sha256(WIN_MESSAGES[1])};
const std::vector<ByteData256> LOSE_MESSAGES_HASH = {
HashUtil::Sha256(LOSE_MESSAGES[0]), HashUtil::Sha256(LOSE_MESSAGES[1])};
const std::vector<ByteData256> WIN_MESSAGES_HASH_FEWER_MESSAGES = {
HashUtil::Sha256(WIN_MESSAGES[0])};
const std::vector<ByteData256> LOSE_MESSAGES_HASH_FEWER_MESSAGES = {
HashUtil::Sha256(LOSE_MESSAGES[0])};
const std::vector<std::vector<ByteData256>> MESSAGES_HASH = {
WIN_MESSAGES_HASH, LOSE_MESSAGES_HASH};
const Privkey ORACLE_PRIVKEY(
Expand Down Expand Up @@ -568,3 +572,36 @@ TEST(DlcManager, AdaptorSigMultipleNonces) {
fund_amount, WitnessVersion::kVersion0);
EXPECT_TRUE(is_valid);
}

TEST(DlcManager, AdaptorSigMultipleNoncesWithFewerMessagesThanNonces) {
std::vector<DlcOutcome> outcomes = {{WIN_AMOUNT, LOSE_AMOUNT},
{LOSE_AMOUNT, WIN_AMOUNT}};
auto dlc_transactions = DlcManager::CreateDlcTransactions(
outcomes, LOCAL_PARAMS, REMOTE_PARAMS, REFUND_LOCKTIME, 1, PREMIUM_DEST,
OPTION_PREMIUM);
auto fund_transaction = dlc_transactions.fund_transaction;
auto cets = dlc_transactions.cets;
auto cet0 = cets[0];
auto lock_script = DlcManager::CreateFundTxLockingScript(LOCAL_FUND_PUBKEY,
REMOTE_FUND_PUBKEY);

auto fund_amount = fund_transaction.GetTransaction().GetTxOut(0).GetValue();
auto fund_txid = fund_transaction.GetTransaction().GetTxid();

auto adaptor_pairs = DlcManager::CreateCetAdaptorSignatures(
cets, ORACLE_PUBKEY, ORACLE_R_POINTS, LOCAL_FUND_PRIVKEY, lock_script,
fund_amount, {WIN_MESSAGES_HASH_FEWER_MESSAGES, LOSE_MESSAGES_HASH_FEWER_MESSAGES});

EXPECT_TRUE(DlcManager::VerifyCetAdaptorSignature(
adaptor_pairs[1], cets[1], LOCAL_FUND_PUBKEY, ORACLE_PUBKEY,
{ORACLE_R_POINTS[0]}, lock_script, fund_amount, LOSE_MESSAGES_HASH_FEWER_MESSAGES));

auto adaptor_secret = ORACLE_SIGNATURES[0].GetPrivkey();
auto adapted_sig =
AdaptorUtil::Adapt(adaptor_pairs[0].signature, adaptor_secret);

auto is_valid = cet0.VerifyInputSignature(
adapted_sig, LOCAL_FUND_PUBKEY, fund_txid, 0, lock_script, SigHashType(),
fund_amount, WitnessVersion::kVersion0);
EXPECT_TRUE(is_valid);
}