# @file for_authpath_01_32.py (2023-03-16T14:29Z)
# @author David Ireland <www.di-mgt.com.au/contact>
# @copyright 2023 DI Management Services Pty Ltd
# @license Apache-2.0
"""Compute the authpaths from scratch for the FORS trees with i=1 and i=32."""
from spx_adrs import Adrs
from spx_util import hash_root, authpath
from spx_sha256 import PRF, F
PKseed = 'B505D7CFAD1B497499323C8686325E47'
SKseed = '7C9935A0B07694AA0C6D10E4DB6B1ADD'
t = 64
leaves = []
skeys = []
# Set up ADRS object
adrs = Adrs(Adrs.FORS_TREE, layer=0)
adrs.setTreeAddress(0x28daecdc86eb8761)
adrs.setKeyPairAddress(6)
# Compute FORS sk and pk values for tree (i = 1)
i = 1
for j in range(t):
treeindex = i * t + j
adrs.setTreeIndex(treeindex)
print(f"ADRS={adrs.toHex()}")
sk = PRF(SKseed, adrs.toHex())
print(f"fors_sk[{i}][{j}]={sk}")
pk = F(PKseed, adrs.toHex(), sk)
print(f"fors_pk[{i}][{j}]={pk}")
leaves.append(pk)
skeys.append(sk)
print("leaves=", leaves, sep='\n')
# Compute the root value and the authpath for index 57
idx = 57
print(f"fors_sk[{i}][{idx}]={skeys[idx]}")
assert(skeys[idx] == "229f6db83fc861d6fc5877405f5b9466")
adrs = Adrs(Adrs.FORS_TREE, layer=0)
adrs.setTreeAddress(0x28daecdc86eb8761)
adrs.setKeyPairAddress(6)
print(f"ADRS={adrs.toHex()}")
root = hash_root(leaves, adrs, PKseed, i * t)
print(f"root[{i}]={root}")
assert(root == "4ff05f5821b513a402be41ef4e76f81f")
auth = authpath(leaves, adrs, PKseed, idx, i * t, showdebug=True)
print(f"fors_auth_path[{i}]:")
[print(a) for a in auth]
assert(auth[5] == '903938edcd9718d81330ed6645316ce7')
# REPEAT THE ABOVE FOR i=32
leaves = []
skeys = []
# Set up ADRS object
adrs = Adrs(Adrs.FORS_TREE, layer=0)
adrs.setTreeAddress(0x28daecdc86eb8761)
adrs.setKeyPairAddress(6)
# Compute FORS sk and pk values for 33rd tree (i = 32)
i = 32
for j in range(t):
treeindex = i * t + j
adrs.setTreeIndex(treeindex)
print(f"ADRS={adrs.toHex()}")
sk = PRF(SKseed, adrs.toHex())
print(f"fors_sk[{i}][{j}]={sk}")
pk = F(PKseed, adrs.toHex(), sk)
print(f"fors_pk[{i}][{j}]={pk}")
leaves.append(pk)
skeys.append(sk)
print("leaves=", leaves, sep='\n')
# Compute the root value and the authpath for index 28
idx = 28
print(f"fors_sk[{i}][{idx}]={skeys[idx]}")
assert(skeys[idx] == "446d9fc66808fcc5e0d47c0c381c7f9e")
adrs = Adrs(Adrs.FORS_TREE, layer=0)
adrs.setTreeAddress(0x28daecdc86eb8761)
adrs.setKeyPairAddress(6)
print(f"ADRS={adrs.toHex()}")
root = hash_root(leaves, adrs, PKseed, i * t)
print(f"root[{i}]={root}")
assert(root == "b77027ab7c2815483900f93fa9e8335f")
auth = authpath(leaves, adrs, PKseed, idx, i * t)
print(f"fors_auth_path[{i}]:")
[print(a) for a in auth]
assert(auth[5] == '302f791fccc4ded35f988a70205be088')