-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (74 loc) · 2.46 KB
/
main.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
package main
import (
"crypto/rand"
"fmt"
"math/big"
"github.com/lucasmenendez/gopsi/internal/encoder"
"github.com/lucasmenendez/gopsi/pkg/sra"
)
func main() {
var err error
// Agree a prime seed
prime, _ := rand.Prime(rand.Reader, 256)
// Create Alice key pair
var alice *sra.SRAKey
if alice, err = sra.NewKey(prime, 32); err != nil {
fmt.Println(err)
}
// Create Bob key pair
var bob *sra.SRAKey
if bob, err = sra.NewKey(prime, 32); err != nil {
fmt.Println(err)
}
// Create and encode Alice secret
aliceMsg := "[email protected]"
encodedAliceMsg := encoder.StrToInts(aliceMsg)
// Create and encode Bob secret
bobMsg := "[email protected]"
encodedBobMsg := encoder.StrToInts(bobMsg)
// Encrypt Alice original message by Alice first, and then by Bob
var encryptedAlice []*big.Int
var encryptedAliceBob []*big.Int
for _, aliceWord := range encodedAliceMsg {
encryptedWordAlice := alice.Encrypt(aliceWord)
encryptedAlice = append(encryptedAlice, encryptedWordAlice)
encryptedWordAliceBob := bob.Encrypt(encryptedWordAlice)
encryptedAliceBob = append(encryptedAliceBob, encryptedWordAliceBob)
}
// Encrypt Bob original message by Bob, and then by Alice
var encryptedBob []*big.Int
var encryptedBobAlice []*big.Int
for _, bobWord := range encodedBobMsg {
encryptedWordBob := bob.Encrypt(bobWord)
encryptedBob = append(encryptedBob, encryptedWordBob)
encryptedWordBobAlice := alice.Encrypt(encryptedWordBob)
encryptedBobAlice = append(encryptedBobAlice, encryptedWordBobAlice)
}
// Compare partial results
var lenAlice, lenBob int = len(encryptedAlice), len(encryptedBob)
var sameLen bool = lenAlice == lenBob
fmt.Printf("Have both partial encrypted messages same len? %v\n", sameLen)
var max int = lenAlice
if !sameLen && lenBob > max {
max = lenBob
}
var areBothEqual bool
for i := 0; i < max; i++ {
if !areBothEqual || i >= lenAlice || i >= lenBob {
break
}
areBothEqual = encryptedAlice[i].Cmp(encryptedBob[i]) == 0
}
fmt.Printf("Are both partial encrypted messages equal? %v\n", areBothEqual)
// Compare final results
var areFinalEqual bool = true
for i := 0; i < max; i++ {
if !areFinalEqual || i >= lenAlice || i >= lenBob {
break
}
areFinalEqual = encryptedAliceBob[i].Cmp(encryptedBobAlice[i]) == 0
}
fmt.Printf("Are both final encrypted messages equal? %v\n", areFinalEqual)
// Output: Are both partial encrypted messages equal? false
// Are both final encrypted messages equal? true
}