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

fix: mod reduce msg_hash as input of the HMAC function #102

Open
wants to merge 2 commits into
base: main
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
3 changes: 2 additions & 1 deletion eth_keys/backends/native/ecdsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ def deterministic_generate_k(

def ecdsa_raw_sign(msg_hash: bytes, private_key_bytes: bytes) -> Tuple[int, int, int]:
z = big_endian_to_int(msg_hash)
k = deterministic_generate_k(msg_hash, private_key_bytes)
msg_hash_mod_n = pad32(int_to_big_endian(z % N))
k = deterministic_generate_k(msg_hash_mod_n, private_key_bytes)

r, y = fast_multiply(G, k)
s_raw = inv(k, N) * (z + r * big_endian_to_int(private_key_bytes)) % N
Expand Down
1 change: 1 addition & 0 deletions newsfragments/101.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Modulo reduce the message digest before passing it to the HMAC function
12 changes: 12 additions & 0 deletions tests/backends/test_native_backend_against_coincurve.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import pytest

from eth_utils import (
int_to_big_endian,
keccak,
)
from hypothesis import (
example,
given,
settings,
strategies as st,
Expand All @@ -21,9 +23,15 @@
CoinCurveECCBackend,
NativeECCBackend,
)
from eth_keys.constants import (
SECPK1_N,
)
from eth_keys.exceptions import (
BadSignature,
)
from eth_keys.utils.padding import (
pad32,
)

MSG = b"message"
MSGHASH = keccak(MSG)
Expand Down Expand Up @@ -58,6 +66,10 @@ def test_public_key_generation_is_equal(
private_key_bytes=private_key_st,
message_hash=message_hash_st,
)
@example(
private_key_bytes=pad32(int_to_big_endian(1)),
message_hash=int_to_big_endian(SECPK1_N),
)
@settings(max_examples=MAX_EXAMPLES)
def test_signing_is_equal(
private_key_bytes, message_hash, native_key_api, coincurve_key_api
Expand Down