mirror of
https://github.com/fumiama/go-x25519.git
synced 2026-06-22 21:14:42 +08:00
Refactor
This commit is contained in:
@@ -15,7 +15,7 @@ var (
|
||||
sharedSecret = "4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742"
|
||||
)
|
||||
|
||||
func TestGenerateKey(t *testing.T) {
|
||||
func TestStardardKey(t *testing.T) {
|
||||
askhex, err := hex.DecodeString(aliceSK)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -38,33 +38,40 @@ func TestGenerateKey(t *testing.T) {
|
||||
t.Fatal("public key failed")
|
||||
}
|
||||
|
||||
s1 := new([32]byte)
|
||||
s2 := new([32]byte)
|
||||
ask.Shared(s1, bpk)
|
||||
bsk.Shared(s2, apk)
|
||||
if !bytes.Equal(s1[:], s2[:]) {
|
||||
s1 := ask.Shared(bpk)
|
||||
s2 := bsk.Shared(apk)
|
||||
if !bytes.Equal(s1, s2) {
|
||||
t.Fatal("shared secret failed")
|
||||
}
|
||||
if hex.EncodeToString(s1[:]) != sharedSecret {
|
||||
if hex.EncodeToString(s1) != sharedSecret {
|
||||
t.Fatal("shared secret failed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateKey2(t *testing.T) {
|
||||
ourSK, _ := GenerateKey(nil)
|
||||
theirSK, _ := GenerateKey(nil)
|
||||
|
||||
t.Logf("our secret = %0x", ourSK)
|
||||
t.Logf("our public = %0x", ourSK.Public())
|
||||
t.Logf("their secret = %0x", theirSK)
|
||||
t.Logf("their public = %0x", theirSK.Public())
|
||||
|
||||
s1 := new([32]byte)
|
||||
s2 := new([32]byte)
|
||||
ourSK.Shared(s1, theirSK.Public())
|
||||
theirSK.Shared(s2, ourSK.Public())
|
||||
if !bytes.Equal(s1[:], s2[:]) {
|
||||
t.Fatal("computed shared secrets differs")
|
||||
func TestGenerateKey(t *testing.T) {
|
||||
for i := 0; i < 100; i++ {
|
||||
ourSK, _ := GenerateKey(nil)
|
||||
theirSK, _ := GenerateKey(nil)
|
||||
s1 := ourSK.Shared(theirSK.Public())
|
||||
s2 := theirSK.Shared(ourSK.Public())
|
||||
if !bytes.Equal(s1, s2) {
|
||||
t.Fatal("computed shared secrets differs")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUniformRepresentative(t *testing.T) {
|
||||
|
||||
for i := 0; i < 1; i++ {
|
||||
k, _ := GenerateKey(nil)
|
||||
sk, _ := GenerateKeyUniform(nil)
|
||||
pk := sk.Public()
|
||||
ur := sk.PublicUniform()
|
||||
if !bytes.Equal(ur.Public(), pk) {
|
||||
t.Fatal("public key and its uniform representative do not match")
|
||||
}
|
||||
if !bytes.Equal(k.Shared(sk.Public()), k.SharedUniform(sk.PublicUniform())) {
|
||||
t.Fatalf("shared secrets do not match")
|
||||
}
|
||||
}
|
||||
t.Logf("shared secret = %0x", s1)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user