use super::{
super::{ToElements, WORD_SIZE},
ByteReader, ByteWriter, Deserializable, DeserializationError, Digest, Felt, Kernel, Program,
Serializable, Vec,
};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ProgramInfo {
program_hash: Digest,
kernel: Kernel,
}
impl ProgramInfo {
pub const fn new(program_hash: Digest, kernel: Kernel) -> Self {
Self {
program_hash,
kernel,
}
}
pub const fn program_hash(&self) -> &Digest {
&self.program_hash
}
pub const fn kernel(&self) -> &Kernel {
&self.kernel
}
pub fn kernel_procedures(&self) -> &[Digest] {
self.kernel.proc_hashes()
}
}
impl From<Program> for ProgramInfo {
fn from(program: Program) -> Self {
let Program { root, kernel, .. } = program;
let program_hash = root.hash();
Self {
program_hash,
kernel,
}
}
}
impl Serializable for ProgramInfo {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.program_hash.write_into(target);
<Kernel as Serializable>::write_into(&self.kernel, target);
}
}
impl Deserializable for ProgramInfo {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let program_hash = source.read()?;
let kernel = source.read()?;
Ok(Self {
program_hash,
kernel,
})
}
}
impl ToElements<Felt> for ProgramInfo {
fn to_elements(&self) -> Vec<Felt> {
let num_kernel_proc_elements = self.kernel.proc_hashes().len() * WORD_SIZE;
let mut result = Vec::with_capacity(WORD_SIZE + num_kernel_proc_elements);
result.extend_from_slice(self.program_hash.as_elements());
for proc_hash in self.kernel.proc_hashes() {
result.extend_from_slice(proc_hash.as_elements());
}
result
}
}