@@ -120,6 +120,8 @@ export function computeProposerIndex(
120
120
const shuffledResult = new Map < number , number > ( ) ;
121
121
122
122
let i = 0 ;
123
+ const cachedHashInput = Buffer . allocUnsafe ( 32 + 8 ) ;
124
+ cachedHashInput . set ( seed , 0 ) ;
123
125
let cachedHash : Uint8Array | null = null ;
124
126
while ( true ) {
125
127
// an optimized version of the below naive code
@@ -134,7 +136,8 @@ export function computeProposerIndex(
134
136
135
137
// compute a new hash every 16 iterations
136
138
if ( i % 16 === 0 ) {
137
- cachedHash = digest ( Buffer . concat ( [ seed , intToBytes ( Math . floor ( i / 16 ) , 8 , "le" ) ] ) ) ;
139
+ cachedHashInput . writeUint32LE ( Math . floor ( i / 16 ) , 32 ) ;
140
+ cachedHash = digest ( cachedHashInput ) ;
138
141
}
139
142
140
143
if ( cachedHash == null ) {
@@ -270,6 +273,8 @@ export function getNextSyncCommitteeIndices(
270
273
271
274
let i = 0 ;
272
275
let cachedHash : Uint8Array | null = null ;
276
+ const cachedHashInput = Buffer . allocUnsafe ( 32 + 8 ) ;
277
+ cachedHashInput . set ( seed , 0 ) ;
273
278
// this simple cache makes sure we don't have to recompute the shuffled index for the next round of activeValidatorCount
274
279
const shuffledResult = new Map < number , number > ( ) ;
275
280
while ( syncCommitteeIndices . length < SYNC_COMMITTEE_SIZE ) {
@@ -285,7 +290,8 @@ export function getNextSyncCommitteeIndices(
285
290
286
291
// compute a new hash every 16 iterations
287
292
if ( i % 16 === 0 ) {
288
- cachedHash = digest ( Buffer . concat ( [ seed , intToBytes ( Math . floor ( i / 16 ) , 8 , "le" ) ] ) ) ;
293
+ cachedHashInput . writeUint32LE ( Math . floor ( i / 16 ) , 32 ) ;
294
+ cachedHash = digest ( cachedHashInput ) ;
289
295
}
290
296
291
297
if ( cachedHash == null ) {
@@ -402,7 +408,7 @@ export function getComputeShuffledIndexFn(indexCount: number, seed: Bytes32): Co
402
408
// bytesToBigInt(digest(Buffer.concat([_seed, intToBytes(i, 1)])).slice(0, 8)) % BigInt(indexCount)
403
409
// );
404
410
pivotBuffer [ 32 ] = i % 256 ;
405
- pivot = Number ( bytesToBigInt ( digest ( pivotBuffer ) . slice ( 0 , 8 ) ) % BigInt ( indexCount ) ) ;
411
+ pivot = Number ( bytesToBigInt ( digest ( pivotBuffer ) . subarray ( 0 , 8 ) ) % BigInt ( indexCount ) ) ;
406
412
pivotByIndex . set ( i , pivot ) ;
407
413
}
408
414
0 commit comments