forked from teserakt-io/golang-ed25519
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
edwards25519.FromBytes: do not negate the point
edwards25519.FromBytes is an exported method and should thus live up to its name. In particular, X == ToBytes(FromBytes(X)) should hold. Up to now, FromBytes silently negate the point it was deserializing, and ed25519.Verify was missing a negation -- these two defects cancelled each other out.
- Loading branch information
Showing
3 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package ed25519 | ||
|
||
import ( | ||
"crypto/rand" | ||
"testing" | ||
|
||
"github.com/agl/ed25519/edwards25519" | ||
) | ||
|
||
func TestUnmarshalMarshal(t *testing.T) { | ||
pk, _, _ := GenerateKey(rand.Reader) | ||
|
||
var A edwards25519.ExtendedGroupElement | ||
ret := A.FromBytes(pk) | ||
|
||
var pk2 [32]byte | ||
A.ToBytes(&pk2) | ||
|
||
if *pk != pk2 { | ||
_ = ret | ||
t.Errorf("FromBytes(%v)->ToBytes not idempotent:\n%x\nbytes:\n\t%x\n\t%x\ndelta: %x\n", ret, A, *pk, pk2, int(pk[31])-int(pk2[31])) | ||
} | ||
} | ||
|
||
func TestUnmarshalMarshalTwice(t *testing.T) { | ||
pk, _, _ := GenerateKey(rand.Reader) | ||
|
||
var A edwards25519.ExtendedGroupElement | ||
A.FromBytes(pk) | ||
|
||
var pk2 [32]byte | ||
A.ToBytes(&pk2) | ||
|
||
var B edwards25519.ExtendedGroupElement | ||
ret := B.FromBytes(&pk2) | ||
|
||
var pk3 [32]byte | ||
B.ToBytes(&pk3) | ||
|
||
if *pk != pk3 { | ||
t.Errorf("FromBytes(%v)->ToBytes not idempotent:\n%x\nbytes:\n\t%x\n\t%x\ndelta: %x\n", ret, A, *pk, pk3, int(pk[31])-int(pk2[31])) | ||
} | ||
} | ||
|
||
func TestUnmarshalMarshalNegative(t *testing.T) { | ||
pk, _, _ := GenerateKey(rand.Reader) | ||
|
||
var A edwards25519.ExtendedGroupElement | ||
ret := A.FromBytes(pk) | ||
|
||
var pk2 [32]byte | ||
A.ToBytes(&pk2) | ||
pk2[31] ^= 0x80 | ||
|
||
if *pk == pk2 { | ||
t.Errorf("flipping sign did not change public key:\n%x\nbytes:\n\t%x\n\t%x\ndelta: %x\n", ret, A, *pk, pk2, int(pk[31])-int(pk2[31])) | ||
} | ||
} |