From 537db4a96840016adffb813b3d2150434b801ce2 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Tue, 27 Feb 2024 22:34:30 +0000 Subject: [PATCH] hash-to-curve: adjust dst logic a bit --- src/abstract/hash-to-curve.ts | 11 ++--------- src/abstract/utils.ts | 2 +- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/abstract/hash-to-curve.ts b/src/abstract/hash-to-curve.ts index ce27c5d..474f7fb 100644 --- a/src/abstract/hash-to-curve.ts +++ b/src/abstract/hash-to-curve.ts @@ -2,7 +2,7 @@ import type { Group, GroupConstructor, AffinePoint } from './curve.js'; import { mod, IField } from './modular.js'; import type { CHash } from './utils.js'; -import { bytesToNumberBE, abytes, isBytes, concatBytes, utf8ToBytes, validateObject } from './utils.js'; +import { bytesToNumberBE, abytes, concatBytes, utf8ToBytes, validateObject } from './utils.js'; /** * * `DST` is a domain separation tag, defined in section 2.2.5 @@ -22,12 +22,6 @@ export type Opts = { hash: CHash; }; -function validateDST(dst: UnicodeOrBytes): Uint8Array { - if (isBytes(dst)) return dst; - if (typeof dst === 'string') return utf8ToBytes(dst); - throw new Error('DST must be Uint8Array or string'); -} - // Octet Stream to Integer. "spec" implementation of os2ip is 2.5x slower vs bytesToNumberBE. const os2ip = bytesToNumberBE; @@ -52,7 +46,6 @@ function strxor(a: Uint8Array, b: Uint8Array): Uint8Array { return arr; } - function anum(item: unknown): void { if (!Number.isSafeInteger(item)) throw new Error('number expected'); } @@ -140,7 +133,7 @@ export function hash_to_field(msg: Uint8Array, count: number, options: Opts): bi const { p, k, m, hash, expand, DST: _DST } = options; abytes(msg); anum(count); - const DST = validateDST(_DST); + const DST = typeof _DST === 'string' ? utf8ToBytes(_DST) : _DST; const log2p = p.toString(2).length; const L = Math.ceil((log2p + k) / 8); // section 5.1 of ietf draft link above const len_in_bytes = count * m * L; diff --git a/src/abstract/utils.ts b/src/abstract/utils.ts index 186040f..51b0147 100644 --- a/src/abstract/utils.ts +++ b/src/abstract/utils.ts @@ -200,7 +200,7 @@ export function bitGet(n: bigint, pos: number) { */ export function bitSet(n: bigint, pos: number, value: boolean) { return n | ((value ? _1n : _0n) << BigInt(pos)); -}; +} /** * Calculate mask for N bits. Not using ** operator with bigints because of old engines.