Skip to content
Merged
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
7 changes: 7 additions & 0 deletions src/Neo/Wallets/BIP32/ExtendedKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,15 @@ public ExtendedKey Derive(uint index)
static byte[] AddModN(ReadOnlySpan<byte> a, ReadOnlySpan<byte> 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<byte> tmp = stackalloc byte[32];
r.TryWriteBytes(tmp, out int bytesWritten, isUnsigned: true, isBigEndian: true);
Expand Down