Skip to content

Commit 31bf0f1

Browse files
authored
core: fix is_valid_signature (#2804)
1 parent a78f744 commit 31bf0f1

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

silkworm/core/crypto/secp256k1n.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ bool is_valid_signature(const intx::uint256& r, const intx::uint256& s, bool hom
2222
if (!r || !s) {
2323
return false;
2424
}
25-
if (r >= kSecp256k1n && s >= kSecp256k1n) {
25+
if (r >= kSecp256k1n || s >= kSecp256k1n) {
2626
return false;
2727
}
2828
// https://eips.ethereum.org/EIPS/eip-2
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright 2025 The Silkworm Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#include "secp256k1n.hpp"
18+
19+
#include <catch2/catch_test_macros.hpp>
20+
21+
namespace silkworm {
22+
23+
TEST_CASE("is_valid_signature") {
24+
bool homestead = false;
25+
CHECK(!is_valid_signature(0, 0, homestead));
26+
CHECK(!is_valid_signature(0, 1, homestead));
27+
CHECK(!is_valid_signature(1, 0, homestead));
28+
CHECK(is_valid_signature(1, 1, homestead));
29+
CHECK(is_valid_signature(1, kSecp256k1Halfn, homestead));
30+
CHECK(is_valid_signature(1, kSecp256k1Halfn + 1, homestead));
31+
CHECK(is_valid_signature(kSecp256k1n - 1, kSecp256k1n - 1, homestead));
32+
CHECK(!is_valid_signature(kSecp256k1n - 1, kSecp256k1n, homestead));
33+
CHECK(!is_valid_signature(kSecp256k1n, kSecp256k1n - 1, homestead));
34+
CHECK(!is_valid_signature(kSecp256k1n, kSecp256k1n, homestead));
35+
36+
homestead = true;
37+
CHECK(!is_valid_signature(0, 0, homestead));
38+
CHECK(!is_valid_signature(0, 1, homestead));
39+
CHECK(!is_valid_signature(1, 0, homestead));
40+
CHECK(is_valid_signature(1, 1, homestead));
41+
CHECK(is_valid_signature(1, kSecp256k1Halfn, homestead));
42+
CHECK(!is_valid_signature(1, kSecp256k1Halfn + 1, homestead));
43+
CHECK(!is_valid_signature(kSecp256k1n - 1, kSecp256k1n - 1, homestead));
44+
CHECK(!is_valid_signature(kSecp256k1n - 1, kSecp256k1n, homestead));
45+
CHECK(!is_valid_signature(kSecp256k1n, kSecp256k1n - 1, homestead));
46+
CHECK(!is_valid_signature(kSecp256k1n, kSecp256k1n, homestead));
47+
}
48+
49+
} // namespace silkworm

0 commit comments

Comments
 (0)