Bitcoin Forum
May 28, 2024, 03:11:24 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
  Home Help Search Login Register More  
  Show Posts
Pages: [1] 2 »
1  Bitcoin / Development & Technical Discussion / Re: How to add multihreading to a continuous SECP256K1 Python function? on: November 12, 2022, 06:49:16 PM
best approach to python multithreading is to use c++ instead. that's from my secp tool
Code:
void brough(unsigned long long f);
int main()
{
    std::vector<unsigned long long> a{2, 4, 6, 8, 10, 12, 16};
    std::cout << std::endl;
    std::thread h[7];


    for(auto f : a)
    {
        h[f == 16 ? 6 : f/2 - 1] = std::thread(&brough, f);
    }
    h[0].join();
    h[1].join();
    h[2].join();
    h[3].join();
    h[4].join();
    h[5].join();
    h[6].join();

    return 0;
}

https[Suspicious link removed]yneu/secp256k1-cxx/blob/master/main.cpp#L33-L54

I see your tool over here: https[Suspicious link removed]yneu/secp256k1-cxx
Unfortunatelly, there's a question, how do you compile that?
You just need to have Visual Studio with Cmake and Boost installed and hit the "debug/release" button on main.cpp?
2  Bitcoin / Development & Technical Discussion / How to add multihreading to a continuous SECP256K1 Python function? on: November 06, 2022, 08:57:29 AM
I have an infinite loop Python function for measuring how fast are SECP256K1 public Keys are generated.

The script:

Code:
from time import time
   
    a = 0
    b = 7
    n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141
    gx = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
    gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
    prime = 2**256 - 2**32 - 977
   
    def addition(currentX, currentY, gx, gy, a, b, prime):
        if gy == 0:
            return (None, None)
        elif currentX is None and currentY is None:
            return (gx, gy)
        elif currentX == gx and currentY != gy:
            return (None, None)
        elif currentX == gx and currentY == gy and currentY == 0:
            return (None, None)
        elif currentX == gx and currentY == gy:
            s1 = (3 * pow(gx, 2, prime) + a) % prime
            s2 = (gy * 2) % prime
            s = (s1 * pow(s2, (prime - 2), prime)) % prime
            currentX = (s ** 2 - 2 * gx) % prime
            currentY = (s * (gx - currentX) - gy) % prime
        elif currentX != gx:
            s1 = (currentY - gy)
            s2 = (currentX - gx)
            s = (s1 * pow(s2, (prime - 2), prime)) % prime
            currentX = ((s ** 2) - gx - currentX) % prime
            currentY = ((s * (gx - currentX)) - gy) % prime
   
        return (currentX, currentY)
   
    def secp256k1BinaryExpansion(privateKey, gx, gy, a, b, prime):
        #if pow(gy, 2, prime) != (pow(gx, 3, prime) + a * gx + b) % prime:
            #return "The point is not on the curve"
        coef = privateKey
        currentX, currentY = gx, gy
        resultX, resultY = None, None
        while coef:
            if coef & 1:
                resultX, resultY = addition(resultX, resultY, currentX, currentY, a, b, prime)
            currentX, currentY = addition(currentX, currentY, currentX, currentY, a, b, prime)
            coef >>= 1
        return (resultX, resultY)
   
    def testLoop(gx, gy, a, b, prime):
        count = 1 #Count is the number of all calculations
        counter = 0 #Counter is for measuring the speed of the function
        timeOne = time()
        pubX, pubY = None, None
        while True:
            pubX, pubY = secp256k1BinaryExpansion(count, gx, gy, a, b, prime)
            #print("Case ", count,":", pubX,pubY)
            count += 1
            counter += 1
            timeTwo = time()
            if (timeTwo - timeOne) >= 10:
                print("The speed is: ", counter / (timeTwo - timeOne), "c/s")
                timeOne = time()
                counter = 0
   
    testLoop(gx, gy, a, b, prime)

Whenever I am launching the script on Pycharm, it outputs aroud 100 c/s on Windows and 300 c/s on Ubuntu.

When it happens, on both os, only 1 core out ouf 4 gets loaded with this task for 100%, hence only 25% of CPU power is allocated to this. The CPU: intel core i5-4440 cpu @ 3.10ghz

I'd like to allocate 2-3 cores to the task, so it gets loaded like: 50-75%.

The truth is I've read documentation and watched tutorials on Python Parallelism/Multithreading and it's confusing.

Not really sure how to allocate a single job across the cores.

May be you could help out?
3  Bitcoin / Development & Technical Discussion / Re: Is there a way to use Python GPU for ECC speed up? on: October 17, 2022, 06:50:47 PM
I don't know any ready-to-use 256bit number numpy libraries, but it is possible to create one, using 64 or 32bit numbers for math operations.
You cannot just speed up individual operations like point multiplication by using GPU, because single CUDA core is much slower than CPU. You need to divide full computing work into many independent tasks which will run in parallel in order to get the performance gain.

What you are suggesting is that it is just easier to use C++ with boost, while simultaneously implementing a multithreading approach, and everything would be running on a CPU?

I mean it could be a better way to think about it, since I also want to port that on laptop or other devices that don't have either a GPU or NVIDIA tool kit installed
4  Bitcoin / Development & Technical Discussion / Is there a way to use Python GPU for ECC speed up? on: October 16, 2022, 03:55:00 PM
Hello. Got interested recently in Cuda technology for existing Python SECP256K1 code speed up. Installed Nvidia 10.2 toolkit and It seems to be running OK to an extent.

I've scrapped a small script for PyCuda that just doubles an integer
Code:
import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule

import numpy
#Working with integers
a = 126
a = numpy.int64(a)
a_gpu = cuda.mem_alloc(a.nbytes)
cuda.memcpy_htod(a_gpu, a)
mod = SourceModule("""
  __global__ void doublify(int *a)
  {
    int idx = threadIdx.x + threadIdx.y*4;
    a[idx] *= 2;
  }
  """)
func = mod.get_function("doublify")
func(a_gpu, block=(4,4,1))
a_doubled = numpy.empty_like(a)
cuda.memcpy_dtoh(a_doubled, a_gpu)
print(a_doubled)
print(a)

However, It can't work with big numbers (of 256 bit size). When passing for example:
Code:
a = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140

There's an error:
Code:
OverflowError: int too big to convert

Is there a way to use Big integers in PyCuda, CuPy, just a GPU implementation of Python?
Stumbled on Stackoverflow post
https://stackoverflow.com/questions/68215238/numpy-256-bit-computing
but didn't understand anything in it.

I know that in C++, you could use Boost library/dependency to have bigger integer variables. Is there a way to do the same in Python GPU?

Also, Does it even make any sense to use Python GPU solutions, since the main calculations are made in "SourceModule" kernel that has to be coded in C++ anyway?

May be it is just easier to recode the existing python code in C++ with boost library and later add CUDA GPU rendering?
5  Bitcoin / Development & Technical Discussion / Re: Pubkeys with even y coordinate correspond to privKeys that are less than n/2? on: October 02, 2022, 05:21:19 PM
I use ecctools for this shit.

./md 8 / 3
Result: 55555555555555555555555555555554e8e4f44ce51835693ff0ca2ef01215c3

./md 2 / 3
Result: 55555555555555555555555555555554e8e4f44ce51835693ff0ca2ef01215c1


then you bruteforce in range from

55555555555555555555555555555554e8e4f44ce51835693ff0ca2ef01215c1

to

55555555555555555555555555555554e8e4f44ce51835693ff0ca2ef01215c3

you need only 2 step to get

55555555555555555555555555555554e8e4f44ce51835693ff0ca2ef01215c3 = then brute you get  .... c1, ...c2,... c3

only 2 3 step instaed of 8 then you brute from 1 to 8

this is one of most used ideas of this shit. Shit this is real shit what is too hard or imposible use in real cracking / bruting world.

Are you saying that this is one of the methods of finding out a public key of an address that never revealed its public key, but we know a range of its private keys (Although it is still almost impossible to brute force the private key itself)?

Suppose that there is an address:
Code:
13zb1hQbWVsc2S7ZTZnP2G4undNNpdh5so

The range of its private keys is:

Starting key:

Code:
0000000000000000000000000000000000000000000000020000000000000000
let's call it priv1
Which is (36893488147419103232). Which is also exactly equal to 2^65.

Ending key:

Code:
000000000000000000000000000000000000000000000003ffffffffffffffff
lets call it priv2
Which is (73786976294838206463). Which is also exactly equal to 2^66.

Using ecctools we would have to:
Code:
./md 36893488147419103232 / 66
and
Code:
./md 73786976294838206463 / 66

In this case we would get 2 numbers 66 numbers apart from each other.
Meaning that we have 66 numbers (combinations) at our disposal.
In this scenario, What would the numbers represent? What are they? Private keys? Public keys? Something else?
Why would they be valuable for brute forcing a specific public key (or may be a private key)?



6  Bitcoin / Development & Technical Discussion / Re: Pubkeys with even y coordinate correspond to privKeys that are less than n/2? on: September 18, 2022, 06:29:25 PM
The question is, if the ycoordinate of a public key is even, does it mean that the corresponding private key is less than n/2 by its value? If the y is odd, the private key is more than n/2?
Is there any relationship between the eveness/oddness of the y (or x) coordinate and the value of the corresponding private key?
No, there isn't. You cannot infer anything about the private key from knowledge of only the public key.

A quick example. Private key 4 gives the following public key:
Code:
x = E493DBF1C10D80F3581E4904930B1404CC6C13900EE0758474FA94ABE8C4CD13
y = 51ED993EA0D455B75642E2098EA51448D967AE33BFBDFE40CFE97BDC47739922

Private key 6 gives the following public key:
Code:
x = FFF97BD5755EEEA420453A14355235D382F6472F8568A18B2F057A1460297556
y = AE12777AACFBB620F3BE96017F45C560DE80F0F6518FE4A03C870C36B075F297

As you can see, both x coordinates and both y coordinates have opposite signs.

Is there any way to know that the private key is more or less than n/2 while not knowing the private key itself?
Again, no.

Is there a way to find out the public key of an address that never sent Bitcoin but only received it?
Depends on the address. If the address is a hash of the public key, such as in P2PKH or P2WPKH, then no. If the address is not a hash of the public key, such as in P2PK or P2TR, then yes.

The other option is if the public key has been revealed via another means, such as a signing a message, openly being shared, or being leaked.

I got a few more questions. Suppose that we have a Public Key that is generated by an "odd" private key like 113*G.
What will happen if we try to "half" this public key using these methods?
https://crypto.stackexchange.com/questions/59972/half-of-any-bitcoin-crypto-public-key-public-key-half-is-possible
https://bitcointalk.org/index.php?topic=4455904.0
Does "halving" an even Public Key makes any difference?

7  Bitcoin / Development & Technical Discussion / Pubkeys with even y coordinate correspond to privKeys that are less than n/2? on: September 18, 2022, 12:20:12 PM
The question is somewhat complex and directed to clearing thing out.

Suppose that n is the order of the cyclic group, n - 1 is the number of all private keys possible

Code:
n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141 

We also know that every private and public key has its modular inverse. To get a modular inverse of a private key, we need to subtract the private key from n.

Code:
n - privKey

To get a modular inverse of a public key, we'll have to multiply its y coordinate by -1 and modulo by the p - order of the finite field.

Code:
x,y = x, -y % p 

A modular inversed public key has the same x coordinate as original public key, but different y coordinate, and the y coordinate is always different in its polarity. If the original y was odd, in a modular inversed key it will be even, and vice versa.

If a compressed public key has "02" index at the biggining then it has even y. If it is "03" then it is odd.

The question is, if the ycoordinate of a public key is even, does it mean that the corresponding private key is less than n/2 by its value? If the y is odd, the private key is more than n/2?

Is there any relationship between the eveness/oddness of the y (or x) coordinate and the value of the corresponding private key?

Is there any way to know that the private key is more or less than n/2 while not knowing the private key itself?

Is there a way to find out the public key of an address that never sent Bitcoin but only received it?
8  Bitcoin / Development & Technical Discussion / Re: How to convert a compressed public key into uncompressed one in Python? on: August 30, 2022, 06:51:39 PM
The Python script you've already discovered, over here works correctly as far as I can tell.

Substituting your example keys into the "compressed_key" variable on line 12 and running the script three times produced the following results:

compressed_key = '025A2146590B80D1F0D97CC7104E702011AFFF21BFAF817F5C7002446369BA9DDC'
Code:
045a2146590b80d1f0d97cc7104e702011afff21bfaf817f5c7002446369ba9ddc9bd5dcd1b4a737244d6bb7b96e256391b8597d3a7972a6f8ca9096d4aea1f37e

compressed_key = '035728F4692D85D411DF3643CD69FE05C411A0D507C7D814008F56C8F260AD7ED9'
Code:
045728f4692d85d411df3643cd69fe05c411a0d507c7d814008f56c8f260ad7ed99e2df8d9cb1a575d55264692629ae22e518bc14ad02592941c13be6755c72973

compressed_key = '039E87EB177890FDD788B95843ED53AD4FB6E877E3F730EF1E73593964C2AB9D15'
Code:
049e87eb177890fdd788b95843ed53ad4fb6e877e3f730ef1e73593964c2ab9d15a3b647c8c4a0766420917b7b445cdcd6bfec2900175c5534c6113954f3ff00d9

I found the error. If inputting 0x... hex values with str() conversion into the function, it is going to output incorrect values. So it requires the compressed inputting public key to be a string with ' ' single quotes like:
https://i.stack.imgur.com/AdY3o.png
New test results:
Test 1:
Code:
045a2146590b80d1f0d97cc7104e702011afff21bfaf817f5c7002446369ba9ddc9bd5dcd1b4a737244d6bb7b96e256391b8597d3a7972a6f8ca9096d4aea1f37e
Test 2:
Code:
045728f4692d85d411df3643cd69fe05c411a0d507c7d814008f56c8f260ad7ed99e2df8d9cb1a575d55264692629ae22e518bc14ad02592941c13be6755c72973
Test 3:
Code:
049e87eb177890fdd788b95843ed53ad4fb6e877e3f730ef1e73593964c2ab9d15a3b647c8c4a0766420917b7b445cdcd6bfec2900175c5534c6113954f3ff00d9
So, thanks!
9  Bitcoin / Development & Technical Discussion / Re: How to convert a compressed public key into uncompressed one in Python? on: August 30, 2022, 06:20:46 PM
The Python script you've already discovered, over here works correctly as far as I can tell.

Substituting your example keys into the "compressed_key" variable on line 12 and running the script three times produced the following results:

compressed_key = '025A2146590B80D1F0D97CC7104E702011AFFF21BFAF817F5C7002446369BA9DDC'
Code:
045a2146590b80d1f0d97cc7104e702011afff21bfaf817f5c7002446369ba9ddc9bd5dcd1b4a737244d6bb7b96e256391b8597d3a7972a6f8ca9096d4aea1f37e

compressed_key = '035728F4692D85D411DF3643CD69FE05C411A0D507C7D814008F56C8F260AD7ED9'
Code:
045728f4692d85d411df3643cd69fe05c411a0d507c7d814008f56c8f260ad7ed99e2df8d9cb1a575d55264692629ae22e518bc14ad02592941c13be6755c72973

compressed_key = '039E87EB177890FDD788B95843ED53AD4FB6E877E3F730EF1E73593964C2AB9D15'
Code:
049e87eb177890fdd788b95843ed53ad4fb6e877e3f730ef1e73593964c2ab9d15a3b647c8c4a0766420917b7b445cdcd6bfec2900175c5534c6113954f3ff00d9

Unfortunatelly in my case it outputs incorrect values (Pycharm Python 3.10.5)
Here's the rundown of the cases:
https://i.stack.imgur.com/VgZhr.png


Test 1:
Code:
042351126323155009915566305515988222562668831973356296881287277305661165837788ee29766cfd677f845e830bea820e1ef951b86fdc335247a8aa697e6baf1ef78
Test 2:
Code:
046799846282981021315422058923075594964980219729412475060169781627609169100505f87b4bda8ed8b93cff4207e66c65b4909a48a3b54076aac6d6ba7626e15ba875
Test 3:
Code:
049081844685017147717691639198226214703337765349173402524026447080150572702997d7b2db226c460a5e7167c150fab386c8688dd4170d3d453a2c3efad529c73e75


10  Bitcoin / Development & Technical Discussion / How to convert a compressed public key into uncompressed one in Python? on: August 30, 2022, 10:05:16 AM
I've searched really hard for a Python script that coverts a compressed public key into uncompressed one, since I don't know how to code this. I stumbled upon these links:

https://stackoverflow.com/questions/43629265/deriving-an-ecdsa-uncompressed-public-key-from-a-compressed-one
https://bitcoin.stackexchange.com/questions/86234/how-to-uncompress-a-public-key
https://bitcointalk.org/index.php?topic=644919.0

but all python scripts/functions either don't work or outputting incorrect values. I tried to debug them and failed. Could you point out how to code something like this in Python or give an example of the working code? Here are the values to play with (HEX):

Code:
#Test case 1
#Compressed Pubkey: 025A2146590B80D1F0D97CC7104E702011AFFF21BFAF817F5C7002446369BA9DDC
'''
Right Uncompressed Pubkey:
045A2146590B80D1F0D97CC7104E702011AFFF21BFAF817F5C7002446369BA9DDC9BD5DCD1B4A737244D6BB7B96E256391B8597D3A7972A6F8CA9096D4AEA1F37E
'''

#Test case 2
#Compressed Pubkey: 035728F4692D85D411DF3643CD69FE05C411A0D507C7D814008F56C8F260AD7ED9
'''
Right Uncompressed Pubkey:
045728F4692D85D411DF3643CD69FE05C411A0D507C7D814008F56C8F260AD7ED99E2DF8D9CB1A575D55264692629AE22E518BC14AD02592941C13BE6755C72973
'''

#Test case 3
#Compressed Pubkey:039E87EB177890FDD788B95843ED53AD4FB6E877E3F730EF1E73593964C2AB9D15
'''
Right Uncompressed Pubkey:
049E87EB177890FDD788B95843ED53AD4FB6E877E3F730EF1E73593964C2AB9D15A3B647C8C4A0766420917B7B445CDCD6BFEC2900175C5534C6113954F3FF00D9
11  Bitcoin / Development & Technical Discussion / Re: Fail at coding my own Secp256k1 function (Pyhon) on: August 12, 2022, 12:42:37 PM
...
I got it, and  your suggestions are useful. I changed the code here and on Github. However It still doesn't work for small numbers (See the test case 1)
https://github.com/MaltoonYezi/Python-DSA/blob/main/Cryptography/SECP256k1Procedural.py
Sorry, for a delayed response

You got mistaken somewhere (or, as usual, in many places).

Test case 1 asks us to multiply (47,71) by 8 modulo 223 (with a=0, b=7). However with this prime the curve has structure 42x6, it is noncyclic.  Point (47,71) is from the group of (7,166); while (49,71) is from the group of (8,127). So, you could never reach either point from the other one.

You might want to change the test. If you insist on modulo 223, then b=5 is a good choice. Otherwise (b=7) use modulo 67 or 79, they are a bit like p and n in secp256k1, modulo one of them the group size is the other.



Yeah that's a good idea. However these test case 1 values I took from the book, and I checked the values once again and the problem was that the right values in the comments were incorrect. Now that I've refactored the script, it works fine. Also, Public keys outputs are being correct as well.
Thanks for your tips!
12  Bitcoin / Development & Technical Discussion / Re: Fail at coding my own Secp256k1 function (Pyhon) on: August 11, 2022, 12:25:17 PM
You're almost there, change this (in secp256k1BinaryExpansion):

Code:
if coef & 1:
    resultX, resultY = addition(currentX, currentY, gx, gy, a, b, prime)
currentX, currentY = addition(currentX, currentY, gx, gy, a, b, prime)

To this:

Code:
if coef & 1:
    resultX, resultY = addition(resultX, resultY, currentX, currentY, a, b, prime)
currentX, currentY = addition(currentX, currentY, currentX, currentY, a, b, prime)

And your second test case will pass (i.e. 0x45300f2b990d332c0ee0efd69f2c21c323d0e2d20e7bfa7b1970bbf169174c82 => (40766947848522619068424335498612406856128862642075168802372109289834906557916, 70486353993054234343658342414815626812704078223802622900411169732153437188990))

I haven't checked your code carefully though, so I wouldn't consider this "working" just yet. Smiley

I got it, and  your suggestions are useful. I changed the code here and on Github. However It still doesn't work for small numbers (See the test case 1)
https://github.com/MaltoonYezi/Python-DSA/blob/main/Cryptography/SECP256k1Procedural.py
Sorry, for a delayed response
13  Bitcoin / Development & Technical Discussion / Re: Fail at coding my own Secp256k1 function (Pyhon) on: August 11, 2022, 12:09:21 PM
Code:
    while coef:
        if coef & 1:
            resultX, resultY = addition(currentX, currentY, gx, gy, a, b, prime)
        currentX, currentY = addition(currentX, currentY, gx, gy, a, b, prime)
        coef >>= 1

Let's unroll this loop:

- Current (x,y) is set to G
- Start at the least-significant bit
- If the bit is odd:
-- Then set Result = Current(x,y) + G [for the first iteration this means G+G]
- Set CurrentX += G [again, for the first iteration, it is G+G].

Do you see the problem here?

As you go through all of the bits, you are *adding* G to itself, this will make G, 2G, 3G, and so forth. You have to multiply the CurrentX by 2 each time, to get G, 2G, 4G, 8G, 16G,... (2^256-1)*G.

And each time the bit is odd, you are adding another G to the result which is already full of G's you're adding in succession, when you should set result = 0 in the initialization, and then you add it to Current (x,y). That is to say, Result += Current(x,y).

Binary expansion on private keys doesn't work without multiplication.


Ok, It is helpful. I've made a few changes. But it still doesn't work for small numbers (Don't know why),
https://github.com/MaltoonYezi/Python-DSA/blob/main/Cryptography/SECP256k1Procedural.py
Sorry, for a delayed response
14  Bitcoin / Development & Technical Discussion / [Solved] Fail at coding my own Secp256k1 function (Pyhon) on: August 09, 2022, 07:36:06 AM
Fail at coding my private to public key converter script for Bitcoin (Secp256k1)

Currently going through the book "Programming Bitcoin by Jimmy Song", got stuck on page 61 (Chapter 3), but completed the exercise 5 from chapter 3. You can view the source code: here or in Github .

Even though the book is great for understanding cryptographic different concepts, highly abstracted OOP code from the book makes it somewhat harder to gaining the intuition of the fundamental low-level concepts behind key principles. That's why apart from completing exercises, I like to also code my own procedural functions that solve the same problems.

I've tried to code an ECC Secp256k1 priv-to-pub key conversion function, but my implementation... just doesn't work.

It converts numbers incorrectly.

The code for the script is down below, I've highlighted the part where the function stops
Code:
#Secp256k1 Bitcoin private to public key converter script
a = 0
b = 7
#Order of the finite field
prime = 2**256 - 2**32 - 977
#G coordinates
gx = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
#Order of the group G
n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141
#n -1 => is the number of all possible private keys
privateKey = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140

def addition(currentX, currentY, gx, gy, a, b, prime):
    if gy == 0:
        return (None, None)
    elif currentX is None and currentY is None:
        return (gx, gy)
    elif currentX == gx and currentY != gy:
        return (None, None)
    elif currentX == gx and currentY == gy and currentY == 0:
        return (None, None)
    elif currentX == gx and currentY == gy:
        s1 = (3 * pow(gx, 2, prime) + a) % prime
        s2 = (gy * 2) % prime
        s = (s1 * pow(s2, (prime - 2), prime)) % prime
        currentX = (s ** 2 - 2 * gx) % prime
        currentY = (s * (gx - currentX) - gy) % prime
    elif currentX != gx:
        s1 = (currentY - gy)
        s2 = (currentX - gx)
        s = (s1 * pow(s2, (prime - 2), prime)) % prime
        currentX = ((s ** 2) - gx - currentX) % prime
        currentY = ((s * (gx - currentX)) - gy) % prime

    return (currentX, currentY)


def secp256k1BinaryExpansion(privateKey, gx, gy, a, b, prime):
    if pow(gy, 2, prime) != (pow(gx, 3, prime) + a * gx + b) % prime:
        return "The point is not on the curve"
    coef = privateKey
    currentX, currentY = gx, gy
    resultX, resultY = None, None
    while coef:
        if coef & 1:
            resultX, resultY = addition(resultX, resultY, currentX, currentY, a, b, prime)
        currentX, currentY = addition(currentX, currentY, currentX, currentY, a, b, prime)
        coef >>= 1
    return (resultX, resultY)

#privateKey, gx, gy, a, b, prime
#Smaller numbers (Not Secp256k1). Right output for this is: (116, 55)
print(secp256k1BinaryExpansion(8, 47, 71, a, b, 223))

#Test case 2
priv = 0x45300f2b990d332c0ee0efd69f2c21c323d0e2d20e7bfa7b1970bbf169174c82
xPub, yPub = secp256k1BinaryExpansion(priv, gx, gy, a, b, prime)
xPub, yPub = xPub, yPub
print("Public key coordinates:", xPub,",", yPub)
#The right values for test case 2:
#x = 40766947848522619068424335498612406856128862642075168802372109289834906557916
#y = 70486353993054234343658342414815626812704078223802622900411169732153437188990

#Test case 2.1. Full Pulic key for test case 2:
print("Public key (hex), (Uncompressed) :", "04" + (str(hex(xPub)[2:])) + (str(hex(yPub)[2:])))
#The right public key (Uncormpressed):
#045A2146590B80D1F0D97CC7104E702011AFFF21BFAF817F5C7002446369BA9DDC9BD5DCD1B4A737244D6BB7B96E256391B8597D3A7972A6F8CA9096D4AEA1F37E
print("Public key (hex), (Compressed) :", "02" + (str(hex(xPub)[2:])))
#The right public key (Compressed):
#025A2146590B80D1F0D97CC7104E702011AFFF21BFAF817F5C7002446369BA9DDC

Link for the script

The main function uses "Binary expansion" technique, but it seems like the problem lies in the "Addition" function that doesn't have it.

To see some results I copied OOP code from the book, refactored it a bit uploaded to github and it works:

https://github.com/MaltoonYezi/Python-DSA/blob/main/Cryptography/SECP256K1OOP.py

Tried to debug the 1st code by myself, but failed. If you could help, I'd appreciate it!

Edit 1: Updated the code, here on GitHub. Works for big numbers but works correctly for small numbers.
In test case 2 coordinates for public key are right, but the public key itself is wrong

Edit 2: I've refactored the script once again and everything works now. The test case 1 values in the comments were incorrect and now it outputs the right values. Also the public key outputs are also working properly.
Thanks for your help!
 
15  Bitcoin / Development & Technical Discussion / Re: [Solved] Programming Bitcoin , Jimmy Song. ecc.py functions aren't working on: January 21, 2022, 06:00:37 PM
I see you solved your issue, however there was one thing I want to point out.
The Python error messages actually tell you a lot about what's going wrong, to the point where I could figure out some of the issues completely statically. One example is where it's telling you that the function __add__ is not defined. We can see in your code that your indents are all over the place, putting the __add__ function within the __ne__ function, so it's invisible (nonexistent) from the outside.
Quote
To be safe I copied and pasted functions from answers.py into ecc.py . The problem still persists
Picture 1: https://i.stack.imgur.com/Cihsv.jpg
Picture 2: https://i.stack.imgur.com/tlIig.jpg
Picture 3: https://i.stack.imgur.com/gQHf9.jpg

Also here, it directly tells you your indent is off and you seem just not to read these error messages. They exist for a reason.
Quote

I think that Jimmy Song here wanted to make readers implement everything in a TDD way, which is a very good idea and teaches learners a lot about the language itself and how to develop cleanly. You make use of the test framework by writing tests beforehand according to spec / concept, then develop until your tests are all 'green'.



Also, it's actually a rule here not to write consecutive posts, but merging them instead, by the way.
32. Posting multiple posts in a row (excluding bumps and reserved posts by the thread starter) is not allowed.


Thank you for the tip!!!!!!!!!!!!!!!!!!!
16  Bitcoin / Development & Technical Discussion / Re: Programming Bitcoin , Jimmy Song. ecc.py functions aren't working on: January 17, 2022, 05:43:28 PM
--snip--
I can confirm copying the answer to ecc.py (rather than trying add function to imported object) works (passed the test).

Yeah, I've been aware of ecc.py and answers.py . The functions were implemented into ecc.py from the beginning

To be safe I copied and pasted functions from answers.py into ecc.py . The problem still persists
Picture 1: https://i.stack.imgur.com/Cihsv.jpg
Picture 2: https[Suspicious link removed]rcise 1 only ask you to edit def __ne__ on ecc.py, so you don't need to modify cell Exercise 1 on Chapter1.ipynb. Just run the cell and see whether you passed the test or not.



LMAO, this is really a silly mistake  Cheesy Cheesy Cheesy. I thought that I was supposed to call these functions, from ecc.py to Chapter1.ipynb, and then use the functions to solve specific problems.....

but Anyway, the tests are running OK, just like in the screenshot you've provided....

Thanks!!
17  Bitcoin / Development & Technical Discussion / Re: Programming Bitcoin , Jimmy Song. ecc.py functions aren't working on: January 15, 2022, 05:33:29 PM
Your installation is fine; it all looks like it's running fine.
The errors appear because it's your task to implement the functions so that the tests (who output the errors you're seeing) 'pass'.
As we can see in the code, the __ne__ function is simply not implemented yet and a NotImplementedError error is raised. You are meant to remove this line and implement the function's functionality. That's the whole task here.

Spoiler: Here, in the answers.py file, you can see the solution to the exercise 1:
https://github.com/jimmysong/programmingbitcoin/blob/3fba6b992ece443e4256df057595cfbe91edda75/code-ch01/answers.py#L126

As I suspected, you're meant to simply implement the __ne__ function. So no, there are no systematic errors, it's the whole repo's purpose to give you programming problems to solve, which then result in working tests (and no error messages).

I tried to do what you've recommended, but It still doesn't work
The functions are implemented.
Please see my replies above

btw
Sorry for the delayed response
18  Bitcoin / Development & Technical Discussion / Re: Programming Bitcoin , Jimmy Song. ecc.py functions aren't working on: January 15, 2022, 05:12:45 PM
In the second picture, it says that test_add() is not defined. You should make sure that function actually exists by running its cell first, if the function is something you meant to write yourself and not copy from a book somewhere.

test_ne() test_add() and test_sub() are defined
Picture 1: https://i.stack.imgur.com/ROZ4n.jpg

Unfortunately, they are still not working
Picture 2: https://i.stack.imgur.com/jI4qa.jpg
Picture 3: https://i.stack.imgur.com/LUiAs.jpg


19  Bitcoin / Development & Technical Discussion / Re: Programming Bitcoin , Jimmy Song. ecc.py functions aren't working on: January 15, 2022, 04:58:19 PM

The errors appear because it's your task to implement the functions so that the tests (who output the errors you're seeing) 'pass'.
As we can see in the code, the __ne__ function is simply not implemented yet and a NotImplementedError error is raised. You are meant to remove this line and implement the function's functionality. That's the whole task here.

FYI, chapter 1 of the book also state you need to open ecc.py to start the exercise. I can see this confuse some people though, since i expected explicit instruction such as "To solve exercise 1, you need to open ecc.py and edit __ne__ function".

You can see this in action in the code that accompanies this book. Once you’ve set up Jupyter Notebook (see [setting_up]), you can navigate to code-ch01/Chapter1.ipynb and run the code to see the results. For the next exercise, you’ll want to open up ecc.py by clicking the link in the Exercise 1 box. If you get stuck, please remember that the answers to every exercise are in [appendix_solutions].



Spoiler: Here, in the answers.py file, you can see the solution to the exercise 1:
https://github.com/jimmysong/programmingbitcoin/blob/3fba6b992ece443e4256df057595cfbe91edda75/code-ch01/answers.py#L126

As I suspected, you're meant to simply implement the __ne__ function. So no, there are no systematic errors, it's the whole repo's purpose to give you programming problems to solve, which then result in working tests (and no error messages).

I can confirm copying the answer to ecc.py (rather than trying add function to imported object) works (passed the test).

Yeah, I've been aware of ecc.py and answers.py . The functions were implemented into ecc.py from the beginning

To be safe I copied and pasted functions from answers.py into ecc.py . The problem still persists
Picture 1: https://i.stack.imgur.com/Cihsv.jpg
Picture 2: https://i.stack.imgur.com/tlIig.jpg
Picture 3: https://i.stack.imgur.com/gQHf9.jpg

Importing answers.py itself into doesn't do anything
Picture 4: https://i.stack.imgur.com/kHf5w.jpg

Copying and pasting the functions themselves in the program does not help as well
Picture 5: https://i.stack.imgur.com/H89nK.jpg

Sorry for the delayed response



20  Bitcoin / Development & Technical Discussion / [Solved] Programming Bitcoin , Jimmy Song. ecc.py functions aren't working on: January 13, 2022, 07:35:45 PM
Programming Bitcoin book, Jimmy Song. Chapter 1 ecc.py functions aren't recognized even though the ecc.py (the source file of the functions) is in the same folder as the file in which the Functions are being called
Picture 1: https://i.stack.imgur.com/njYMU.jpg
Picture 2: https://i.stack.imgur.com/ZTg8R.jpg
Picture 3: https://i.stack.imgur.com/nNuTL.jpg
Picture 4: https://i.stack.imgur.com/eje4Q.jpg
The functions are written:
Picture 5: https://i.stack.imgur.com/hLQPM.jpg
Picture 6: https://i.stack.imgur.com/jdEcZ.jpg
Picture 7: https://i.stack.imgur.com/sMnoC.jpg


I start the Jupyter notebook every time by typing these commands in the command prompt
Picture 8: https://i.stack.imgur.com/pSAZa.jpg

Code:
@echo on
cd C:\programmingbitcoin\programmingbitcoin

virtualenv -p C:\programmingbitcoin\programmingbitcoin\.venv\Scripts\python.exe .venv

.venv\Scripts\activate.bat

jupyter notebook
REM DO NOT CLOSE THE COMMAND PROMPT! THAT WOULD SHUTDOWN THE JUPYTER!
pause

and keep the command prompt running

Picture 9: https://i.stack.imgur.com/3VE7D.jpg

as I said, These files are in the same folder
Picture 10: https://i.stack.imgur.com/QX8Ia.jpg

The installation guide to Jupyter Notebook:
https://www.oreilly.com/library/view/programming-bitcoin/9781492031482/preface01.html#setting_up
The book's github:
https://github.com/jimmysong/programmingbitcoin

The steps to installation and starting the Jupyter Notebook I got from that guide

The question is:
What seems to be the problem here?

Why is this question being asked? There's the possibility that the problem may be systematic across the whole repository. To prevent further errors, it would be good to solve everything in advance.
The odd thing is that for the 1st time these functions were working, but after I've restarted the Jupyter notebook, they stopped doing so
The question is also posted on StackOverflow: https://stackoverflow.com/questions/70701871/programming-bitcoin-jimmy-song-ecc-py-functions-arent-working
Pages: [1] 2 »
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!