Skip to content

Commit ed73aa8

Browse files
authored
Merge pull request #1859 from headfire94/egor/perf/prefix-ondemand
chore: initialise TAGGED_HASH_PREFIXES on-demand
2 parents 3e9827c + cc1b23e commit ed73aa8

File tree

5 files changed

+148
-22
lines changed

5 files changed

+148
-22
lines changed

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/crypto.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ export declare function sha1(buffer: Buffer): Buffer;
44
export declare function sha256(buffer: Buffer): Buffer;
55
export declare function hash160(buffer: Buffer): Buffer;
66
export declare function hash256(buffer: Buffer): Buffer;
7-
declare const TAGS: readonly ["BIP0340/challenge", "BIP0340/aux", "BIP0340/nonce", "TapLeaf", "TapBranch", "TapSighash", "TapTweak", "KeyAgg list", "KeyAgg coefficient"];
7+
export declare const TAGS: readonly ["BIP0340/challenge", "BIP0340/aux", "BIP0340/nonce", "TapLeaf", "TapBranch", "TapSighash", "TapTweak", "KeyAgg list", "KeyAgg coefficient"];
88
export type TaggedHashPrefix = typeof TAGS[number];
9+
type TaggedHashPrefixes = {
10+
[key in TaggedHashPrefix]: Buffer;
11+
};
12+
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
13+
export declare const TAGGED_HASH_PREFIXES: TaggedHashPrefixes;
914
export declare function taggedHash(prefix: TaggedHashPrefix, data: Buffer): Buffer;
1015
export {};

src/crypto.js

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22
Object.defineProperty(exports, '__esModule', { value: true });
33
exports.taggedHash =
4+
exports.TAGGED_HASH_PREFIXES =
5+
exports.TAGS =
46
exports.hash256 =
57
exports.hash160 =
68
exports.sha256 =
@@ -37,7 +39,7 @@ function hash256(buffer) {
3739
return sha256(sha256(buffer));
3840
}
3941
exports.hash256 = hash256;
40-
const TAGS = [
42+
exports.TAGS = [
4143
'BIP0340/challenge',
4244
'BIP0340/aux',
4345
'BIP0340/nonce',
@@ -49,13 +51,63 @@ const TAGS = [
4951
'KeyAgg coefficient',
5052
];
5153
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
52-
const TAGGED_HASH_PREFIXES = Object.fromEntries(
53-
TAGS.map(tag => {
54-
const tagHash = sha256(Buffer.from(tag));
55-
return [tag, Buffer.concat([tagHash, tagHash])];
56-
}),
57-
);
54+
exports.TAGGED_HASH_PREFIXES = {
55+
'BIP0340/challenge': Buffer.from([
56+
123, 181, 45, 122, 159, 239, 88, 50, 62, 177, 191, 122, 64, 125, 179, 130,
57+
210, 243, 242, 216, 27, 177, 34, 79, 73, 254, 81, 143, 109, 72, 211, 124,
58+
123, 181, 45, 122, 159, 239, 88, 50, 62, 177, 191, 122, 64, 125, 179, 130,
59+
210, 243, 242, 216, 27, 177, 34, 79, 73, 254, 81, 143, 109, 72, 211, 124,
60+
]),
61+
'BIP0340/aux': Buffer.from([
62+
241, 239, 78, 94, 192, 99, 202, 218, 109, 148, 202, 250, 157, 152, 126, 160,
63+
105, 38, 88, 57, 236, 193, 31, 151, 45, 119, 165, 46, 216, 193, 204, 144,
64+
241, 239, 78, 94, 192, 99, 202, 218, 109, 148, 202, 250, 157, 152, 126, 160,
65+
105, 38, 88, 57, 236, 193, 31, 151, 45, 119, 165, 46, 216, 193, 204, 144,
66+
]),
67+
'BIP0340/nonce': Buffer.from([
68+
7, 73, 119, 52, 167, 155, 203, 53, 91, 155, 140, 125, 3, 79, 18, 28, 244,
69+
52, 215, 62, 247, 45, 218, 25, 135, 0, 97, 251, 82, 191, 235, 47, 7, 73,
70+
119, 52, 167, 155, 203, 53, 91, 155, 140, 125, 3, 79, 18, 28, 244, 52, 215,
71+
62, 247, 45, 218, 25, 135, 0, 97, 251, 82, 191, 235, 47,
72+
]),
73+
TapLeaf: Buffer.from([
74+
174, 234, 143, 220, 66, 8, 152, 49, 5, 115, 75, 88, 8, 29, 30, 38, 56, 211,
75+
95, 28, 181, 64, 8, 212, 211, 87, 202, 3, 190, 120, 233, 238, 174, 234, 143,
76+
220, 66, 8, 152, 49, 5, 115, 75, 88, 8, 29, 30, 38, 56, 211, 95, 28, 181,
77+
64, 8, 212, 211, 87, 202, 3, 190, 120, 233, 238,
78+
]),
79+
TapBranch: Buffer.from([
80+
25, 65, 161, 242, 229, 110, 185, 95, 162, 169, 241, 148, 190, 92, 1, 247,
81+
33, 111, 51, 237, 130, 176, 145, 70, 52, 144, 208, 91, 245, 22, 160, 21, 25,
82+
65, 161, 242, 229, 110, 185, 95, 162, 169, 241, 148, 190, 92, 1, 247, 33,
83+
111, 51, 237, 130, 176, 145, 70, 52, 144, 208, 91, 245, 22, 160, 21,
84+
]),
85+
TapSighash: Buffer.from([
86+
244, 10, 72, 223, 75, 42, 112, 200, 180, 146, 75, 242, 101, 70, 97, 237, 61,
87+
149, 253, 102, 163, 19, 235, 135, 35, 117, 151, 198, 40, 228, 160, 49, 244,
88+
10, 72, 223, 75, 42, 112, 200, 180, 146, 75, 242, 101, 70, 97, 237, 61, 149,
89+
253, 102, 163, 19, 235, 135, 35, 117, 151, 198, 40, 228, 160, 49,
90+
]),
91+
TapTweak: Buffer.from([
92+
232, 15, 225, 99, 156, 156, 160, 80, 227, 175, 27, 57, 193, 67, 198, 62, 66,
93+
156, 188, 235, 21, 217, 64, 251, 181, 197, 161, 244, 175, 87, 197, 233, 232,
94+
15, 225, 99, 156, 156, 160, 80, 227, 175, 27, 57, 193, 67, 198, 62, 66, 156,
95+
188, 235, 21, 217, 64, 251, 181, 197, 161, 244, 175, 87, 197, 233,
96+
]),
97+
'KeyAgg list': Buffer.from([
98+
72, 28, 151, 28, 60, 11, 70, 215, 240, 178, 117, 174, 89, 141, 78, 44, 126,
99+
215, 49, 156, 89, 74, 92, 110, 199, 158, 160, 212, 153, 2, 148, 240, 72, 28,
100+
151, 28, 60, 11, 70, 215, 240, 178, 117, 174, 89, 141, 78, 44, 126, 215, 49,
101+
156, 89, 74, 92, 110, 199, 158, 160, 212, 153, 2, 148, 240,
102+
]),
103+
'KeyAgg coefficient': Buffer.from([
104+
191, 201, 4, 3, 77, 28, 136, 232, 200, 14, 34, 229, 61, 36, 86, 109, 100,
105+
130, 78, 214, 66, 114, 129, 192, 145, 0, 249, 77, 205, 82, 201, 129, 191,
106+
201, 4, 3, 77, 28, 136, 232, 200, 14, 34, 229, 61, 36, 86, 109, 100, 130,
107+
78, 214, 66, 114, 129, 192, 145, 0, 249, 77, 205, 82, 201, 129,
108+
]),
109+
};
58110
function taggedHash(prefix, data) {
59-
return sha256(Buffer.concat([TAGGED_HASH_PREFIXES[prefix], data]));
111+
return sha256(Buffer.concat([exports.TAGGED_HASH_PREFIXES[prefix], data]));
60112
}
61113
exports.taggedHash = taggedHash;

test/crypto.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as assert from 'assert';
22
import { describe, it } from 'mocha';
33
import { crypto as bcrypto, TaggedHashPrefix } from '..';
44
import * as fixtures from './fixtures/crypto.json';
5+
import { sha256, TAGS, TAGGED_HASH_PREFIXES } from '../src/crypto';
56

67
describe('crypto', () => {
78
['hash160', 'hash256', 'ripemd160', 'sha1', 'sha256'].forEach(algorithm => {
@@ -30,4 +31,19 @@ describe('crypto', () => {
3031
});
3132
});
3233
});
34+
35+
describe('TAGGED_HASH_PREFIXES', () => {
36+
const taggedHashPrefixes = Object.fromEntries(
37+
TAGS.map((tag: TaggedHashPrefix) => {
38+
const tagHash = sha256(Buffer.from(tag));
39+
return [tag, Buffer.concat([tagHash, tagHash])];
40+
}),
41+
);
42+
it('stored the result of operation', () => {
43+
assert.strictEqual(
44+
JSON.stringify(TAGGED_HASH_PREFIXES),
45+
JSON.stringify(taggedHashPrefixes),
46+
);
47+
});
48+
});
3349
});

ts_src/crypto.ts

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function hash256(buffer: Buffer): Buffer {
2929
return sha256(sha256(buffer));
3030
}
3131

32-
const TAGS = [
32+
export const TAGS = [
3333
'BIP0340/challenge',
3434
'BIP0340/aux',
3535
'BIP0340/nonce',
@@ -41,13 +41,66 @@ const TAGS = [
4141
'KeyAgg coefficient',
4242
] as const;
4343
export type TaggedHashPrefix = typeof TAGS[number];
44+
type TaggedHashPrefixes = {
45+
[key in TaggedHashPrefix]: Buffer;
46+
};
4447
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
45-
const TAGGED_HASH_PREFIXES = Object.fromEntries(
46-
TAGS.map(tag => {
47-
const tagHash = sha256(Buffer.from(tag));
48-
return [tag, Buffer.concat([tagHash, tagHash])];
49-
}),
50-
) as { [k in TaggedHashPrefix]: Buffer };
48+
export const TAGGED_HASH_PREFIXES: TaggedHashPrefixes = {
49+
'BIP0340/challenge': Buffer.from([
50+
123, 181, 45, 122, 159, 239, 88, 50, 62, 177, 191, 122, 64, 125, 179, 130,
51+
210, 243, 242, 216, 27, 177, 34, 79, 73, 254, 81, 143, 109, 72, 211, 124,
52+
123, 181, 45, 122, 159, 239, 88, 50, 62, 177, 191, 122, 64, 125, 179, 130,
53+
210, 243, 242, 216, 27, 177, 34, 79, 73, 254, 81, 143, 109, 72, 211, 124,
54+
]),
55+
'BIP0340/aux': Buffer.from([
56+
241, 239, 78, 94, 192, 99, 202, 218, 109, 148, 202, 250, 157, 152, 126, 160,
57+
105, 38, 88, 57, 236, 193, 31, 151, 45, 119, 165, 46, 216, 193, 204, 144,
58+
241, 239, 78, 94, 192, 99, 202, 218, 109, 148, 202, 250, 157, 152, 126, 160,
59+
105, 38, 88, 57, 236, 193, 31, 151, 45, 119, 165, 46, 216, 193, 204, 144,
60+
]),
61+
'BIP0340/nonce': Buffer.from([
62+
7, 73, 119, 52, 167, 155, 203, 53, 91, 155, 140, 125, 3, 79, 18, 28, 244,
63+
52, 215, 62, 247, 45, 218, 25, 135, 0, 97, 251, 82, 191, 235, 47, 7, 73,
64+
119, 52, 167, 155, 203, 53, 91, 155, 140, 125, 3, 79, 18, 28, 244, 52, 215,
65+
62, 247, 45, 218, 25, 135, 0, 97, 251, 82, 191, 235, 47,
66+
]),
67+
TapLeaf: Buffer.from([
68+
174, 234, 143, 220, 66, 8, 152, 49, 5, 115, 75, 88, 8, 29, 30, 38, 56, 211,
69+
95, 28, 181, 64, 8, 212, 211, 87, 202, 3, 190, 120, 233, 238, 174, 234, 143,
70+
220, 66, 8, 152, 49, 5, 115, 75, 88, 8, 29, 30, 38, 56, 211, 95, 28, 181,
71+
64, 8, 212, 211, 87, 202, 3, 190, 120, 233, 238,
72+
]),
73+
TapBranch: Buffer.from([
74+
25, 65, 161, 242, 229, 110, 185, 95, 162, 169, 241, 148, 190, 92, 1, 247,
75+
33, 111, 51, 237, 130, 176, 145, 70, 52, 144, 208, 91, 245, 22, 160, 21, 25,
76+
65, 161, 242, 229, 110, 185, 95, 162, 169, 241, 148, 190, 92, 1, 247, 33,
77+
111, 51, 237, 130, 176, 145, 70, 52, 144, 208, 91, 245, 22, 160, 21,
78+
]),
79+
TapSighash: Buffer.from([
80+
244, 10, 72, 223, 75, 42, 112, 200, 180, 146, 75, 242, 101, 70, 97, 237, 61,
81+
149, 253, 102, 163, 19, 235, 135, 35, 117, 151, 198, 40, 228, 160, 49, 244,
82+
10, 72, 223, 75, 42, 112, 200, 180, 146, 75, 242, 101, 70, 97, 237, 61, 149,
83+
253, 102, 163, 19, 235, 135, 35, 117, 151, 198, 40, 228, 160, 49,
84+
]),
85+
TapTweak: Buffer.from([
86+
232, 15, 225, 99, 156, 156, 160, 80, 227, 175, 27, 57, 193, 67, 198, 62, 66,
87+
156, 188, 235, 21, 217, 64, 251, 181, 197, 161, 244, 175, 87, 197, 233, 232,
88+
15, 225, 99, 156, 156, 160, 80, 227, 175, 27, 57, 193, 67, 198, 62, 66, 156,
89+
188, 235, 21, 217, 64, 251, 181, 197, 161, 244, 175, 87, 197, 233,
90+
]),
91+
'KeyAgg list': Buffer.from([
92+
72, 28, 151, 28, 60, 11, 70, 215, 240, 178, 117, 174, 89, 141, 78, 44, 126,
93+
215, 49, 156, 89, 74, 92, 110, 199, 158, 160, 212, 153, 2, 148, 240, 72, 28,
94+
151, 28, 60, 11, 70, 215, 240, 178, 117, 174, 89, 141, 78, 44, 126, 215, 49,
95+
156, 89, 74, 92, 110, 199, 158, 160, 212, 153, 2, 148, 240,
96+
]),
97+
'KeyAgg coefficient': Buffer.from([
98+
191, 201, 4, 3, 77, 28, 136, 232, 200, 14, 34, 229, 61, 36, 86, 109, 100,
99+
130, 78, 214, 66, 114, 129, 192, 145, 0, 249, 77, 205, 82, 201, 129, 191,
100+
201, 4, 3, 77, 28, 136, 232, 200, 14, 34, 229, 61, 36, 86, 109, 100, 130,
101+
78, 214, 66, 114, 129, 192, 145, 0, 249, 77, 205, 82, 201, 129,
102+
]),
103+
};
51104

52105
export function taggedHash(prefix: TaggedHashPrefix, data: Buffer): Buffer {
53106
return sha256(Buffer.concat([TAGGED_HASH_PREFIXES[prefix], data]));

0 commit comments

Comments
 (0)