Does anyone know where I could find a sample of C code that can perform an EC multiply, or in other words, convert a bitcoin private key to public key? The code needs to be in straight C with no library dependencies of any kind.
I would suggest downloading the source code for OpenSSL, and simply purloining the small set of C routines involved in EC point arithmetic.
http://www.openssl.org/source/I have my own EC routines in my J client, if you just want to know what the math looks like.
NB. Modular reciprocal
mrcp =: 4 : 0
x =. x: x
y =. x | x: y
qq =: x: 0 0
rr =: x,y
tt =: x: 0 1
while. 0 ~: _1 { rr
do.
qq =: qq,(<.@%)/_2 _1{rr
rr =: rr,|/_1 _2{rr
tt =: x|tt,(_2{tt)-(_1{qq)*(_1{tt)
end.
_2{tt
)
prcp =: Ep & mrcp
NB. Doubling a point
pdub =: 3 : 0
'xj yj' =: x: y
if. yj = 0
do. _
else.
s =: Ep | 3 * xj * xj * prcp 2 * yj
xl =: Ep| (s * s) - 2 * xj
yl =: Ep | (-yj) + s * (xj - xl)
xl,yl
end.
)
NB. EC add
padd =: 4 : 0
'xj yj' =: x: x
'xk yk' =: x: y
if. xj ~: xk
do.
s =: Ep | (yj - yk) * prcp xj - xk
xl =: Ep | (s*s) - (xj + xk)
yl =: Ep | (-yj) + s *(xj - xl)
xl,yl
else.
if. yj = yk
do. pdub x
else. _
end.
end.
)
NB. EC Multiply
pmul =: 4 : 0
y =. x: y
x =. x: x
if. x = 0
do. _
else.
z =: 0 2$0x
while. x ~: 0
do.
if. 1 = 2 | x
do. z =: z,y
end.
x =. <. x % 2
y =. pdub y
end.
padd/z
end.
)
NB. Making a public key from a private key
pubkey =: 3 : 'y pmul EG'
Happy Programming.