// @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, getMerkleProof.
// Adaptation: selected device indices, checked wire metadata and exclusive rows.
import attributes;
struct PushConstants {
uint level_index;
uint indices_index;
uint output_index;
uint count;
uint leaf_count;
uint source_count;
uint depth;
uint level;
uint frame_bytes;
};
[[vk::push_constant]] PushConstants push;
[[vk::binding(0, 0)]] RWByteAddressBuffer storage_buffers[];
[kernel_name("merkle_proof_gather")]
[domain("cryptography")]
[variant("generic")]
[dtype("u8")]
[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 source_extent, indices_extent, output_extent;
storage_buffers[push.level_index].GetDimensions(source_extent);
storage_buffers[push.indices_index].GetDimensions(indices_extent);
storage_buffers[push.output_index].GetDimensions(output_extent);
if (push.leaf_count == 0u || push.depth > 32u
|| push.frame_bytes != 16u + push.depth * 32u
|| push.count > indices_extent / 4u || push.count > output_extent / push.frame_bytes
|| push.source_count > source_extent / 32u) return;
uint expected_count = push.leaf_count;
uint expected_depth = 0u;
[loop]
while (expected_count > 1u) {
expected_count = expected_count / 2u + (expected_count & 1u);
++expected_depth;
}
if (push.depth != expected_depth || (push.depth == 0u ? push.level != 0u : push.level >= push.depth)) return;
expected_count = push.leaf_count;
[loop]
for (uint level = 0u; level < push.level; ++level)
expected_count = expected_count / 2u + (expected_count & 1u);
if (expected_count != push.source_count) return;
uint index = storage_buffers[push.indices_index].Load(row * 4u);
bool valid = index < push.leaf_count;
uint base = row * push.frame_bytes;
if (push.level == 0u) {
storage_buffers[push.output_index].Store<uint>(base, 1u);
storage_buffers[push.output_index].Store<uint>(base + 4u, push.leaf_count);
storage_buffers[push.output_index].Store<uint>(base + 8u, index);
storage_buffers[push.output_index].Store<uint>(base + 12u, valid ? 1u : 0u);
}
if (push.depth == 0u) return;
uint node = index >> push.level;
uint sibling = min(node ^ 1u, push.source_count - 1u);
uint output_base = base + 16u + push.level * 32u;
[loop]
for (uint word = 0u; word < 32u / 4u; ++word) {
uint value = 0u;
if (valid) value = storage_buffers[push.level_index].Load(sibling * 32u + word * 4u);
storage_buffers[push.output_index].Store<uint>(output_base + word * 4u, value);
}
}