mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-28 22:25:07 +03:00
Improve genkeys with bit selection
This commit is contained in:
parent
947b6ad7aa
commit
2795260d21
2 changed files with 69 additions and 16 deletions
|
@ -13,12 +13,14 @@ package main
|
||||||
import (
|
import (
|
||||||
"crypto/ed25519"
|
"crypto/ed25519"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
||||||
|
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type keySet struct {
|
type keySet struct {
|
||||||
|
@ -27,9 +29,22 @@ type keySet struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
security := flag.Int("security", 0, "generates a key with a specific amount of security bits. defaults to 0 which continuously generates more keys")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
if (*security > 0) {
|
||||||
|
// If higher than 0, generates a key with the set amount of security bits
|
||||||
|
var secureKey keySet
|
||||||
|
secureKey.priv, secureKey.pub = config.NewSecureKeyPair(*security)
|
||||||
|
fmt.Println("-----", time.Since(start))
|
||||||
|
fmt.Println("Priv:", hex.EncodeToString(secureKey.priv))
|
||||||
|
fmt.Println("Pub:", hex.EncodeToString(secureKey.pub))
|
||||||
|
addr := address.AddrForKey(secureKey.pub)
|
||||||
|
fmt.Println("IP:", net.IP(addr[:]).String())
|
||||||
|
} else {
|
||||||
threads := runtime.GOMAXPROCS(0)
|
threads := runtime.GOMAXPROCS(0)
|
||||||
fmt.Println("Threads:", threads)
|
fmt.Println("Threads:", threads)
|
||||||
start := time.Now()
|
|
||||||
var currentBest ed25519.PublicKey
|
var currentBest ed25519.PublicKey
|
||||||
newKeys := make(chan keySet, threads)
|
newKeys := make(chan keySet, threads)
|
||||||
for i := 0; i < threads; i++ {
|
for i := 0; i < threads; i++ {
|
||||||
|
@ -47,6 +62,7 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func isBetter(oldPub, newPub ed25519.PublicKey) bool {
|
func isBetter(oldPub, newPub ed25519.PublicKey) bool {
|
||||||
for idx := range oldPub {
|
for idx := range oldPub {
|
||||||
|
|
|
@ -30,6 +30,8 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hjson/hjson-go/v4"
|
"github.com/hjson/hjson-go/v4"
|
||||||
|
@ -208,6 +210,41 @@ func (cfg *NodeConfig) NewPrivateKey() {
|
||||||
cfg.PrivateKey = KeyBytes(spriv)
|
cfg.PrivateKey = KeyBytes(spriv)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewSecureKeyPair(bits int) (priv ed25519.PrivateKey, pub ed25519.PublicKey) {
|
||||||
|
// Generates a key pair with a prescribed number of security bits.
|
||||||
|
threads := runtime.GOMAXPROCS(0)
|
||||||
|
if (bits > 64) {
|
||||||
|
bits = 64
|
||||||
|
// Bounding the maximum number of security bits to the maximum public key length of 64.
|
||||||
|
}
|
||||||
|
type keySet struct {
|
||||||
|
priv ed25519.PrivateKey
|
||||||
|
pub ed25519.PublicKey
|
||||||
|
}
|
||||||
|
expected := strings.Repeat("0", bits)
|
||||||
|
// Generates the expected security substring in advance
|
||||||
|
newKeys := make(chan keySet, threads)
|
||||||
|
for i := 0; i < threads; i++ {
|
||||||
|
go func(out chan<- keySet) {
|
||||||
|
for {
|
||||||
|
pub, priv, err := ed25519.GenerateKey(nil)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if !(hex.EncodeToString(pub)[0:bits] == expected) {
|
||||||
|
// Checks if the public key contains the expected security substring
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
out <- keySet{priv, pub}
|
||||||
|
}
|
||||||
|
}(newKeys)
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
newKey := <-newKeys
|
||||||
|
return newKey.priv, newKey.pub
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (cfg *NodeConfig) MarshalPEMPrivateKey() ([]byte, error) {
|
func (cfg *NodeConfig) MarshalPEMPrivateKey() ([]byte, error) {
|
||||||
b, err := x509.MarshalPKCS8PrivateKey(ed25519.PrivateKey(cfg.PrivateKey))
|
b, err := x509.MarshalPKCS8PrivateKey(ed25519.PrivateKey(cfg.PrivateKey))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue