NBitcoin generates duplicate Bitcoin addresse corresponding to private keys equal to 1 and 256 and

457 Views Asked by At

I was developing a solution with NBitcoin Nuget and suddenly I found it generated duplicate address for some private keys that are equal to 1 and 256 and ... The code I used for generating Bitcoin addresses is as follows:

_bitcoin_address=_private_key.PubKey.GetAddress(ScriptPubKeyType.Legacy, Network.Main).ToString();

when _private_key=1 or _private_key=512 the same address e.g. 1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH is generated, the same problem occurs when _private_key=4 or _private_key=256.

Is it a bug or am I making a mistake?

You can use following code as POC:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using NBitcoin;

namespace Bitcoin_address_generation
{
    class Program
    {
        static void Main(string[] args)
        {
            //generating a private key that is equal to 1
            byte[] _priv_bytes1 = IntegerToBytes(1, 32);

            Key _private_key1 = new Key();
            _private_key1.FromBytes(_priv_bytes1);


            //generating a private key that is equal to 256
            byte[] _priv_bytes256 = IntegerToBytes(256, 32);

            Key _private_key256 = new Key();
            _private_key256.FromBytes(_priv_bytes1);

            Console.WriteLine(_private_key1.PubKey.GetAddress(ScriptPubKeyType.Legacy, Network.Main).ToString());

            Console.WriteLine(_private_key256.PubKey.GetAddress(ScriptPubKeyType.Legacy, Network.Main).ToString());
            Console.ReadKey();

        }

        /// <summary>
        /// convert a big integer to byte array
        /// </summary>
        /// <param name="s"></param>
        /// <param name="qLength"></param>
        /// <returns></returns>
        public static byte[] IntegerToBytes(BigInteger s, int qLength)
        {
            byte[] bytes = s.ToByteArray();

            if (qLength < bytes.Length)
            {
                byte[] tmp = new byte[qLength];

                Array.Copy(bytes, bytes.Length - tmp.Length, tmp, 0, tmp.Length);

                return tmp;
            }

            if (qLength > bytes.Length)
            {
                byte[] tmp = new byte[qLength];

                Array.Copy(bytes, 0, tmp, tmp.Length - bytes.Length, bytes.Length);

                return tmp;
            }

            return bytes;
        }
    }
}
1

There are 1 best solutions below

0
On

After some question/answer I found NBitcoin has not any bug but below function is guilty:

public static byte[] IntegerToBytes(BigInteger s, int qLength)
{
    byte[] bytes = s.ToByteArray();

    if (qLength < bytes.Length)
    {
        byte[] tmp = new byte[qLength];

        Array.Copy(bytes, bytes.Length - tmp.Length, tmp, 0, tmp.Length);

        return tmp;
    }

    if (qLength > bytes.Length)
    {
        byte[] tmp = new byte[qLength];

        Array.Copy(bytes, 0, tmp, tmp.Length - bytes.Length, bytes.Length);

        return tmp;
    }

    return bytes;
}

The corrected function must be as below:

/// <summary>
/// convert a big integer to byte array
/// </summary>
/// <param name="s"></param>
/// <param name="qLength"></param>
/// <returns></returns>
public static byte[] convert_integer_to_byte(BigInteger _input, int qlength)
{
    byte[] _temp_byte = _input.ToByteArray();
    _temp_byte.Reverse();
    byte[] _result_byte;
    if (_temp_byte.Length < qlength)
    {
        _result_byte = new byte[qlength];
        _temp_byte.Reverse();
        _temp_byte.CopyTo(_result_byte, 0);
    }
    else
    {
        _result_byte = new byte[qlength];
        _temp_byte.CopyTo(_result_byte, 0);
    }
    //_result_byte.Reverse();
    return _result_byte;
}

Finally thaks to Lucas Ontivero and Dan Gershony who helped me to find the problem and resolve it.