diff --git a/src/Neo/Wallets/BIP32/ExtendedKey.cs b/src/Neo/Wallets/BIP32/ExtendedKey.cs index 18d171a104..b5e6954706 100644 --- a/src/Neo/Wallets/BIP32/ExtendedKey.cs +++ b/src/Neo/Wallets/BIP32/ExtendedKey.cs @@ -70,8 +70,15 @@ public ExtendedKey Derive(uint index) static byte[] AddModN(ReadOnlySpan a, ReadOnlySpan b, BigInteger n) { BigInteger aInt = new(a, isUnsigned: true, isBigEndian: true); + // Check if parse256(IL) >= n (BIP32 requirement) + if (aInt >= n) + throw new InvalidOperationException("Derived child private key is invalid."); + BigInteger bInt = new(b, isUnsigned: true, isBigEndian: true); BigInteger r = (aInt + bInt) % n; + if (r.IsZero) + throw new InvalidOperationException("Derived child private key is invalid."); + byte[] result = new byte[32]; Span tmp = stackalloc byte[32]; r.TryWriteBytes(tmp, out int bytesWritten, isUnsigned: true, isBigEndian: true);