I mistakenly used 32 bytes of random as a ed25519.PrivateKey and was extremely confused why signatures were not validating, was frantically triple checking the data I was attempting to sign/verify was the same on both ends, etc. I am an experienced user and if I can get tripped up by this, less experienced users could as well.
Adding a method for users to check whether a private key is valid could help alleviate this by suggesting a different source of error, and also helping users ensure that they are using valid keys to sign data. Roughly it could do:
// ValidPrivateKey reports whether in is a valid ed25519 private key.
func ValidPrivateKey(in []byte) bool {
origKey := copy(in)
in[0] &= 248
in[31] &= 127
in[31] |= 64
return bytes.Equal(origKey, in)
}
You could also do func (p *PrivateKey) Valid() bool though I worry about letting people create a PrivateKey object and then determine whether that is valid or not, because they might try to use it later to sign stuff.
I mistakenly used 32 bytes of random as a ed25519.PrivateKey and was extremely confused why signatures were not validating, was frantically triple checking the data I was attempting to sign/verify was the same on both ends, etc. I am an experienced user and if I can get tripped up by this, less experienced users could as well.
Adding a method for users to check whether a private key is valid could help alleviate this by suggesting a different source of error, and also helping users ensure that they are using valid keys to sign data. Roughly it could do:
You could also do
func (p *PrivateKey) Valid() boolthough I worry about letting people create a PrivateKey object and then determine whether that is valid or not, because they might try to use it later to sign stuff.