1// Copyright 2013 The Go Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5package extra25519 6 7import ( 8 "bytes" 9 "crypto/rand" 10 "testing" 11 12 "github.com/agl/ed25519" 13 "golang.org/x/crypto/curve25519" 14) 15 16func TestCurve25519Conversion(t *testing.T) { 17 public, private, _ := ed25519.GenerateKey(rand.Reader) 18 19 var curve25519Public, curve25519Public2, curve25519Private [32]byte 20 PrivateKeyToCurve25519(&curve25519Private, private) 21 curve25519.ScalarBaseMult(&curve25519Public, &curve25519Private) 22 23 if !PublicKeyToCurve25519(&curve25519Public2, public) { 24 t.Fatalf("PublicKeyToCurve25519 failed") 25 } 26 27 if !bytes.Equal(curve25519Public[:], curve25519Public2[:]) { 28 t.Errorf("Values didn't match: curve25519 produced %x, conversion produced %x", curve25519Public[:], curve25519Public2[:]) 29 } 30} 31 32func TestElligator(t *testing.T) { 33 var publicKey, publicKey2, publicKey3, representative, privateKey [32]byte 34 35 for i := 0; i < 1000; i++ { 36 rand.Reader.Read(privateKey[:]) 37 38 if !ScalarBaseMult(&publicKey, &representative, &privateKey) { 39 continue 40 } 41 RepresentativeToPublicKey(&publicKey2, &representative) 42 if !bytes.Equal(publicKey[:], publicKey2[:]) { 43 t.Fatal("The resulting public key doesn't match the initial one.") 44 } 45 46 curve25519.ScalarBaseMult(&publicKey3, &privateKey) 47 if !bytes.Equal(publicKey[:], publicKey3[:]) { 48 t.Fatal("The public key doesn't match the value that curve25519 produced.") 49 } 50 } 51} 52 53func BenchmarkKeyGeneration(b *testing.B) { 54 var publicKey, representative, privateKey [32]byte 55 56 // Find the private key that results in a point that's in the image of the map. 57 for { 58 rand.Reader.Read(privateKey[:]) 59 if ScalarBaseMult(&publicKey, &representative, &privateKey) { 60 break 61 } 62 } 63 64 b.ResetTimer() 65 for i := 0; i < b.N; i++ { 66 ScalarBaseMult(&publicKey, &representative, &privateKey) 67 } 68} 69 70func BenchmarkMap(b *testing.B) { 71 var publicKey, representative [32]byte 72 rand.Reader.Read(representative[:]) 73 74 b.ResetTimer() 75 for i := 0; i < b.N; i++ { 76 RepresentativeToPublicKey(&publicKey, &representative) 77 } 78} 79