Skip to content

Test vector scripts#115

Draft
lrettig wants to merge 15 commits intodevelopfrom
addresstests
Draft

Test vector scripts#115
lrettig wants to merge 15 commits intodevelopfrom
addresstests

Conversation

@lrettig
Copy link
Contributor

@lrettig lrettig commented Jun 21, 2024

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

lrettig added 4 commits June 21, 2024 11:56
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
Comment on lines 262 to 272
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()

Comment on lines 208 to 209
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
Contributor Author

lrettig commented Jun 23, 2024

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

vectors.txt

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
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

Comments