Fix types

This commit is contained in:
Paul Miller 2023-02-26 18:10:50 +00:00
parent b8b2e91f74
commit 3e90930e9d
No known key found for this signature in database
GPG Key ID: 697079DA6878B89B
2 changed files with 7 additions and 6 deletions

@ -472,7 +472,7 @@ const x25519 = montgomery({
The module allows to hash arbitrary strings to elliptic curve points. Implements [hash-to-curve v16](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-16). The module allows to hash arbitrary strings to elliptic curve points. Implements [hash-to-curve v16](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-16).
Every curve has exported `hashToCurve` and `encodeToCurve` methods: Every curve has exported `hashToCurve` and `encodeToCurve` methods. You should always prefer `hashToCurve` for security:
```ts ```ts
import { hashToCurve, encodeToCurve } from '@noble/curves/secp256k1'; import { hashToCurve, encodeToCurve } from '@noble/curves/secp256k1';

@ -11,8 +11,9 @@ import { bytesToNumberBE, CHash, concatBytes, utf8ToBytes, validateObject } from
* * `expand` is `xmd` (SHA2, SHA3, BLAKE) or `xof` (SHAKE, BLAKE-XOF) * * `expand` is `xmd` (SHA2, SHA3, BLAKE) or `xof` (SHAKE, BLAKE-XOF)
* * `hash` conforming to `utils.CHash` interface, with `outputLen` / `blockLen` props * * `hash` conforming to `utils.CHash` interface, with `outputLen` / `blockLen` props
*/ */
type UnicodeOrBytes = string | Uint8Array;
export type Opts = { export type Opts = {
DST: string | Uint8Array; DST: UnicodeOrBytes;
p: bigint; p: bigint;
m: number; m: number;
k: number; k: number;
@ -20,7 +21,7 @@ export type Opts = {
hash: CHash; hash: CHash;
}; };
function validateDST(dst: string | Uint8Array): Uint8Array { function validateDST(dst: UnicodeOrBytes): Uint8Array {
if (dst instanceof Uint8Array) return dst; if (dst instanceof Uint8Array) return dst;
if (typeof dst === 'string') return utf8ToBytes(dst); if (typeof dst === 'string') return utf8ToBytes(dst);
throw new Error('DST must be Uint8Array or string'); throw new Error('DST must be Uint8Array or string');
@ -183,12 +184,12 @@ export type MapToCurve<T> = (scalar: bigint[]) => AffinePoint<T>;
// Separated from initialization opts, so users won't accidentally change per-curve parameters // Separated from initialization opts, so users won't accidentally change per-curve parameters
// (changing DST is ok!) // (changing DST is ok!)
export type htfBasicOpts = { DST: string }; export type htfBasicOpts = { DST: UnicodeOrBytes };
export function createHasher<T>( export function createHasher<T>(
Point: H2CPointConstructor<T>, Point: H2CPointConstructor<T>,
mapToCurve: MapToCurve<T>, mapToCurve: MapToCurve<T>,
def: Opts & { encodeDST?: string } def: Opts & { encodeDST?: UnicodeOrBytes }
) { ) {
validateObject(def, { validateObject(def, {
DST: 'string', DST: 'string',
@ -200,7 +201,7 @@ export function createHasher<T>(
if (typeof mapToCurve !== 'function') throw new Error('mapToCurve() must be defined'); if (typeof mapToCurve !== 'function') throw new Error('mapToCurve() must be defined');
return { return {
// Encodes byte string to elliptic curve // Encodes byte string to elliptic curve
// https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-11#section-3 // https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-16#section-3
hashToCurve(msg: Uint8Array, options?: htfBasicOpts) { hashToCurve(msg: Uint8Array, options?: htfBasicOpts) {
const u = hash_to_field(msg, 2, { ...def, DST: def.DST, ...options } as Opts); const u = hash_to_field(msg, 2, { ...def, DST: def.DST, ...options } as Opts);
const u0 = Point.fromAffine(mapToCurve(u[0])); const u0 = Point.fromAffine(mapToCurve(u[0]));