-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
cost.rs
379 lines (353 loc) · 19.8 KB
/
cost.rs
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the snarkVM library.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::{Process, Stack, StackProgramTypes};
use console::{
prelude::*,
program::{FinalizeType, Identifier, LiteralType, PlaintextType},
};
use ledger_block::{Deployment, Execution};
use synthesizer_program::{CastType, Command, Finalize, Instruction, Operand, StackProgram};
/// Returns the *minimum* cost in microcredits to publish the given deployment (total cost, (storage cost, synthesis cost, namespace cost)).
pub fn deployment_cost<N: Network>(deployment: &Deployment<N>) -> Result<(u64, (u64, u64, u64))> {
// Determine the number of bytes in the deployment.
let size_in_bytes = deployment.size_in_bytes()?;
// Retrieve the program ID.
let program_id = deployment.program_id();
// Determine the number of characters in the program ID.
let num_characters = u32::try_from(program_id.name().to_string().len())?;
// Compute the number of combined constraints in the program.
let num_combined_constraints = deployment.num_combined_constraints()?;
// Compute the storage cost in microcredits.
let storage_cost = size_in_bytes
.checked_mul(N::DEPLOYMENT_FEE_MULTIPLIER)
.ok_or(anyhow!("The storage cost computation overflowed for a deployment"))?;
// Compute the synthesis cost in microcredits.
let synthesis_cost = num_combined_constraints * N::SYNTHESIS_FEE_MULTIPLIER;
// Compute the namespace cost in credits: 10^(10 - num_characters).
let namespace_cost = 10u64
.checked_pow(10u32.saturating_sub(num_characters))
.ok_or(anyhow!("The namespace cost computation overflowed for a deployment"))?
.saturating_mul(1_000_000); // 1 microcredit = 1e-6 credits.
// Compute the total cost in microcredits.
let total_cost = storage_cost
.checked_add(synthesis_cost)
.and_then(|x| x.checked_add(namespace_cost))
.ok_or(anyhow!("The total cost computation overflowed for a deployment"))?;
Ok((total_cost, (storage_cost, synthesis_cost, namespace_cost)))
}
/// Returns the *minimum* cost in microcredits to publish the given execution (total cost, (storage cost, finalize cost)).
pub fn execution_cost<N: Network>(process: &Process<N>, execution: &Execution<N>) -> Result<(u64, (u64, u64))> {
// Compute the storage cost in microcredits.
let storage_cost = execution.size_in_bytes()?;
// Compute the finalize cost in microcredits.
let mut finalize_cost = 0u64;
// Iterate over the transitions to accumulate the finalize cost.
for transition in execution.transitions() {
// Retrieve the program ID and function name.
let (program_id, function_name) = (transition.program_id(), transition.function_name());
// Retrieve the finalize cost.
let cost = cost_in_microcredits(process.get_stack(program_id)?, function_name)?;
// Accumulate the finalize cost.
if cost > 0 {
finalize_cost = finalize_cost
.checked_add(cost)
.ok_or(anyhow!("The finalize cost computation overflowed on '{program_id}/{function_name}'"))?;
}
}
// Compute the total cost in microcredits.
let total_cost = storage_cost
.checked_add(finalize_cost)
.ok_or(anyhow!("The total cost computation overflowed for an execution"))?;
Ok((total_cost, (storage_cost, finalize_cost)))
}
/// Returns the minimum number of microcredits required to run the finalize.
pub fn cost_in_microcredits<N: Network>(stack: &Stack<N>, function_name: &Identifier<N>) -> Result<u64> {
/// A helper function to determine the plaintext type in bytes.
fn plaintext_size_in_bytes<N: Network>(stack: &Stack<N>, plaintext_type: &PlaintextType<N>) -> Result<u64> {
match plaintext_type {
PlaintextType::Literal(literal_type) => Ok(literal_type.size_in_bytes::<N>() as u64),
PlaintextType::Struct(struct_name) => {
// Retrieve the struct from the stack.
let struct_ = stack.program().get_struct(struct_name)?;
// Retrieve the size of the struct name.
let size_of_name = struct_.name().to_bytes_le()?.len() as u64;
// Retrieve the size of all the members of the struct.
let size_of_members = struct_.members().iter().try_fold(0u64, |acc, (_, member_type)| {
acc.checked_add(plaintext_size_in_bytes(stack, member_type)?).ok_or(anyhow!(
"Overflowed while computing the size of the struct '{}/{struct_name}' - {member_type}",
stack.program_id()
))
})?;
// Return the size of the struct.
Ok(size_of_name.saturating_add(size_of_members))
}
PlaintextType::Array(array_type) => {
// Retrieve the number of elements in the array.
let num_elements = **array_type.length() as u64;
// Compute the size of an array element.
let size_of_element = plaintext_size_in_bytes(stack, array_type.next_element_type())?;
// Return the size of the array.
Ok(num_elements.saturating_mul(size_of_element))
}
}
}
/// A helper function to compute the following: base_cost + (byte_multiplier * size_of_operands).
fn cost_in_size<'a, N: Network>(
stack: &Stack<N>,
finalize: &Finalize<N>,
operands: impl IntoIterator<Item = &'a Operand<N>>,
byte_multiplier: u64,
base_cost: u64,
) -> Result<u64> {
// Retrieve the finalize types.
let finalize_types = stack.get_finalize_types(finalize.name())?;
// Compute the size of the operands.
let size_of_operands = operands.into_iter().try_fold(0u64, |acc, operand| {
// Determine the size of the operand.
let operand_size = match finalize_types.get_type_from_operand(stack, operand)? {
FinalizeType::Plaintext(plaintext_type) => plaintext_size_in_bytes(stack, &plaintext_type)?,
FinalizeType::Future(future) => {
bail!("Future '{future}' is not a valid operand in the finalize scope");
}
};
// Safely add the size to the accumulator.
acc.checked_add(operand_size).ok_or(anyhow!(
"Overflowed while computing the size of the operand '{operand}' in '{}/{}' (finalize)",
stack.program_id(),
finalize.name()
))
})?;
// Return the cost.
Ok(base_cost.saturating_add(byte_multiplier.saturating_mul(size_of_operands)))
}
// Finalize costs for compute heavy operations, derived as:
// `BASE_COST + (PER_BYTE_COST * SIZE_IN_BYTES)`.
const CAST_BASE_COST: u64 = 500;
const CAST_PER_BYTE_COST: u64 = 30;
const HASH_BASE_COST: u64 = 10_000;
const HASH_PER_BYTE_COST: u64 = 30;
const HASH_BHP_BASE_COST: u64 = 50_000;
const HASH_BHP_PER_BYTE_COST: u64 = 300;
const HASH_PSD_BASE_COST: u64 = 40_000;
const HASH_PSD_PER_BYTE_COST: u64 = 75;
const MAPPING_BASE_COST: u64 = 10_000;
const MAPPING_PER_BYTE_COST: u64 = 10;
const SET_BASE_COST: u64 = 10_000;
const SET_PER_BYTE_COST: u64 = 100;
// Retrieve the finalize logic.
let Some(finalize) = stack.get_function_ref(function_name)?.finalize_logic() else {
// Return a finalize cost of 0, if the function does not have a finalize scope.
return Ok(0);
};
// Measure the cost of each command.
let cost = |command: &Command<N>| match command {
Command::Instruction(Instruction::Abs(_)) => Ok(500),
Command::Instruction(Instruction::AbsWrapped(_)) => Ok(500),
Command::Instruction(Instruction::Add(_)) => Ok(500),
Command::Instruction(Instruction::AddWrapped(_)) => Ok(500),
Command::Instruction(Instruction::And(_)) => Ok(500),
Command::Instruction(Instruction::AssertEq(_)) => Ok(500),
Command::Instruction(Instruction::AssertNeq(_)) => Ok(500),
Command::Instruction(Instruction::Async(_)) => bail!("'async' is not supported in finalize"),
Command::Instruction(Instruction::Call(_)) => bail!("'call' is not supported in finalize"),
Command::Instruction(Instruction::Cast(cast)) => match cast.cast_type() {
CastType::Plaintext(PlaintextType::Literal(_)) => Ok(500),
CastType::Plaintext(plaintext_type) => Ok(plaintext_size_in_bytes(stack, plaintext_type)?
.saturating_mul(CAST_PER_BYTE_COST)
.saturating_add(CAST_BASE_COST)),
CastType::GroupXCoordinate
| CastType::GroupYCoordinate
| CastType::Record(_)
| CastType::ExternalRecord(_) => Ok(500),
},
Command::Instruction(Instruction::CastLossy(cast_lossy)) => match cast_lossy.cast_type() {
CastType::Plaintext(PlaintextType::Literal(_)) => Ok(500),
CastType::Plaintext(plaintext_type) => Ok(plaintext_size_in_bytes(stack, plaintext_type)?
.saturating_mul(CAST_PER_BYTE_COST)
.saturating_add(CAST_BASE_COST)),
CastType::GroupXCoordinate
| CastType::GroupYCoordinate
| CastType::Record(_)
| CastType::ExternalRecord(_) => Ok(500),
},
Command::Instruction(Instruction::CommitBHP256(commit)) => {
cost_in_size(stack, finalize, commit.operands(), HASH_BHP_PER_BYTE_COST, HASH_BHP_BASE_COST)
}
Command::Instruction(Instruction::CommitBHP512(commit)) => {
cost_in_size(stack, finalize, commit.operands(), HASH_BHP_PER_BYTE_COST, HASH_BHP_BASE_COST)
}
Command::Instruction(Instruction::CommitBHP768(commit)) => {
cost_in_size(stack, finalize, commit.operands(), HASH_BHP_PER_BYTE_COST, HASH_BHP_BASE_COST)
}
Command::Instruction(Instruction::CommitBHP1024(commit)) => {
cost_in_size(stack, finalize, commit.operands(), HASH_BHP_PER_BYTE_COST, HASH_BHP_BASE_COST)
}
Command::Instruction(Instruction::CommitPED64(commit)) => {
cost_in_size(stack, finalize, commit.operands(), HASH_PER_BYTE_COST, HASH_BASE_COST)
}
Command::Instruction(Instruction::CommitPED128(commit)) => {
cost_in_size(stack, finalize, commit.operands(), HASH_PER_BYTE_COST, HASH_BASE_COST)
}
Command::Instruction(Instruction::Div(div)) => {
// Ensure `div` has exactly two operands.
ensure!(div.operands().len() == 2, "'div' must contain exactly 2 operands");
// Retrieve the finalize types.
let finalize_types = stack.get_finalize_types(finalize.name())?;
// Retrieve the price by the operand type.
match finalize_types.get_type_from_operand(stack, &div.operands()[0])? {
FinalizeType::Plaintext(PlaintextType::Literal(LiteralType::Field)) => Ok(1_500),
FinalizeType::Plaintext(PlaintextType::Literal(_)) => Ok(500),
FinalizeType::Plaintext(PlaintextType::Array(_)) => bail!("'div' does not support arrays"),
FinalizeType::Plaintext(PlaintextType::Struct(_)) => bail!("'div' does not support structs"),
FinalizeType::Future(_) => bail!("'div' does not support futures"),
}
}
Command::Instruction(Instruction::DivWrapped(_)) => Ok(500),
Command::Instruction(Instruction::Double(_)) => Ok(500),
Command::Instruction(Instruction::GreaterThan(_)) => Ok(500),
Command::Instruction(Instruction::GreaterThanOrEqual(_)) => Ok(500),
Command::Instruction(Instruction::HashBHP256(hash)) => {
cost_in_size(stack, finalize, hash.operands(), HASH_BHP_PER_BYTE_COST, HASH_BHP_BASE_COST)
}
Command::Instruction(Instruction::HashBHP512(hash)) => {
cost_in_size(stack, finalize, hash.operands(), HASH_BHP_PER_BYTE_COST, HASH_BHP_BASE_COST)
}
Command::Instruction(Instruction::HashBHP768(hash)) => {
cost_in_size(stack, finalize, hash.operands(), HASH_BHP_PER_BYTE_COST, HASH_BHP_BASE_COST)
}
Command::Instruction(Instruction::HashBHP1024(hash)) => {
cost_in_size(stack, finalize, hash.operands(), HASH_BHP_PER_BYTE_COST, HASH_BHP_BASE_COST)
}
Command::Instruction(Instruction::HashKeccak256(hash)) => {
cost_in_size(stack, finalize, hash.operands(), HASH_PER_BYTE_COST, HASH_BASE_COST)
}
Command::Instruction(Instruction::HashKeccak384(hash)) => {
cost_in_size(stack, finalize, hash.operands(), HASH_PER_BYTE_COST, HASH_BASE_COST)
}
Command::Instruction(Instruction::HashKeccak512(hash)) => {
cost_in_size(stack, finalize, hash.operands(), HASH_PER_BYTE_COST, HASH_BASE_COST)
}
Command::Instruction(Instruction::HashPED64(hash)) => {
cost_in_size(stack, finalize, hash.operands(), HASH_PER_BYTE_COST, HASH_BASE_COST)
}
Command::Instruction(Instruction::HashPED128(hash)) => {
cost_in_size(stack, finalize, hash.operands(), HASH_PER_BYTE_COST, HASH_BASE_COST)
}
Command::Instruction(Instruction::HashPSD2(hash)) => {
cost_in_size(stack, finalize, hash.operands(), HASH_PSD_PER_BYTE_COST, HASH_PSD_BASE_COST)
}
Command::Instruction(Instruction::HashPSD4(hash)) => {
cost_in_size(stack, finalize, hash.operands(), HASH_PSD_PER_BYTE_COST, HASH_PSD_BASE_COST)
}
Command::Instruction(Instruction::HashPSD8(hash)) => {
cost_in_size(stack, finalize, hash.operands(), HASH_PSD_PER_BYTE_COST, HASH_PSD_BASE_COST)
}
Command::Instruction(Instruction::HashSha3_256(hash)) => {
cost_in_size(stack, finalize, hash.operands(), HASH_PER_BYTE_COST, HASH_BASE_COST)
}
Command::Instruction(Instruction::HashSha3_384(hash)) => {
cost_in_size(stack, finalize, hash.operands(), HASH_PER_BYTE_COST, HASH_BASE_COST)
}
Command::Instruction(Instruction::HashSha3_512(hash)) => {
cost_in_size(stack, finalize, hash.operands(), HASH_PER_BYTE_COST, HASH_BASE_COST)
}
Command::Instruction(Instruction::HashManyPSD2(_)) => {
bail!("`hash_many.psd2` is not supported in finalize")
}
Command::Instruction(Instruction::HashManyPSD4(_)) => {
bail!("`hash_many.psd4` is not supported in finalize")
}
Command::Instruction(Instruction::HashManyPSD8(_)) => {
bail!("`hash_many.psd8` is not supported in finalize")
}
Command::Instruction(Instruction::Inv(_)) => Ok(2_500),
Command::Instruction(Instruction::IsEq(_)) => Ok(500),
Command::Instruction(Instruction::IsNeq(_)) => Ok(500),
Command::Instruction(Instruction::LessThan(_)) => Ok(500),
Command::Instruction(Instruction::LessThanOrEqual(_)) => Ok(500),
Command::Instruction(Instruction::Modulo(_)) => Ok(500),
Command::Instruction(Instruction::Mul(mul)) => {
// Ensure `mul` has exactly two operands.
ensure!(mul.operands().len() == 2, "'mul' must contain exactly 2 operands");
// Retrieve the finalize types.
let finalize_types = stack.get_finalize_types(finalize.name())?;
// Retrieve the price by operand type.
match finalize_types.get_type_from_operand(stack, &mul.operands()[0])? {
FinalizeType::Plaintext(PlaintextType::Literal(LiteralType::Group)) => Ok(10_000),
FinalizeType::Plaintext(PlaintextType::Literal(LiteralType::Scalar)) => Ok(10_000),
FinalizeType::Plaintext(PlaintextType::Literal(_)) => Ok(500),
FinalizeType::Plaintext(PlaintextType::Array(_)) => bail!("'mul' does not support arrays"),
FinalizeType::Plaintext(PlaintextType::Struct(_)) => bail!("'mul' does not support structs"),
FinalizeType::Future(_) => bail!("'mul' does not support futures"),
}
}
Command::Instruction(Instruction::MulWrapped(_)) => Ok(500),
Command::Instruction(Instruction::Nand(_)) => Ok(500),
Command::Instruction(Instruction::Neg(_)) => Ok(500),
Command::Instruction(Instruction::Nor(_)) => Ok(500),
Command::Instruction(Instruction::Not(_)) => Ok(500),
Command::Instruction(Instruction::Or(_)) => Ok(500),
Command::Instruction(Instruction::Pow(pow)) => {
// Ensure `pow` has at least one operand.
ensure!(!pow.operands().is_empty(), "'pow' must contain at least 1 operand");
// Retrieve the finalize types.
let finalize_types = stack.get_finalize_types(finalize.name())?;
// Retrieve the price by operand type.
match finalize_types.get_type_from_operand(stack, &pow.operands()[0])? {
FinalizeType::Plaintext(PlaintextType::Literal(LiteralType::Field)) => Ok(1_500),
FinalizeType::Plaintext(PlaintextType::Literal(_)) => Ok(500),
FinalizeType::Plaintext(PlaintextType::Array(_)) => bail!("'pow' does not support arrays"),
FinalizeType::Plaintext(PlaintextType::Struct(_)) => bail!("'pow' does not support structs"),
FinalizeType::Future(_) => bail!("'pow' does not support futures"),
}
}
Command::Instruction(Instruction::PowWrapped(_)) => Ok(500),
Command::Instruction(Instruction::Rem(_)) => Ok(500),
Command::Instruction(Instruction::RemWrapped(_)) => Ok(500),
Command::Instruction(Instruction::SignVerify(sign)) => {
cost_in_size(stack, finalize, sign.operands(), HASH_PSD_PER_BYTE_COST, HASH_PSD_BASE_COST)
}
Command::Instruction(Instruction::Shl(_)) => Ok(500),
Command::Instruction(Instruction::ShlWrapped(_)) => Ok(500),
Command::Instruction(Instruction::Shr(_)) => Ok(500),
Command::Instruction(Instruction::ShrWrapped(_)) => Ok(500),
Command::Instruction(Instruction::Square(_)) => Ok(500),
Command::Instruction(Instruction::SquareRoot(_)) => Ok(2_500),
Command::Instruction(Instruction::Sub(_)) => Ok(500),
Command::Instruction(Instruction::SubWrapped(_)) => Ok(500),
Command::Instruction(Instruction::Ternary(_)) => Ok(500),
Command::Instruction(Instruction::Xor(_)) => Ok(500),
Command::Await(_) => Ok(500),
Command::Contains(command) => {
cost_in_size(stack, finalize, [command.key()], MAPPING_PER_BYTE_COST, MAPPING_BASE_COST)
}
Command::Get(command) => {
cost_in_size(stack, finalize, [command.key()], MAPPING_PER_BYTE_COST, MAPPING_BASE_COST)
}
Command::GetOrUse(command) => {
cost_in_size(stack, finalize, [command.key()], MAPPING_PER_BYTE_COST, MAPPING_BASE_COST)
}
Command::RandChaCha(_) => Ok(25_000),
Command::Remove(_) => Ok(MAPPING_BASE_COST),
Command::Set(command) => {
cost_in_size(stack, finalize, [command.key(), command.value()], SET_PER_BYTE_COST, SET_BASE_COST)
}
Command::BranchEq(_) | Command::BranchNeq(_) => Ok(500),
Command::Position(_) => Ok(100),
};
// Aggregate the cost of all commands in the program.
finalize
.commands()
.iter()
.map(cost)
.try_fold(0u64, |acc, res| res.and_then(|x| acc.checked_add(x).ok_or(anyhow!("Finalize cost overflowed"))))
}