1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use crate::{
op_sponge,
opcodes::{OpHint, UserOps as OpCode},
BaseElement, FieldElement, BASE_CYCLE_LENGTH, HACC_NUM_ROUNDS, MAX_PUBLIC_INPUTS,
OP_SPONGE_WIDTH, PROGRAM_DIGEST_SIZE,
};
use core::fmt;
pub mod blocks;
use blocks::{Group, ProgramBlock};
mod inputs;
pub use inputs::ProgramInputs;
mod hashing;
use hashing::{hash_acc, hash_op, hash_seq};
#[cfg(test)]
mod tests;
#[derive(Clone)]
pub struct Program {
root: Group,
hash: [u8; 32],
}
impl Program {
pub fn new(root: Group) -> Program {
match &root.body()[0] {
ProgramBlock::Span(block) => {
let (op_code, _) = block.get_op(0);
assert!(
op_code == OpCode::Begin,
"a program must start with BEGIN operation"
);
}
_ => panic!("a program must start with a Span block"),
}
let (v0, v1) = root.get_hash();
let hash = hash_acc(BaseElement::ZERO, v0, v1);
let mut hash_bytes = [0u8; 32];
hash_bytes.copy_from_slice(BaseElement::elements_as_bytes(&hash[..PROGRAM_DIGEST_SIZE]));
Program {
root,
hash: hash_bytes,
}
}
pub fn root(&self) -> &Group {
&self.root
}
pub fn hash(&self) -> &[u8; 32] {
&self.hash
}
}
impl fmt::Debug for Program {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut body_code = format!("{:?}", self.root);
body_code.replace_range(..6, "");
write!(f, "{}", body_code)?;
Ok(())
}
}