Repository navigation
Expand file tree
/
Copy pathschnorr_tests.cpp
More file actions
173 lines (149 loc) · 7.91 KB
/
Copy pathschnorr_tests.cpp
File metadata and controls
173 lines (149 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) 2019 The Bitcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <script/interpreter.h>
#include <test/lcg.h>
#include <test/setup_common.h>
#include <boost/test/unit_test.hpp>
#include <array>
#include <bitset>
typedef std::vector<uint8_t> valtype;
typedef std::vector<valtype> stacktype;
BOOST_FIXTURE_TEST_SUITE(schnorr_tests, BasicTestingSetup)
static valtype SignatureWithHashType(valtype vchSig, SigHashType sigHash) {
vchSig.push_back(static_cast<uint8_t>(sigHash.getRawSigHashType()));
return vchSig;
}
const uint8_t vchPrivkey[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
struct KeyData {
CKey privkeyC;
CPubKey pubkeyC;
KeyData() {
privkeyC.Set(vchPrivkey, vchPrivkey + 32, true);
pubkeyC = privkeyC.GetPubKey();
}
};
static
void CheckError(uint32_t flags, const stacktype &original_stack, const CScript &script, ScriptError expected) {
BaseSignatureChecker sigchecker;
ScriptError err = ScriptError::OK;
stacktype stack{original_stack};
auto const null_context = std::nullopt;
bool r = EvalScript(stack, script, flags, sigchecker, null_context, &err);
BOOST_CHECK(!r);
BOOST_CHECK(err == expected);
}
static
void CheckPass(uint32_t flags, const stacktype &original_stack, const CScript &script, const stacktype &expected) {
BaseSignatureChecker sigchecker;
ScriptError err = ScriptError::OK;
stacktype stack{original_stack};
auto const null_context = std::nullopt;
bool r = EvalScript(stack, script, flags, sigchecker, null_context, &err);
BOOST_CHECK(r);
BOOST_CHECK(err == ScriptError::OK);
BOOST_CHECK(stack == expected);
}
BOOST_AUTO_TEST_CASE(opcodes_random_flags) {
// Test script execution of the six signature opcodes with Schnorr-sized
// signatures, and probe failure mode under a very wide variety of flags.
// A counterpart to this can be found in sigencoding_tests.cpp, which only
// probes the sig encoding functions.
// Grab the various pubkey types.
KeyData kd;
valtype pubkeyC = ToByteVector(kd.pubkeyC);
// Script endings. The non-verify variants will complete OK and the verify
// variant will complete with ScriptError::<opcodename>, that is, unless
// there is a flag-dependent error which we will be testing for.
const CScript scriptCHECKSIG = CScript()
<< OP_CHECKSIG << OP_NOT << OP_VERIFY;
const CScript scriptCHECKSIGVERIFY = CScript() << OP_CHECKSIGVERIFY;
const CScript scriptCHECKDATASIG = CScript() << OP_CHECKDATASIG << OP_NOT
<< OP_VERIFY;
const CScript scriptCHECKDATASIGVERIFY = CScript() << OP_CHECKDATASIGVERIFY;
const CScript scriptCHECKMULTISIG = CScript() << OP_CHECKMULTISIG << OP_NOT
<< OP_VERIFY;
const CScript scriptCHECKMULTISIGVERIFY = CScript()
<< OP_CHECKMULTISIGVERIFY;
// all-zero signature: valid encoding for Schnorr but invalid for DER.
valtype Zero64{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
// this is a validly-encoded 64 byte DER sig; also a valid Schnorr encoding.
valtype DER64{0x30, 0x3e, 0x02, 0x1d, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
0x44, 0x44, 0x44, 0x02, 0x1d, 0x44, 0x44, 0x44, 0x44, 0x44,
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
0x44, 0x44, 0x44, 0x44};
// for variety we start off at a different seed than sigencoding_tests
// The first lcg.next() call is still 0x00000000 though.
MMIXLinearCongruentialGenerator lcg(1234);
for (int i = 0; i < 4096; i++) {
uint32_t flags = lcg.next();
const bool hasForkId = (flags & SCRIPT_ENABLE_SIGHASH_FORKID) != 0;
const bool hasNullFail = (flags & SCRIPT_VERIFY_NULLFAIL) != 0;
// Prepare 65-byte transaction sigs with right hashtype byte.
valtype DER64_with_hashtype =
SignatureWithHashType(DER64, SigHashType().withForkId(hasForkId));
valtype Zero64_with_hashtype =
SignatureWithHashType(Zero64, SigHashType().withForkId(hasForkId));
// Test CHECKSIG & CHECKDATASIG with he non-DER sig, which can fail from
// encoding, otherwise upon verification.
if (hasNullFail) {
CheckError(flags, {Zero64_with_hashtype, pubkeyC}, scriptCHECKSIG,
ScriptError::SIG_NULLFAIL);
CheckError(flags, {Zero64_with_hashtype, pubkeyC},
scriptCHECKSIGVERIFY, ScriptError::SIG_NULLFAIL);
CheckError(flags, {Zero64, {}, pubkeyC}, scriptCHECKDATASIG,
ScriptError::SIG_NULLFAIL);
CheckError(flags, {Zero64, {}, pubkeyC}, scriptCHECKDATASIGVERIFY,
ScriptError::SIG_NULLFAIL);
} else {
CheckPass(flags, {Zero64_with_hashtype, pubkeyC}, scriptCHECKSIG,
{});
CheckError(flags, {Zero64_with_hashtype, pubkeyC},
scriptCHECKSIGVERIFY, ScriptError::CHECKSIGVERIFY);
CheckPass(flags, {Zero64, {}, pubkeyC}, scriptCHECKDATASIG, {});
CheckError(flags, {Zero64, {}, pubkeyC}, scriptCHECKDATASIGVERIFY,
ScriptError::CHECKDATASIGVERIFY);
}
// Test CHECKSIG & CHECKDATASIG with DER sig, which fails upon
// verification.
if (hasNullFail) {
CheckError(flags, {DER64_with_hashtype, pubkeyC}, scriptCHECKSIG,
ScriptError::SIG_NULLFAIL);
CheckError(flags, {DER64_with_hashtype, pubkeyC},
scriptCHECKSIGVERIFY, ScriptError::SIG_NULLFAIL);
CheckError(flags, {DER64, {}, pubkeyC}, scriptCHECKDATASIG,
ScriptError::SIG_NULLFAIL);
CheckError(flags, {DER64, {}, pubkeyC}, scriptCHECKDATASIGVERIFY,
ScriptError::SIG_NULLFAIL);
} else {
CheckPass(flags, {DER64_with_hashtype, pubkeyC}, scriptCHECKSIG,
{});
CheckError(flags, {DER64_with_hashtype, pubkeyC},
scriptCHECKSIGVERIFY, ScriptError::CHECKSIGVERIFY);
CheckPass(flags, {DER64, {}, pubkeyC}, scriptCHECKDATASIG, {});
CheckError(flags, {DER64, {}, pubkeyC}, scriptCHECKDATASIGVERIFY,
ScriptError::CHECKDATASIGVERIFY);
}
// test OP_CHECKMULTISIG/VERIFY
// We fail with BADLENGTH no matter what.
CheckError(flags, {{}, Zero64_with_hashtype, {1}, pubkeyC, {1}},
scriptCHECKMULTISIG, ScriptError::SIG_BADLENGTH);
CheckError(flags, {{}, Zero64_with_hashtype, {1}, pubkeyC, {1}},
scriptCHECKMULTISIGVERIFY, ScriptError::SIG_BADLENGTH);
CheckError(flags, {{}, DER64_with_hashtype, {1}, pubkeyC, {1}},
scriptCHECKMULTISIG, ScriptError::SIG_BADLENGTH);
CheckError(flags, {{}, DER64_with_hashtype, {1}, pubkeyC, {1}},
scriptCHECKMULTISIGVERIFY, ScriptError::SIG_BADLENGTH);
}
}
BOOST_AUTO_TEST_SUITE_END()