Skip to content

Commit d5a2507

Browse files
committed
Update frontier so that Adds work
1 parent 2107392 commit d5a2507

File tree

3 files changed

+22
-25
lines changed

3 files changed

+22
-25
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
async function userAdd() {
111111
let last = members[members.length - 1];
112112
let leaf = base64.random(32);
113-
let gik = last.groupInitKey
113+
let gik = last.groupInitKey;
114114
let ua = await StateType.join(leaf, gik);
115115

116116
for (let m of members) {

src/treekem-state.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,10 @@ class TreeKEMState {
130130
}
131131

132132
async handleRemove(remove) {
133-
console.log(">>> remove", this.index, remove);
134133
let pt = await this.tkem.decrypt(remove.index, remove.ciphertexts);
135-
console.log('--- remove');
136134
this.tkem.merge(pt.root);
137-
console.log('--- remove');
138135
this.tkem.merge(remove.subtreeHeads, true);
139-
console.log('--- remove');
140136
this.tkem.remove(remove.index);
141-
console.log('<<< remove');
142137
}
143138
}
144139

src/treekem.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,22 @@ class TreeKEM {
8989
* Returns:
9090
* * {Node: T}
9191
*/
92-
async mapSubtree(node, func) {
92+
mapSubtree(node, func) {
9393
let out = {};
9494

9595
if (this.nodes[node]) {
96-
out[node] = await func(node);
96+
out[node] = func(node);
9797
return out;
9898
}
9999

100100
let left = tm.left(node);
101101
if (left != node) {
102-
Object.assign(out, await this.mapSubtree(left, func));
102+
Object.assign(out, this.mapSubtree(left, func));
103103
}
104104

105105
let right = tm.right(node, this.size);
106106
if (right != node) {
107-
Object.assign(out, await this.mapSubtree(right, func));
107+
Object.assign(out, this.mapSubtree(right, func));
108108
}
109109

110110
return out;
@@ -116,17 +116,22 @@ class TreeKEM {
116116
* excluded.
117117
*/
118118
async encryptToSubtree(head, value) {
119-
return this.mapSubtree(head, async node => {
119+
let encryptions = this.mapSubtree(head, async node => {
120120
return await ECKEM.encrypt(value, this.nodes[node].public);
121121
});
122+
123+
for (let n in encryptions) {
124+
encryptions[n] = await encryptions[n];
125+
}
126+
return encryptions;
122127
}
123128

124129
/*
125130
* Gather the heads of the populated subtrees below the specified
126131
* subtree head
127132
*/
128-
async gatherSubtree(head) {
129-
return await this.mapSubtree(head, node => util.publicNode(this.nodes[node]));
133+
gatherSubtree(head) {
134+
return this.mapSubtree(head, node => util.publicNode(this.nodes[node]));
130135
}
131136

132137
/*
@@ -173,9 +178,7 @@ class TreeKEM {
173178
// Gather subtree heads
174179
// NB: This is not necessary if other members have built a copy
175180
// of the tree. Unlike `nodes`, it's not new.
176-
let subtreeHeads = await Promise.all(copath.map(async n => {
177-
return await this.gatherSubtree(n);
178-
}));
181+
let subtreeHeads = copath.map(n => this.gatherSubtree(n));
179182
subtreeHeads = subtreeHeads.reduce((a, b) => Object.assign(a, b));
180183

181184
return {
@@ -202,42 +205,35 @@ class TreeKEM {
202205
* }
203206
*/
204207
async decrypt(index, ciphertexts) {
205-
console.log('>>> decrypt', index, ciphertexts);
206208
// These are the nodes that the sender encrypted to
207209
let senderSize = (index == this.size)? this.size + 1 : this.size;
208210
let copath = tm.copath(2 * index, senderSize);
209-
console.log('--- decrypt');
210211

211212
// These are the nodes that we should have private keys for
212213
let dirpath = tm.dirpath(2 * this.index, this.size);
213214
dirpath.push(tm.root(this.size));
214-
console.log('--- decrypt');
215215

216216
// Decrypt at the point where the dirpath and copath overlap
217217
let overlap = dirpath.filter(x => copath.includes(x))[0];
218218
let coIndex = copath.indexOf(overlap);
219219
let dirIndex = dirpath.indexOf(overlap);
220220
let encryptions = ciphertexts[coIndex];
221-
console.log('--- decrypt');
222221

223222
// Extract an encrypted value that we can decrypt, and decrypt it
224223
let decNode = Object.keys(encryptions)
225224
.map(x => parseInt(x))
226225
.filter(x => dirpath.includes(x))[0];
227226
let h = await ECKEM.decrypt(encryptions[decNode], this.nodes[decNode].private);
228-
console.log('--- decrypt');
229227

230228
// Hash up to the root (plus one if we're growing the tree)
231229
let rootNode = tm.root(senderSize);
232230
let newDirpath = tm.dirpath(2 * this.index, senderSize);
233231
newDirpath.push(rootNode);
234232
let nodes = await TreeKEM.hashUp(newDirpath[dirIndex+1], senderSize, h);
235-
console.log('--- decrypt');
236233

237234
let root = {}
238235
root[rootNode] = nodes[root];
239236

240-
console.log('<<< decrypt');
241237
return {
242238
root: root,
243239
nodes: nodes,
@@ -278,10 +274,16 @@ class TreeKEM {
278274
}
279275

280276
/*
281-
* Returns the nodes on the frontier of the tree { Int: Node }
277+
* Returns the nodes on the frontier of the tree { Int: Node },
278+
* including subtree heads if the tree is incomplete.
282279
*/
283280
frontier() {
284-
return util.nodePath(this.nodes, tm.frontier(this.size));
281+
let f = tm.frontier(this.size)
282+
.map(n => this.gatherSubtree(n))
283+
.reduce((a, b) => Object.assign(a, b));
284+
285+
console.log("frontier:", JSON.stringify(f));
286+
return f;
285287
}
286288

287289
/*

0 commit comments

Comments
 (0)