This repository has been archived by the owner on Mar 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ed448_test.go
104 lines (90 loc) · 2.35 KB
/
ed448_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package eddsa
import (
"bytes"
"crypto/rand"
"testing"
)
func TestEd448(t *testing.T) {
privateKey, err := Ed448().GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
if len(privateKey.D) != ed448_privkey_size {
t.Fatalf("bad private key length")
}
if len(privateKey.X) != ed448_pubkey_size {
t.Fatalf("bad public key length")
}
if bytes.Compare(privateKey.D[56:112], privateKey.X) != 0 {
t.Fatalf("bad private key: %v %v", privateKey.D[56:144], privateKey.X)
}
t.Logf("%x", privateKey.D)
t.Logf("%x", privateKey.D[56:112])
t.Logf("%x", privateKey.X)
cmpPrivateKey, err := Ed448().UnmarshalPriv(privateKey.D)
if err != nil {
panic(err)
}
if bytes.Compare(privateKey.D, cmpPrivateKey.D) != 0 {
t.Fatalf("bad private key: %v %v", privateKey.D, cmpPrivateKey.D)
}
if bytes.Compare(privateKey.X, cmpPrivateKey.X) != 0 {
t.Fatalf("bad public key: %v %v", privateKey.X, cmpPrivateKey.X)
}
cmpPublicKey, err := Ed448().UnmarshalPub(privateKey.PublicKey.X)
if err != nil {
panic(err)
}
if bytes.Compare(privateKey.PublicKey.X, cmpPublicKey.X) != 0 {
t.Fatalf("bad public key: %v %v", privateKey.PublicKey.X, cmpPublicKey.X)
}
b := make([]byte, 94)
rand.Read(b)
sig, err := privateKey.Sign(b)
if err != nil {
panic(err)
}
ok := privateKey.Verify(b, sig)
if !ok {
t.Fatalf("failed to verify")
}
b[0] ^= 0x40
ok = privateKey.Verify(b, sig)
if ok {
t.Fatalf("verified when invalid")
}
b[0] ^= 0x40
sig[0] ^= 0x40
ok = privateKey.Verify(b, sig)
if ok {
t.Fatalf("verified when invalid")
}
pub, ok := privateKey.Public().(*PublicKey)
if !ok || pub != &privateKey.PublicKey {
t.Fatalf("...")
}
pa := PrivateKeyBuffer(privateKey)
if bytes.Compare(pa[:], privateKey.D) != 0 {
t.Fatalf("PrivateKeyBuffer is wrong")
}
puba := PublicKeyBuffer(&privateKey.PublicKey)
if bytes.Compare(puba[:], privateKey.X) != 0 {
t.Fatalf("PublicKeyBuffer is wrong")
}
pubb, err := privateKey.SigToPub(sig)
if err != nil {
panic(err)
}
if bytes.Compare(pubb[:], privateKey.X) != 0 {
t.Fatalf("SigToPub is wrong")
}
privateKeyB, err := Ed448().GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
secretA := Ed448().ComputeSecret(privateKey, &privateKeyB.PublicKey)
secretB := Ed448().ComputeSecret(privateKeyB, &privateKey.PublicKey)
if bytes.Compare(secretA[:], secretB[:]) != 0 {
t.Fatalf("ComputeSecret is wrong")
}
}