-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwisted_test.go
73 lines (62 loc) · 1.68 KB
/
twisted_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
package sm2elgamal
import (
"crypto/rand"
"testing"
)
var te *TwistedElgamal
var priv *TwistedPrivateKey
func init() {
te, _ = NewTwistedElgamal(rand.Reader)
priv, _ = te.GenerateKey(rand.Reader)
}
func testTwistedEncryptDecryptUint32(t *testing.T, priv *TwistedPrivateKey, m uint32) {
ciphertext, err := priv.EncryptUint32(rand.Reader, m)
if err != nil {
t.Fatal(err)
}
v, err := priv.DecryptUint32(ciphertext)
if err != nil {
t.Fatal(err)
}
if v != m {
t.Fatalf("expected %x, got %x", m, v)
}
}
func TestTwistedEncryptDecryptUint32(t *testing.T) {
for i := 0; i < 10; i++ {
testTwistedEncryptDecryptUint32(t, priv, uint32(i))
}
for i := 0xffffffff; i > 0xfffffff0; i-- {
testTwistedEncryptDecryptUint32(t, priv, uint32(i))
}
for i := 1; i < 10; i++ {
testTwistedEncryptDecryptUint32(t, priv, uint32(i*babySteps))
}
}
func testTwistedEncryptDecryptInt32(t *testing.T, priv *TwistedPrivateKey, m int32) {
ciphertext, err := priv.EncryptInt32(rand.Reader, m)
if err != nil {
t.Fatal(err)
}
v, err := priv.DecryptInt32(ciphertext)
if err != nil {
t.Fatal(err)
}
if v != m {
t.Fatalf("expected %x, got %x", m, v)
}
}
func TestTwistedEncryptDecryptInt32(t *testing.T) {
for i := 0; i < 10; i++ {
testTwistedEncryptDecryptInt32(t, priv, int32(i))
}
for i := -10; i < 0; i++ {
testTwistedEncryptDecryptInt32(t, priv, int32(i))
}
testTwistedEncryptDecryptInt32(t, priv, 0x7fffffff)
testTwistedEncryptDecryptInt32(t, priv, -0x7fffffff)
for i := 1; i < 10; i++ {
testTwistedEncryptDecryptInt32(t, priv, int32(i*babySteps))
testTwistedEncryptDecryptInt32(t, priv, int32(-i*babySteps))
}
}