1

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))
        

    }   
    
}   

}

stefano
  • 11
  • 2

4 Answers4

0

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?

jochen
  • 3,728
  • 2
  • 39
  • 49
0

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
stefano
  • 11
  • 2
  • There is a problem with your private key. It can’t be converted to big.Int. https://go.dev/play/p/7DLyErFqXzh https://pkg.go.dev/math/big#Int.SetString – wesley Apr 07 '22 at 23:44
  • Thank you very much Mr. Wesley for your kind reply. Please can you give me some solution for this problem Infinite Thanks – stefano Apr 08 '22 at 00:23
  • I’ll try to look into it some more. Can you provide some code on how you are generating the private key? – wesley Apr 08 '22 at 13:10
0

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.

wesley
  • 553
  • 1
  • 4
  • 9
  • 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 – stefano Apr 13 '22 at 12:58
  • I’m unclear about what you are trying to achieve. Are you wanting to manipulate a private key to deterministically create new ones? – wesley Apr 14 '22 at 09:50
  • I am trying to generate public keys from private keys sequentially not random – stefano Apr 14 '22 at 13:15
  • Is this what you are trying to do? https://crypto.stackexchange.com/questions/47499/sequential-public-key-generation-with-secret-private-keys – wesley Apr 14 '22 at 23:43
  • Thank you so much Mr. Wesley not what I need but thank you for your availability – stefano Apr 19 '22 at 22:45
0

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

stefano
  • 11
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 11 '22 at 02:02