-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbb84.go
More file actions
73 lines (60 loc) · 1.57 KB
/
Copy pathbb84.go
File metadata and controls
73 lines (60 loc) · 1.57 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package bb84
import (
"math/rand"
"time"
)
// Photon
type Photon struct {
bit, phase bool
}
// Create a new photon with the given bit and phase
func NewPhoton(bit, phase bool) Photon {
return Photon{bit, phase}
}
// Polarize a photon with the given filter
func (p *Photon) Measure(base bool) bool {
if p.phase != base {
rand.Seed(time.Now().Unix())
p.bit = rand.Intn(2) == 0
p.phase = base
}
return p.bit
}
func BitsToPhotons(bits, phases []byte) []Photon {
if len(bits) != len(phases) {
panic("length of bits and phases must be equal")
}
photons := make([]Photon, len(bits))
for i := range photons {
bit := (bits[i/8] & (1 << uint(i%8))) != 0
phase := (phases[i/8] & (1 << uint(i%8))) != 0
photons[i] = NewPhoton(bit, phase)
}
return photons
}
func PhotonsToBits(photons []Photon, phases []byte) []byte {
if len(photons) != len(phases) {
panic("length of photons and phases must be equal")
}
bits := make([]byte, len(photons))
for i := range bits {
if photons[i].Measure(phases[i/8]&(1<<uint(i%8)) != 0) {
bits[i/8] |= 1 << uint(i%8)
}
}
return bits
}
func GenerateKeys(bits1, bits2, phases1, phases2 []byte) ([]byte, []byte) {
if len(bits1) != len(bits2) || len(bits1) != len(phases1) || len(bits1) != len(phases2) {
panic("length of bits and phases must be equal")
}
key1 := make([]byte, len(bits1))
key2 := make([]byte, len(bits2))
for i := range key1 {
if phases1[i/8]&(1<<uint(i%8)) == phases2[i/8]&(1<<uint(i%8)) {
key1[i/8] |= bits1[i/8] & (1 << uint(i%8))
key2[i/8] |= bits2[i/8] & (1 << uint(i%8))
}
}
return key1, key2
}