Author

Topic: updated python question (Read 574 times)

newbie
Activity: 8
Merit: 0
November 30, 2013, 06: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.
member
Activity: 96
Merit: 10
November 30, 2013, 03:58:18 PM
#2
hmmm... and... what's your question?

there is not even a "?" in your whole post  Tongue
newbie
Activity: 8
Merit: 0
November 30, 2013, 10:01:09 AM
#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
Jump to: