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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use super::DeserializationError;
use vm_core::{
crypto::hash::{Blake3_192, Blake3_256, Hasher, Rpo256},
utils::collections::Vec,
};
use winter_air::{proof::StarkProof, FieldExtension, ProofOptions as WinterProofOptions};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExecutionProof {
proof: StarkProof,
hash_fn: HashFunction,
}
impl ExecutionProof {
pub const fn new(proof: StarkProof, hash_fn: HashFunction) -> Self {
Self { proof, hash_fn }
}
pub const fn stark_proof(&self) -> &StarkProof {
&self.proof
}
pub const fn hash_fn(&self) -> HashFunction {
self.hash_fn
}
pub fn security_level(&self) -> u32 {
match self.hash_fn {
HashFunction::Blake3_192 => self.proof.security_level::<Blake3_192>(true),
HashFunction::Blake3_256 => self.proof.security_level::<Blake3_256>(true),
HashFunction::Rpo256 => self.proof.security_level::<Rpo256>(true),
}
}
pub fn to_bytes(&self) -> Vec<u8> {
let mut bytes = self.proof.to_bytes();
assert!(!bytes.is_empty(), "invalid STARK proof");
bytes.insert(0, self.hash_fn as u8);
bytes
}
pub fn from_bytes(source: &[u8]) -> Result<Self, DeserializationError> {
if source.len() < 2 {
return Err(DeserializationError::UnexpectedEOF);
}
let hash_fn = HashFunction::try_from(source[0])?;
let proof = StarkProof::from_bytes(&source[1..])?;
Ok(Self::new(proof, hash_fn))
}
pub fn into_parts(self) -> (HashFunction, StarkProof) {
(self.hash_fn, self.proof)
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ProofOptions {
options: WinterProofOptions,
hash_fn: HashFunction,
}
impl ProofOptions {
pub fn new(
num_queries: usize,
blowup_factor: usize,
grinding_factor: u32,
field_extension: FieldExtension,
fri_folding_factor: usize,
fri_max_remainder_size: usize,
hash_fn: HashFunction,
) -> Self {
let options = WinterProofOptions::new(
num_queries,
blowup_factor,
grinding_factor,
field_extension,
fri_folding_factor,
fri_max_remainder_size,
);
Self { options, hash_fn }
}
pub fn with_96_bit_security() -> Self {
let options = WinterProofOptions::new(27, 8, 16, FieldExtension::Quadratic, 8, 255);
Self {
hash_fn: HashFunction::Blake3_192,
options,
}
}
pub fn with_128_bit_security() -> Self {
let options = WinterProofOptions::new(27, 16, 21, FieldExtension::Cubic, 8, 255);
Self {
hash_fn: HashFunction::Blake3_256,
options,
}
}
pub const fn hash_fn(&self) -> HashFunction {
self.hash_fn
}
}
impl Default for ProofOptions {
fn default() -> Self {
Self::with_96_bit_security()
}
}
impl From<ProofOptions> for WinterProofOptions {
fn from(options: ProofOptions) -> Self {
options.options
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum HashFunction {
Blake3_192 = 0x00,
Blake3_256 = 0x01,
Rpo256 = 0x02,
}
impl Default for HashFunction {
fn default() -> Self {
Self::Blake3_192
}
}
impl HashFunction {
pub const fn collision_resistance(&self) -> u32 {
match self {
HashFunction::Blake3_192 => Blake3_192::COLLISION_RESISTANCE,
HashFunction::Blake3_256 => Blake3_256::COLLISION_RESISTANCE,
HashFunction::Rpo256 => Rpo256::COLLISION_RESISTANCE,
}
}
}
impl TryFrom<u8> for HashFunction {
type Error = DeserializationError;
fn try_from(repr: u8) -> Result<Self, Self::Error> {
match repr {
0x00 => Ok(Self::Blake3_192),
0x01 => Ok(Self::Blake3_256),
0x02 => Ok(Self::Rpo256),
_ => Err(DeserializationError::InvalidValue(format!(
"the hash function representation {repr} is not valid!"
))),
}
}
}