From 2234fa124268fb035e156f98c6ab17232808f345 Mon Sep 17 00:00:00 2001 From: Daira Hopwood Date: Tue, 1 Feb 2022 15:54:50 +0000 Subject: [PATCH] Refactor the Orchard FullViewingKey constructor by adding FullViewingKey.from_spending_key. Signed-off-by: Daira Hopwood --- unified_address.py | 2 +- unified_full_viewing_keys.py | 2 +- zcash_test_vectors/orchard/key_components.py | 16 ++++++++-------- zcash_test_vectors/orchard/note.py | 2 +- zcash_test_vectors/orchard/note_encryption.py | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/unified_address.py b/unified_address.py index 059983f..e415004 100755 --- a/unified_address.py +++ b/unified_address.py @@ -42,7 +42,7 @@ def main(): has_o_addr = (not has_s_addr) or rand.bool() if has_o_addr: orchard_sk = orchard_key_components.SpendingKey(rand.b(32)) - orchard_fvk = orchard_key_components.FullViewingKey(orchard_sk) + orchard_fvk = orchard_key_components.FullViewingKey.from_spending_key(orchard_sk) orchard_default_d = orchard_fvk.default_d() orchard_default_pk_d = orchard_fvk.default_pkd() orchard_raw_addr = b"".join([orchard_default_d[:11], bytes(orchard_default_pk_d)[:32]]) diff --git a/unified_full_viewing_keys.py b/unified_full_viewing_keys.py index 0c29384..3ab75d4 100755 --- a/unified_full_viewing_keys.py +++ b/unified_full_viewing_keys.py @@ -58,7 +58,7 @@ def main(): has_o_key = (not has_s_key) or rand.bool() if has_o_key: orchard_sk = orchard_key_components.SpendingKey(rand.b(32)) - orchard_fvk = orchard_key_components.FullViewingKey(orchard_sk) + orchard_fvk = orchard_key_components.FullViewingKey.from_spending_key(orchard_sk) orchard_fvk_bytes = b"".join([ bytes(orchard_fvk.ak), bytes(orchard_fvk.nk), diff --git a/zcash_test_vectors/orchard/key_components.py b/zcash_test_vectors/orchard/key_components.py index 0eae5d2..8f2be14 100755 --- a/zcash_test_vectors/orchard/key_components.py +++ b/zcash_test_vectors/orchard/key_components.py @@ -53,17 +53,17 @@ class SpendingKey: class FullViewingKey(object): - def __init__(self, sk): - if isinstance(sk, SpendingKey): - (self.rivk, self.ak, self.nk) = (sk.rivk, sk.ak, sk.nk) - else: - (self.rivk, self.ak, self.nk) = sk - + def __init__(self, rivk, ak, nk): + (self.rivk, self.ak, self.nk) = (rivk, ak, nk) K = i2leosp(256, self.rivk.s) R = prf_expand(K, b'\x82' + i2leosp(256, self.ak.s) + i2leosp(256, self.nk.s)) self.dk = R[:32] self.ovk = R[32:] + @classmethod + def from_spending_key(cls, sk): + return cls(sk.rivk, sk.ak, sk.nk) + def ivk(self): return commit_ivk(self.rivk, self.ak, self.nk) @@ -80,7 +80,7 @@ class FullViewingKey(object): def internal(self): K = i2leosp(256, self.rivk.s) rivk_internal = to_scalar(prf_expand(K, b'\x83' + i2leosp(256, self.ak.s) + i2leosp(256, self.nk.s))) - return self.__class__((rivk_internal, self.ak, self.nk)) + return self.__class__(rivk_internal, self.ak, self.nk) def main(): @@ -101,7 +101,7 @@ def main(): test_vectors = [] for _ in range(0, 10): sk = SpendingKey(rand.b(32)) - fvk = FullViewingKey(sk) + fvk = FullViewingKey.from_spending_key(sk) default_d = fvk.default_d() default_pk_d = fvk.default_pkd() diff --git a/zcash_test_vectors/orchard/note.py b/zcash_test_vectors/orchard/note.py index 2a9cebc..7f8f066 100644 --- a/zcash_test_vectors/orchard/note.py +++ b/zcash_test_vectors/orchard/note.py @@ -63,7 +63,7 @@ class OrchardNotePlaintext(object): def dummy_nullifier(self, rand): sk = SpendingKey(rand.b(32)) - fvk = FullViewingKey(sk) + fvk = FullViewingKey.from_spending_key(sk) pk_d = fvk.default_pkd() d = fvk.default_d() diff --git a/zcash_test_vectors/orchard/note_encryption.py b/zcash_test_vectors/orchard/note_encryption.py index 701b469..9414bc0 100755 --- a/zcash_test_vectors/orchard/note_encryption.py +++ b/zcash_test_vectors/orchard/note_encryption.py @@ -214,7 +214,7 @@ def main(): sender_ovk = rand.b(32) receiver_sk = SpendingKey(rand.b(32)) - receiver_fvk = FullViewingKey(receiver_sk) + receiver_fvk = FullViewingKey.from_spending_key(receiver_sk) ivk = receiver_fvk.ivk() d = receiver_fvk.default_d() pk_d = receiver_fvk.default_pkd()