update genkeys to new address format

This commit is contained in:
Arceliar 2021-05-10 05:58:06 -05:00
parent b48962a69a
commit 6cb958e3dc

View File

@ -13,116 +13,66 @@ This only matters if it's high enough to make you the root of the tree.
package main package main
import ( import (
"crypto/ed25519"
"encoding/hex" "encoding/hex"
"flag"
"fmt" "fmt"
"net" "net"
"runtime" "runtime"
"github.com/yggdrasil-network/yggdrasil-go/src/address" "github.com/yggdrasil-network/yggdrasil-go/src/address"
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
) )
var doSig = flag.Bool("sig", false, "generate new signing keys instead")
type keySet struct { type keySet struct {
priv []byte priv ed25519.PrivateKey
pub []byte pub ed25519.PublicKey
id []byte
ip string
} }
func main() { func main() {
threads := runtime.GOMAXPROCS(0) threads := runtime.GOMAXPROCS(0)
var threadChannels []chan []byte var currentBest ed25519.PublicKey
var currentBest []byte
newKeys := make(chan keySet, threads) newKeys := make(chan keySet, threads)
flag.Parse()
for i := 0; i < threads; i++ { for i := 0; i < threads; i++ {
threadChannels = append(threadChannels, make(chan []byte, threads)) go doKeys(newKeys)
switch {
case *doSig:
go doSigKeys(newKeys, threadChannels[i])
default:
go doBoxKeys(newKeys, threadChannels[i])
}
} }
for { for {
newKey := <-newKeys newKey := <-newKeys
if isBetter(currentBest, newKey.id[:]) || len(currentBest) == 0 { if isBetter(currentBest, newKey.pub) || len(currentBest) == 0 {
currentBest = newKey.id currentBest = newKey.pub
for _, channel := range threadChannels { fmt.Println("-----")
select { fmt.Println("Priv:", hex.EncodeToString(newKey.priv))
case channel <- newKey.id: fmt.Println("Pub:", hex.EncodeToString(newKey.pub))
} addr := address.AddrForKey(newKey.pub)
} fmt.Println("IP:", net.IP(addr[:]).String())
fmt.Println("--------------------------------------------------------------------------------")
switch {
case *doSig:
fmt.Println("sigPriv:", hex.EncodeToString(newKey.priv))
fmt.Println("sigPub:", hex.EncodeToString(newKey.pub))
fmt.Println("TreeID:", hex.EncodeToString(newKey.id))
default:
fmt.Println("boxPriv:", hex.EncodeToString(newKey.priv))
fmt.Println("boxPub:", hex.EncodeToString(newKey.pub))
fmt.Println("NodeID:", hex.EncodeToString(newKey.id))
fmt.Println("IP:", newKey.ip)
}
} }
} }
} }
func isBetter(oldID, newID []byte) bool { func isBetter(oldPub, newPub ed25519.PublicKey) bool {
for idx := range oldID { for idx := range oldPub {
if newID[idx] != oldID[idx] { if newPub[idx] < oldPub[idx] {
return newID[idx] > oldID[idx] return true
}
if newPub[idx] > oldPub[idx] {
break
} }
} }
return false return false
} }
func doBoxKeys(out chan<- keySet, in <-chan []byte) { func doKeys(out chan<- keySet) {
var bestID crypto.NodeID bestKey := make(ed25519.PublicKey, ed25519.PublicKeySize)
for { for idx := range bestKey {
select { bestKey[idx] = 0xff
case newBestID := <-in:
if isBetter(bestID[:], newBestID) {
copy(bestID[:], newBestID)
}
default:
pub, priv := crypto.NewBoxKeys()
id := crypto.GetNodeID(pub)
if !isBetter(bestID[:], id[:]) {
continue
}
bestID = *id
ip := net.IP(address.AddrForNodeID(id)[:]).String()
out <- keySet{priv[:], pub[:], id[:], ip}
}
}
}
func doSigKeys(out chan<- keySet, in <-chan []byte) {
var bestID crypto.TreeID
for idx := range bestID {
bestID[idx] = 0
} }
for { for {
select { pub, priv, err := ed25519.GenerateKey(nil)
case newBestID := <-in: if err != nil {
if isBetter(bestID[:], newBestID) { panic(err)
copy(bestID[:], newBestID)
}
default:
} }
pub, priv := crypto.NewSigKeys() if !isBetter(bestKey, pub) {
id := crypto.GetTreeID(pub)
if !isBetter(bestID[:], id[:]) {
continue continue
} }
bestID = *id bestKey = pub
out <- keySet{priv[:], pub[:], id[:], ""} out <- keySet{priv, pub}
} }
} }