-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test vector scripts #115
base: develop
Are you sure you want to change the base?
Test vector scripts #115
Conversation
Quick and dirty tool for generating address test vectors
First working version, still WIP
Getting there
cmd/testvectors/main.go
Outdated
func getKey() (pub signing.PublicKey, priv signing.PrivateKey) { | ||
// generate a random pubkey and discard the private key | ||
edPub, edPriv, err := ed25519.GenerateKey(rand.New(rand.NewSource(rand.Int63()))) | ||
if err != nil { | ||
log.Fatal("failed to generate ed25519 key") | ||
} | ||
// pub = *signing.NewPublicKey(edPub) | ||
pub = signing.PublicKey{PublicKey: edPub} | ||
priv = signing.PrivateKey(edPriv) | ||
return | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the signing
package we have signing.NewEdSigner()
that already creates a random key:
sig, err := signing.NewEdSigner()
_ = sig.PrivateKey()
_ = sig.PublicKey()
cmd/testvectors/main.go
Outdated
// frustratingly, we need the same list of pubkeys in a different format | ||
// https://github.com/spacemeshos/go-spacemesh/issues/6061 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not a different format - signing.Pubkey
is a wrapper around ed25519.Pubkey
to add additional methods. The conversion to bytes and then ed25519.Pubkey
isn't necessary since the later is a field in the wrapper:
edPubKeys := make([]ed25519.PublicKey, n)
edPubKeys[0] = pub.PublicKey
The real problem is core.PublicKey
why is it defined as types.Hash32
? A key isn't a hash, and types.Hash32
is an array, not a slice so every conversion between a "real" key and this custom type requires a copy of the value.
This line:
core.PublicKey(types.BytesToHash(pub2.Bytes()))
copies the key to an array and then the function using the core.PubicKey
copies it back into a slice when using it 😕
Here's an initial set of vectors produced by the script: |
cmd/testvectors/main.go
Outdated
templateAddress types.Address, | ||
principalMultisig types.Address, | ||
spawnArgsMultisig *templateMultisig.SpawnArguments, | ||
pubkeysSigning []signing.PublicKey, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Argument is unused.
Instead of passing the same key 4 times I suggest you just pass the *signing.EdSigner
created with signing.NewEdSigner()
and access the public and private key as needed with:
pub := edSigner.PublicKey() // type signing.PublicKey - a wrapper around `ed25519.PublicKey` that can be accessed with
edPub := pub.PublicKey
// core.PublicKey is a weird type that should be removed in the future -
// it requires copying the key twice from a slice to an array and back to be used
// which is inefficient - for now convert it from
corePub := types.BytesToHash(edSigner.PublicKey().PublicKey)
priv := edSigner.PrivateKey() // type signing.PrivateKey which is an alias to `ed25519.PrivateKey` so a cast should never be necessary
pubkeysCore []core.PublicKey, | ||
pubkeysEd []ed25519.PublicKey, | ||
privkeys []ed25519.PrivateKey, | ||
m, n uint8, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
n
is unused.
cmd/testvectors/main.go
Outdated
func generateKeys(n int) ([]signing.PublicKey, []core.PublicKey, []ed25519.PublicKey, []ed25519.PrivateKey) { | ||
// generate the required set of keypairs | ||
|
||
// frustratingly, we need the same list of pubkeys in multiple formats | ||
// https://github.com/spacemeshos/go-spacemesh/issues/6061 | ||
pubkeysSigning := make([]signing.PublicKey, n) | ||
pubkeysCore := make([]core.PublicKey, n) | ||
pubkeysEd := make([]ed25519.PublicKey, n) | ||
privkeys := make([]signing.PrivateKey, n) | ||
for i := 0; i < n; i++ { | ||
pubkeysEd[i], privkeys[i] = getKey() | ||
pubkeysCore[i] = types.BytesToHash(pubkeysEd[i]) | ||
pubkeysSigning[i] = signing.PublicKey{PublicKey: pubkeysEd[i]} | ||
} | ||
return pubkeysSigning, pubkeysCore, pubkeysEd, privkeys | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my other comment. I feel this could be simpler. getKey
isn't needed at all if signing.EdSigner()
is used instead:
func generateKeys(n int) ([]signing.PublicKey, []core.PublicKey, []ed25519.PublicKey, []ed25519.PrivateKey) { | |
// generate the required set of keypairs | |
// frustratingly, we need the same list of pubkeys in multiple formats | |
// https://github.com/spacemeshos/go-spacemesh/issues/6061 | |
pubkeysSigning := make([]signing.PublicKey, n) | |
pubkeysCore := make([]core.PublicKey, n) | |
pubkeysEd := make([]ed25519.PublicKey, n) | |
privkeys := make([]signing.PrivateKey, n) | |
for i := 0; i < n; i++ { | |
pubkeysEd[i], privkeys[i] = getKey() | |
pubkeysCore[i] = types.BytesToHash(pubkeysEd[i]) | |
pubkeysSigning[i] = signing.PublicKey{PublicKey: pubkeysEd[i]} | |
} | |
return pubkeysSigning, pubkeysCore, pubkeysEd, privkeys | |
} | |
func generateKeys(n int) []*signing.EdSigner { | |
identities := make([]*signing.EdSigner, 0, n) | |
for range n { | |
identity, err := signing.NewEdSigner() | |
log.With().Fatal("failed creating identity", log.Err(err)) | |
identities = append(identities, identity) | |
} | |
return identities | |
} |
Adds two scripts for generating test vectors: addresses, and transactions.