hash-to-curve: adjust dst logic a bit

This commit is contained in:
Paul Miller 2024-02-27 22:34:30 +00:00
parent a70501cec4
commit 537db4a968
No known key found for this signature in database
GPG Key ID: 697079DA6878B89B
2 changed files with 3 additions and 10 deletions

@ -2,7 +2,7 @@
import type { Group, GroupConstructor, AffinePoint } from './curve.js'; import type { Group, GroupConstructor, AffinePoint } from './curve.js';
import { mod, IField } from './modular.js'; import { mod, IField } from './modular.js';
import type { CHash } from './utils.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 * * `DST` is a domain separation tag, defined in section 2.2.5
@ -22,12 +22,6 @@ export type Opts = {
hash: CHash; 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. // Octet Stream to Integer. "spec" implementation of os2ip is 2.5x slower vs bytesToNumberBE.
const os2ip = bytesToNumberBE; const os2ip = bytesToNumberBE;
@ -52,7 +46,6 @@ function strxor(a: Uint8Array, b: Uint8Array): Uint8Array {
return arr; return arr;
} }
function anum(item: unknown): void { function anum(item: unknown): void {
if (!Number.isSafeInteger(item)) throw new Error('number expected'); 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; const { p, k, m, hash, expand, DST: _DST } = options;
abytes(msg); abytes(msg);
anum(count); anum(count);
const DST = validateDST(_DST); const DST = typeof _DST === 'string' ? utf8ToBytes(_DST) : _DST;
const log2p = p.toString(2).length; const log2p = p.toString(2).length;
const L = Math.ceil((log2p + k) / 8); // section 5.1 of ietf draft link above const L = Math.ceil((log2p + k) / 8); // section 5.1 of ietf draft link above
const len_in_bytes = count * m * L; const len_in_bytes = count * m * L;

@ -200,7 +200,7 @@ export function bitGet(n: bigint, pos: number) {
*/ */
export function bitSet(n: bigint, pos: number, value: boolean) { export function bitSet(n: bigint, pos: number, value: boolean) {
return n | ((value ? _1n : _0n) << BigInt(pos)); return n | ((value ? _1n : _0n) << BigInt(pos));
}; }
/** /**
* Calculate mask for N bits. Not using ** operator with bigints because of old engines. * Calculate mask for N bits. Not using ** operator with bigints because of old engines.