|
4 | 4 |
|
5 | 5 | #include <compressor.h>
|
6 | 6 | #include <script/script.h>
|
| 7 | +#include <test/util/random.h> |
7 | 8 | #include <test/util/setup_common.h>
|
8 | 9 |
|
9 | 10 | #include <stdint.h>
|
@@ -131,4 +132,36 @@ BOOST_AUTO_TEST_CASE(compress_script_to_uncompressed_pubkey_id)
|
131 | 132 | BOOST_CHECK_EQUAL(out[0], 0x04 | (script[65] & 0x01)); // least significant bit (lsb) of last char of pubkey is mapped into out[0]
|
132 | 133 | }
|
133 | 134 |
|
| 135 | +BOOST_AUTO_TEST_CASE(compress_p2pk_scripts_not_on_curve) |
| 136 | +{ |
| 137 | + XOnlyPubKey x_not_on_curve; |
| 138 | + do { |
| 139 | + x_not_on_curve = XOnlyPubKey(g_insecure_rand_ctx.randbytes(32)); |
| 140 | + } while (x_not_on_curve.IsFullyValid()); |
| 141 | + |
| 142 | + // Check that P2PK script with uncompressed pubkey [=> OP_PUSH65 <0x04 .....> OP_CHECKSIG] |
| 143 | + // which is not fully valid (i.e. point is not on curve) can't be compressed |
| 144 | + std::vector<unsigned char> pubkey_raw(65, 0); |
| 145 | + pubkey_raw[0] = 4; |
| 146 | + std::copy(x_not_on_curve.begin(), x_not_on_curve.end(), &pubkey_raw[1]); |
| 147 | + CPubKey pubkey_not_on_curve(pubkey_raw); |
| 148 | + assert(pubkey_not_on_curve.IsValid()); |
| 149 | + assert(!pubkey_not_on_curve.IsFullyValid()); |
| 150 | + CScript script = CScript() << ToByteVector(pubkey_not_on_curve) << OP_CHECKSIG; |
| 151 | + BOOST_CHECK_EQUAL(script.size(), 67U); |
| 152 | + |
| 153 | + CompressedScript out; |
| 154 | + bool done = CompressScript(script, out); |
| 155 | + BOOST_CHECK_EQUAL(done, false); |
| 156 | + |
| 157 | + // Check that compressed P2PK script with uncompressed pubkey that is not fully |
| 158 | + // valid (i.e. x coordinate of the pubkey is not on curve) can't be decompressed |
| 159 | + CompressedScript compressed_script(x_not_on_curve.begin(), x_not_on_curve.end()); |
| 160 | + for (unsigned int compression_id : {4, 5}) { |
| 161 | + CScript uncompressed_script; |
| 162 | + bool success = DecompressScript(uncompressed_script, compression_id, compressed_script); |
| 163 | + BOOST_CHECK_EQUAL(success, false); |
| 164 | + } |
| 165 | +} |
| 166 | + |
134 | 167 | BOOST_AUTO_TEST_SUITE_END()
|
0 commit comments