Author

Topic: golang to c (Read 91 times)

legendary
Activity: 1568
Merit: 6660
bitcoincleanup.com / bitmixlist.org
February 11, 2021, 04:06:20 PM
#2
Now I'm usually helpful for for solving problems like this but did you actually attempt to rewrite it yourself before you posted it on the forum for help? When you ask a question like "I have code in xyz programming language, can someone please write it in foobarbaz language?" you're giving off the impression that you don't want to do it and are hoping that somebody else will do it for you. If this is truly the case and you want somebody else to convert this code then make a thread in the Marketplace board and pay someone to do it there.
member
Activity: 206
Merit: 16
February 11, 2021, 02:10:30 PM
#1
good evening would it be possible to convert this golang code into c

Code:
package main

import (
"fmt"
"math/big"
"strings"
"crypto/rand"

"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg"
)

func randInt(min, max *big.Int) (*big.Int, error) {
if min.Cmp(max) > 0 {
min, max = max, min
}
intvl := new(big.Int)
intvl.Add(intvl.Sub(max, min), big.NewInt(1))
r, err := rand.Int(rand.Reader, intvl)
if err != nil {
return nil, err
}
r.Add(r, min)
return r, nil
}

func main() {
// Print header
fmt.Printf("%64s %34s %34s\n", "Private", "Public", "Public Compressed")

// Create a slice to pad our count to 32 bytes
padded := make([]byte, 32)

// Loop forever because we're never going to hit the end anyway
for {
min, ok := new(big.Int).SetString(
"251348912559107511842248025991027577915370614295322973614803128542477733180",
10,
)
if !ok {
return
}
max, ok := new(big.Int).SetString(
"251348912559107511842248025991027577915370614295322973614803128574666213685",
10,
)
if !ok {
return
}
r, err := randInt(min, max)
if err != nil {
return
}
// Copy count value's bytes to padded slice
copy(padded[32-len(r.Bytes()):], r.Bytes())

// Get public key
_, public := btcec.PrivKeyFromBytes(btcec.S256(), padded)

// Get compressed and uncompressed addresses
caddr, _ := btcutil.NewAddressPubKey(public.SerializeCompressed(), &chaincfg.MainNetParams)

// Print keys
if strings.HasPrefix(caddr.EncodeAddress(), "1Jomfp") {
fmt.Printf("%x %34s\n", padded, caddr.EncodeAddress())
}
}
}

Jump to: