Skip to content

Commit 3a73581

Browse files
committed
Switch from new uint8array to u8a.from. Closes noble-curves#176.
1 parent abd443f commit 3a73581

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/*

index.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ const isu8 = (a) => (a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.cons
2626
const au8 = (a, l) => // assert is Uint8Array (of specific length)
2727
!isu8(a) || (typeof l === 'number' && l > 0 && a.length !== l) ?
2828
err('Uint8Array expected') : a;
29-
const u8n = (data) => new Uint8Array(data); // creates Uint8Array
30-
const toU8 = (a, len) => au8(isS(a) ? h2b(a) : u8n(au8(a)), len); // norm(hex/u8a) to u8a
29+
const u8n = (len) => new Uint8Array(len); // creates Uint8Array
30+
const u8fr = (buf) => Uint8Array.from(buf);
31+
const toU8 = (a, len) => au8(isS(a) ? h2b(a) : u8fr(au8(a)), len); // norm(hex/u8a) to u8a
3132
const M = (a, b = P) => {
3233
const r = a % b;
3334
return r >= 0n ? r : b + r;
@@ -362,12 +363,12 @@ function hmacDrbg(asynchronous) {
362363
const _e = 'drbg: tried 1000 values';
363364
if (asynchronous) { // asynchronous=true
364365
const h = (...b) => etc.hmacSha256Async(k, v, ...b); // hmac(k)(v, ...values)
365-
const reseed = async (seed = u8n()) => {
366-
k = await h(u8n([0x00]), seed); // k = hmac(K || V || 0x00 || seed)
366+
const reseed = async (seed = u8n(0)) => {
367+
k = await h(u8fr([0x00]), seed); // k = hmac(K || V || 0x00 || seed)
367368
v = await h(); // v = hmac(K || V)
368369
if (seed.length === 0)
369370
return;
370-
k = await h(u8n([0x01]), seed); // k = hmac(K || V || 0x01 || seed)
371+
k = await h(u8fr([0x01]), seed); // k = hmac(K || V || 0x01 || seed)
371372
v = await h(); // v = hmac(K || V)
372373
};
373374
const gen = async () => {
@@ -393,12 +394,12 @@ function hmacDrbg(asynchronous) {
393394
err('etc.hmacSha256Sync not set');
394395
return f(k, v, ...b); // hmac(k)(v, ...values)
395396
};
396-
const reseed = (seed = u8n()) => {
397-
k = h(u8n([0x00]), seed); // k = hmac(k || v || 0x00 || seed)
397+
const reseed = (seed = u8n(0)) => {
398+
k = h(u8fr([0x00]), seed); // k = hmac(k || v || 0x00 || seed)
398399
v = h(); // v = hmac(k || v)
399400
if (seed.length === 0)
400401
return;
401-
k = h(u8n([0x01]), seed); // k = hmac(k || v || 0x01 || seed)
402+
k = h(u8fr([0x01]), seed); // k = hmac(k || v || 0x01 || seed)
402403
v = h(); // v = hmac(k || v)
403404
};
404405
const gen = () => {

index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ const isu8 = (a: unknown): a is Uint8Array => (
3737
const au8 = (a: unknown, l?: number): Bytes => // assert is Uint8Array (of specific length)
3838
!isu8(a) || (typeof l === 'number' && l > 0 && a.length !== l) ?
3939
err('Uint8Array expected') : a;
40-
const u8n = (data?: any) => new Uint8Array(data); // creates Uint8Array
41-
const toU8 = (a: Hex, len?: number) => au8(isS(a) ? h2b(a) : u8n(au8(a)), len); // norm(hex/u8a) to u8a
40+
const u8n = (len: number) => new Uint8Array(len); // creates Uint8Array
41+
const u8fr = (buf: ArrayLike<number>) => Uint8Array.from(buf);
42+
const toU8 = (a: Hex, len?: number) => au8(isS(a) ? h2b(a) : u8fr(au8(a)), len); // norm(hex/u8a) to u8a
4243
const M = (a: bigint, b: bigint = P) => { // mod division
4344
const r = a % b; return r >= 0n ? r : b + r;
4445
};
@@ -326,11 +327,11 @@ function hmacDrbg<T>(asynchronous: boolean) { // HMAC-DRBG async
326327
const _e = 'drbg: tried 1000 values';
327328
if (asynchronous) { // asynchronous=true
328329
const h = (...b: Bytes[]) => etc.hmacSha256Async(k, v, ...b); // hmac(k)(v, ...values)
329-
const reseed = async (seed = u8n()) => { // HMAC-DRBG reseed() function. Steps D-G
330-
k = await h(u8n([0x00]), seed); // k = hmac(K || V || 0x00 || seed)
330+
const reseed = async (seed = u8n(0)) => { // HMAC-DRBG reseed() function. Steps D-G
331+
k = await h(u8fr([0x00]), seed); // k = hmac(K || V || 0x00 || seed)
331332
v = await h(); // v = hmac(K || V)
332333
if (seed.length === 0) return;
333-
k = await h(u8n([0x01]), seed); // k = hmac(K || V || 0x01 || seed)
334+
k = await h(u8fr([0x01]), seed); // k = hmac(K || V || 0x01 || seed)
334335
v = await h(); // v = hmac(K || V)
335336
};
336337
const gen = async () => { // HMAC-DRBG generate() function
@@ -352,11 +353,11 @@ function hmacDrbg<T>(asynchronous: boolean) { // HMAC-DRBG async
352353
if (!f) err('etc.hmacSha256Sync not set');
353354
return f!(k, v, ...b); // hmac(k)(v, ...values)
354355
};
355-
const reseed = (seed = u8n()) => { // HMAC-DRBG reseed() function. Steps D-G
356-
k = h(u8n([0x00]), seed); // k = hmac(k || v || 0x00 || seed)
356+
const reseed = (seed = u8n(0)) => { // HMAC-DRBG reseed() function. Steps D-G
357+
k = h(u8fr([0x00]), seed); // k = hmac(k || v || 0x00 || seed)
357358
v = h(); // v = hmac(k || v)
358359
if (seed.length === 0) return;
359-
k = h(u8n([0x01]), seed); // k = hmac(k || v || 0x01 || seed)
360+
k = h(u8fr([0x01]), seed); // k = hmac(k || v || 0x01 || seed)
360361
v = h(); // v = hmac(k || v)
361362
};
362363
const gen = () => { // HMAC-DRBG generate() function

0 commit comments

Comments
 (0)