From 3e90930e9d147b6cf642b0ca1c49dc6de8664bfe Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Sun, 26 Feb 2023 18:10:50 +0000 Subject: [PATCH] Fix types --- README.md | 2 +- src/abstract/hash-to-curve.ts | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0f1489a..f2cac8a 100644 --- a/README.md +++ b/README.md @@ -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). -Every curve has exported `hashToCurve` and `encodeToCurve` methods: +Every curve has exported `hashToCurve` and `encodeToCurve` methods. You should always prefer `hashToCurve` for security: ```ts import { hashToCurve, encodeToCurve } from '@noble/curves/secp256k1'; diff --git a/src/abstract/hash-to-curve.ts b/src/abstract/hash-to-curve.ts index 6befe77..b5a88d9 100644 --- a/src/abstract/hash-to-curve.ts +++ b/src/abstract/hash-to-curve.ts @@ -11,8 +11,9 @@ import { bytesToNumberBE, CHash, concatBytes, utf8ToBytes, validateObject } from * * `expand` is `xmd` (SHA2, SHA3, BLAKE) or `xof` (SHAKE, BLAKE-XOF) * * `hash` conforming to `utils.CHash` interface, with `outputLen` / `blockLen` props */ +type UnicodeOrBytes = string | Uint8Array; export type Opts = { - DST: string | Uint8Array; + DST: UnicodeOrBytes; p: bigint; m: number; k: number; @@ -20,7 +21,7 @@ export type Opts = { hash: CHash; }; -function validateDST(dst: string | Uint8Array): Uint8Array { +function validateDST(dst: UnicodeOrBytes): Uint8Array { if (dst instanceof Uint8Array) return dst; if (typeof dst === 'string') return utf8ToBytes(dst); throw new Error('DST must be Uint8Array or string'); @@ -183,12 +184,12 @@ export type MapToCurve = (scalar: bigint[]) => AffinePoint; // Separated from initialization opts, so users won't accidentally change per-curve parameters // (changing DST is ok!) -export type htfBasicOpts = { DST: string }; +export type htfBasicOpts = { DST: UnicodeOrBytes }; export function createHasher( Point: H2CPointConstructor, mapToCurve: MapToCurve, - def: Opts & { encodeDST?: string } + def: Opts & { encodeDST?: UnicodeOrBytes } ) { validateObject(def, { DST: 'string', @@ -200,7 +201,7 @@ export function createHasher( if (typeof mapToCurve !== 'function') throw new Error('mapToCurve() must be defined'); return { // 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) { const u = hash_to_field(msg, 2, { ...def, DST: def.DST, ...options } as Opts); const u0 = Point.fromAffine(mapToCurve(u[0]));