Skip to content
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

Draft
wants to merge 15 commits into
base: develop
Choose a base branch
from
Draft

Test vector scripts #115

wants to merge 15 commits into from

Conversation

lrettig
Copy link
Member

@lrettig lrettig commented Jun 21, 2024

Adds two scripts for generating test vectors: addresses, and transactions.

Quick and dirty tool for generating address test vectors
First working version, still WIP
Getting there
@lrettig lrettig requested a review from fasmat June 21, 2024 23:48
go.mod Outdated Show resolved Hide resolved
Comment on lines 262 to 272
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
}
Copy link
Member

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 Show resolved Hide resolved
Comment on lines 208 to 209
// frustratingly, we need the same list of pubkeys in a different format
// https://github.com/spacemeshos/go-spacemesh/issues/6061
Copy link
Member

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 😕

@lrettig
Copy link
Member Author

lrettig commented Jun 23, 2024

Here's an initial set of vectors produced by the script:

vectors.txt

templateAddress types.Address,
principalMultisig types.Address,
spawnArgsMultisig *templateMultisig.SpawnArguments,
pubkeysSigning []signing.PublicKey,
Copy link
Member

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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n is unused.

Comment on lines 391 to 406
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
}
Copy link
Member

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:

Suggested change
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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants