Skip to content

Commit

Permalink
hash-to-curve: adjust dst logic a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Feb 27, 2024
1 parent a70501c commit 537db4a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 10 deletions.
11 changes: 2 additions & 9 deletions src/abstract/hash-to-curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand All @@ -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');
}
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/abstract/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 537db4a

Please sign in to comment.