Generate Poseidon hash test vectors

This commit is contained in:
therealyingtong 2021-05-07 12:33:09 +08:00
parent 6184981ccd
commit 25f5ccd445
1 changed files with 48 additions and 0 deletions

48
orchard_poseidon_hash.py Normal file
View File

@ -0,0 +1,48 @@
from orchard_pallas import Fp
from orchard_poseidon import perm
from sapling_utils import leos2ip
from tv_output import render_args, render_tv
from tv_rand import Rand
# Initial capacity element
CAPACITY_ELEMENT = Fp(1 << 65)
def poseidon_hash(x, y):
assert isinstance(x, Fp)
assert isinstance(y, Fp)
return perm([x, y, CAPACITY_ELEMENT])
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(),
'poseidon_hash',
(
('input', '[[u8; 32]; 2]'),
('output', '[[u8; 32]; 3]'),
),
[{
'input': list(map(bytes, input)),
'output': list(map(bytes, poseidon_hash(input[0], input[1]))),
} for input in test_vectors],
)
if __name__ == "__main__":
main()