Skip to content

Commit 3821e5e

Browse files
instagibbsjgriffiths
authored andcommitted
bip340: add bip340 tagged hash functionality
1 parent ff76cd1 commit 3821e5e

File tree

9 files changed

+51
-0
lines changed

9 files changed

+51
-0
lines changed

include/wally.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,12 @@ inline int bip32_key_to_address(const HDKEY& hdkey, uint32_t flags, uint32_t ver
416416
return ret;
417417
}
418418

419+
template <class BYTES, class TAG, class BYTES_OUT>
420+
inline int bip340_tagged_hash(const BYTES& bytes, const TAG& tag, BYTES_OUT& bytes_out) {
421+
int ret = ::wally_bip340_tagged_hash(bytes.data(), bytes.size(), detail::get_p(tag), bytes_out.data(), bytes_out.size());
422+
return ret;
423+
}
424+
419425
template <class BYTES>
420426
inline int bzero(const BYTES& bytes, size_t bytes_len) {
421427
int ret = ::wally_bzero(detail::get_p(bytes), bytes_len);

include/wally_crypto.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,22 @@ WALLY_CORE_API int wally_sha512(
153153
unsigned char *bytes_out,
154154
size_t len);
155155

156+
/**
157+
* BIP340 tagged hash: SHA-256(SHA-256(tag) || SHA-256(tag) || m).
158+
*
159+
* :param bytes: The message to hash.
160+
* :param bytes_len: The length of ``bytes`` in bytes.
161+
* :param tag: The BIP340 UTF-8 domain tag.
162+
* :param bytes_out: Destination for the resulting hash.
163+
* FIXED_SIZED_OUTPUT(len, bytes_out, SHA256_LEN)
164+
*/
165+
WALLY_CORE_API int wally_bip340_tagged_hash(
166+
const unsigned char *bytes,
167+
size_t bytes_len,
168+
const char *tag,
169+
unsigned char *bytes_out,
170+
size_t len);
171+
156172
/** Output length for `wally_ripemd160` */
157173
#define RIPEMD160_LEN 20
158174

src/internal.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,29 @@ int wally_bzero(void *bytes, size_t len)
103103
return WALLY_OK;
104104
}
105105

106+
int wally_bip340_tagged_hash(const unsigned char *bytes, size_t bytes_len,
107+
const char *tag, unsigned char *bytes_out, size_t len)
108+
{
109+
struct sha256 sha;
110+
struct sha256_ctx ctx;
111+
112+
if (!bytes || !bytes_len || !tag || !bytes_out || len != SHA256_LEN)
113+
return WALLY_EINVAL;
114+
115+
/* SHA256(SHA256(tag) || SHA256(tag) || msg) */
116+
/* TODO: Add optimised impls for Taproot fixed tags */
117+
sha256(&sha, tag, strlen(tag));
118+
sha256_init(&ctx);
119+
sha256_update(&ctx, &sha, sizeof(sha));
120+
sha256_update(&ctx, &sha, sizeof(sha));
121+
sha256_update(&ctx, bytes, bytes_len);
122+
sha256_done(&ctx, &sha);
123+
124+
memcpy(bytes_out, &sha, sizeof(sha));
125+
wally_clear_2(&sha, sizeof(sha), &ctx, sizeof(ctx));
126+
return WALLY_OK;
127+
}
128+
106129
int wally_sha256(const unsigned char *bytes, size_t bytes_len,
107130
unsigned char *bytes_out, size_t len)
108131
{

src/swig_java/swig.i

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ static jbyteArray create_jintArray(JNIEnv *jenv, const uint32_t* p, size_t len)
488488
%returns_size_t(wally_base64_get_maximum_length);
489489
%returns_string(wally_bip32_key_to_address);
490490
%returns_string(wally_bip32_key_to_addr_segwit);
491+
%returns_array_(wally_bip340_tagged_hash, 4, 5, SHA256_LEN);
491492
%returns_string(wally_confidential_addr_to_addr);
492493
%returns_array_(wally_confidential_addr_to_ec_public_key, 3, 4, EC_PUBLIC_KEY_LEN);
493494
%returns_string(wally_confidential_addr_from_addr);

src/swig_python/python_extra.py_in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ sha256 = _wrap_bin(sha256, SHA256_LEN)
124124
sha256_midstate = _wrap_bin(sha256_midstate, SHA256_LEN)
125125
sha256d = _wrap_bin(sha256d, SHA256_LEN)
126126
sha512 = _wrap_bin(sha512, SHA512_LEN)
127+
bip340_tagged_hash = _wrap_bin(bip340_tagged_hash, SHA256_LEN)
127128
ripemd160 = _wrap_bin(ripemd160, RIPEMD160_LEN)
128129
hash160 = _wrap_bin(hash160, HASH160_LEN)
129130
hmac_sha256 = _wrap_bin(hmac_sha256, HMAC_SHA256_LEN)

src/test/util.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ class wally_psbt(Structure):
286286
('wally_base64_to_bytes', c_int, [c_char_p, c_uint32, c_void_p, c_size_t, c_size_t_p]),
287287
('wally_bip32_key_to_addr_segwit', c_int, [POINTER(ext_key), c_char_p, c_uint32, c_char_p_p]),
288288
('wally_bip32_key_to_address', c_int, [POINTER(ext_key), c_uint32, c_uint32, c_char_p_p]),
289+
('wally_bip340_tagged_hash', c_int, [c_void_p, c_size_t, c_char_p, c_void_p, c_size_t]),
289290
('wally_bzero', c_int, [c_void_p, c_size_t]),
290291
('wally_cleanup', c_int, [c_uint32]),
291292
('wally_confidential_addr_from_addr', c_int, [c_char_p, c_uint32, c_void_p, c_size_t, c_char_p_p]),

src/wasm_package/functions.js

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

src/wasm_package/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export function bip32_key_unserialize(bytes: Buffer|Uint8Array): Ref_ext_key;
8989
export function bip32_key_unserialize_noalloc(bytes: Buffer|Uint8Array, output: Ref_ext_key): void;
9090
export function bip32_key_with_tweak_from_parent_path(hdkey: Ref_ext_key, child_path: Uint32Array|number[], flags: number): Ref_ext_key;
9191
export function bip32_key_with_tweak_from_parent_path_noalloc(hdkey: Ref_ext_key, child_path: Uint32Array|number[], flags: number, output: Ref_ext_key): void;
92+
export function bip340_tagged_hash(bytes: Buffer|Uint8Array, tag: string): Buffer;
9293
export function bip38_from_private_key(bytes: Buffer|Uint8Array, pass: Buffer|Uint8Array, flags: number): string;
9394
export function bip38_get_flags(bip38: string): number;
9495
export function bip38_raw_from_private_key(bytes: Buffer|Uint8Array, pass: Buffer|Uint8Array, flags: number): Buffer;

tools/wasm_exports.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ EXPORTED_FUNCTIONS="['_malloc','_free','_bip32_key_free' \
7272
,'_wally_base64_to_bytes' \
7373
,'_wally_bip32_key_to_addr_segwit' \
7474
,'_wally_bip32_key_to_address' \
75+
,'_wally_bip340_tagged_hash' \
7576
,'_wally_bzero' \
7677
,'_wally_cleanup' \
7778
,'_wally_ec_private_key_verify' \

0 commit comments

Comments
 (0)