diff --git a/ed25519_test.go b/ed25519_test.go index 290e340..0b4651e 100644 --- a/ed25519_test.go +++ b/ed25519_test.go @@ -8,11 +8,14 @@ import ( "bufio" "bytes" "compress/gzip" + "crypto/rand" "encoding/hex" "io" "os" "strings" "testing" + + "github.com/agl/ed25519/edwards25519" ) type zeroReader struct{} @@ -24,6 +27,22 @@ func (zeroReader) Read(buf []byte) (int, error) { return len(buf), nil } +func TestUnmarshalMarshal(t *testing.T) { + pub, _, _ := GenerateKey(rand.Reader) + + var A edwards25519.ExtendedGroupElement + if !A.FromBytes(pub) { + t.Fatalf("ExtendedGroupElement.FromBytes failed") + } + + var pub2 [32]byte + A.ToBytes(&pub2) + + if *pub != pub2 { + t.Errorf("FromBytes(%v)->ToBytes does not round-trip, got %x\n", *pub, pub2) + } +} + func TestSignVerify(t *testing.T) { var zero zeroReader public, private, _ := GenerateKey(zero) diff --git a/marshal_test.go b/marshal_test.go deleted file mode 100644 index 5f01d35..0000000 --- a/marshal_test.go +++ /dev/null @@ -1,58 +0,0 @@ -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])) - } -}