-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdppk_test.go
79 lines (65 loc) · 2.08 KB
/
dppk_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
// # Copyright (c) 2024 xtaci
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package dppk
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDPPK(t *testing.T) {
alice, err := GenerateKey(10)
assert.Nil(t, err)
secret := []byte("hello quantum")
kem, err := Encrypt(&alice.PublicKey, secret)
assert.Nil(t, err)
t.Log("secret:", string(secret))
x1, x2, err := alice.Decrypt(kem)
assert.Nil(t, err)
t.Log("x1:", string(x1.Bytes()))
t.Log("x2:", string(x2.Bytes()))
assert.Equal(t, alice.Public().Order(), 10)
equal := bytes.Equal(secret, x1.Bytes()) || bytes.Equal(secret, x2.Bytes())
assert.True(t, equal)
}
func TestDPPKSmallPrime(t *testing.T) {
prime := "977"
alice, err := GenerateKeyWithPrime(10, prime)
assert.Nil(t, err)
secret := []byte("X")
kem, err := Encrypt(&alice.PublicKey, secret)
assert.Nil(t, err)
t.Log("secret:", string(secret))
x1, x2, err := alice.Decrypt(kem)
assert.Nil(t, err)
t.Log("x1:", string(x1.Bytes()))
t.Log("x2:", string(x2.Bytes()))
equal := bytes.Equal(secret, x1.Bytes()) || bytes.Equal(secret, x2.Bytes())
assert.True(t, equal)
}
func BenchmarkDPPKEncrypt(b *testing.B) {
dppk, _ := GenerateKey(5)
secret := []byte("hello quantum")
for i := 0; i < b.N; i++ {
_, _ = Encrypt(&dppk.PublicKey, secret)
}
}
func BenchmarkDPPKDecrypt(b *testing.B) {
dppk, _ := GenerateKey(5)
secret := []byte("hello quantum")
kem, _ := Encrypt(&dppk.PublicKey, secret)
for i := 0; i < b.N; i++ {
_, _, _ = dppk.Decrypt(kem)
}
}