// @generated by tool/gen/fn/generate.py; DO NOT EDIT.
// schema=cryptography_hash.json schema_version=1 generator_version=64
// schema_sha256=982731ed954c1dddbb0b1430f6561e03cbaf7cb6e7880fae9ba317ad9a1fa87b
// OA donor: source/cpp/lib/oa/crypto/hash.cpp, verifyMerkleProof.
// Adaptation: canonical self-describing frames, device flags, checked ranges.
// Each invocation owns one U32 result; invalid content always yields zero.
import attributes;
import merkle_pair;
struct PushConstants {
uint leaves_index;
uint proofs_index;
uint roots_index;
uint output_index;
uint count;
uint root_count;
uint frame_bytes;
uint leaf_count;
};
[[vk::push_constant]] PushConstants push;
[[vk::binding(0, 0)]] RWByteAddressBuffer storage_buffers[];
[kernel_name("merkle_proof_verify")]
[domain("cryptography")]
[variant("generic")]
[dtype("u32")]
[status("experimental")]
[shader("compute")]
[numthreads(32, 1, 1)]
void main(uint3 dispatch_thread_id : SV_DispatchThreadID) {
uint row = dispatch_thread_id.x;
if (row >= push.count) return;
uint leaf_extent, proof_extent, root_extent, output_extent;
storage_buffers[push.leaves_index].GetDimensions(leaf_extent);
storage_buffers[push.proofs_index].GetDimensions(proof_extent);
storage_buffers[push.roots_index].GetDimensions(root_extent);
storage_buffers[push.output_index].GetDimensions(output_extent);
if (push.frame_bytes < 16u
|| (push.frame_bytes - 16u) % 32u != 0u
|| (push.frame_bytes - 16u) / 32u > 32u
|| (push.root_count != 1u && push.root_count != push.count)
|| push.count > leaf_extent / 32u || push.count > proof_extent / push.frame_bytes
|| push.root_count > root_extent / 32u || push.count > output_extent / 4u) return;
storage_buffers[push.output_index].Store<uint>(row * 4u, 0u);
uint base = row * push.frame_bytes;
uint version = storage_buffers[push.proofs_index].Load(base);
uint nodes = storage_buffers[push.proofs_index].Load(base + 4u);
uint index = storage_buffers[push.proofs_index].Load(base + 8u);
uint valid = storage_buffers[push.proofs_index].Load(base + 12u);
if (version != 1u || nodes == 0u || nodes != push.leaf_count || index >= nodes || valid != 1u) return;
uint depth = (push.frame_bytes - 16u) / 32u;
uint expected_depth = 0u;
uint count = nodes;
[loop]
while (count > 1u) {
count = count / 2u + (count & 1u);
++expected_depth;
}
if (depth != expected_depth) return;
uint current[8];
[loop]
for (uint word = 0u; word < 8u; ++word)
current[word] = storage_buffers[push.leaves_index].Load(row * 32u + word * 4u);
[loop]
for (uint level = 0u; level < depth; ++level) {
uint sibling_base = base + 16u + level * 32u;
bool sibling_left = (index & 1u) != 0u;
if (!sibling_left && index + 1u == nodes) {
// Canonical odd-tail proofs repeat the running node itself.
uint mismatch = 0u;
[loop]
for (uint word = 0u; word < 8u; ++word)
mismatch |= current[word] ^ storage_buffers[push.proofs_index].Load(sibling_base + word * 4u);
if (mismatch != 0u) return;
}
merkle_hash_pair(current, storage_buffers[push.proofs_index], sibling_base, sibling_left);
index >>= 1u;
nodes = nodes / 2u + (nodes & 1u);
}
uint root_base = (push.root_count == 1u ? 0u : row) * 32u;
uint mismatch = 0u;
[loop]
for (uint word = 0u; word < 8u; ++word)
mismatch |= current[word] ^ storage_buffers[push.roots_index].Load(root_base + word * 4u);
storage_buffers[push.output_index].Store<uint>(row * 4u, mismatch == 0u ? 1u : 0u);
}