Bitcoin Forum
May 12, 2024, 06:03:09 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: updated python question  (Read 552 times)
mank (OP)
Newbie
*
Offline Offline

Activity: 8
Merit: 0


View Profile
November 30, 2013, 03:01:09 PM
 #1

From the last post private to public question.  Having gone over the web pages that talk about elliptic math, there is still an implementation question.  The only place I have found this is in the pybitcointools.  I emailed the author with no reply.  Below is the code.  You will notice the INV function.  This is interesting as it replaces a divide.  I have not seen this done before. 

Can anyone decode this to a simple math function.  Because of the INV high/low and private key odd/even it looks like the system falls into four states of functionality.  Not knowing python enough to unravel the nested functions it is a rosetta problem for me.  Some thing like Public X = ( long normal function ) * Private key Mod P or the like.  It looks like with the two tests will wind up with four possible normal functions.  In base ten would be good as then all of the conversions are set aside.

P = 2**256-2**32-2**9-2**8-2**7-2**6-2**4-1
N = 115792089237316195423570985008687907852837564279074904382605163141518161494337
A = 0
Gx = 55066263022277343669578718895168534326250603453777594175500187360389116729240
Gy = 32670510020758816978083085130507043184471273380659243275938904335757337482424
G = (Gx,Gy)

def inv(a,n):
  lm, hm = 1,0
  low, high = a%n,n
  while low > 1:
    r = high/low
    nm, new = hm-lm*r, high-low*r
    lm, low, hm, high = nm, new, lm, low
  return lm % n

### Elliptic Curve functions

def isinf(p): return p[0] == 0 and p[1] == 0

def base10_add(a,b):
  if isinf(a): return b[0],b[1]
  if isinf(b): return a[0],a[1]
  if a[0] == b[0]:
    if a[1] == b[1]: return base10_double(a[0],a[1])
    else: return (0,0)
  m = ((b[1]-a[1]) * inv(b[0]-a[0],P)) % P
  x = (m*m-a[0]-b[0]) % P
  y = (m*(a[0]-x)-a[1]) % P
  return (x,y)
 
def base10_double(a):
  if isinf(a): return (0,0)
  m = ((3*a[0]*a[0]+A)*inv(2*a[1],P)) % P
  x = (m*m-2*a[0]) % P
  y = (m*(a[0]-x)-a[1]) % P
  return (x,y)

def base10_multiply(a,n):
  if isinf(a) or n == 0: return (0,0)
  if n == 1: return a
  if n < 0 or n >= N: return base10_multiply(a,n%N)
  if (n%2) == 0: return base10_double(base10_multiply(a,n/2))
  if (n%2) == 1: return base10_add(base10_double(base10_multiply(a,n/2)),a)

def privkey_to_pubkey(privkey):
  if isinstance(privkey,(int,long)):
      return base10_multiply(G,privkey)
  if len(privkey) == 64:
      return point_to_hex(base10_multiply(G,decode(privkey,16)))
  elif len(privkey) == 66:
      return compress(base10_multiply(G,decode(privkey[:-2],16)),'hex')
  elif len(privkey) == 32:
      return point_to_hex(base10_multiply(G,decode(privkey,16)))
  elif len(privkey) == 33:
      return compress(base10_multiply(G,decode(privkey[:-1],16)),'bin')
  else:
      return privkey_to_pubkey(b58check_to_hex(privkey))

privtopub = privkey_to_pubkey
1715493789
Hero Member
*
Offline Offline

Posts: 1715493789

View Profile Personal Message (Offline)

Ignore
1715493789
Reply with quote  #2

1715493789
Report to moderator
1715493789
Hero Member
*
Offline Offline

Posts: 1715493789

View Profile Personal Message (Offline)

Ignore
1715493789
Reply with quote  #2

1715493789
Report to moderator
BitcoinCleanup.com: Learn why Bitcoin isn't bad for the environment
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715493789
Hero Member
*
Offline Offline

Posts: 1715493789

View Profile Personal Message (Offline)

Ignore
1715493789
Reply with quote  #2

1715493789
Report to moderator
1715493789
Hero Member
*
Offline Offline

Posts: 1715493789

View Profile Personal Message (Offline)

Ignore
1715493789
Reply with quote  #2

1715493789
Report to moderator
w00dy
Member
**
Offline Offline

Activity: 96
Merit: 10


View Profile
November 30, 2013, 08:58:18 PM
 #2

hmmm... and... what's your question?

there is not even a "?" in your whole post  Tongue
mank (OP)
Newbie
*
Offline Offline

Activity: 8
Merit: 0


View Profile
November 30, 2013, 11:03:57 PM
 #3

Basically unravel the nesting so that one has distinct functions.  Like the Public X = ( long normal math function ) Mod P, then the same for Public Y.  It looks like this is possible.  No one has laid this out this way so it can be explained in layman's terms.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!