-
Notifications
You must be signed in to change notification settings - Fork 0
/
hibkemtree.go
76 lines (62 loc) · 1.72 KB
/
hibkemtree.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
package HIBKEM
import (
"crypto/rand"
"errors"
)
//find all the siblings except existing in dup.
//generate private key for each sibling.
func allSiblingsInPath(params *Params, ancestor *PrivateKey, n string, dup []*PrivateKey) ([]*PrivateKey, error) {
if !IsAncestor(ancestor.ID, n) {
return nil, errors.New("Not Ancestor!")
}
siblings := make([]*PrivateKey, 0)
ancestorID := ancestor.ID
sbilingID := ancestor.ID
for _, ch := range n[len(ancestor.ID):] {
sbilingID = ancestorID + string(ch^0x1)
ancestorID = ancestorID + string(ch)
isNotDup := true
for _, item := range dup {
if sbilingID == item.ID {
isNotDup = false
break
}
}
if isNotDup {
move, err := KeyGen(rand.Reader, params, ancestor, sbilingID)
if err != nil {
return nil, err
}
siblings = append(siblings, move)
}
}
if len(siblings) == 0 {
return nil, errors.New("All siblings are in the dup set!")
}
return siblings, nil
}
// puncture tree
func PunctureTree(params *Params, nodeset []*PrivateKey, n string) ([]*PrivateKey, []*PrivateKey) {
setPrime := make([]*PrivateKey, 0)
var oldest *PrivateKey
j := 0
for _, item := range nodeset {
if !IsAncestor(item.ID, n) && !IsDescendant(item.ID, n) && item.ID != n {
//item would be moved from nodeset to setPrime - step 1
setPrime = append(setPrime, item)
} else {
if IsAncestor(item.ID, n) && (oldest == nil || IsAncestor(item.ID, oldest.ID)) {
//find the oldest descendant, a tricky workround
oldest = item
}
//item would be moved from nodeset to setPrime - step 2
nodeset[j] = item
j++
}
}
if oldest != nil {
set, _ := allSiblingsInPath(params, oldest, n, setPrime)
setPrime = append(setPrime, set...)
}
return nodeset[:j], setPrime
}