@@ -42,7 +42,7 @@ type BitShifts = readonly BitShift[]
4242//
4343// Each value slice uses 6 bits of entropy. In bits, 36 is 100100.
4444// Now suppose we slice the value 50. In bits, 50 is 110010.
45- //
45+ //
4646// Only two bits are necessary to determine 100100 < 110010
4747//
4848const bitShifts = ( chars : string ) : BitShifts => {
@@ -90,6 +90,7 @@ const entropyByValues = (skipBytes: number, entropyBuffer: EntropyBuffer, source
9090 }
9191}
9292
93+ // Fill passed entropy buffer using entropy function
9394const fillEntropy = ( entropyOffset : number , entropyBuffer : ArrayBuffer , entropyFunction : EntropyFunction ) : number => {
9495 const entropyBytes : PuidBytes = new Uint8Array ( entropyBuffer )
9596
@@ -145,7 +146,7 @@ const valueAt = (lOffset: number, nBits: number, puidBytes: PuidBytes): number =
145146 return lValue + rValue
146147}
147148
148- export default ( puidLen : number , puidChars : string , entropyFunction : EntropyFunction ) : PuidBitsMuncherResult => {
149+ export default ( puidLen : number , puidChars : string , entropyFunction : EntropyFunction ) : PuidBitsMuncher => {
149150 const nBitsPerChar = bitsPerChar ( puidChars )
150151 const nBitsPerPuid = nBitsPerChar * puidLen
151152 const nBytesPerPuid = ceil ( nBitsPerPuid / 8 )
@@ -160,20 +161,20 @@ export default (puidLen: number, puidChars: string, entropyFunction: EntropyFunc
160161 const nChars = puidChars . length
161162 const mapper = new Array ( puidLen ) . fill ( 0 ) . map ( ( zero , ndx ) => zero + ndx )
162163
164+ // When chars count is a power of 2, sliced bits always yield a valid value
163165 if ( isPow2 ( nChars ) ) {
164- // When chars count is a power of 2, sliced bits always yield a valid value
165- const bitsMuncher = ( ) => {
166+ return ( ) => {
166167 entropyOffset = fillEntropy ( entropyOffset , entropyBuffer , entropyFunction )
167168 const codes = mapper . map ( ( ndx : number ) =>
168169 charsEncoder ( valueAt ( entropyOffset + ndx * nBitsPerChar , nBitsPerChar , entropyBytes ) )
169170 )
170171 entropyOffset += nBitsPerPuid
171172 return String . fromCharCode ( ...codes )
172173 }
173-
174- return { success : bitsMuncher }
175174 }
176175
176+ // When chars count not a power of 2, sliced bits may yield an invalid value
177+
177178 const nEntropyBits = 8 * entropyBytes . length
178179 const puidShifts = bitShifts ( puidChars )
179180
@@ -189,14 +190,15 @@ export default (puidLen: number, puidChars: string, entropyFunction: EntropyFunc
189190 return [ false , shift || nBitsPerChar ]
190191 }
191192
192- // When chars count not a power of 2, sliced bits may yield an invalid value
193+ // Slice value from entropy bytes
193194 const sliceValue = ( ) => {
195+ // Add more entropy bytes if necessary
194196 if ( nEntropyBits < entropyOffset + nBitsPerChar ) {
195197 entropyOffset = fillEntropy ( entropyOffset , entropyBuffer , entropyFunction )
196198 }
197199
198200 const slicedValue = valueAt ( entropyOffset , nBitsPerChar , entropyBytes )
199-
201+
200202 const [ accept , shift ] = acceptValue ( slicedValue )
201203 // Returned shift is the minimal bits necessary to determine if slice value is valid
202204 entropyOffset += shift
@@ -208,9 +210,7 @@ export default (puidLen: number, puidChars: string, entropyFunction: EntropyFunc
208210 return sliceValue ( )
209211 }
210212
211- const bitsMuncher = ( ) => String . fromCharCode ( ...mapper . map ( ( ) => sliceValue ( ) ) )
212-
213- return { success : bitsMuncher }
213+ return ( ) => String . fromCharCode ( ...mapper . map ( ( ) => sliceValue ( ) ) )
214214}
215215
216216export { bitShifts }
0 commit comments