feat: RNG ts 테스트 일치
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
export type Bytes = ArrayBuffer | DataView | Uint8Array;
|
||||
export type BytesLike = Bytes | string;
|
||||
+20
-33
@@ -2,15 +2,14 @@ import { RNG } from "./RNG";
|
||||
|
||||
import { sha512 } from 'js-sha512';
|
||||
|
||||
//Buffer를 써도 된다면 Buffer가 최선이지만..
|
||||
type Bytes = ArrayBuffer | DataView | Uint8Array;
|
||||
type MaybeBytes = Bytes | string;
|
||||
import { convertBytesLikeToUint8Array } from "./convertBytesLikeToUint8Array";
|
||||
import { BytesLike } from "./BytesLike";
|
||||
|
||||
const maxRngSupportBit = 53;
|
||||
const maxInt = 0x1f_ffff_ffff_ffff; // NOTE: b 0, 10000110011, 11...11
|
||||
const maxIntMore1 = 0x20_0000_0000_0000n; //NOTE: b 0, 10000110100, 00...00
|
||||
const maxIntMore1f = Number(maxIntMore1);
|
||||
const bufferByteSize = 512 / 8; //SHA512
|
||||
export const bufferByteSize = 512 / 8; //SHA512
|
||||
|
||||
const intBitMapMask = new Map([
|
||||
[0x1n, 1],
|
||||
@@ -85,7 +84,7 @@ export class LiteHashDRBG implements RNG {
|
||||
protected hq: DataView;
|
||||
protected hqIdxPos: number;
|
||||
|
||||
public constructor(protected seed: MaybeBytes, protected stateIdx = 0, bufferIdx = 0) {
|
||||
public constructor(protected seed: BytesLike, protected stateIdx = 0, bufferIdx = 0) {
|
||||
if(bufferIdx < 0){
|
||||
throw new Error(`bufferIdx ${bufferIdx} < 0`);
|
||||
}
|
||||
@@ -96,18 +95,7 @@ export class LiteHashDRBG implements RNG {
|
||||
throw new Error(`stateIdx ${stateIdx} < 0`);
|
||||
}
|
||||
|
||||
const seedU8 = (() => {
|
||||
if (seed instanceof Uint8Array) {
|
||||
return seed;
|
||||
}
|
||||
if (seed instanceof ArrayBuffer) {
|
||||
return new Uint8Array(seed);
|
||||
}
|
||||
if (typeof(seed) === 'string'){
|
||||
return (new TextEncoder()).encode(seed);
|
||||
}
|
||||
return new Uint8Array(seed.buffer);
|
||||
})();
|
||||
const seedU8 = convertBytesLikeToUint8Array(seed);
|
||||
const hqBuffer = new ArrayBuffer(seedU8.byteLength + 4);
|
||||
const hqU8 = new Uint8Array(hqBuffer);
|
||||
|
||||
@@ -131,7 +119,7 @@ export class LiteHashDRBG implements RNG {
|
||||
return maxInt;
|
||||
}
|
||||
|
||||
public nextBytes(bytes: number, baseBytes?: number): ArrayBuffer {
|
||||
public nextBytes(bytes: number, baseBytes?: number): Uint8Array {
|
||||
bytes |= 0;
|
||||
if (bytes <= 0) {
|
||||
throw new Error(`${bytes} <= 0`);
|
||||
@@ -139,17 +127,17 @@ export class LiteHashDRBG implements RNG {
|
||||
|
||||
if (this.bufferIdx + bytes <= bufferByteSize) {
|
||||
if(baseBytes === undefined || bytes >= baseBytes){
|
||||
const result = this.buffer.slice(this.bufferIdx, bytes);
|
||||
const result = this.buffer.slice(this.bufferIdx, this.bufferIdx + bytes);
|
||||
this.bufferIdx += bytes;
|
||||
if (this.bufferIdx === bufferByteSize) {
|
||||
this.genNextBlock();
|
||||
}
|
||||
return result;
|
||||
return new Uint8Array(result);
|
||||
}
|
||||
|
||||
const result = new ArrayBuffer(Math.max(bytes, baseBytes));
|
||||
const resultU8 = new Uint8Array(result);
|
||||
resultU8.set(new Uint8Array(this.buffer, this.bufferIdx, bytes));
|
||||
const resultBuffer = new ArrayBuffer(Math.max(bytes, baseBytes));
|
||||
const result = new Uint8Array(resultBuffer);
|
||||
result.set(new Uint8Array(this.buffer, this.bufferIdx, bytes));
|
||||
this.bufferIdx += bytes;
|
||||
if( this.bufferIdx === bufferByteSize){
|
||||
this.genNextBlock();
|
||||
@@ -157,16 +145,16 @@ export class LiteHashDRBG implements RNG {
|
||||
return result;
|
||||
}
|
||||
|
||||
const result = new ArrayBuffer(baseBytes ? Math.max(bytes, baseBytes) : bytes);
|
||||
const resultU8 = new Uint8Array(result);
|
||||
const resultBuffer = new ArrayBuffer(baseBytes ? Math.max(bytes, baseBytes) : bytes);
|
||||
const result = new Uint8Array(resultBuffer);
|
||||
|
||||
resultU8.set(new Uint8Array(this.buffer, this.bufferIdx));
|
||||
result.set(new Uint8Array(this.buffer, this.bufferIdx));
|
||||
let offset = bufferByteSize - this.bufferIdx;
|
||||
let remain = bytes - offset;
|
||||
|
||||
while (remain > bufferByteSize) {
|
||||
this.genNextBlock();
|
||||
resultU8.set(new Uint8Array(this.buffer), offset);
|
||||
result.set(new Uint8Array(this.buffer), offset);
|
||||
offset += bufferByteSize;
|
||||
remain -= bufferByteSize;
|
||||
}
|
||||
@@ -176,12 +164,12 @@ export class LiteHashDRBG implements RNG {
|
||||
return result;
|
||||
}
|
||||
|
||||
resultU8.set(new Uint8Array(this.buffer, 0, remain), offset);
|
||||
result.set(new Uint8Array(this.buffer, 0, remain), offset);
|
||||
this.bufferIdx = remain;
|
||||
return result;
|
||||
}
|
||||
|
||||
public nextBits(bits: number, baseBytes?: number): ArrayBuffer {
|
||||
public nextBits(bits: number, baseBytes?: number): Uint8Array {
|
||||
bits |= 0;
|
||||
const bytes = (bits + 7) >> 3;
|
||||
const headBits = bits & 0x7;
|
||||
@@ -191,14 +179,13 @@ export class LiteHashDRBG implements RNG {
|
||||
return result;
|
||||
}
|
||||
|
||||
const resultU8 = new Uint8Array(result);
|
||||
resultU8[bytes - 1] &= 0xff >> (8 - headBits);
|
||||
result[bytes - 1] &= 0xff >> (8 - headBits);
|
||||
return result;
|
||||
}
|
||||
|
||||
protected _nextInt(bits: number): bigint{
|
||||
const buffer = this.nextBits(bits, 8);
|
||||
const dataView = new DataView(buffer);
|
||||
const dataView = new DataView(buffer.buffer);
|
||||
return dataView.getBigUint64(0, true);
|
||||
}
|
||||
|
||||
@@ -239,7 +226,7 @@ export class LiteHashDRBG implements RNG {
|
||||
}
|
||||
}
|
||||
|
||||
public static build(seed: MaybeBytes, stateIdx = 0): LiteHashDRBG{
|
||||
public static build(seed: BytesLike, stateIdx = 0): LiteHashDRBG{
|
||||
return new LiteHashDRBG(seed, stateIdx);
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -5,8 +5,8 @@ export interface RNG {
|
||||
*/
|
||||
getMaxInt(): number;
|
||||
|
||||
nextBytes(bytes: number): ArrayBuffer;
|
||||
nextBits(bits: number): ArrayBuffer;
|
||||
nextBytes(bytes: number): Uint8Array;
|
||||
nextBits(bits: number): Uint8Array;
|
||||
|
||||
nextInt(max?: number): number;
|
||||
nextFloat1(): number;
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import { BytesLike } from "./BytesLike";
|
||||
|
||||
export function convertBytesLikeToArrayBuffer(data: BytesLike, encodeUTF8 = true): ArrayBuffer{
|
||||
if (data instanceof ArrayBuffer) {
|
||||
return data;
|
||||
}
|
||||
if (data instanceof Uint8Array) {
|
||||
return data.buffer;
|
||||
}
|
||||
if (typeof(data) === 'string'){
|
||||
if(encodeUTF8){
|
||||
return (new TextEncoder()).encode(data);
|
||||
}
|
||||
return new Uint8Array(data.split('').map(s=>s.codePointAt(0) as number));
|
||||
}
|
||||
return data.buffer;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { BytesLike } from "./BytesLike";
|
||||
|
||||
export function convertBytesLikeToUint8Array(data: BytesLike, encodeUTF8 = true): Uint8Array {
|
||||
if (data instanceof Uint8Array) {
|
||||
return data;
|
||||
}
|
||||
if (data instanceof ArrayBuffer) {
|
||||
return new Uint8Array(data);
|
||||
}
|
||||
if (typeof (data) === 'string') {
|
||||
if(encodeUTF8){
|
||||
return (new TextEncoder()).encode(data);
|
||||
}
|
||||
return new Uint8Array(data.split('').map(s=>s.codePointAt(0) as number));
|
||||
}
|
||||
return new Uint8Array(data.buffer);
|
||||
}
|
||||
Reference in New Issue
Block a user