-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils_test.go
84 lines (70 loc) · 1.93 KB
/
utils_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
80
81
82
83
84
package hibe
import (
"bytes"
"crypto/rand"
"math/big"
"testing"
"vuvuzela.io/crypto/bn256"
)
var ID = []string{"a", "b", "c"}
var Message = bn256.Pair(new(bn256.G1).ScalarBaseMult(big.NewInt(3)), new(bn256.G2).ScalarBaseMult(big.NewInt(5)))
func IDToInts(id []string) []*big.Int {
ints := make([]*big.Int, len(id))
for i, component := range id {
ints[i] = HashToZp([]byte(component))
}
return ints
}
func TestHashID(t *testing.T) {
idints := IDToInts(ID)
for _, idint := range idints {
if idint.Cmp(bn256.Order) != -1 || idint.Cmp(big.NewInt(0)) != 1 {
t.Fatal("ID components are not in Zp*")
}
}
}
func TestTopLevelWithMarshalling(t *testing.T) {
// Set up parameters
params, key, err := Setup(rand.Reader, 10)
if err != nil {
t.Fatal(err)
}
parambytes := params.Marshal()
params = new(Params)
_, ok := params.Unmarshal(parambytes)
if !ok {
t.Fatal("Could not unmarshal Params")
}
// Come up with a message to encrypt
message := NewMessage()
// Encrypt a message under the top level public key
ciphertext, err := Encrypt(rand.Reader, params, IDToInts(ID[:1]), message)
if err != nil {
t.Fatal(err)
}
ciphertextbytes := ciphertext.Marshal()
ciphertext = new(Ciphertext)
_, ok = ciphertext.Unmarshal(ciphertextbytes)
if !ok {
t.Fatal("Could not unmarshal Ciphertext")
}
// Generate key for the top level
toplevelkey, err := KeyGenFromMaster(rand.Reader, params, key, IDToInts(ID[:1]))
if err != nil {
t.Fatal(err)
}
toplevelkeybytes := toplevelkey.Marshal()
toplevelkey = new(PrivateKey)
_, ok = toplevelkey.Unmarshal(toplevelkeybytes)
if !ok {
t.Fatal("Could not unmarshal private key")
}
if toplevelkey.DepthLeft() != 9 {
t.Fatal("Depth remaining on key is incorrect")
}
// Decrypt ciphertext with key and check that it is correct
decrypted := Decrypt(toplevelkey, ciphertext)
if !bytes.Equal(message.Marshal(), decrypted.Marshal()) {
t.Fatal("Original and decrypted messages differ")
}
}