_shortw_utils.ts 868 B

1234567891011121314151617181920
  1. /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
  2. import { hmac } from '@noble/hashes/hmac';
  3. import { concatBytes, randomBytes } from '@noble/hashes/utils';
  4. import { CHash } from './abstract/utils.js';
  5. import { CurveType, weierstrass } from './abstract/weierstrass.js';
  6. // connects noble-curves to noble-hashes
  7. export function getHash(hash: CHash) {
  8. return {
  9. hash,
  10. hmac: (key: Uint8Array, ...msgs: Uint8Array[]) => hmac(hash, key, concatBytes(...msgs)),
  11. randomBytes,
  12. };
  13. }
  14. // Same API as @noble/hashes, with ability to create curve with custom hash
  15. type CurveDef = Readonly<Omit<CurveType, 'hash' | 'hmac' | 'randomBytes'>>;
  16. export function createCurve(curveDef: CurveDef, defHash: CHash) {
  17. const create = (hash: CHash) => weierstrass({ ...curveDef, ...getHash(hash) });
  18. return Object.freeze({ ...create(defHash), create });
  19. }