0

I tried my best to convert to this vb.net code from python code, line by line, but I really don't know how to make this a working code. It is a elliptic curve public key generation from a private key. How to rectify this code? it doesn't even run. I'm working on this for days converting line by line manually, still can't get this correct.

non working vb.net code

Dim Pcurve As Double = 2 ^ 256 - 2 ^ 32 - 2 ^ 9 - 2 ^ 8 - 2 ^ 7 - 2 ^ 6 - 2 ^ 4 - 1 ' The proven prime
    Dim Acurve As Integer = 0 'These two defines the elliptic curve. y^2 = x^3 + Acurve * x + Bcurve
    Dim Bcurve As Integer = 7
    Dim Gx As String = "79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798"
    Dim Gy = "483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8"

    Dim GX_number = Convert.ToUInt64(Gx, 16)
    Dim GY_number = Convert.ToUInt64(Gy, 16)

    Dim N As String = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141" 'Number Of points In the field
    Dim N_number As Integer = Convert.ToInt64(N, 16)
    Dim Privatekey As String = "79FE45D61339181238E49424E905446A35497A8ADEA8B7D5241A1E7F2C95A04D" 'PRIVATE KEY  

   Private Function HexStringToByteArray(ByVal shex As String) As Byte()
        Dim B As Byte() = Enumerable.Range(0, shex.Length).Where(Function(x) x Mod 2 = 0).[Select](Function(x) Convert.ToByte(shex.Substring(x, 2), 16)).ToArray()
        Return Enumerable.Range(0, shex.Length).Where(Function(x) x Mod 2 = 0).[Select](Function(x) Convert.ToByte(shex.Substring(x, 2), 16)).ToArray()
    End Function


Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click

        Dim publickey As String
        publickey = EccMultiply(GX_number, GY_number, Privatekey)



    End Sub

    Function Modinv(a)     'Extended Euclidean Algorithm/'division' in elliptic curves
        Dim lm As Integer = 1
        Dim hm As Integer = 0
        Dim low As Integer = a Mod Pcurve
        Dim high As Integer = Pcurve
        Dim ratio As Integer
        Dim nm As Integer
        Dim New_n As Integer
        Do While low > 1
            ratio = high / low
            nm = (hm - lm) * ratio
            New_n = (high - low) * ratio

            lm = nm
            low = New_n
            hm = lm
            high = low
        Loop

        Return lm Mod Pcurve
    End Function

    Function EccMultiply(x, y, ScalarHex) 'Doubling & Addition
        'If ScalarHex = 0 Or ScalarHex >= N Then
        '    Exception("Invalid Scalar/Private Key")
        'End If


        Dim ScalarBin As String = Convert.ToString(HexStringToByteArray)

        Dim x_num = x
        Dim y_num = y

        Dim point_ECD As Point
        For i = 1 To ScalarBin.Length - 1
            point_ECD = ECdouble(x, y)
            ' X_ECD = ECdouble(x, y)
            If ScalarBin = "1" Then i = "1"
            Point = ECadd(Point, GenPoint)

        Next
        Return (x, y)

    End Function

    Function ECdouble(x_num As Integer, y_num As Integer) ' EC Doubling
        Dim Lam = ((3 * x_num * (x_num + Acurve)) * Modinv((2 * y_num))) Mod Pcurve
        Dim x = (Lam * Lam - 2 * x_num) Mod Pcurve
        Dim y = (Lam * (x_num - x) - y_num) Mod Pcurve
        Return (x, y)
    End Function

    Function ECadd(a0, a1, b0, b1) 'EC Addition
        Dim LamAdd As ULong = ((b1 - a1) * Modinv(b0 - a0)) Mod Pcurve
        Dim x As ULong = (LamAdd * LamAdd - a0 - b0) Mod Pcurve
        Dim y As ULong = (LamAdd * (a0 - x) - a1) Mod Pcurve
        Return (x, y)
    End Function

working Python code

Pcurve = 2**256 - 2**32 - 2**9 - 2**8 - 2**7 - 2**6 - 2**4 -1 # The proven prime
Acurve = 0; # These two defines the elliptic curve. y^2 = x^3 + Acurve * x + Bcurve
Bcurve = 7;
Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798 
Gy = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8
GPoint = (int(Gx),int(Gy)) # This is our generator point.
N=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 # Number of points in the field

# Replace with any private key
privKey = 0x79FE45D61339181238E49424E905446A35497A8ADEA8B7D5241A1E7F2C95A04D

def modinv(a,n=Pcurve): #Extended Euclidean Algorithm/'division' in elliptic curves
    lm, hm = 1,0
    low, high = a%n,n
    while low > 1:
        ratio = high/low
        nm, new = hm-lm*ratio, high-low*ratio
        lm, low, hm, high = nm, new, lm, low
    return lm % n

def ECadd(a,b): # EC Addition
    LamAdd = ((b[1]-a[1]) * modinv(b[0]-a[0],Pcurve)) % Pcurve
    x = (LamAdd*LamAdd-a[0]-b[0]) % Pcurve
    y = (LamAdd*(a[0]-x)-a[1]) % Pcurve
    return (x,y)

def ECdouble(a): # EC Doubling
    Lam = ((3*a[0]*a[0]+Acurve) * modinv((2*a[1]),Pcurve)) % Pcurve
    x = (Lam*Lam-2*a[0]) % Pcurve
    y = (Lam*(a[0]-x)-a[1]) % Pcurve
    return (x,y)

def EccMultiply(GenPoint,ScalarHex): # Doubling & Addition
    if ScalarHex == 0 or ScalarHex >= N: raise Exception("Invalid Scalar/Private Key")
    ScalarBin = str(bin(ScalarHex))[2:]
    Q=GenPoint
    for i in range (1, len(ScalarBin)):
        Q=ECdouble(Q); 
        if ScalarBin[i] == "1":
            Q=ECadd(Q,GenPoint);
    return (Q)

 print "******* Bitcoin Public Key Generation *********";  
print
PublicKey = EccMultiply(GPoint,privKey)
print "the private key:"; 
print hex(privKey); print

print "the public key x-value (Hex):"; 
print "0x" + "%064x" % PublicKey[0];
print;

print "the public key y-value (Hex):"; 
print "0x" + "%064x" % PublicKey[1];
print;

print "the public key (Hex):"; 
print "0x" + "%064x" % PublicKey[0] + "%064x" % PublicKey[1];
print;
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
PGRL
  • 13
  • 1
  • It looks like you are aware of the difference in `^` between languages (XOR vs to the power of). But, "it doesn't even run" doesn't give us much to work with. What error messages does it give? Is the initial value of `Pcurve` as intended? – Andrew Morton Apr 12 '20 at 17:41
  • hi, @AndrewMorton : What do you mean by I'm aware of the `^`? it doesn't give useful error the error displayed `"there were build errors, do you want to run the last successful build"` that is what I meant that it even won't run. – PGRL Apr 12 '20 at 17:49
  • and pcurve value is intended, it's a constant value. only thing you can modify is the private key. all other assigned values are constants and will not change. the pcurve value is there to set the properties of the curve. pls help – PGRL Apr 12 '20 at 17:52
  • (a) Oh, in that case, there should be a tab "Errors" that you can click on (maybe near the bottom) to find out what it is complaining about; sometimes you can double-click on those to take you to the line. (b) Please use [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) so that VS can tell you when things are wrong and offers suggestions to fix it. – Andrew Morton Apr 12 '20 at 17:57
  • Class level variables need a datatype. Functions need a return type. Parameters for methods require datatypes. – Mary Apr 12 '20 at 18:17

0 Answers0