From d6d69f838889360d35c42ae572fd1c82069ec6e6 Mon Sep 17 00:00:00 2001 From: Daira Hopwood Date: Tue, 8 Feb 2022 14:56:48 +0000 Subject: [PATCH] Merge files that were only separate in order to provide multiple entry points. Signed-off-by: Daira Hopwood --- pyproject.toml | 6 +-- zcash_test_vectors/f4jumble.py | 29 +++++++++++ zcash_test_vectors/f4jumble_long.py | 41 --------------- zcash_test_vectors/orchard/group_hash.py | 49 ++++++++++++++++- zcash_test_vectors/orchard/map_to_curve.py | 58 --------------------- zcash_test_vectors/orchard/poseidon.py | 40 +++++++++++++- zcash_test_vectors/orchard/poseidon_hash.py | 44 ---------------- 7 files changed, 117 insertions(+), 150 deletions(-) delete mode 100755 zcash_test_vectors/f4jumble_long.py delete mode 100755 zcash_test_vectors/orchard/map_to_curve.py delete mode 100755 zcash_test_vectors/orchard/poseidon_hash.py diff --git a/pyproject.toml b/pyproject.toml index ee78abf..4220609 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,7 +31,7 @@ secp256k1 = "0.14.0" [tool.poetry.scripts] # General test vectors f4jumble = "zcash_test_vectors.f4jumble:main" -f4jumble_long = "zcash_test_vectors.f4jumble_long:main" +f4jumble_long = "zcash_test_vectors.f4jumble:long_test_vectors" unified_address = "zcash_test_vectors.unified_address:main" unified_full_viewing_keys = "zcash_test_vectors.unified_full_viewing_keys:main" unified_incoming_viewing_keys = "zcash_test_vectors.unified_incoming_viewing_keys:main" @@ -51,10 +51,10 @@ sapling_zip32 = "zcash_test_vectors.sapling.zip32:main" orchard_empty_roots = "zcash_test_vectors.orchard.empty_roots:main" orchard_generators = "zcash_test_vectors.orchard.generators:main" orchard_group_hash = "zcash_test_vectors.orchard.group_hash:main" +orchard_map_to_curve = "zcash_test_vectors.orchard.group_hash:map_to_curve_test_vectors" orchard_key_components = "zcash_test_vectors.orchard.key_components:main" -orchard_map_to_curve = "zcash_test_vectors.orchard.map_to_curve:main" orchard_merkle_tree = "zcash_test_vectors.orchard.merkle_tree:main" orchard_note_encryption = "zcash_test_vectors.orchard.note_encryption:main" orchard_poseidon = "zcash_test_vectors.orchard.poseidon:main" -orchard_poseidon_hash = "zcash_test_vectors.orchard.poseidon_hash:main" +orchard_poseidon_hash = "zcash_test_vectors.orchard.poseidon:hash_test_vectors" orchard_sinsemilla = "zcash_test_vectors.orchard.sinsemilla:main" diff --git a/zcash_test_vectors/f4jumble.py b/zcash_test_vectors/f4jumble.py index 1d47dff..884d30e 100755 --- a/zcash_test_vectors/f4jumble.py +++ b/zcash_test_vectors/f4jumble.py @@ -123,6 +123,35 @@ def main(): plain_test_vectors, ) +def long_test_vectors(): + args = render_args() + + hashed_test_vectors = [] + + for l_M in [ + 3246395, + MAX_l_M, + ]: + M = bytes([i & 0xFF for i in range(l_M)]) + jumbled = f4jumble(M) + assert len(jumbled) == len(M) + assert f4jumble_inv(jumbled) == M + + hashed_test_vectors.append({ + 'length': l_M, + 'jumbled_hash': blake2b(jumbled).digest() + }) + + render_tv( + args, + 'f4jumble_long', + ( + ('length', 'usize'), + ('jumbled_hash', '[u8; 64]'), + ), + hashed_test_vectors, + ) + if __name__ == "__main__": main() diff --git a/zcash_test_vectors/f4jumble_long.py b/zcash_test_vectors/f4jumble_long.py deleted file mode 100755 index 82208ee..0000000 --- a/zcash_test_vectors/f4jumble_long.py +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env python3 -import sys; assert sys.version_info[0] >= 3, "Python 3 required." - -from hashlib import blake2b - -from .output import render_args, render_tv -from .f4jumble import f4jumble, f4jumble_inv, MAX_l_M - - -def main(): - args = render_args() - - hashed_test_vectors = [] - - for l_M in [ - 3246395, - MAX_l_M, - ]: - M = bytes([i & 0xFF for i in range(l_M)]) - jumbled = f4jumble(M) - assert len(jumbled) == len(M) - assert f4jumble_inv(jumbled) == M - - hashed_test_vectors.append({ - 'length': l_M, - 'jumbled_hash': blake2b(jumbled).digest() - }) - - render_tv( - args, - 'f4jumble_long', - ( - ('length', 'usize'), - ('jumbled_hash', '[u8; 64]'), - ), - hashed_test_vectors, - ) - - -if __name__ == "__main__": - main() diff --git a/zcash_test_vectors/orchard/group_hash.py b/zcash_test_vectors/orchard/group_hash.py index 30db8fd..2fc1e57 100755 --- a/zcash_test_vectors/orchard/group_hash.py +++ b/zcash_test_vectors/orchard/group_hash.py @@ -7,11 +7,13 @@ import math from . import iso_pallas from .pallas import Fp, p, q, PALLAS_B, Point -from .iso_pallas import PALLAS_ISO_B, PALLAS_ISO_A -from ..utils import i2beosp, cldiv, beos2ip, i2leosp, lebs2ip +from .iso_pallas import PALLAS_ISO_B, PALLAS_ISO_A, Point as IsoPoint + +from ..utils import i2beosp, cldiv, beos2ip, i2leosp, lebs2ip, leos2ip from ..output import render_args, render_tv from ..rand import Rand + # https://stackoverflow.com/questions/2612720/how-to-do-bitwise-exclusive-or-of-two-strings-in-python def sxor(s1,s2): return bytes([a ^ b for a,b in zip(s1,s2)]) @@ -182,6 +184,49 @@ def main(): } for (domain, msg) in test_vectors], ) +def map_to_curve_test_vectors(): + fixed_test_vectors = [ + (Fp(0), IsoPoint(Fp(19938918781445865934736160264407396416050199005817793816893455093350997047296), + Fp(1448774895934493446148762800986014913165975534940595774801697325542407056356))), + (Fp(1), IsoPoint(Fp(5290181550357368025040301950220623271393946308300025648720253222947454165280), + Fp(24520995241805476578231005891941079870703368870355132644748659103632565232759))), + (Fp(0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef0123), + IsoPoint(Fp(16711718778908753690082328243251803703269853000652055785581237369882690082595), + Fp(1764705856161931038824461929646873031992914829456409784642560948827969833589))), + ] + + for (u, point) in fixed_test_vectors: + P = map_to_curve_simple_swu(u) + assert P == point + + test_vectors = [u for (u, _) in fixed_test_vectors] + + from random import Random + rng = Random(0xabad533d) + def randbytes(l): + ret = [] + while len(ret) < l: + ret.append(rng.randrange(0, 256)) + return bytes(ret) + rand = Rand(randbytes) + + # Generate random test vectors + for _ in range(10): + test_vectors.append(Fp(leos2ip(rand.b(32)))) + + render_tv( + render_args(), + 'orchard_map_to_curve', + ( + ('u', '[u8; 32]'), + ('point', '[u8; 32]'), + ), + [{ + 'u': bytes(u), + 'point': bytes(map_to_curve_simple_swu(u)), + } for u in test_vectors], + ) + if __name__ == "__main__": main() diff --git a/zcash_test_vectors/orchard/map_to_curve.py b/zcash_test_vectors/orchard/map_to_curve.py deleted file mode 100755 index 1653d81..0000000 --- a/zcash_test_vectors/orchard/map_to_curve.py +++ /dev/null @@ -1,58 +0,0 @@ -#!/usr/bin/env python3 -import sys; assert sys.version_info[0] >= 3, "Python 3 required." - -from .group_hash import map_to_curve_simple_swu -from .iso_pallas import Point as IsoPoint -from .pallas import Fp - -from ..utils import leos2ip -from ..output import render_args, render_tv -from ..rand import Rand - - -def main(): - fixed_test_vectors = [ - (Fp(0), IsoPoint(Fp(19938918781445865934736160264407396416050199005817793816893455093350997047296), - Fp(1448774895934493446148762800986014913165975534940595774801697325542407056356))), - (Fp(1), IsoPoint(Fp(5290181550357368025040301950220623271393946308300025648720253222947454165280), - Fp(24520995241805476578231005891941079870703368870355132644748659103632565232759))), - (Fp(0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef0123), - IsoPoint(Fp(16711718778908753690082328243251803703269853000652055785581237369882690082595), - Fp(1764705856161931038824461929646873031992914829456409784642560948827969833589))), - ] - - for (u, point) in fixed_test_vectors: - P = map_to_curve_simple_swu(u) - assert P == point - - test_vectors = [u for (u, _) in fixed_test_vectors] - - from random import Random - rng = Random(0xabad533d) - def randbytes(l): - ret = [] - while len(ret) < l: - ret.append(rng.randrange(0, 256)) - return bytes(ret) - rand = Rand(randbytes) - - # Generate random test vectors - for _ in range(10): - test_vectors.append(Fp(leos2ip(rand.b(32)))) - - render_tv( - render_args(), - 'orchard_map_to_curve', - ( - ('u', '[u8; 32]'), - ('point', '[u8; 32]'), - ), - [{ - 'u': bytes(u), - 'point': bytes(map_to_curve_simple_swu(u)), - } for u in test_vectors], - ) - - -if __name__ == "__main__": - main() diff --git a/zcash_test_vectors/orchard/poseidon.py b/zcash_test_vectors/orchard/poseidon.py index 403508a..9149e81 100755 --- a/zcash_test_vectors/orchard/poseidon.py +++ b/zcash_test_vectors/orchard/poseidon.py @@ -1,13 +1,16 @@ #!/usr/bin/env python3 import sys; assert sys.version_info[0] >= 3, "Python 3 required." -from ..orchard.pallas import Fp import numpy as np from itertools import chain + +from .pallas import Fp + from ..utils import leos2ip from ..output import render_args, render_tv from ..rand import Rand + # Number of full rounds R_F = 8 # Number of partial rounds @@ -156,8 +159,8 @@ def hash(x, y): assert isinstance(y, Fp) return perm([x, y, CAPACITY_ELEMENT])[0] -def main(): +def main(): # These are test vectors from https://github.com/daira/pasta-hadeshash/commit/f7ca15dcf8568f1a4b2c4b7188815e80e9ab8975. fixed_test_input = [ Fp(0x0000000000000000000000000000000000000000000000000000000000000000), @@ -204,5 +207,38 @@ def main(): } for input in test_vectors], ) +def hash_test_vectors(): + test_vectors = [[Fp.ZERO, Fp(1)]] + + from random import Random + rng = Random(0xabad533d) + def randbytes(l): + ret = [] + while len(ret) < l: + ret.append(rng.randrange(0, 256)) + return bytes(ret) + rand = Rand(randbytes) + + # Generate random test vectors + for _ in range(10): + test_vectors.append([ + Fp(leos2ip(rand.b(32))), + Fp(leos2ip(rand.b(32))), + ]) + + render_tv( + render_args(), + 'orchard_poseidon_hash', + ( + ('input', '[[u8; 32]; 2]'), + ('output', '[u8; 32]'), + ), + [{ + 'input': list(map(bytes, input)), + 'output': bytes(hash(input[0], input[1])), + } for input in test_vectors], + ) + + if __name__ == "__main__": main() diff --git a/zcash_test_vectors/orchard/poseidon_hash.py b/zcash_test_vectors/orchard/poseidon_hash.py deleted file mode 100755 index 885bbbe..0000000 --- a/zcash_test_vectors/orchard/poseidon_hash.py +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env python3 -import sys; assert sys.version_info[0] >= 3, "Python 3 required." - -from .pallas import Fp -from . import poseidon - -from ..utils import leos2ip -from ..output import render_args, render_tv -from ..rand import Rand - -def main(): - test_vectors = [[Fp.ZERO, Fp(1)]] - - from random import Random - rng = Random(0xabad533d) - def randbytes(l): - ret = [] - while len(ret) < l: - ret.append(rng.randrange(0, 256)) - return bytes(ret) - rand = Rand(randbytes) - - # Generate random test vectors - for _ in range(10): - test_vectors.append([ - Fp(leos2ip(rand.b(32))), - Fp(leos2ip(rand.b(32))), - ]) - - render_tv( - render_args(), - 'orchard_poseidon_hash', - ( - ('input', '[[u8; 32]; 2]'), - ('output', '[u8; 32]'), - ), - [{ - 'input': list(map(bytes, input)), - 'output': bytes(poseidon.hash(input[0], input[1])), - } for input in test_vectors], - ) - -if __name__ == "__main__": - main()