use crate::{
DType, Error, Matrix, OpAttribute, Result,
runtime::{BufferBinding, ComputeDispatch, KernelId, PushConstant, SemanticDispatch},
};
pub fn shake128(input: &Matrix, output_length: usize) -> Result<Matrix> {
let operation = crate::core::operation::cryptography::hash::SHAKE128;
let kernel = KernelId::CryptographyShake128U8;
let rate = 168;
let default_output_length = 16;
let [rows, message_len] = validate_byte_rows(input, operation.name())?;
let output_length = if output_length == 0 {
default_output_length
} else {
output_length
};
let output_row_bytes = output_length
.checked_add(7)
.ok_or_else(|| Error::invalid_argument("SHAKE output length overflows usize"))?
/ 8
* 8;
let element_count = rows
.checked_mul(output_row_bytes)
.ok_or_else(|| Error::invalid_argument("SHAKE output size overflows usize"))?;
shader_u32(element_count, "output byte size", operation.name())?;
let output = Matrix::allocate_output(
input.engine_handle(),
vec![rows, output_row_bytes],
element_count,
DType::U8,
)?;
let rows_u32 = shader_u32(rows, "batch size", operation.name())?;
let message_len_u32 = shader_u32(message_len, "message length", operation.name())?;
let num_blocks = message_len
.checked_div(rate)
.and_then(|blocks| blocks.checked_add(1))
.ok_or_else(|| Error::invalid_argument("SHAKE block count overflows usize"))?;
let padded_message_len = num_blocks
.checked_mul(rate)
.ok_or_else(|| Error::invalid_argument("SHAKE padded message size overflows usize"))?;
shader_u32(
padded_message_len,
"padded message length",
operation.name(),
)?;
let num_blocks = shader_u32(num_blocks, "block count", operation.name())?;
let squeeze_u64 = shader_u32(output_row_bytes / 8, "squeeze length", operation.name())?;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [
PushConstant::U32(rows_u32),
PushConstant::U32(message_len_u32),
PushConstant::U32(num_blocks),
PushConstant::U32(squeeze_u64),
];
let inputs = [input];
let outputs = [&output];
let attributes = [OpAttribute::UnsignedInteger {
name: "output_length".into(),
value: u64::try_from(output_length)
.map_err(|_| Error::invalid_argument("SHAKE output length exceeds u64"))?,
}];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(rows_u32),
},
SemanticDispatch {
contract: operation,
inputs: &inputs,
outputs: &outputs,
attributes: &attributes,
},
)?;
Ok(output)
}
pub fn shake256(input: &Matrix, output_length: usize) -> Result<Matrix> {
let operation = crate::core::operation::cryptography::hash::SHAKE256;
let kernel = KernelId::CryptographyShake256U8;
let rate = 136;
let default_output_length = 32;
let [rows, message_len] = validate_byte_rows(input, operation.name())?;
let output_length = if output_length == 0 {
default_output_length
} else {
output_length
};
let output_row_bytes = output_length
.checked_add(7)
.ok_or_else(|| Error::invalid_argument("SHAKE output length overflows usize"))?
/ 8
* 8;
let element_count = rows
.checked_mul(output_row_bytes)
.ok_or_else(|| Error::invalid_argument("SHAKE output size overflows usize"))?;
shader_u32(element_count, "output byte size", operation.name())?;
let output = Matrix::allocate_output(
input.engine_handle(),
vec![rows, output_row_bytes],
element_count,
DType::U8,
)?;
let rows_u32 = shader_u32(rows, "batch size", operation.name())?;
let message_len_u32 = shader_u32(message_len, "message length", operation.name())?;
let num_blocks = message_len
.checked_div(rate)
.and_then(|blocks| blocks.checked_add(1))
.ok_or_else(|| Error::invalid_argument("SHAKE block count overflows usize"))?;
let padded_message_len = num_blocks
.checked_mul(rate)
.ok_or_else(|| Error::invalid_argument("SHAKE padded message size overflows usize"))?;
shader_u32(
padded_message_len,
"padded message length",
operation.name(),
)?;
let num_blocks = shader_u32(num_blocks, "block count", operation.name())?;
let squeeze_u64 = shader_u32(output_row_bytes / 8, "squeeze length", operation.name())?;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [
PushConstant::U32(rows_u32),
PushConstant::U32(message_len_u32),
PushConstant::U32(num_blocks),
PushConstant::U32(squeeze_u64),
];
let inputs = [input];
let outputs = [&output];
let attributes = [OpAttribute::UnsignedInteger {
name: "output_length".into(),
value: u64::try_from(output_length)
.map_err(|_| Error::invalid_argument("SHAKE output length exceeds u64"))?,
}];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(rows_u32),
},
SemanticDispatch {
contract: operation,
inputs: &inputs,
outputs: &outputs,
attributes: &attributes,
},
)?;
Ok(output)
}
pub fn keccak_f1600(input: &Matrix) -> Result<Matrix> {
const OPERATION: &str = crate::core::operation::cryptography::hash::KECCAK_F1600.name();
let [rows, row_bytes] = validate_byte_rows(input, OPERATION)?;
if row_bytes != 200 {
return Err(Error::invalid_argument(format!(
"{OPERATION} requires 200-byte rows; found {row_bytes}"
)));
}
let output = Matrix::allocate_output(
input.engine_handle(),
input.shape().to_vec(),
input.num_elements(),
DType::U8,
)?;
let count = shader_u32(rows, "batch size", OPERATION)?;
let kernel = KernelId::CryptographyKeccakF1600U8;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [PushConstant::U32(count)];
let inputs = [input];
let outputs = [&output];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(count),
},
SemanticDispatch {
contract: crate::core::operation::cryptography::hash::KECCAK_F1600,
inputs: &inputs,
outputs: &outputs,
attributes: &[],
},
)?;
Ok(output)
}
pub fn merkle_root(input: &Matrix) -> Result<Matrix> {
const OPERATION: &str = crate::core::operation::cryptography::hash::MERKLE_ROOT.name();
let [leaf_count, hash_bytes] = validate_byte_rows(input, OPERATION)?;
if hash_bytes != 32 {
return Err(Error::invalid_argument(format!(
"{OPERATION} requires 32-byte leaf hashes; found {hash_bytes}"
)));
}
if leaf_count == 1 {
let output = Matrix::allocate_output(input.engine_handle(), vec![1, 32], 32, DType::U8)?;
let kernel = KernelId::CryptographyMerkleCopyU8;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [PushConstant::U32(8)];
let inputs = [input];
let outputs = [&output];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(8),
},
SemanticDispatch {
contract: crate::core::operation::cryptography::hash::MERKLE_ROOT,
inputs: &inputs,
outputs: &outputs,
attributes: &[],
},
)?;
return Ok(output);
}
if leaf_count == 2 {
let output = Matrix::allocate_output(input.engine_handle(), vec![1, 32], 32, DType::U8)?;
let kernel = KernelId::CryptographyMerkleReduceU8;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [
PushConstant::U32(1),
PushConstant::U32(32),
PushConstant::U32(2),
];
let inputs = [input];
let outputs = [&output];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(1),
},
SemanticDispatch {
contract: crate::core::operation::cryptography::hash::MERKLE_ROOT,
inputs: &inputs,
outputs: &outputs,
attributes: &[],
},
)?;
return Ok(output);
}
let mut levels = Vec::new();
let mut count = leaf_count.div_ceil(2);
loop {
levels.push(Matrix::allocate_output(
input.engine_handle(),
vec![count, 32],
count
.checked_mul(32)
.ok_or_else(|| Error::invalid_argument("Merkle level size overflows usize"))?,
DType::U8,
)?);
if count == 1 {
break;
}
count = count.div_ceil(2);
}
{
let binding_sets = levels
.iter()
.enumerate()
.map(|(index, output)| {
let source = if index == 0 {
input
} else {
&levels[index - 1]
};
[
BufferBinding::read(source.storage()),
BufferBinding::write(output.storage()),
]
})
.collect::<Vec<_>>();
let push_sets = levels
.iter()
.enumerate()
.map(|(index, level)| {
let source = if index == 0 {
input
} else {
&levels[index - 1]
};
Ok([
PushConstant::U32(shader_u32(level.shape()[0], "level count", OPERATION)?),
PushConstant::U32(32),
PushConstant::U32(shader_u32(
source.shape()[0],
"source leaf count",
OPERATION,
)?),
])
})
.collect::<Result<Vec<_>>>()?;
let kernel = KernelId::CryptographyMerkleReduceU8;
let dispatches = binding_sets
.iter()
.zip(&push_sets)
.map(|(buffers, push_constants)| {
let PushConstant::U32(count) = push_constants[0] else {
unreachable!();
};
ComputeDispatch {
kernel,
buffers,
push_constants,
workgroups: kernel.linear_workgroups(count),
}
})
.collect::<Vec<_>>();
let inputs = [input];
let outputs = [&levels[levels.len() - 1]];
input.engine_handle().record_split_semantic(
&dispatches,
SemanticDispatch {
contract: crate::core::operation::cryptography::hash::MERKLE_ROOT,
inputs: &inputs,
outputs: &outputs,
attributes: &[],
},
)?;
}
levels
.pop()
.ok_or_else(|| Error::internal("Merkle lowering produced no output"))
}
pub fn sha224(input: &Matrix) -> Result<Matrix> {
const OPERATION: &str = crate::core::operation::cryptography::hash::SHA224.name();
let [rows, message_bytes] = validate_byte_rows(input, OPERATION)?;
let digest_bytes = 28;
let element_count = rows
.checked_mul(digest_bytes)
.ok_or_else(|| Error::invalid_argument(format!("{OPERATION} output size overflows usize")))?;
shader_u32(element_count, "output byte size", OPERATION)?;
let count = shader_u32(rows, "batch size", OPERATION)?;
let message_len = shader_u32(message_bytes, "message length", OPERATION)?;
let output = Matrix::allocate_output(
input.engine_handle(),
vec![rows, digest_bytes],
element_count,
DType::U8,
)?;
let kernel = KernelId::CryptographySha224U8;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [PushConstant::U32(count), PushConstant::U32(message_len)];
let inputs = [input];
let outputs = [&output];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(count),
},
SemanticDispatch {
contract: crate::core::operation::cryptography::hash::SHA224,
inputs: &inputs,
outputs: &outputs,
attributes: &[],
},
)?;
Ok(output)
}
pub fn sha256(input: &Matrix) -> Result<Matrix> {
const OPERATION: &str = crate::core::operation::cryptography::hash::SHA256.name();
let [rows, message_bytes] = validate_byte_rows(input, OPERATION)?;
let digest_bytes = 32;
let element_count = rows
.checked_mul(digest_bytes)
.ok_or_else(|| Error::invalid_argument(format!("{OPERATION} output size overflows usize")))?;
shader_u32(element_count, "output byte size", OPERATION)?;
let count = shader_u32(rows, "batch size", OPERATION)?;
let message_len = shader_u32(message_bytes, "message length", OPERATION)?;
let output = Matrix::allocate_output(
input.engine_handle(),
vec![rows, digest_bytes],
element_count,
DType::U8,
)?;
let kernel = KernelId::CryptographySha256U8;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [PushConstant::U32(count), PushConstant::U32(message_len)];
let inputs = [input];
let outputs = [&output];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(count),
},
SemanticDispatch {
contract: crate::core::operation::cryptography::hash::SHA256,
inputs: &inputs,
outputs: &outputs,
attributes: &[],
},
)?;
Ok(output)
}
pub fn sha384(input: &Matrix) -> Result<Matrix> {
const OPERATION: &str = crate::core::operation::cryptography::hash::SHA384.name();
let [rows, message_bytes] = validate_byte_rows(input, OPERATION)?;
let digest_bytes = 48;
let element_count = rows
.checked_mul(digest_bytes)
.ok_or_else(|| Error::invalid_argument(format!("{OPERATION} output size overflows usize")))?;
shader_u32(element_count, "output byte size", OPERATION)?;
let count = shader_u32(rows, "batch size", OPERATION)?;
let message_len = shader_u32(message_bytes, "message length", OPERATION)?;
let output = Matrix::allocate_output(
input.engine_handle(),
vec![rows, digest_bytes],
element_count,
DType::U8,
)?;
let kernel = KernelId::CryptographySha384U8;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [PushConstant::U32(count), PushConstant::U32(message_len)];
let inputs = [input];
let outputs = [&output];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(count),
},
SemanticDispatch {
contract: crate::core::operation::cryptography::hash::SHA384,
inputs: &inputs,
outputs: &outputs,
attributes: &[],
},
)?;
Ok(output)
}
pub fn sha512(input: &Matrix) -> Result<Matrix> {
const OPERATION: &str = crate::core::operation::cryptography::hash::SHA512.name();
let [rows, message_bytes] = validate_byte_rows(input, OPERATION)?;
let digest_bytes = 64;
let element_count = rows
.checked_mul(digest_bytes)
.ok_or_else(|| Error::invalid_argument(format!("{OPERATION} output size overflows usize")))?;
shader_u32(element_count, "output byte size", OPERATION)?;
let count = shader_u32(rows, "batch size", OPERATION)?;
let message_len = shader_u32(message_bytes, "message length", OPERATION)?;
let output = Matrix::allocate_output(
input.engine_handle(),
vec![rows, digest_bytes],
element_count,
DType::U8,
)?;
let kernel = KernelId::CryptographySha512U8;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [PushConstant::U32(count), PushConstant::U32(message_len)];
let inputs = [input];
let outputs = [&output];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(count),
},
SemanticDispatch {
contract: crate::core::operation::cryptography::hash::SHA512,
inputs: &inputs,
outputs: &outputs,
attributes: &[],
},
)?;
Ok(output)
}
pub fn sha512_224(input: &Matrix) -> Result<Matrix> {
const OPERATION: &str = crate::core::operation::cryptography::hash::SHA512_224.name();
let [rows, message_bytes] = validate_byte_rows(input, OPERATION)?;
let digest_bytes = 28;
let element_count = rows
.checked_mul(digest_bytes)
.ok_or_else(|| Error::invalid_argument(format!("{OPERATION} output size overflows usize")))?;
shader_u32(element_count, "output byte size", OPERATION)?;
let count = shader_u32(rows, "batch size", OPERATION)?;
let message_len = shader_u32(message_bytes, "message length", OPERATION)?;
let output = Matrix::allocate_output(
input.engine_handle(),
vec![rows, digest_bytes],
element_count,
DType::U8,
)?;
let kernel = KernelId::CryptographySha512224U8;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [PushConstant::U32(count), PushConstant::U32(message_len)];
let inputs = [input];
let outputs = [&output];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(count),
},
SemanticDispatch {
contract: crate::core::operation::cryptography::hash::SHA512_224,
inputs: &inputs,
outputs: &outputs,
attributes: &[],
},
)?;
Ok(output)
}
pub fn sha512_256(input: &Matrix) -> Result<Matrix> {
const OPERATION: &str = crate::core::operation::cryptography::hash::SHA512_256.name();
let [rows, message_bytes] = validate_byte_rows(input, OPERATION)?;
let digest_bytes = 32;
let element_count = rows
.checked_mul(digest_bytes)
.ok_or_else(|| Error::invalid_argument(format!("{OPERATION} output size overflows usize")))?;
shader_u32(element_count, "output byte size", OPERATION)?;
let count = shader_u32(rows, "batch size", OPERATION)?;
let message_len = shader_u32(message_bytes, "message length", OPERATION)?;
let output = Matrix::allocate_output(
input.engine_handle(),
vec![rows, digest_bytes],
element_count,
DType::U8,
)?;
let kernel = KernelId::CryptographySha512256U8;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [PushConstant::U32(count), PushConstant::U32(message_len)];
let inputs = [input];
let outputs = [&output];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(count),
},
SemanticDispatch {
contract: crate::core::operation::cryptography::hash::SHA512_256,
inputs: &inputs,
outputs: &outputs,
attributes: &[],
},
)?;
Ok(output)
}
pub fn sha3_224(input: &Matrix) -> Result<Matrix> {
const OPERATION: &str = crate::core::operation::cryptography::hash::SHA3_224.name();
let [rows, message_bytes] = validate_byte_rows(input, OPERATION)?;
let digest_bytes = 28;
let element_count = rows
.checked_mul(digest_bytes)
.ok_or_else(|| Error::invalid_argument(format!("{OPERATION} output size overflows usize")))?;
shader_u32(element_count, "output byte size", OPERATION)?;
let count = shader_u32(rows, "batch size", OPERATION)?;
let message_len = shader_u32(message_bytes, "message length", OPERATION)?;
let output = Matrix::allocate_output(
input.engine_handle(),
vec![rows, digest_bytes],
element_count,
DType::U8,
)?;
let kernel = KernelId::CryptographySha3224U8;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [PushConstant::U32(count), PushConstant::U32(message_len)];
let inputs = [input];
let outputs = [&output];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(count),
},
SemanticDispatch {
contract: crate::core::operation::cryptography::hash::SHA3_224,
inputs: &inputs,
outputs: &outputs,
attributes: &[],
},
)?;
Ok(output)
}
pub fn sha3_256(input: &Matrix) -> Result<Matrix> {
const OPERATION: &str = crate::core::operation::cryptography::hash::SHA3_256.name();
let [rows, message_bytes] = validate_byte_rows(input, OPERATION)?;
let digest_bytes = 32;
let element_count = rows
.checked_mul(digest_bytes)
.ok_or_else(|| Error::invalid_argument(format!("{OPERATION} output size overflows usize")))?;
shader_u32(element_count, "output byte size", OPERATION)?;
let count = shader_u32(rows, "batch size", OPERATION)?;
let message_len = shader_u32(message_bytes, "message length", OPERATION)?;
let output = Matrix::allocate_output(
input.engine_handle(),
vec![rows, digest_bytes],
element_count,
DType::U8,
)?;
let kernel = KernelId::CryptographySha3256U8;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [PushConstant::U32(count), PushConstant::U32(message_len)];
let inputs = [input];
let outputs = [&output];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(count),
},
SemanticDispatch {
contract: crate::core::operation::cryptography::hash::SHA3_256,
inputs: &inputs,
outputs: &outputs,
attributes: &[],
},
)?;
Ok(output)
}
pub fn sha3_384(input: &Matrix) -> Result<Matrix> {
const OPERATION: &str = crate::core::operation::cryptography::hash::SHA3_384.name();
let [rows, message_bytes] = validate_byte_rows(input, OPERATION)?;
let digest_bytes = 48;
let element_count = rows
.checked_mul(digest_bytes)
.ok_or_else(|| Error::invalid_argument(format!("{OPERATION} output size overflows usize")))?;
shader_u32(element_count, "output byte size", OPERATION)?;
let count = shader_u32(rows, "batch size", OPERATION)?;
let message_len = shader_u32(message_bytes, "message length", OPERATION)?;
let output = Matrix::allocate_output(
input.engine_handle(),
vec![rows, digest_bytes],
element_count,
DType::U8,
)?;
let kernel = KernelId::CryptographySha3384U8;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [PushConstant::U32(count), PushConstant::U32(message_len)];
let inputs = [input];
let outputs = [&output];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(count),
},
SemanticDispatch {
contract: crate::core::operation::cryptography::hash::SHA3_384,
inputs: &inputs,
outputs: &outputs,
attributes: &[],
},
)?;
Ok(output)
}
pub fn sha3_512(input: &Matrix) -> Result<Matrix> {
const OPERATION: &str = crate::core::operation::cryptography::hash::SHA3_512.name();
let [rows, message_bytes] = validate_byte_rows(input, OPERATION)?;
let digest_bytes = 64;
let element_count = rows
.checked_mul(digest_bytes)
.ok_or_else(|| Error::invalid_argument(format!("{OPERATION} output size overflows usize")))?;
shader_u32(element_count, "output byte size", OPERATION)?;
let count = shader_u32(rows, "batch size", OPERATION)?;
let message_len = shader_u32(message_bytes, "message length", OPERATION)?;
let output = Matrix::allocate_output(
input.engine_handle(),
vec![rows, digest_bytes],
element_count,
DType::U8,
)?;
let kernel = KernelId::CryptographySha3512U8;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [PushConstant::U32(count), PushConstant::U32(message_len)];
let inputs = [input];
let outputs = [&output];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(count),
},
SemanticDispatch {
contract: crate::core::operation::cryptography::hash::SHA3_512,
inputs: &inputs,
outputs: &outputs,
attributes: &[],
},
)?;
Ok(output)
}
pub fn merkle_proofs(leaves: &Matrix, indices: &Matrix) -> Result<Matrix> {
const OPERATION: &str = crate::core::operation::cryptography::hash::MERKLE_PROOFS.name();
let [leaf_count, leaf_bytes] = validate_byte_rows(leaves, OPERATION)?;
let header_bytes = 16;
let hash_bytes = 32;
if leaf_bytes != hash_bytes
|| indices.dtype() != DType::U32
|| indices.shape().len() != 1
|| !leaves.engine_handle().same_as(indices.engine_handle())
{
return Err(Error::invalid_argument(
"Merkle proof generation requires matching-owner U8 [N, 32] leaves and rank-one U32 indices",
));
}
let proof_count = indices.num_elements();
let mut nodes = leaf_count;
let mut depth = 0_usize;
while nodes > 1 {
nodes = nodes.div_ceil(2);
depth += 1;
}
let frame_bytes = header_bytes + depth * hash_bytes;
let element_count = proof_count
.checked_mul(frame_bytes)
.ok_or_else(|| Error::invalid_argument("Merkle proof output size overflows usize"))?;
shader_u32(element_count, "proof byte size", OPERATION)?;
let proof_count_u32 = shader_u32(proof_count, "proof count", OPERATION)?;
let leaf_count_u32 = shader_u32(leaf_count, "leaf count", OPERATION)?;
let frame_bytes_u32 = shader_u32(frame_bytes, "frame size", OPERATION)?;
let depth_u32 = shader_u32(depth, "depth", OPERATION)?;
let hash_bytes_u32 = shader_u32(hash_bytes, "hash size", OPERATION)?;
let output = Matrix::allocate_output(
leaves.engine_handle(),
vec![proof_count, frame_bytes],
element_count,
DType::U8,
)?;
if proof_count == 0 {
return Ok(output);
}
let mut levels = Vec::with_capacity(depth.saturating_sub(1));
let mut count = leaf_count;
for _ in 1..depth {
count = count.div_ceil(2);
levels.push(Matrix::allocate_output(
leaves.engine_handle(),
vec![count, hash_bytes],
count * hash_bytes,
DType::U8,
)?);
}
let sources = std::iter::once(leaves)
.chain(levels.iter())
.collect::<Vec<_>>();
let reduce_buffers = levels
.iter()
.enumerate()
.map(|(index, level)| {
let source = sources[index];
[
BufferBinding::read(source.storage()),
BufferBinding::write(level.storage()),
]
})
.collect::<Vec<_>>();
let reduce_pushes = levels
.iter()
.enumerate()
.map(|(index, level)| {
let source = sources[index];
Ok([
PushConstant::U32(shader_u32(level.shape()[0], "parent count", OPERATION)?),
PushConstant::U32(hash_bytes_u32),
PushConstant::U32(shader_u32(source.shape()[0], "source count", OPERATION)?),
])
})
.collect::<Result<Vec<_>>>()?;
let gather_buffers = sources
.iter()
.map(|source| {
[
BufferBinding::read(source.storage()),
BufferBinding::read(indices.storage()),
BufferBinding::write(output.storage()),
]
})
.collect::<Vec<_>>();
let gather_pushes = sources
.iter()
.enumerate()
.map(|(index, source)| {
Ok([
PushConstant::U32(proof_count_u32),
PushConstant::U32(leaf_count_u32),
PushConstant::U32(shader_u32(source.shape()[0], "source count", OPERATION)?),
PushConstant::U32(depth_u32),
PushConstant::U32(shader_u32(index, "proof level", OPERATION)?),
PushConstant::U32(frame_bytes_u32),
])
})
.collect::<Result<Vec<_>>>()?;
let gather = KernelId::CryptographyMerkleProofGatherU8;
let reduce = KernelId::CryptographyMerkleReduceU8;
let mut dispatches = Vec::with_capacity(gather_buffers.len() + reduce_buffers.len());
for index in 0..gather_buffers.len() {
dispatches.push(ComputeDispatch {
kernel: gather,
buffers: &gather_buffers[index],
push_constants: &gather_pushes[index],
workgroups: gather.linear_workgroups(proof_count_u32),
});
if let Some(buffers) = reduce_buffers.get(index) {
let count = shader_u32(levels[index].shape()[0], "parent count", OPERATION)?;
dispatches.push(ComputeDispatch {
kernel: reduce,
buffers,
push_constants: &reduce_pushes[index],
workgroups: reduce.linear_workgroups(count),
});
}
}
let inputs = [leaves, indices];
let outputs = [&output];
let semantic = SemanticDispatch {
contract: crate::core::operation::cryptography::hash::MERKLE_PROOFS,
inputs: &inputs,
outputs: &outputs,
attributes: &[],
};
if dispatches.len() == 1 {
leaves
.engine_handle()
.record_semantic(dispatches.remove(0), semantic)?;
} else {
leaves
.engine_handle()
.record_split_semantic(&dispatches, semantic)?;
}
Ok(output)
}
pub fn verify_merkle_proofs(
leaves: &Matrix,
proofs: &Matrix,
roots: &Matrix,
leaf_count: u32,
) -> Result<Matrix> {
const OPERATION: &str = crate::core::operation::cryptography::hash::VERIFY_MERKLE_PROOFS.name();
let header_bytes = 16;
let hash_bytes = 32;
let max_depth = 32;
if [leaves, proofs, roots].iter().any(|value| {
value.dtype() != DType::U8
|| value.shape().len() != 2
|| !leaves.engine_handle().same_as(value.engine_handle())
}) {
return Err(Error::invalid_argument(
"Merkle proof verification requires matching-owner rank-two U8 inputs",
));
}
let rows = leaves.shape()[0];
let frame = proofs.shape()[1];
if leaf_count == 0
|| leaves.shape()[1] != hash_bytes
|| roots.shape()[1] != hash_bytes
|| proofs.shape()[0] != rows
|| (roots.shape()[0] != 1 && roots.shape()[0] != rows)
|| frame < header_bytes
|| !(frame - header_bytes).is_multiple_of(hash_bytes)
|| (frame - header_bytes) / hash_bytes > max_depth
{
return Err(Error::invalid_argument(
"Merkle proof verification shape or frame extent mismatch",
));
}
for input in [leaves, proofs, roots] {
shader_u32(input.num_elements(), "input byte size", OPERATION)?;
}
let count = shader_u32(rows, "proof count", OPERATION)?;
let root_count = shader_u32(roots.shape()[0], "root count", OPERATION)?;
let frame_bytes = shader_u32(frame, "frame size", OPERATION)?;
shader_u32(
rows
.checked_mul(4)
.ok_or_else(|| Error::invalid_argument("Merkle status byte size overflows usize"))?,
"status byte size",
OPERATION,
)?;
let output = Matrix::allocate_output(leaves.engine_handle(), vec![rows], rows, DType::U32)?;
if rows == 0 {
return Ok(output);
}
let kernel = KernelId::CryptographyMerkleProofVerifyU32;
let buffers = [
BufferBinding::read(leaves.storage()),
BufferBinding::read(proofs.storage()),
BufferBinding::read(roots.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [
PushConstant::U32(count),
PushConstant::U32(root_count),
PushConstant::U32(frame_bytes),
PushConstant::U32(leaf_count),
];
let inputs = [leaves, proofs, roots];
let outputs = [&output];
let attributes = [OpAttribute::UnsignedInteger {
name: "leaf_count".into(),
value: u64::from(leaf_count),
}];
leaves.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(count),
},
SemanticDispatch {
contract: crate::core::operation::cryptography::hash::VERIFY_MERKLE_PROOFS,
inputs: &inputs,
outputs: &outputs,
attributes: &attributes,
},
)?;
Ok(output)
}
pub fn poseidon2_babybear16(input: &Matrix, output_length: usize) -> Result<Matrix> {
let contract = crate::core::operation::cryptography::hash::POSEIDON2_BABYBEAR16;
let operation = contract.name();
let [rows, elements] = match input.shape() {
[rows, elements] => [*rows, *elements],
_ => {
return Err(Error::invalid_argument(format!(
"{operation} requires rank-two field rows"
)));
}
};
if input.dtype() != DType::U32 {
return Err(Error::invalid_argument(format!(
"{operation} requires U32 field elements"
)));
}
let output_length = if output_length == 0 { 8 } else { output_length };
let count = shader_u32(rows, "batch size", operation)?;
let input_elements = shader_u32(elements, "input elements", operation)?;
let output_elements = shader_u32(output_length, "output elements", operation)?;
let input_bytes = input.num_elements().checked_mul(4).ok_or_else(|| {
Error::invalid_argument(format!("{operation} input byte extent overflows usize"))
})?;
shader_u32(input_bytes, "input byte extent", operation)?;
let output_count = rows
.checked_mul(output_length)
.ok_or_else(|| Error::invalid_argument(format!("{operation} output extent overflows usize")))?;
let output_bytes = output_count.checked_mul(4).ok_or_else(|| {
Error::invalid_argument(format!("{operation} output byte extent overflows usize"))
})?;
shader_u32(output_bytes, "output byte extent", operation)?;
let output = Matrix::allocate_output(
input.engine_handle(),
vec![rows, output_length],
output_count,
DType::U32,
)?;
if rows != 0 {
let kernel = KernelId::CryptographyPoseidon2Babybear16U32;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [
PushConstant::U32(count),
PushConstant::U32(input_elements),
PushConstant::U32(output_elements),
];
let inputs = [input];
let outputs = [&output];
let attributes = [OpAttribute::UnsignedInteger {
name: "output_length".into(),
value: output_length as u64,
}];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(count),
},
SemanticDispatch {
contract,
inputs: &inputs,
outputs: &outputs,
attributes: &attributes,
},
)?;
}
Ok(output)
}
pub fn poseidon2_babybear16_permute(input: &Matrix) -> Result<Matrix> {
let contract = crate::core::operation::cryptography::hash::POSEIDON2_BABYBEAR16_PERMUTE;
let operation = contract.name();
let [rows, elements] = match input.shape() {
[rows, elements] => [*rows, *elements],
_ => {
return Err(Error::invalid_argument(format!(
"{operation} requires rank-two field rows"
)));
}
};
if input.dtype() != DType::U32 || elements != 16 {
return Err(Error::invalid_argument(format!(
"{operation} requires U32 rows of the permutation width"
)));
}
let count = shader_u32(rows, "batch size", operation)?;
let input_elements = shader_u32(elements, "input elements", operation)?;
let bytes = input
.num_elements()
.checked_mul(4)
.ok_or_else(|| Error::invalid_argument(format!("{operation} byte extent overflows usize")))?;
shader_u32(bytes, "input byte extent", operation)?;
let output = Matrix::allocate_output(
input.engine_handle(),
input.shape().to_vec(),
input.num_elements(),
DType::U32,
)?;
if rows != 0 {
let kernel = KernelId::CryptographyPoseidon2Babybear16PermuteU32;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [PushConstant::U32(count), PushConstant::U32(input_elements)];
let inputs = [input];
let outputs = [&output];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(count),
},
SemanticDispatch {
contract,
inputs: &inputs,
outputs: &outputs,
attributes: &[],
},
)?;
}
Ok(output)
}
pub fn poseidon2_babybear24(input: &Matrix, output_length: usize) -> Result<Matrix> {
let contract = crate::core::operation::cryptography::hash::POSEIDON2_BABYBEAR24;
let operation = contract.name();
let [rows, elements] = match input.shape() {
[rows, elements] => [*rows, *elements],
_ => {
return Err(Error::invalid_argument(format!(
"{operation} requires rank-two field rows"
)));
}
};
if input.dtype() != DType::U32 {
return Err(Error::invalid_argument(format!(
"{operation} requires U32 field elements"
)));
}
let output_length = if output_length == 0 { 8 } else { output_length };
let count = shader_u32(rows, "batch size", operation)?;
let input_elements = shader_u32(elements, "input elements", operation)?;
let output_elements = shader_u32(output_length, "output elements", operation)?;
let input_bytes = input.num_elements().checked_mul(4).ok_or_else(|| {
Error::invalid_argument(format!("{operation} input byte extent overflows usize"))
})?;
shader_u32(input_bytes, "input byte extent", operation)?;
let output_count = rows
.checked_mul(output_length)
.ok_or_else(|| Error::invalid_argument(format!("{operation} output extent overflows usize")))?;
let output_bytes = output_count.checked_mul(4).ok_or_else(|| {
Error::invalid_argument(format!("{operation} output byte extent overflows usize"))
})?;
shader_u32(output_bytes, "output byte extent", operation)?;
let output = Matrix::allocate_output(
input.engine_handle(),
vec![rows, output_length],
output_count,
DType::U32,
)?;
if rows != 0 {
let kernel = KernelId::CryptographyPoseidon2Babybear24U32;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [
PushConstant::U32(count),
PushConstant::U32(input_elements),
PushConstant::U32(output_elements),
];
let inputs = [input];
let outputs = [&output];
let attributes = [OpAttribute::UnsignedInteger {
name: "output_length".into(),
value: output_length as u64,
}];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(count),
},
SemanticDispatch {
contract,
inputs: &inputs,
outputs: &outputs,
attributes: &attributes,
},
)?;
}
Ok(output)
}
pub fn poseidon2_babybear24_permute(input: &Matrix) -> Result<Matrix> {
let contract = crate::core::operation::cryptography::hash::POSEIDON2_BABYBEAR24_PERMUTE;
let operation = contract.name();
let [rows, elements] = match input.shape() {
[rows, elements] => [*rows, *elements],
_ => {
return Err(Error::invalid_argument(format!(
"{operation} requires rank-two field rows"
)));
}
};
if input.dtype() != DType::U32 || elements != 24 {
return Err(Error::invalid_argument(format!(
"{operation} requires U32 rows of the permutation width"
)));
}
let count = shader_u32(rows, "batch size", operation)?;
let input_elements = shader_u32(elements, "input elements", operation)?;
let bytes = input
.num_elements()
.checked_mul(4)
.ok_or_else(|| Error::invalid_argument(format!("{operation} byte extent overflows usize")))?;
shader_u32(bytes, "input byte extent", operation)?;
let output = Matrix::allocate_output(
input.engine_handle(),
input.shape().to_vec(),
input.num_elements(),
DType::U32,
)?;
if rows != 0 {
let kernel = KernelId::CryptographyPoseidon2Babybear24PermuteU32;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [PushConstant::U32(count), PushConstant::U32(input_elements)];
let inputs = [input];
let outputs = [&output];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(count),
},
SemanticDispatch {
contract,
inputs: &inputs,
outputs: &outputs,
attributes: &[],
},
)?;
}
Ok(output)
}
pub fn poseidon2_koalabear16(input: &Matrix, output_length: usize) -> Result<Matrix> {
let contract = crate::core::operation::cryptography::hash::POSEIDON2_KOALABEAR16;
let operation = contract.name();
let [rows, elements] = match input.shape() {
[rows, elements] => [*rows, *elements],
_ => {
return Err(Error::invalid_argument(format!(
"{operation} requires rank-two field rows"
)));
}
};
if input.dtype() != DType::U32 {
return Err(Error::invalid_argument(format!(
"{operation} requires U32 field elements"
)));
}
let output_length = if output_length == 0 { 8 } else { output_length };
let count = shader_u32(rows, "batch size", operation)?;
let input_elements = shader_u32(elements, "input elements", operation)?;
let output_elements = shader_u32(output_length, "output elements", operation)?;
let input_bytes = input.num_elements().checked_mul(4).ok_or_else(|| {
Error::invalid_argument(format!("{operation} input byte extent overflows usize"))
})?;
shader_u32(input_bytes, "input byte extent", operation)?;
let output_count = rows
.checked_mul(output_length)
.ok_or_else(|| Error::invalid_argument(format!("{operation} output extent overflows usize")))?;
let output_bytes = output_count.checked_mul(4).ok_or_else(|| {
Error::invalid_argument(format!("{operation} output byte extent overflows usize"))
})?;
shader_u32(output_bytes, "output byte extent", operation)?;
let output = Matrix::allocate_output(
input.engine_handle(),
vec![rows, output_length],
output_count,
DType::U32,
)?;
if rows != 0 {
let kernel = KernelId::CryptographyPoseidon2Koalabear16U32;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [
PushConstant::U32(count),
PushConstant::U32(input_elements),
PushConstant::U32(output_elements),
];
let inputs = [input];
let outputs = [&output];
let attributes = [OpAttribute::UnsignedInteger {
name: "output_length".into(),
value: output_length as u64,
}];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(count),
},
SemanticDispatch {
contract,
inputs: &inputs,
outputs: &outputs,
attributes: &attributes,
},
)?;
}
Ok(output)
}
pub fn poseidon2_koalabear16_permute(input: &Matrix) -> Result<Matrix> {
let contract = crate::core::operation::cryptography::hash::POSEIDON2_KOALABEAR16_PERMUTE;
let operation = contract.name();
let [rows, elements] = match input.shape() {
[rows, elements] => [*rows, *elements],
_ => {
return Err(Error::invalid_argument(format!(
"{operation} requires rank-two field rows"
)));
}
};
if input.dtype() != DType::U32 || elements != 16 {
return Err(Error::invalid_argument(format!(
"{operation} requires U32 rows of the permutation width"
)));
}
let count = shader_u32(rows, "batch size", operation)?;
let input_elements = shader_u32(elements, "input elements", operation)?;
let bytes = input
.num_elements()
.checked_mul(4)
.ok_or_else(|| Error::invalid_argument(format!("{operation} byte extent overflows usize")))?;
shader_u32(bytes, "input byte extent", operation)?;
let output = Matrix::allocate_output(
input.engine_handle(),
input.shape().to_vec(),
input.num_elements(),
DType::U32,
)?;
if rows != 0 {
let kernel = KernelId::CryptographyPoseidon2Koalabear16PermuteU32;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [PushConstant::U32(count), PushConstant::U32(input_elements)];
let inputs = [input];
let outputs = [&output];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(count),
},
SemanticDispatch {
contract,
inputs: &inputs,
outputs: &outputs,
attributes: &[],
},
)?;
}
Ok(output)
}
pub fn poseidon2_koalabear24(input: &Matrix, output_length: usize) -> Result<Matrix> {
let contract = crate::core::operation::cryptography::hash::POSEIDON2_KOALABEAR24;
let operation = contract.name();
let [rows, elements] = match input.shape() {
[rows, elements] => [*rows, *elements],
_ => {
return Err(Error::invalid_argument(format!(
"{operation} requires rank-two field rows"
)));
}
};
if input.dtype() != DType::U32 {
return Err(Error::invalid_argument(format!(
"{operation} requires U32 field elements"
)));
}
let output_length = if output_length == 0 { 8 } else { output_length };
let count = shader_u32(rows, "batch size", operation)?;
let input_elements = shader_u32(elements, "input elements", operation)?;
let output_elements = shader_u32(output_length, "output elements", operation)?;
let input_bytes = input.num_elements().checked_mul(4).ok_or_else(|| {
Error::invalid_argument(format!("{operation} input byte extent overflows usize"))
})?;
shader_u32(input_bytes, "input byte extent", operation)?;
let output_count = rows
.checked_mul(output_length)
.ok_or_else(|| Error::invalid_argument(format!("{operation} output extent overflows usize")))?;
let output_bytes = output_count.checked_mul(4).ok_or_else(|| {
Error::invalid_argument(format!("{operation} output byte extent overflows usize"))
})?;
shader_u32(output_bytes, "output byte extent", operation)?;
let output = Matrix::allocate_output(
input.engine_handle(),
vec![rows, output_length],
output_count,
DType::U32,
)?;
if rows != 0 {
let kernel = KernelId::CryptographyPoseidon2Koalabear24U32;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [
PushConstant::U32(count),
PushConstant::U32(input_elements),
PushConstant::U32(output_elements),
];
let inputs = [input];
let outputs = [&output];
let attributes = [OpAttribute::UnsignedInteger {
name: "output_length".into(),
value: output_length as u64,
}];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(count),
},
SemanticDispatch {
contract,
inputs: &inputs,
outputs: &outputs,
attributes: &attributes,
},
)?;
}
Ok(output)
}
pub fn poseidon2_koalabear24_permute(input: &Matrix) -> Result<Matrix> {
let contract = crate::core::operation::cryptography::hash::POSEIDON2_KOALABEAR24_PERMUTE;
let operation = contract.name();
let [rows, elements] = match input.shape() {
[rows, elements] => [*rows, *elements],
_ => {
return Err(Error::invalid_argument(format!(
"{operation} requires rank-two field rows"
)));
}
};
if input.dtype() != DType::U32 || elements != 24 {
return Err(Error::invalid_argument(format!(
"{operation} requires U32 rows of the permutation width"
)));
}
let count = shader_u32(rows, "batch size", operation)?;
let input_elements = shader_u32(elements, "input elements", operation)?;
let bytes = input
.num_elements()
.checked_mul(4)
.ok_or_else(|| Error::invalid_argument(format!("{operation} byte extent overflows usize")))?;
shader_u32(bytes, "input byte extent", operation)?;
let output = Matrix::allocate_output(
input.engine_handle(),
input.shape().to_vec(),
input.num_elements(),
DType::U32,
)?;
if rows != 0 {
let kernel = KernelId::CryptographyPoseidon2Koalabear24PermuteU32;
let buffers = [
BufferBinding::read(input.storage()),
BufferBinding::write(output.storage()),
];
let push_constants = [PushConstant::U32(count), PushConstant::U32(input_elements)];
let inputs = [input];
let outputs = [&output];
input.engine_handle().record_semantic(
ComputeDispatch {
kernel,
buffers: &buffers,
push_constants: &push_constants,
workgroups: kernel.linear_workgroups(count),
},
SemanticDispatch {
contract,
inputs: &inputs,
outputs: &outputs,
attributes: &[],
},
)?;
}
Ok(output)
}
fn validate_byte_rows(input: &Matrix, operation: &'static str) -> Result<[usize; 2]> {
if input.dtype() != DType::U8 {
return Err(Error::invalid_argument(format!(
"{operation} requires a U8 Matrix; found {}",
input.dtype().token()
)));
}
let [rows, columns] = input.shape() else {
return Err(Error::invalid_argument(format!(
"{operation} requires rank-2 [batch, bytes] input; found {:?}",
input.shape()
)));
};
if *rows == 0 {
return Err(Error::invalid_argument(format!(
"{operation} requires a non-zero batch size"
)));
}
shader_u32(input.num_elements(), "input byte size", operation)?;
Ok([*rows, *columns])
}
fn shader_u32(value: usize, label: &str, operation: &'static str) -> Result<u32> {
u32::try_from(value)
.map_err(|_| Error::invalid_argument(format!("{operation} {label} exceeds u32")))
}