I cannot generate the ECDSA public key from her private key sequentially

Viewed 313

I cannot generate the public keys in sequence.

output :./keysgo.go:33:33: cannot use PrivateKey (type []byte) as type string in argument to Public

Thank you so much for your help

important to keep this part:

func Public(PrivateKey string) (publicKey string) {
    var e ecdsa.PrivateKey
    e.D, _ = new(big.Int).SetString(PrivateKey, 16)
    e.PublicKey.Curve = secp256k1.S256()
    e.PublicKey.X, e.PublicKey.Y = e.PublicKey.Curve.ScalarBaseMult(e.D.Bytes())
    return fmt.Sprintf("%x", elliptic.MarshalCompressed(secp256k1.S256(), e.X, e.Y))

i tried this

package main 

import (
    "crypto/ecdsa"
    "crypto/elliptic"
    "fmt"
    "math/big"
    "github.com/ethereum/go-ethereum/crypto/secp256k1"
   
    
    
)
func Public(PrivateKey string) (publicKey string) {
    var e ecdsa.PrivateKey
    e.D, _ = new(big.Int).SetString(PrivateKey, 16)
    e.PublicKey.Curve = secp256k1.S256()
    e.PublicKey.X, e.PublicKey.Y = e.PublicKey.Curve.ScalarBaseMult(e.D.Bytes())
    return fmt.Sprintf("%x", elliptic.MarshalCompressed(secp256k1.S256(), e.X, e.Y))

}

func main() {
        count, one := big.NewInt(1), big.NewInt(1)
    count.SetString("9404625697166532776746648320380374280100293470930272690489102837043110636674",10)
        PrivateKey := make([]byte, 32)
   for {
        count.Add(count, one)
        copy(PrivateKey[32-len(count.Bytes()):], count.Bytes())
        
        fmt.Printf("%x\n",Public(PrivateKey))
        

    }   
    
}   

}

4 Answers

You define PrivateKey as a byte slice:

PrivateKey := make([]byte, 32)

The function Public is defined to take a string as its arguments:

func Public(PrivateKey string) (publicKey string) {...}

Thus Public(PrivateKey) cannot work, because privateKey is the wrong type.

I am not quite sure what the code is attempting to do, but maybe Public(string(PrivateKey)) is what you need?

Thank you very much for your kind reply

I tried Public(string(PrivateKey))

the program's response

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x4b47f9]

goroutine 1 [running]:
math/big.(*Int).Bytes(...)
        /usr/local/go/src/math/big/int.go:453

main.Public({0xc00001e1a0, 0x20})
        /root/Desktop/tuttogoprova/adressgoprova.go:17 +0x99
main.main()
        /root/Desktop/tuttogoprova/adressgoprova.go:37 +0xd3
exit status 2

As stated by jochen's answer, you need to convert the PrivateKey byte slice to a string before sending it to the Public function.

Once that is done, pointer dereference error is happening because the string you built for the private is not a valid bigint.

I solved the dereference issue by copying count.String() into the PrivateKey instead of count.Bytes()

See this answer for a better way to store key pairs.

Here is some working code that demonstrates all this. It does require cgo since the original code uses it, so it doesn't run in the playground.

Thank you so much Mr. Wesley what i am looking for is something like this https://go.dev/play/p/ws2c_bIc4M5 but i can't generate keys (private e pubbliche) sequentially THANK YOU FOR YOUR KIND

Related