@@ -89,22 +89,22 @@ class TreeKEM {
89
89
* Returns:
90
90
* * {Node: T}
91
91
*/
92
- async mapSubtree ( node , func ) {
92
+ mapSubtree ( node , func ) {
93
93
let out = { } ;
94
94
95
95
if ( this . nodes [ node ] ) {
96
- out [ node ] = await func ( node ) ;
96
+ out [ node ] = func ( node ) ;
97
97
return out ;
98
98
}
99
99
100
100
let left = tm . left ( node ) ;
101
101
if ( left != node ) {
102
- Object . assign ( out , await this . mapSubtree ( left , func ) ) ;
102
+ Object . assign ( out , this . mapSubtree ( left , func ) ) ;
103
103
}
104
104
105
105
let right = tm . right ( node , this . size ) ;
106
106
if ( right != node ) {
107
- Object . assign ( out , await this . mapSubtree ( right , func ) ) ;
107
+ Object . assign ( out , this . mapSubtree ( right , func ) ) ;
108
108
}
109
109
110
110
return out ;
@@ -116,17 +116,22 @@ class TreeKEM {
116
116
* excluded.
117
117
*/
118
118
async encryptToSubtree ( head , value ) {
119
- return this . mapSubtree ( head , async node => {
119
+ let encryptions = this . mapSubtree ( head , async node => {
120
120
return await ECKEM . encrypt ( value , this . nodes [ node ] . public ) ;
121
121
} ) ;
122
+
123
+ for ( let n in encryptions ) {
124
+ encryptions [ n ] = await encryptions [ n ] ;
125
+ }
126
+ return encryptions ;
122
127
}
123
128
124
129
/*
125
130
* Gather the heads of the populated subtrees below the specified
126
131
* subtree head
127
132
*/
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 ] ) ) ;
130
135
}
131
136
132
137
/*
@@ -173,9 +178,7 @@ class TreeKEM {
173
178
// Gather subtree heads
174
179
// NB: This is not necessary if other members have built a copy
175
180
// 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 ) ) ;
179
182
subtreeHeads = subtreeHeads . reduce ( ( a , b ) => Object . assign ( a , b ) ) ;
180
183
181
184
return {
@@ -202,42 +205,35 @@ class TreeKEM {
202
205
* }
203
206
*/
204
207
async decrypt ( index , ciphertexts ) {
205
- console . log ( '>>> decrypt' , index , ciphertexts ) ;
206
208
// These are the nodes that the sender encrypted to
207
209
let senderSize = ( index == this . size ) ? this . size + 1 : this . size ;
208
210
let copath = tm . copath ( 2 * index , senderSize ) ;
209
- console . log ( '--- decrypt' ) ;
210
211
211
212
// These are the nodes that we should have private keys for
212
213
let dirpath = tm . dirpath ( 2 * this . index , this . size ) ;
213
214
dirpath . push ( tm . root ( this . size ) ) ;
214
- console . log ( '--- decrypt' ) ;
215
215
216
216
// Decrypt at the point where the dirpath and copath overlap
217
217
let overlap = dirpath . filter ( x => copath . includes ( x ) ) [ 0 ] ;
218
218
let coIndex = copath . indexOf ( overlap ) ;
219
219
let dirIndex = dirpath . indexOf ( overlap ) ;
220
220
let encryptions = ciphertexts [ coIndex ] ;
221
- console . log ( '--- decrypt' ) ;
222
221
223
222
// Extract an encrypted value that we can decrypt, and decrypt it
224
223
let decNode = Object . keys ( encryptions )
225
224
. map ( x => parseInt ( x ) )
226
225
. filter ( x => dirpath . includes ( x ) ) [ 0 ] ;
227
226
let h = await ECKEM . decrypt ( encryptions [ decNode ] , this . nodes [ decNode ] . private ) ;
228
- console . log ( '--- decrypt' ) ;
229
227
230
228
// Hash up to the root (plus one if we're growing the tree)
231
229
let rootNode = tm . root ( senderSize ) ;
232
230
let newDirpath = tm . dirpath ( 2 * this . index , senderSize ) ;
233
231
newDirpath . push ( rootNode ) ;
234
232
let nodes = await TreeKEM . hashUp ( newDirpath [ dirIndex + 1 ] , senderSize , h ) ;
235
- console . log ( '--- decrypt' ) ;
236
233
237
234
let root = { }
238
235
root [ rootNode ] = nodes [ root ] ;
239
236
240
- console . log ( '<<< decrypt' ) ;
241
237
return {
242
238
root : root ,
243
239
nodes : nodes ,
@@ -278,10 +274,16 @@ class TreeKEM {
278
274
}
279
275
280
276
/*
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.
282
279
*/
283
280
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 ;
285
287
}
286
288
287
289
/*
0 commit comments