Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: dalydir on June 23, 2014, 03:34:53 AM



Title: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: dalydir on June 23, 2014, 03:34:53 AM

http://coliru.stacked-crooked.com/a/74648b16c2692525

That is my C++ code where I'm trying to spit out a bunch of sequential private keys.  I know that's not a good idea, but if my program can't do it sequentially, its not going to work in any other manner either, so, I've got to start somewhere.

This program results in the proper X,Y coordinates for private key 0x03, but 0x04 and 0x05 both have the wrong Y values but proper X values, and 0x06 and on are both wrong X values and Y values.  This whole thing is very confusing and I am not getting it and have already put well over a week into just getting this far in this program (I don't exactly have the greatest knowledge or experience in C++).  Any help anyone could provide from looking at my code would be greatly appreciated.

If this doesn't really belong in this forum, please let me know.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: jonald_fyookball on June 23, 2014, 09:49:53 PM
took a quick look.

Why aren't you simply using an ECDSA library?




Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: dalydir on June 23, 2014, 09:53:05 PM
I'm not aware of one.  I had a hell of a time even finding a 256+ bit integer library as it was.  Do you recommend a particular ecdsa library?


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: DeathAndTaxes on June 23, 2014, 10:43:29 PM
I took a look at your code and honestly I am not sure what you are doing.   As pointed out trying to reinvent ECDSA support is probably not a good idea.   There are a number of Bitcoin specific libraries but if you want to drop down a level both bouncy castle and openssl support all the ECDSA functions needed to implement a bitcoin node.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: dalydir on June 24, 2014, 12:32:43 AM
Current theory is the only thing wrong with my code is something to do with the inverse function.  My programming abilities are not....all that great.  I would DEFINITELY like to implement opencl as it would run substantially faster to (at least that is my understanding) but I might trip over those ideas even harder than my current dilemmas.  Based upon your suggestions I will look into those things in the meantime though, thank you.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: jonald_fyookball on June 24, 2014, 01:15:45 AM
I'm not aware of one.  I had a hell of a time even finding a 256+ bit integer library as it was.  Do you recommend a particular ecdsa library?

no offense but you need to work on your googling skills...

looks like plenty of options.

https://www.google.com/?gws_rd=ssl#q=ECDSA+library+C%2B%2B



Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: DeathAndTaxes on June 24, 2014, 01:39:31 AM
Current theory is the only thing wrong with my code is something to do with the inverse function.  My programming abilities are not....all that great.  I would DEFINITELY like to implement opencl as it would run substantially faster to (at least that is my understanding) but I might trip over those ideas even harder than my current dilemmas.  Based upon your suggestions I will look into those things in the meantime though, thank you.

If your programming skills are "not all that great" I would strongly recommend not trying to implement the nuts and bolts of low level crypto.  Even working with high level bitcoin specific libraries (like bitcoinj for java) can be a challenge and using libraries like that all the low level plumbing is abstracted away.   This isn't to say you shouldn't ever build a crypto library but to start there but it would be like someone deciding they want to make a video game and despite having limited programming skills accepting nothing less than writing it all in assembly language so it is optimized.

As for using OpenCL for acceleration I am pretty sure it would be a decelerator.  I would recommend a lot of reading (both wiki and the bitcoin core source code) about how Bitcoin works.  Verification of transactions and blocks is almost never CPU limited.   The disk (IO not capacity) and network bandwidth are more significant bottlenecks, after that is probably memory space (although luckily RAM is dirt cheap), far behind that would probably be disk capacity (especially for higher performance disks like SSD), and then way way way behind that would be processing power.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: dalydir on June 24, 2014, 05:32:01 AM
jonald_fyookball, I didn't say I had searched for that in particular.  I hadn't googled it at that time.  I hadn't considered that phrasing or consider the existence of something as simple as an "ECDSA library".

DeathandTaxes, I do not doubt the accuracy of your analogy, but to the extent I can piece things together, I intend to continue.  Your references to speed are accurate.  Though when my processing power is bottlenecked by my SSD I also have RAMDisk and can run the entire operation from RAM if I need too, though my bottleneck has actually been the processing.

If anyone can make sense of:

def inverse(x, p):
"""
Calculate the modular inverse of x ( mod p )
the modular inverse is a number such that:
(inverse(x, p) * x) % p == 1
you could think of this as: 1/x
"""
inv1 = 1
inv2 = 0
while p != 1 and p!=0:
inv1, inv2 = inv2, inv1 - inv2 * (x / p)
x, p = p, x % p
 
return inv2

Which is in Python, it would solve my dilemma.  The commas don't make sense to me (IE: "How can a comma work with the equal sign").  That seems to be the part of my program which doesn't function correctly.  Yes, my code has that as well; that's the only part of my code I don't understand piece for piece (as I had to copy and paste that part).  All I'm really trying to do is to get this code to work.  This code should just spit out the public key for addresses represented by the number 4 through the number 10.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: jonald_fyookball on June 24, 2014, 02:03:39 PM
see this:

http://stackoverflow.com/questions/17818092/unpacking-strange-commas-in-python


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: dalydir on June 25, 2014, 05:01:48 AM
Thanks jonald_fyookball, I checked that out.  Turns out that wasn't enough to get it fully going.  Currently researching how to do the modular arithmetic from scratch, its basically the next step.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: Remember remember the 5th of November on June 25, 2014, 10:52:27 AM
If the man wants to reimplement something, let him. That is how he is going to learn.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: dalydir on June 25, 2014, 09:18:43 PM
Even if what I'm doing fully fits the definition of "reimplementation", It doesn't sound so bad due to the following:

All of us are trusting Bitcoin, the programming, the algorithms, etc.  I think it stands to reason that we have nothing to lose by better understanding the equations as fundamentally as we really can.  What if one in a million people notices something..."wrong" with it?  That's kind of one of the very points of "open source".  Anyone who wishes to verify the coding is permitted and able to do so.  Obviously understanding it is another matter, but with posts like mine, we may be able to assist people in doing just that.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: Remember remember the 5th of November on June 25, 2014, 09:46:25 PM
Even if what I'm doing fully fits the definition of "reimplementation", It doesn't sound so bad due to the following:

All of us are trusting Bitcoin, the programming, the algorithms, etc.  I think it stands to reason that we have nothing to lose by better understanding the equations as fundamentally as we really can.  What if one in a million people notices something..."wrong" with it?  That's kind of one of the very points of "open source".  Anyone who wishes to verify the coding is permitted and able to do so.  Obviously understanding it is another matter, but with posts like mine, we may be able to assist people in doing just that.
You'll have to excuse Death and Taxes's god complex, he likes to butt in conversations and push his own views onto people(i.e discourage them from reimplementing stuff).


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: jonald_fyookball on June 25, 2014, 10:13:28 PM
If the man wants to reimplement something, let him. That is how he is going to learn.

I agree... let him code it if he wants to!

Gotta say though, I don't agree at all about DeathandTaxes having any kind
of superiority complex.  I've been in several conversations
with him, and read many of his posts, and I've never seen him talk
down to anyone.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: dalydir on June 26, 2014, 01:57:17 AM
Appreciate the support.  Though I have provided a large portion of the necessary program for people to understand some of the bitcoin equations...  I wouldn't mind some additional input either. 

http://rosettacode.org/wiki/Modular_inverse

Supposedly shows the coding for C++ for modular multiplicative inverse (which is my "mul_inv" function).  Mine is basically the same equation but the results are not what they are supposed to be.  If anyone can gleen any insight on that and how, maybe, I need to modify it, I'd really appreciate it.  Once this is solved it could assist anyone in understanding this stuff....including (and, perhaps, especially) myself.

This whole dilemma arises because with the 512 int variables, one cannot divide them normally in C++ as C++ and these particular integers do not convert into decimals, so one has to work with them solely as integers (which is kind of a pain).


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: dalydir on June 26, 2014, 02:40:56 AM
the gunpowder treason and plot, i can see no reason why the fifth of november should ever be forgot.

sorry, couldn't resist.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: jonald_fyookball on June 26, 2014, 04:21:32 AM
Appreciate the support.  Though I have provided a large portion of the necessary program for people to understand some of the bitcoin equations...  I wouldn't mind some additional input either. 

http://rosettacode.org/wiki/Modular_inverse

Supposedly shows the coding for C++ for modular multiplicative inverse (which is my "mul_inv" function).  Mine is basically the same equation but the results are not what they are supposed to be.  If anyone can gleen any insight on that and how, maybe, I need to modify it, I'd really appreciate it.  Once this is solved it could assist anyone in understanding this stuff....including (and, perhaps, especially) myself.

This whole dilemma arises because with the 512 int variables, one cannot divide them normally in C++ as C++ and these particular integers do not convert into decimals, so one has to work with them solely as integers (which is kind of a pain).

I really don't have much interest in bogging myself down in low level functions but
With a quick look , seems you are not using the same equation...they are staring off
Defining r as a mod b, and I don't see you doing that.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: jw! on June 26, 2014, 08:33:16 PM
your code
while (b != 0)

the rosetta code
while (a > 1)

your while loop will terminate for very different reasons.
The keys you got right you must have gotten lucky with
now mind telling me what the heck your doing?

calculating the modular multiplicative inverse, how does that make bitcoin?

I thought bitcoin took a big ulgy number and computed it's sha1.
if the sha1 was less then some other ulgy number called the merkle root then the mining program would say yea!


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: Remember remember the 5th of November on June 26, 2014, 08:52:32 PM
your code
while (b != 0)

the rosetta code
while (a > 1)

your while loop will terminate for very different reasons.
The keys you got right you must have gotten lucky with
now mind telling me what the heck your doing?

calculating the modular multiplicative inverse, how does that make bitcoin?

I thought bitcoin took a big ulgy number and computed it's sha1.
if the sha1 was less then some other ulgy number called the merkle root then the mining program would say yea!
SHA1? I haven't read the bitcoin protocol in a while, but last I checked it was SHA-256.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: jonald_fyookball on June 26, 2014, 08:53:38 PM
your code
while (b != 0)

the rosetta code
while (a > 1)

your while loop will terminate for very different reasons.
The keys you got right you must have gotten lucky with
now mind telling me what the heck your doing?

calculating the modular multiplicative inverse, how does that make bitcoin?

I thought bitcoin took a big ulgy number and computed it's sha1.
if the sha1 was less then some other ulgy number called the merkle root then the mining program would say yea!
SHA1? I haven't read the bitcoin protocol in a while, but last I checked it was SHA-256.

Poster is talking nonsense  ;)


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: dalydir on June 26, 2014, 09:22:24 PM
your code
while (b != 0)

the rosetta code
while (a > 1)

your while loop will terminate for very different reasons.
The keys you got right you must have gotten lucky with
now mind telling me what the heck your doing?

calculating the modular multiplicative inverse, how does that make bitcoin?

I thought bitcoin took a big ulgy number and computed it's sha1.
if the sha1 was less then some other ulgy number called the merkle root then the mining program would say yea!

You're right, but when I use the (a > 1) it doesn't produce even a single correct x or y coordinate. 

I'm not "mak[ing] bitcoin".  This is a small program that is supposed to calculate the x and y coordinates from one's private key.  these x and y coordinates are basically your "public key".


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: jonald_fyookball on June 26, 2014, 09:41:16 PM
Public keys are generated from the private key using ECDSA and secp256k1.

You have 3 options:

1. Simply use an ECDSA library (easiest)
2. Take a look an existing library and try to reimplement it. ( a bit harder)
3.  Code ECDSA from scratch (hardest).

If you want to go the hard way, start here:
http://en.m.wikipedia.org/wiki/Elliptic_Curve_DSA

And you'll need the curve bitcoin uses (secp256k1)
https://en.bitcoin.it/wiki/Secp256k1


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: dalydir on June 26, 2014, 10:53:07 PM
Public keys are generated from the private key using ECDSA and secp256k1.

You have 3 options:

1. Simply use an ECDSA library (easiest)
2. Take a look an existing library and try to reimplement it. ( a bit harder)
3.  Code ECDSA from scratch (hardest).

If you want to go the hard way, start here:
http://en.m.wikipedia.org/wiki/Elliptic_Curve_DSA

And you'll need the curve bitcoin uses (secp256k1)
https://en.bitcoin.it/wiki/Secp256k1

The only thing that appears to be incorrect in my application (as it stands) is the multiplicative inverse.  I've coded the majority of the rest of the ECDSA algorithm as I currently intend to implement it.  If you know of an ECDSA library in which all I have to do is give it the private key, and it'll kick back the public key, please let me know and give me a simple example of that. 

Like:

publickey = prvtkeytopublickey ( <insert private key> )

If you know of one that simple I would be very grateful if you could identify that for me.  I can even do these equations on sagemath.com but C++'s coding just doesn't work with the ECDSA equations as smoothly as one would hope.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: jonald_fyookball on June 26, 2014, 11:12:49 PM
http://www.cryptopp.com/


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: dalydir on June 26, 2014, 11:32:17 PM
http://www.cryptopp.com/

Which header file is for ECDSA secp256k1 (bitcoin's algorithm) and what's the command for converting the given private key into a public key?


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: jonald_fyookball on June 27, 2014, 12:28:02 AM
http://www.cryptopp.com/

Which header file is for ECDSA secp256k1 (bitcoin's algorithm) and what's the command for converting the given private key into a public key?

I don't know because I've never used that library.

It feels like you should be researching yourself
and digging in a bit deeper...otherwise, whats
the point of this project?  I thought you wanted
to learn how to write bitcoin related code.

As a developer, sometimes you have to research
things.

Did you download the code library?
Did you go to the ECDSA section?
Have you reviewed the files, classes, and functions?
Is there any documentation?
Are there examples?
Are there likely candidates for which file or command to use?



Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: dalydir on June 27, 2014, 01:05:01 AM
http://www.cryptopp.com/

Which header file is for ECDSA secp256k1 (bitcoin's algorithm) and what's the command for converting the given private key into a public key?

I don't know because I've never used that library.

It feels like you should be researching yourself
and digging in a bit deeper...otherwise, whats
the point of this project?  I thought you wanted
to learn how to write bitcoin related code.

As a developer, sometimes you have to research
things.

Did you download the code library?
Did you go to the ECDSA section?
Have you reviewed the files, classes, and functions?
Is there any documentation?
Are there examples?
Are there likely candidates for which file or command to use?



Just to quote myself:

Quote
If you know of an ECDSA library in which all I have to do is give it the private key, and it'll kick back the public key, please let me know and give me a simple example of that.

Your response:

Quote
http://www.cryptopp.com/

along with

Quote
I don't know because I've never used that library.


So you don't know if that library is actually the answer.  Yes, I did read through it and couldn't find anything for ECDSA. 

You, and others, are insisting I should use <insert library, who's existence is not yet confirmed, here> that will resolve the matter quickly and easily, because you believe it obviously exists.  Then you suggest I use one that, as far as you know, will do nothing for me and be substantially more difficult to even figure out than just fixing the equations I've already presented.

Many of you seem to be behaving as theoreticists regarding this matter because you don't actually know the answer to the question.  I do appreciate you having a desire to help me but you're just pointing in different directions in the off chance that the solution is "that way" when you yourself don't even know if the solution is "that way", as you have not even confirmed it yourself.

Quote
Did you download the code library?

Yes I did, and it wasn't the first time I've downloaded that particular library, but I simply guessed I had perhaps overlooked something when you recommended it.

Quote
Did you go to the ECDSA section?

Yes, and no.  There was no apparent "ECDSA" section I could find.

Quote
Have you reviewed the files, classes, and functions?

To the extent that I am capable of doing, yeah, I did.

Quote
Is there any documentation?

Yes, there was, but not much.  What was there that I did understand did not appear to be sufficient to explain and or do even just the multiplicative inverse, let alone simply private key to public key.

Quote
Are there examples?

Yes, but not examples for what I've been trying to do.

Quote
Are there likely candidates for which file or command to use?

A few distantly caught my attention, but as I read through them they weren't what I was looking for.

Quote
I thought you wanted to learn how to write bitcoin related code.

Some, but the only thing I'm seeking help at this time with is converting a private key into a public key utilizing C++.

Quote
It feels like you should be researching yourself and digging in a bit deeper

I've gone as far as it seems I can go at this time, that's why I came to this forum to seek help.  To see if maybe someone could look at my code and go "oh, yeah man, look at that, you have that one equation wrong, here, go to this coliru page where I've corrected that single line".  Or if someone was going to reference a library for me they'd simply say "yeah, use abcdefg.h and type int512_t = privatekey_to_public_key_function( <private key> ) ".  I don't expect anyone to do a buttload of research for me, nor am I asking anyone to.  Just that if anyone has any insight and realizes whats going on, I'd really appreciate the help.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: jonald_fyookball on June 27, 2014, 01:17:21 AM
I took a look at cryptpop... Actually there is extensive documenation
in the manual section of the website...although, I will admit that
it is quite an abstract library and would be time consuming.

Here's a much more user friendly one in C:

http://jonasfj.dk/blog/2007/12/simpleecdsa-a-simple-implementation-of-ecdsa-in-c/

go into the test.c file, and you will see the example of how
they do it.  Hope that helps.

-Jonald

Code:
bool test_key_generation(FILE* out)
{
//First notice
fprintf(out, "\n--- Test public key generation ---\n");

//Setting up domain parameters
domain_parameters curve = domain_parameters_init();
domain_parameters_load_curve(curve, secp160r1);

//Public key
point Q = point_init();
point Q_check = point_init();

//Private key
mpz_t d;mpz_init(d);

//Load key from GEC test vectors
mpz_set_str(d, "971761939728640320549601132085879836204587084162", 10);

//Load correct result from GEC test vectors
point_set_str(Q_check, "466448783855397898016055842232266600516272889280", "1110706324081757720403272427311003102474457754220", 10);

//Generate public key
signature_generate_key(Q, d, curve);


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: jonald_fyookball on June 27, 2014, 01:31:26 AM
here's another resource you might find useful:

https://github.com/wobine/blackboard101/blob/master/EllipticCurvesPart5-TheMagic-SigningAndVerifying.py
https://www.youtube.com/watch?v=U2bw_N6kQL8


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: dalydir on June 27, 2014, 03:35:00 AM
Thank you for all that jonald.  I'm still going through it all.  Either way, I think I'm going to make some progress from all this stuff here.  Its all further confirming that its all about the multiplicative inverse.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: amaclin on June 27, 2014, 04:49:45 AM
Quote
Like:
publickey = prvtkeytopublickey ( <insert private key> )

Quick and dirty example with openssl
Code:
#include <QCoreApplication>
#include <QByteArray>

#include <stdio.h>

#include <openssl/ec.h>
#include <openssl/evp.h>
#include <openssl/ecdsa.h>
#include <openssl/bn.h>

static EC_KEY* EC_KEY_regenerate_key ( const quint8* priv )
{
  static EC_KEY* eckey = EC_KEY_new_by_curve_name ( NID_secp256k1 );
  static const EC_GROUP* group = EC_KEY_get0_group ( eckey );
  BIGNUM* privkey = BN_bin2bn ( priv, 32, BN_new ( ) );
  BN_CTX* ctx = BN_CTX_new ( );
  EC_POINT* pubkey = EC_POINT_new ( group );
  EC_POINT_mul ( group, pubkey, privkey, 0, 0, ctx );
  EC_KEY_set_private_key ( eckey, privkey );
  EC_KEY_set_public_key ( eckey, pubkey );
  EC_POINT_free ( pubkey );
  BN_CTX_free ( ctx );
  BN_clear_free ( privkey );
  return eckey;
}
//--------------------------------------------------------------
static const char* getPublicKey ( char* buf, const quint8* priv )
{
  quint8 pubkey [65];
  quint8* pbegin = pubkey;
  i2o_ECPublicKey ( EC_KEY_regenerate_key ( priv ), &pbegin );
  memcpy ( buf, pubkey + 1, 64 ); // without 0x04 prefix
  return (const char*)buf;
}
//--------------------------------------------------------------
static const QByteArray getPublicKeyClassic ( const quint8* priv )
{
  char buf [65];
  getPublicKey ( buf + 1, priv );
  buf [0] = 0x04;
  return QByteArray ( buf, 65 );
}
//--------------------------------------------------------------
static const QByteArray getPublicKeyCompressed ( const quint8* priv )
{
  char buf [65];
  getPublicKey ( buf + 1, priv );
  buf [0] = 0x02 + ( buf [64] & 1 );
  return QByteArray ( buf, 33 );
}
//--------------------------------------------------------------
int main ( int argc, char* argv [] )
{
  QCoreApplication a ( argc, argv );
  // correct horse battery staple
  const QByteArray priv ( QByteArray::fromHex ( "c4bbcb1fbec99d65bf59d85c8cb62ee2db963f0fe106f483d9afa73bd4e39a8a" ) );
  printf ( "classic=%s\n", getPublicKeyClassic ( (const quint8*)priv.constData ( ) ).toHex ( ).constData ( ) );
  printf ( "compressed=%s\n", getPublicKeyCompressed ( (const quint8*)priv.constData ( ) ).toHex ( ).constData ( ) );
  return a.exec ( );
}

Output is
Code:
classic=0478d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71a1518063243acd4dfe96b66e3f2ec8013c8e072cd09b3834a19f81f659cc3455
compressed=0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71

You can check that result is correct on brainwallet.org ( use "correct horse battery staple" as passphrase )


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: dalydir on July 01, 2014, 03:35:36 AM
I've gone through all the info everyone has provided.  That which I can make sense of doesn't work (even when cutting and pasting).

I'm starting to wonder, how much Bitcoin should I be offering for someone to just tweak this code so that (to verify the function of the equations) it produces the first 10 bitcoin addresses' public x,y coordinates? 

I've gone through so many ways of trying to achieve my results and none have succeeded.  my Coliru link in the first post is still the basis and I can't seem to locate anything, that, when applied, produces the desired results.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: jonald_fyookball on July 01, 2014, 04:03:46 AM
I've gone through all the info everyone has provided.  That which I can make sense of doesn't work (even when cutting and pasting).

I'm starting to wonder, how much Bitcoin should I be offering for someone to just tweak this code so that (to verify the function of the equations) it produces the first 10 bitcoin addresses' public x,y coordinates?  

I've gone through so many ways of trying to achieve my results and none have succeeded.  my Coliru link in the first post is still the basis and I can't seem to locate anything, that, when applied, produces the desired results.


What 10 addresses do you speak of?  What is the starting point here?
Do you have the public keys, the private keys?  What data do you have
to begin with?

I ran the python code provided above, and it does produce the public
key from the private key using the elliptic curve math.

But I don't think you can produce the x,y coordinates from just
a bitcoin address, as that is a hash of the public key... so, what
are we talking about here?


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: dalydir on July 01, 2014, 04:32:49 AM
My program is just set to take the first and second Private Keys and public keys and using that data to do "point addition" to produce public keys for private keys 3 through 10.

Private Key "1" has a public x,y key of:
55066263022277343669578718895168534326250603453777594175500187360389116729240
32670510020758816978083085130507043184471273380659243275938904335757337482424

Private Key "2" has a public x,y key of:
89565891926547004231252920425935692360644145829622209833684329913297188986597
12158399299693830322967808612713398636155367887041628176798871954788371653930

If you were to use any bitcoin website or program which takes a private key, and gives you a public key, hash, and address, putting "1" in will get you the first x,y coordintes I showed, and putting in a "2" will produce the second set.  

I do intend to use this programming beyond the first 10 addresses, but if I can't get it to work for the first 10 addresses, it won't work for any other address.

http://coliru.stacked-crooked.com/a/74648b16c2692525


And which python code are you referring to?  One I ran gave me the first 1 or two addresses correctly, but not others, and it was all in floating point number format, I need the full x,y coordinates, not "5.50662630222773x10^11" type stuff.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: jonald_fyookball on July 01, 2014, 04:46:29 AM
My program is just set to take the first and second Private Keys and public keys and using that data to do "point addition" to produce public keys for private keys 3 through 10.

Private Key "1" has a public x,y key of:
55066263022277343669578718895168534326250603453777594175500187360389116729240
32670510020758816978083085130507043184471273380659243275938904335757337482424

Private Key "2" has a public x,y key of:
89565891926547004231252920425935692360644145829622209833684329913297188986597
12158399299693830322967808612713398636155367887041628176798871954788371653930

If you were to use any bitcoin website or program which takes a private key, and gives you a public key, hash, and address, putting "1" in will get you the first x,y coordintes I showed, and putting in a "2" will produce the second set.  

I do intend to use this programming beyond the first 10 addresses, but if I can't get it to work for the first 10 addresses, it won't work for any other address.

http://coliru.stacked-crooked.com/a/74648b16c2692525


And which python code are you referring to?  One I ran gave me the first 1 or two addresses correctly, but not others, and it was all in floating point number format, I need the full x,y coordinates, not "5.50662630222773x10^11" type stuff.


The one I just posted...the only one in this thread.

Here is it again.
https://github.com/wobine/blackboard101/blob/master/EllipticCurvesPart5-TheMagic-SigningAndVerifying.py

Just change the privkey variable to 1 , and you get the data you posted...change it to 2, you also get that
other set of numbers you posted...so the output is as you expected.
 
Now how much Bitcoin do I get ?  Whatever you give me will go to Dob & Noella.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: dalydir on July 01, 2014, 04:57:30 AM
My program is just set to take the first and second Private Keys and public keys and using that data to do "point addition" to produce public keys for private keys 3 through 10.

Private Key "1" has a public x,y key of:
55066263022277343669578718895168534326250603453777594175500187360389116729240
32670510020758816978083085130507043184471273380659243275938904335757337482424

Private Key "2" has a public x,y key of:
89565891926547004231252920425935692360644145829622209833684329913297188986597
12158399299693830322967808612713398636155367887041628176798871954788371653930

If you were to use any bitcoin website or program which takes a private key, and gives you a public key, hash, and address, putting "1" in will get you the first x,y coordintes I showed, and putting in a "2" will produce the second set.  

I do intend to use this programming beyond the first 10 addresses, but if I can't get it to work for the first 10 addresses, it won't work for any other address.

http://coliru.stacked-crooked.com/a/74648b16c2692525


And which python code are you referring to?  One I ran gave me the first 1 or two addresses correctly, but not others, and it was all in floating point number format, I need the full x,y coordinates, not "5.50662630222773x10^11" type stuff.


The one I just posted...the only one in this thread.

Here is it again.
https://github.com/wobine/blackboard101/blob/master/EllipticCurvesPart5-TheMagic-SigningAndVerifying.py

Just change the privkey variable to 1 , and you get the data you posted...change it to 2, you also get that
other set of numbers you posted...so the output is as you expected.
 
Now how much Bitcoin do I get ?  Whatever you give me will go to Dob & Noella.

Set it to 3.  It doesn't work.  Or take any random private key and place it there, it doesn't work either.

This is, of course, AFTER one mods the python code to be python 3.0 compatible....


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: jonald_fyookball on July 01, 2014, 01:03:54 PM
3 gives me this :  how do you know its wrong?

 
04 11271166043971060605674865917392967310211497734153940854463061355520977588812
1 25583027980570883691656905877401976406448868254816295069919888960541586679410
 



Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: christianlundkvist on July 01, 2014, 08:51:13 PM

If anyone can make sense of:

def inverse(x, p):
"""
Calculate the modular inverse of x ( mod p )
the modular inverse is a number such that:
(inverse(x, p) * x) % p == 1
you could think of this as: 1/x
"""
inv1 = 1
inv2 = 0
while p != 1 and p!=0:
inv1, inv2 = inv2, inv1 - inv2 * (x / p)
x, p = p, x % p
 
return inv2

Which is in Python, it would solve my dilemma.  The commas don't make sense to me (IE: "How can a comma work with the equal sign").  That seems to be the part of my program which doesn't function correctly.  Yes, my code has that as well; that's the only part of my code I don't understand piece for piece (as I had to copy and paste that part).  All I'm really trying to do is to get this code to work.  This code should just spit out the public key for addresses represented by the number 4 through the number 10.

The algorithm above for the modular inverse is the Extended Euclidean Algorithm (https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm), basically the algorithm will spit out an integer a with the property that a*x + b*p = 1 for some number b.

As for the commas in python, in general a,b = c,d means that a=c and b=d, and you can also do things like swapping the values of a and b by using a,b = b,a.

In our case

Code:
inv1, inv2 = inv2, inv1 - inv2 * (x / p)
x, p = p, x % p

can be written more explicitly as

Code:
temp = inv2
inv2 = inv1 - inv2 * (x / p)
inv1 = temp

temp = p
p = x % p
x = temp

When you test the code, you can check if the value a returned by your function satisfies (a*x) % p == 1.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: dalydir on July 02, 2014, 12:26:49 AM
3 gives me this :  how do you know its wrong?

 
04 11271166043971060605674865917392967310211497734153940854463061355520977588812
1 25583027980570883691656905877401976406448868254816295069919888960541586679410
 



Who knows, you may have a different version of Python installed on your computer whereby it works differently than on mine.  I have python 3 installed so I had to modify the code to keep it from erroring out first.

These are the first 5 bitcoin addresses, represented by the number 1 through 5.  The x01 is just a reference to which address and the two following numbers are the x,y coordinates.

x01
55066263022277343669578718895168534326250603453777594175500187360389116729240
32670510020758816978083085130507043184471273380659243275938904335757337482424


x02
89565891926547004231252920425935692360644145829622209833684329913297188986597
12158399299693830322967808612713398636155367887041628176798871954788371653930

x03
112711660439710606056748659173929673102114977341539408544630613555209775888121
25583027980570883691656905877401976406448868254816295069919888960541586679410

x04
103388573995635080359749164254216598308788835304023601477803095234286494993683
37057141145242123013015316630864329550140216928701153669873286428255828810018

x05
21505829891763648114329055987619236494102133314575206970830385799158076338148
98003708678762621233683240503080860129026887322874138805529884920309963580118


When I say I had to modify the code it was just for the "print" command, not the equations itself.  But typing in 3 as the private key gives me "5.506626302227735e+76 3.267051002075881e+76", because what you showed you got for that address did appear to be correct, which is awesome.  My only guess is you have a different version of python installed.

Christianlundkvist, Thanks for putting it like that, I may be able to work with that too (hopefully that'll identify why my code isn't working properly after address 3


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: dalydir on July 02, 2014, 12:50:17 AM

If anyone can make sense of:

def inverse(x, p):
"""
Calculate the modular inverse of x ( mod p )
the modular inverse is a number such that:
(inverse(x, p) * x) % p == 1
you could think of this as: 1/x
"""
inv1 = 1
inv2 = 0
while p != 1 and p!=0:
inv1, inv2 = inv2, inv1 - inv2 * (x / p)
x, p = p, x % p
 
return inv2

Which is in Python, it would solve my dilemma.  The commas don't make sense to me (IE: "How can a comma work with the equal sign").  That seems to be the part of my program which doesn't function correctly.  Yes, my code has that as well; that's the only part of my code I don't understand piece for piece (as I had to copy and paste that part).  All I'm really trying to do is to get this code to work.  This code should just spit out the public key for addresses represented by the number 4 through the number 10.

The algorithm above for the modular inverse is the Extended Euclidean Algorithm (https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm), basically the algorithm will spit out an integer a with the property that a*x + b*p = 1 for some number b.

As for the commas in python, in general a,b = c,d means that a=c and b=d, and you can also do things like swapping the values of a and b by using a,b = b,a.

In our case

Code:
inv1, inv2 = inv2, inv1 - inv2 * (x / p)
x, p = p, x % p

can be written more explicitly as

Code:
temp = inv2
inv2 = inv1 - inv2 * (x / p)
inv1 = temp

temp = p
p = x % p
x = temp

When you test the code, you can check if the value a returned by your function satisfies (a*x) % p == 1.

Just realized, the problem isn't that I'm getting a solution to the problem, its getting the wrong solution....I think.  The whole result keeps giving me negative y coordinates for the public key, which, mathematically, may make sense, but its not a valid public key in it of itself.  When I add "p", or the mod number to it to provide me with a positive number, it gives me a valid y coordinate, but the wrong y coordinate for the given private key.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: jonald_fyookball on July 02, 2014, 01:17:33 AM
what you said for privkey=3 is the same as what i'm getting -- long number starting with "112",
so i don't know what you're talking about...if you're not seeing those numbers, or seeing them
in e number notation , then you may be right that the python code would need to converted
or somehow configured...should be easy enough... google is your friend...  sorry I don't have
time to help further on that.

JF


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: dalydir on July 02, 2014, 02:24:35 AM
what you said for privkey=3 is the same as what i'm getting -- long number starting with "112",
so i don't know what you're talking about...if you're not seeing those numbers, or seeing them
in e number notation , then you may be right that the python code would need to converted
or somehow configured...should be easy enough... google is your friend...  sorry I don't have
time to help further on that.

JF

Thanks Jonald.  If you do find out what version of python you have on your computer that might make a difference for me too, then I can find out what the differences were between your version and mine.


Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: jonald_fyookball on July 02, 2014, 02:45:51 AM
I run python 2.7.



Title: Re: Issues with programming, Bitcoin, Private Keys, and Public Keys
Post by: dalydir on July 02, 2014, 05:11:01 AM
Thanks Jonald, I reverted my Python from 3.3 to 2.7.  Though I noticed some interesting things;

Some addresses produce the correct x,y coordinate, some do not.  Its really weird.  I verify the coordinates for the given private key with Bitcoin Address Utility.  

So far, these three addresses produce x,y coordinates that don't match the utility:

218177
823783908234
1257139

But several others do match, go figure.