Skip to content

Commit

Permalink
Expose utils: invert, hexToBytes, concatBytes. Closes #46, #48, #63
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed May 19, 2022
1 parent 30ed53b commit 450ebc7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 16 deletions.
3 changes: 3 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,10 @@ export const utils = {
},

bytesToHex,
hexToBytes,
concatBytes,
mod,
invert,

sha256: async (...messages: Uint8Array[]): Promise<Uint8Array> => {
if (crypto.web) {
Expand Down
17 changes: 1 addition & 16 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,7 @@ secp.utils.sha256Sync = (...messages: Uint8Array[]): Uint8Array => {

const toBEHex = (n: number | bigint) => n.toString(16).padStart(64, '0');
const hex = secp.utils.bytesToHex;
// Caching slows it down 2-3x
function hexToBytes(hex: string): Uint8Array {
if (typeof hex !== 'string') {
throw new TypeError('hexToBytes: expected string, got ' + typeof hex);
}
if (hex.length % 2) throw new Error('hexToBytes: received invalid unpadded hex' + hex.length);
const array = new Uint8Array(hex.length / 2);
for (let i = 0; i < array.length; i++) {
const j = i * 2;
const hexByte = hex.slice(j, j + 2);
const byte = Number.parseInt(hexByte, 16);
if (Number.isNaN(byte) || byte < 0) throw new Error('Invalid byte sequence');
array[i] = byte;
}
return array;
}
const hexToBytes = secp.utils.hexToBytes;

function hexToNumber(hex: string): bigint {
if (typeof hex !== 'string') {
Expand Down

0 comments on commit 450ebc7

Please sign in to comment.