-
Notifications
You must be signed in to change notification settings - Fork 1
/
sra_test.go
78 lines (71 loc) · 2.07 KB
/
sra_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
package sra_test
import (
"crypto/rand"
"math/big"
"testing"
"github.com/cretz/go-mental-poker/sra"
"github.com/stretchr/testify/require"
)
func TestSRACommutative(t *testing.T) {
// Gen prime
prime, err := rand.Prime(rand.Reader, 256)
require.NoError(t, err)
// Gen key pairs for alice, bob, and ted
alice, err := sra.GenerateKeyPair(rand.Reader, prime, 32)
require.NoError(t, err)
bob, err := sra.GenerateKeyPair(rand.Reader, prime, 32)
require.NoError(t, err)
ted, err := sra.GenerateKeyPair(rand.Reader, prime, 32)
require.NoError(t, err)
// Make sure it can be encrypted by all the people in any order, and decrypted
peoplePerms := [][]*sra.KeyPair{
{alice, bob, ted},
{alice, ted, bob},
{bob, alice, ted},
{bob, ted, alice},
{ted, alice, bob},
{ted, bob, alice},
}
// Go over each set of people in any order and make sure the decrypted value always comes out right
for _, encPeople := range peoplePerms {
superSecretInt := newSuperSecretInt(t, 32)
encrypted := encryptMulti(t, superSecretInt, encPeople)
for _, decPeople := range peoplePerms {
decrypted := decryptMulti(t, encrypted, decPeople)
require.Zero(t, superSecretInt.Cmp(decrypted))
}
}
}
func encryptMulti(t *testing.T, v *big.Int, people []*sra.KeyPair) *big.Int {
orig := v
for _, person := range people {
v = person.EncryptInt(v)
// Make sure does match orig at any time
if v.Cmp(orig) == 0 {
require.FailNow(t, "Was orig after enc", orig)
}
}
return v
}
func decryptMulti(t *testing.T, v *big.Int, people []*sra.KeyPair) *big.Int {
orig := v
for _, person := range people {
v = person.DecryptInt(v)
// Make sure does match orig at any time
if v.Cmp(orig) == 0 {
require.FailNow(t, "Was orig after dec", orig)
}
}
return v
}
func newSuperSecretInt(t *testing.T, maxBits int) *big.Int {
bigTwo := big.NewInt(2)
max := new(big.Int).Sub(new(big.Int).Exp(bigTwo, big.NewInt(int64(maxBits)), nil), big.NewInt(1))
for {
superSecretInt, err := rand.Int(rand.Reader, max)
require.NoError(t, err)
if superSecretInt.Cmp(bigTwo) >= 0 {
return superSecretInt
}
}
}