-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbb84_test.go
More file actions
55 lines (40 loc) · 1.24 KB
/
bb84_test.go
File metadata and controls
55 lines (40 loc) · 1.24 KB
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
package bb84
import (
"math/rand"
"testing"
"time"
"github.com/tj/assert"
)
// Key length in bytes
const KEY_LENGTH = 1024
// Helper function for generating random bytes
func randomBytes() []byte {
bytes := make([]byte, KEY_LENGTH)
rand.Read(bytes)
return bytes
}
// Alice send bits to Bob without any eavesdropping
func TestSafeKeyDistribution(t *testing.T) {
rand.Seed(time.Now().Unix())
aliceKey := randomBytes()
aliceFilters := randomBytes()
photons := BitsToPhotons(aliceKey, aliceFilters)
bobFilters := randomBytes()
bobKey := PhotonsToBits(photons, bobFilters)
aliceFinalKey, bobFinalKey := GenerateKeys(aliceKey, bobKey, aliceFilters, bobFilters)
assert.Equal(t, aliceFinalKey, bobFinalKey)
}
// Alice send bits to Bob with eavesdropping
func TestUnsafeKeyDistribution(t *testing.T) {
rand.Seed(time.Now().Unix())
aliceKey := randomBytes()
aliceFilters := randomBytes()
photons := BitsToPhotons(aliceKey, aliceFilters)
// Eavesdrop on the photons
eveFilters := randomBytes()
PhotonsToBits(photons, eveFilters)
bobFilters := randomBytes()
bobKey := PhotonsToBits(photons, bobFilters)
aliceFinalKey, bobFinalKey := GenerateKeys(aliceKey, bobKey, aliceFilters, bobFilters)
assert.NotEqual(t, aliceFinalKey, bobFinalKey)
}