Skip to content

Commit

Permalink
Speed-up hex, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Nov 8, 2024
1 parent 90867ff commit 3aa804b
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 202 deletions.
21 changes: 10 additions & 11 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ declare class Point {
toHex(isCompressed?: boolean): string;
toRawBytes(isCompressed?: boolean): Uint8Array;
}
declare function getPublicKey(privKey: PrivKey, isCompressed?: boolean): Uint8Array;
declare const getPublicKey: (privKey: PrivKey, isCompressed?: boolean) => Uint8Array;
type SignatureWithRecovery = Signature & {
recovery: number;
};
Expand All @@ -58,25 +58,24 @@ declare class Signature {
toCompactHex(): string;
}
type HmacFnSync = undefined | ((key: Bytes, ...msgs: Bytes[]) => Bytes);
declare function signAsync(msgh: Hex, priv: PrivKey, opts?: {
declare const signAsync: (msgh: Hex, priv: PrivKey, opts?: {
lowS?: boolean | undefined;
extraEntropy?: boolean | Hex | undefined;
}): Promise<SignatureWithRecovery>;
declare function sign(msgh: Hex, priv: PrivKey, opts?: {
}) => Promise<SignatureWithRecovery>;
declare const sign: (msgh: Hex, priv: PrivKey, opts?: {
lowS?: boolean | undefined;
extraEntropy?: boolean | Hex | undefined;
}): SignatureWithRecovery;
}) => SignatureWithRecovery;
type SigLike = {
r: bigint;
s: bigint;
};
declare function verify(sig: Hex | SigLike, msgh: Hex, pub: Hex, opts?: {
declare const verify: (sig: Hex | SigLike, msgh: Hex, pub: Hex, opts?: {
lowS?: boolean | undefined;
}): boolean;
declare function getSharedSecret(privA: Hex, pubB: Hex, isCompressed?: boolean): Bytes;
declare function hashToPrivateKey(hash: Hex): Bytes;
}) => boolean;
declare const getSharedSecret: (privA: Hex, pubB: Hex, isCompressed?: boolean) => Bytes;
declare const etc: {
hexToBytes: (hex: string) => Bytes;
hexToBytes: (hex: string) => Uint8Array;
bytesToHex: (b: Bytes) => string;
concatBytes: (...arrs: Bytes[]) => Uint8Array;
bytesToNumberBE: (b: Bytes) => bigint;
Expand All @@ -85,7 +84,7 @@ declare const etc: {
invert: (num: bigint, md?: bigint) => bigint;
hmacSha256Async: (key: Bytes, ...msgs: Bytes[]) => Promise<Bytes>;
hmacSha256Sync: HmacFnSync;
hashToPrivateKey: typeof hashToPrivateKey;
hashToPrivateKey: (hash: Hex) => Bytes;
randomBytes: (len?: number) => Bytes;
};
declare const utils: {
Expand Down
211 changes: 111 additions & 100 deletions index.js

Large diffs are not rendered by default.

168 changes: 87 additions & 81 deletions index.ts

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"license": "MIT",
"devDependencies": {
"@noble/hashes": "1.4.0",
"@paulmillr/jsbt": "0.1.0",
"@paulmillr/jsbt": "0.2.1",
"fast-check": "3.0.0",
"micro-bmark": "0.3.0",
"micro-should": "0.4.0",
Expand Down
3 changes: 2 additions & 1 deletion test/secp256k1.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * as secp from '../index.js';
import * as secp256k1 from '../index.js';
import { hmac } from '@noble/hashes/hmac';
import { sha256 } from '@noble/hashes/sha256';
import { isBytes } from '@noble/hashes/utils';
secp256k1.etc.hmacSha256Sync = (key: Uint8Array, ...msgs: Uint8Array[]) => hmac(sha256, key, secp256k1.etc.concatBytes(...msgs))

const { bytesToNumberBE: b2n, hexToBytes: h2b } = secp256k1.etc;
Expand Down Expand Up @@ -31,7 +32,7 @@ export const DER = {
// parse DER signature
const { Err: E } = DER;
const data = typeof hex === 'string' ? h2b(hex) : hex;
if (!(data instanceof Uint8Array)) throw new Error('ui8a expected');
if (!isBytes(data)) throw new Error('ui8a expected');
let l = data.length;
if (l < 2 || data[0] != 0x30) throw new E('Invalid signature tag');
if (data[1] !== l - 2) throw new E('Invalid signature: incorrect length');
Expand Down
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"extends": "@paulmillr/jsbt/tsconfigs/esm.json",
"extends": "@paulmillr/jsbt/tsconfig.esm.json",
"compilerOptions": {
"outDir": ".",
"sourceMap": false
"sourceMap": false,
"declarationMap": false
},
"include": ["index.ts"],
"exclude": ["node_modules", "lib"]
Expand Down

0 comments on commit 3aa804b

Please sign in to comment.