diff --git a/zcash_test_vectors/hd_common.py b/zcash_test_vectors/hd_common.py new file mode 100644 index 0000000..ddd4add --- /dev/null +++ b/zcash_test_vectors/hd_common.py @@ -0,0 +1,7 @@ +# Common definitions for hierarchical derivation. + +ZCASH_MAIN_COINTYPE = 133 + +def hardened(i): + assert 0 <= i and i < (1<<31) + return i + (1<<31) diff --git a/zcash_test_vectors/sapling/zip32.py b/zcash_test_vectors/sapling/zip32.py index bf22772..363fa0e 100755 --- a/zcash_test_vectors/sapling/zip32.py +++ b/zcash_test_vectors/sapling/zip32.py @@ -5,6 +5,8 @@ from hashlib import blake2b from .key_components import to_scalar, prf_expand, diversify_hash, DerivedAkNk, DerivedIvk from .generators import SPENDING_KEY_BASE, PROVING_KEY_BASE + +from ..hd_common import hardened from ..utils import i2leosp, i2lebsp, lebs2osp from ..ff1 import ff1_aes256_encrypt from ..output import render_args, render_tv, option, Some @@ -219,11 +221,6 @@ class ExtendedFullViewingKey(DerivedIvk, ExtendedBase): return self.__class__(self.ak(), nk_internal, ovk_internal, dk_internal, self._c, self.depth(), self.parent_tag(), self._i) -def hardened(i): - assert(i < (1<<31)) - return i + (1<<31) - - def main(): args = render_args() diff --git a/zcash_test_vectors/unified_full_viewing_keys.py b/zcash_test_vectors/unified_full_viewing_keys.py index d38cd20..e54b14e 100755 --- a/zcash_test_vectors/unified_full_viewing_keys.py +++ b/zcash_test_vectors/unified_full_viewing_keys.py @@ -10,9 +10,11 @@ from .output import render_args, render_tv, Some from .rand import Rand, randbytes from .orchard import key_components as orchard_key_components from .sapling import zip32 as sapling_zip32 +from .hd_common import ZCASH_MAIN_COINTYPE, hardened from .unified_encoding import encode_unified, decode_unified from .unified_encoding import P2PKH_ITEM, SAPLING_ITEM, ORCHARD_ITEM + def main(): args = render_args() @@ -37,9 +39,9 @@ def main(): has_s_key = rand.bool() if has_s_key: root_key = sapling_zip32.ExtendedSpendingKey.master(seed) - purpose_key = root_key.child(sapling_zip32.hardened(32)) - coin_key = purpose_key.child(sapling_zip32.hardened(133)) - account_key = coin_key.child(sapling_zip32.hardened(i)) + purpose_key = root_key.child(hardened(32)) + coin_key = purpose_key.child(hardened(ZCASH_MAIN_COINTYPE)) + account_key = coin_key.child(hardened(i)) sapling_fvk = account_key.to_extended_fvk() sapling_fvk_bytes = b"".join([ diff --git a/zcash_test_vectors/unified_incoming_viewing_keys.py b/zcash_test_vectors/unified_incoming_viewing_keys.py index d4747bd..d19312b 100755 --- a/zcash_test_vectors/unified_incoming_viewing_keys.py +++ b/zcash_test_vectors/unified_incoming_viewing_keys.py @@ -10,9 +10,11 @@ from .output import render_args, render_tv, Some from .rand import Rand, randbytes from .orchard import key_components as orchard_key_components from .sapling import zip32 as sapling_zip32 +from .hd_common import ZCASH_MAIN_COINTYPE, hardened from .unified_encoding import encode_unified, decode_unified from .unified_encoding import P2PKH_ITEM, SAPLING_ITEM, ORCHARD_ITEM + def main(): args = render_args() @@ -37,9 +39,9 @@ def main(): has_s_key = rand.bool() if has_s_key: root_key = sapling_zip32.ExtendedSpendingKey.master(seed) - purpose_key = root_key.child(sapling_zip32.hardened(32)) - coin_key = purpose_key.child(sapling_zip32.hardened(133)) - account_key = coin_key.child(sapling_zip32.hardened(i)) + purpose_key = root_key.child(hardened(32)) + coin_key = purpose_key.child(hardened(ZCASH_MAIN_COINTYPE)) + account_key = coin_key.child(hardened(i)) sapling_dk = account_key.to_extended_fvk().dk() sapling_ivk = account_key.ivk() sapling_ivk_bytes = bytes(sapling_dk) + bytes(sapling_ivk)