Skip to content

Commit 242dc55

Browse files
committed
PAK list: add asset_pak_whitelistproof_len for wrapper code
This also fixes the error checks for length computation rather than returning an incorrect length.
1 parent 76b3c71 commit 242dc55

File tree

9 files changed

+85
-37
lines changed

9 files changed

+85
-37
lines changed

include/wally.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,13 @@ inline int asset_pak_whitelistproof(const ONLINE_KEYS& online_keys, const OFFLIN
18141814
return written || ret != WALLY_OK ? ret : n == static_cast<size_t>(bytes_out.size()) ? WALLY_OK : WALLY_EINVAL;
18151815
}
18161816

1817+
template <class ONLINE_KEYS, class OFFLINE_KEYS, class SUB_PUBKEY, class ONLINE_PRIV_KEY, class SUMMED_KEY>
1818+
inline int asset_pak_whitelistproof_len(const ONLINE_KEYS& online_keys, const OFFLINE_KEYS& offline_keys, size_t key_index, const SUB_PUBKEY& sub_pubkey, const ONLINE_PRIV_KEY& online_priv_key, const SUMMED_KEY& summed_key, size_t* written = 0) {
1819+
size_t n;
1820+
int ret = ::wally_asset_pak_whitelistproof_len(online_keys.data(), online_keys.size(), offline_keys.data(), offline_keys.size(), key_index, sub_pubkey.data(), sub_pubkey.size(), online_priv_key.data(), online_priv_key.size(), summed_key.data(), summed_key.size(), written ? written : &n);
1821+
return written || ret != WALLY_OK ? ret : n == static_cast<size_t>(summed_key.size()) ? WALLY_OK : WALLY_EINVAL;
1822+
}
1823+
18171824
inline int asset_pak_whitelistproof_size(size_t num_keys, size_t* written) {
18181825
int ret = ::wally_asset_pak_whitelistproof_size(num_keys, written);
18191826
return ret;

include/wally_elements.h

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -466,32 +466,34 @@ WALLY_CORE_API int wally_asset_blinding_key_to_ec_private_key(
466466
size_t len);
467467

468468
/**
469-
* Calculate the size in bytes of the whitelist proof.
469+
* Calculate the size in bytes of a whitelist proof.
470470
*
471471
* :param num_keys: The number of offline/online keys.
472472
* :param written: Destination for the number of bytes needed for the proof.
473+
*
474+
* .. note:: This function is a simpler variant of `wally_asset_pak_whitelistproof_len`.
473475
*/
474476
WALLY_CORE_API int wally_asset_pak_whitelistproof_size(
475477
size_t num_keys,
476478
size_t *written);
477479

478480
/**
479-
* Generate the whitelist proof for the pegout script.
481+
* Generate a whitelist proof for a pegout script.
480482
*
481-
* :param online_keys: The list of online keys.
482-
* :param online_keys_len: Length of ``online_keys_len`` in bytes. Must be a multiple of ``EC_PUBLIC_KEY_LEN``.
483-
* :param offline_keys: The list of offline keys.
484-
* :param offline_keys_len: Length of ``offline_keys_len`` in bytes. Must be a multiple of ``EC_PUBLIC_KEY_LEN``.
485-
* :param key_index: The index in the PAK list of the key signing this whitelist proof
486-
* :param sub_pubkey: The key to be whitelisted.
483+
* :param online_keys: The list of concatenated online keys.
484+
* :param online_keys_len: Length of ``online_keys`` in bytes. Must be a multiple of ``EC_PUBLIC_KEY_LEN``.
485+
* :param offline_keys: The list of concatenated offline keys.
486+
* :param offline_keys_len: Length of ``offline_keys`` in bytes. Must match ``online_keys_len``.
487+
* :param key_index: The index in the PAK list of the key signing this whitelist proof.
488+
* :param sub_pubkey: The public key to be whitelisted.
487489
* :param sub_pubkey_len: Length of ``sub_pubkey`` in bytes. Must be ``EC_PUBLIC_KEY_LEN``.
488490
* :param online_priv_key: The secret key to the signer's online pubkey.
489491
* :param online_priv_key_len: Length of ``online_priv_key`` in bytes. Must be ``EC_PRIVATE_KEY_LEN``.
490492
* :param summed_key: The secret key to the sum of (whitelisted key, signer's offline pubkey).
491493
* :param summed_key_len: Length of ``summed_key`` in bytes. Must be ``EC_PRIVATE_KEY_LEN``.
492494
* :param bytes_out: Destination for the resulting whitelist proof.
493495
* :param len: Length of ``bytes_out`` in bytes.
494-
* :param written: Number of bytes actually written to ``bytes_out``.
496+
* :param written: Destination for the number of bytes written to ``bytes_out``.
495497
*/
496498
WALLY_CORE_API int wally_asset_pak_whitelistproof(
497499
const unsigned char *online_keys,
@@ -509,6 +511,38 @@ WALLY_CORE_API int wally_asset_pak_whitelistproof(
509511
size_t len,
510512
size_t *written);
511513

514+
/**
515+
* Calculate the size in bytes of a whitelist proof.
516+
*
517+
* :param online_keys: The list of concatenated online keys.
518+
* :param online_keys_len: Length of ``online_keys`` in bytes. Must be a multiple of ``EC_PUBLIC_KEY_LEN``.
519+
* :param offline_keys: The list of concatenated offline keys.
520+
* :param offline_keys_len: Length of ``offline_keys`` in bytes. Must match ``online_keys_len``.
521+
* :param key_index: The index in the PAK list of the key signing this whitelist proof.
522+
* :param sub_pubkey: The public key to be whitelisted.
523+
* :param sub_pubkey_len: Length of ``sub_pubkey`` in bytes. Must be ``EC_PUBLIC_KEY_LEN``.
524+
* :param online_priv_key: The secret key to the signer's online pubkey.
525+
* :param online_priv_key_len: Length of ``online_priv_key`` in bytes. Must be ``EC_PRIVATE_KEY_LEN``.
526+
* :param summed_key: The secret key to the sum of (whitelisted key, signer's offline pubkey).
527+
* :param summed_key_len: Length of ``summed_key`` in bytes. Must be ``EC_PRIVATE_KEY_LEN``.
528+
* :param written: Destination for resulting proof size in bytes.
529+
*
530+
* .. note:: Use `wally_asset_pak_whitelistproof_size` for a simpler call interface.
531+
*/
532+
WALLY_CORE_API int wally_asset_pak_whitelistproof_len(
533+
const unsigned char *online_keys,
534+
size_t online_keys_len,
535+
const unsigned char *offline_keys,
536+
size_t offline_keys_len,
537+
size_t key_index,
538+
const unsigned char *sub_pubkey,
539+
size_t sub_pubkey_len,
540+
const unsigned char *online_priv_key,
541+
size_t online_priv_key_len,
542+
const unsigned char *summed_key,
543+
size_t summed_key_len,
544+
size_t *written);
545+
512546
#endif /* BUILD_ELEMENTS */
513547

514548
#ifdef __cplusplus

src/elements.c

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -715,33 +715,24 @@ int wally_asset_blinding_key_to_ec_private_key(
715715
return ret;
716716
}
717717

718-
int wally_asset_pak_whitelistproof_size(
719-
size_t num_keys,
720-
size_t *written)
718+
int wally_asset_pak_whitelistproof_size(size_t num_keys, size_t *written)
721719
{
722-
if (!written)
720+
if (written)
721+
*written = 0;
722+
if (!written || num_keys > SECP256K1_WHITELIST_MAX_N_KEYS)
723723
return WALLY_EINVAL;
724-
725724
*written = 1 + 32 * (1 + num_keys);
726-
727725
return WALLY_OK;
728726
}
729727

730728
int wally_asset_pak_whitelistproof(
731-
const unsigned char *online_keys,
732-
size_t online_keys_len,
733-
const unsigned char *offline_keys,
734-
size_t offline_keys_len,
729+
const unsigned char *online_keys, size_t online_keys_len,
730+
const unsigned char *offline_keys, size_t offline_keys_len,
735731
size_t key_index,
736-
const unsigned char *sub_pubkey,
737-
size_t sub_pubkey_len,
738-
const unsigned char *online_priv_key,
739-
size_t online_priv_key_len,
740-
const unsigned char *summed_key,
741-
size_t summed_key_len,
742-
unsigned char *bytes_out,
743-
size_t len,
744-
size_t *written)
732+
const unsigned char *sub_pubkey, size_t sub_pubkey_len,
733+
const unsigned char *online_priv_key, size_t online_priv_key_len,
734+
const unsigned char *summed_key, size_t summed_key_len,
735+
unsigned char *bytes_out, size_t len, size_t *written)
745736
{
746737
const secp256k1_context *ctx = secp_ctx();
747738
secp256k1_pubkey online_secp_keys[SECP256K1_WHITELIST_MAX_N_KEYS];
@@ -799,4 +790,22 @@ int wally_asset_pak_whitelistproof(
799790
return ret;
800791
}
801792

793+
int wally_asset_pak_whitelistproof_len(
794+
const unsigned char *online_keys, size_t online_keys_len,
795+
const unsigned char *offline_keys, size_t offline_keys_len,
796+
size_t key_index,
797+
const unsigned char *sub_pubkey, size_t sub_pubkey_len,
798+
const unsigned char *online_priv_key, size_t online_priv_key_len,
799+
const unsigned char *summed_key, size_t summed_key_len,
800+
size_t *written)
801+
{
802+
unsigned char buff[1]; /* Undersized: we only want the length */
803+
return wally_asset_pak_whitelistproof(online_keys, online_keys_len,
804+
offline_keys, offline_keys_len,
805+
key_index,
806+
sub_pubkey, sub_pubkey_len,
807+
online_priv_key, online_priv_key_len,
808+
summed_key, summed_key_len,
809+
buff, sizeof(buff), written);
810+
}
802811
#endif /* BUILD_ELEMENTS */

src/swig_java/swig.i

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,7 @@ static jbyteArray create_jintArray(JNIEnv *jenv, const uint32_t* p, size_t len)
10381038
%returns_size_t(wally_witness_program_from_bytes_and_version);
10391039
%returns_array_(wally_symmetric_key_from_seed, 3, 4, HMAC_SHA512_LEN);
10401040
%returns_array_(wally_symmetric_key_from_parent, 6, 7, HMAC_SHA512_LEN);
1041+
%returns_size_t(wally_asset_pak_whitelistproof_len);
10411042
%returns_size_t(wally_asset_pak_whitelistproof_size);
10421043
%returns_size_t(wally_asset_pak_whitelistproof);
10431044
%returns_array_(wally_s2c_sig_from_bytes, 10, 11, EC_SIGNATURE_LEN);

src/swig_python/python_extra.py_in

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,7 @@ if is_elements_build():
271271
asset_surjectionproof = _wrap_bin(asset_surjectionproof, _asset_surjectionproof_len_fn, resize=True)
272272
asset_blinding_key_from_seed = _wrap_bin(asset_blinding_key_from_seed, HMAC_SHA512_LEN)
273273
asset_blinding_key_to_ec_private_key = _wrap_bin(asset_blinding_key_to_ec_private_key, EC_PRIVATE_KEY_LEN)
274-
def _asset_pak_whitelistproof_len_fn(on_keys, off_keys, idx, sub_pubkey, priv_key, summed_key):
275-
return asset_pak_whitelistproof_size(len(off_keys) // EC_PUBLIC_KEY_LEN)
276-
asset_pak_whitelistproof = _wrap_bin(asset_pak_whitelistproof, _asset_pak_whitelistproof_len_fn)
274+
asset_pak_whitelistproof = _wrap_bin(asset_pak_whitelistproof, asset_pak_whitelistproof_len, resize=True)
277275

278276
def _asset_unblind_fn(fn):
279277
def wrapped(*args):

src/test/util.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ class wally_psbt(Structure):
265265
('wally_asset_final_vbf', c_int, [POINTER(c_uint64), c_size_t, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t]),
266266
('wally_asset_generator_from_bytes', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t]),
267267
('wally_asset_pak_whitelistproof', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_size_t_p]),
268+
('wally_asset_pak_whitelistproof_len', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_size_t_p]),
268269
('wally_asset_pak_whitelistproof_size', c_int, [c_size_t, c_size_t_p]),
269270
('wally_asset_rangeproof', c_int, [c_uint64, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_uint64, c_int, c_int, c_void_p, c_size_t, c_size_t_p]),
270271
('wally_asset_rangeproof_with_nonce', c_int, [c_uint64, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_uint64, c_int, c_int, c_void_p, c_size_t, c_size_t_p]),

src/wasm_package/functions.js

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

tools/build_wrappers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@ def gen_wasm_package(funcs):
305305
'wally_scriptpubkey_csv_2of3_then_2_from_bytes': 'scriptpubkey_csv_2of3_then_2_from_bytes_len, true',
306306

307307
'wally_asset_surjectionproof': 'asset_surjectionproof_len, true',
308-
'wally_asset_pak_whitelistproof': 'asset_pak_whitelistproof_len, false',
309308
'wally_elements_pegout_script_from_bytes': 'elements_pegout_script_from_bytes_len, true',
310309
'wally_elements_pegin_contract_script_from_bytes': 'elements_pegin_contract_script_from_bytes_len, true',
311310
}
@@ -485,4 +484,4 @@ def get_function_defs(non_elements, internal_only):
485484
gen_wally_hpp(external_funcs)
486485

487486
gen_wasm_exports(all_funcs)
488-
gen_wasm_package(all_funcs)
487+
gen_wasm_package(all_funcs)

tools/wasm_exports.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ if [ -n "$ENABLE_ELEMENTS" ]; then
423423
,'_wally_asset_final_vbf' \
424424
,'_wally_asset_generator_from_bytes' \
425425
,'_wally_asset_pak_whitelistproof' \
426+
,'_wally_asset_pak_whitelistproof_len' \
426427
,'_wally_asset_pak_whitelistproof_size' \
427428
,'_wally_asset_rangeproof' \
428429
,'_wally_asset_rangeproof_with_nonce' \

0 commit comments

Comments
 (0)