Skip to content

Commit 427f8c2

Browse files
committed
Merge #20248: test: fix length of R check in key_signature_tests
8989577 Fix length of R check in test/key_tests.cpp:key_signature_tests (Dmitry Petukhov) Pull request description: The code before the fix only checked the length of R value of the last signature in the loop, and only for equality (but the length can be less than 32) The fixed code checks that length of the R value is less than or equal to 32 on each iteration of the loop ACKs for top commit: laanwj: ACK 8989577 Tree-SHA512: 73eb2bb4a6c1c5fc11dd16851b28b43037ac06ef8cfc3b1f957429a1fca295c9422d67ec6c73c0e4bb4919f92e22dc5d733e84840b0d01ad1a529aa20d906ebf
2 parents 5b6f970 + 8989577 commit 427f8c2

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/test/key_tests.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,20 +172,30 @@ BOOST_AUTO_TEST_CASE(key_signature_tests)
172172
}
173173
BOOST_CHECK(found);
174174

175-
// When entropy is not specified, we should always see low R signatures that are less than 70 bytes in 256 tries
175+
// When entropy is not specified, we should always see low R signatures that are less than or equal to 70 bytes in 256 tries
176+
// The low R signatures should always have the value of their "length of R" byte less than or equal to 32
176177
// We should see at least one signature that is less than 70 bytes.
177-
found = true;
178178
bool found_small = false;
179+
bool found_big = false;
180+
bool bad_sign = false;
179181
for (int i = 0; i < 256; ++i) {
180182
sig.clear();
181183
std::string msg = "A message to be signed" + ToString(i);
182184
msg_hash = Hash(msg);
183-
BOOST_CHECK(key.Sign(msg_hash, sig));
184-
found = sig[3] == 0x20;
185-
BOOST_CHECK(sig.size() <= 70);
185+
if (!key.Sign(msg_hash, sig)) {
186+
bad_sign = true;
187+
break;
188+
}
189+
// sig.size() > 70 implies sig[3] > 32, because S is always low.
190+
// But check both conditions anyway, just in case this implication is broken for some reason
191+
if (sig[3] > 32 || sig.size() > 70) {
192+
found_big = true;
193+
break;
194+
}
186195
found_small |= sig.size() < 70;
187196
}
188-
BOOST_CHECK(found);
197+
BOOST_CHECK(!bad_sign);
198+
BOOST_CHECK(!found_big);
189199
BOOST_CHECK(found_small);
190200
}
191201

0 commit comments

Comments
 (0)