sonobe/solidity-verifiers/src/utils/mod.rs
solidity-verifiers/src/utils/mod.rsRaw
1use crate::{GPL3_SDPX_IDENTIFIER, PRAGMA_GROTH16_VERIFIER};
2use askama::Template;
3use crypto::{digest::Digest, sha3::Sha3};
4use num_bigint::BigUint;
5pub mod encoding;
6
7/// Formats call data from a vec of bytes to a hashmap
8/// Useful for debugging directly on the EVM
9/// !! Should follow the contract's function signature, we assuming the order of arguments is correct
10pub fn get_formatted_calldata(calldata: Vec<u8>) -> Vec<String> {
11 let mut formatted_calldata = vec![];
12 for i in (4..calldata.len()).step_by(32) {
13 let val = BigUint::from_bytes_be(&calldata[i..i + 32]);
14 formatted_calldata.push(format!("{}", val));
15 }
16 formatted_calldata
17}
18
19/// Computes the function selector for the nova cyclefold verifier
20/// It is computed on the fly since it depends on the length of the first parameter array
21pub fn get_function_selector_for_nova_cyclefold_verifier(
22 first_param_array_length: usize,
23) -> [u8; 4] {
24 let mut hasher = Sha3::keccak256();
25 let fn_sig = format!("verifyNovaProof(uint256[{}],uint256[4],uint256[2],uint256[3],uint256[2],uint256[2][2],uint256[2],uint256[4],uint256[2][2])", first_param_array_length);
26 hasher.input_str(&fn_sig);
27 let hash = &mut [0u8; 32];
28 hasher.result(hash);
29 [hash[0], hash[1], hash[2], hash[3]]
30}
31
32#[derive(Template)]
33#[template(path = "header_template.askama.sol", ext = "sol")]
34pub struct HeaderInclusion<T: Template> {
35 /// SPDX-License-Identifier
36 pub sdpx: String,
37 /// The `pragma` statement.
38 pub pragma_version: String,
39 /// The template to render alongside the header.
40 pub template: T,
41}
42
43impl<T: Template + Default> HeaderInclusion<T> {
44 pub fn builder() -> HeaderInclusionBuilder<T> {
45 HeaderInclusionBuilder::default()
46 }
47}
48
49#[derive(Debug)]
50pub struct HeaderInclusionBuilder<T: Template + Default> {
51 /// SPDX-License-Identifier
52 sdpx: String,
53 /// The `pragma` statement.
54 pragma_version: String,
55 /// The template to render alongside the header.
56 template: T,
57}
58
59impl<T: Template + Default> Default for HeaderInclusionBuilder<T> {
60 fn default() -> Self {
61 Self {
62 sdpx: GPL3_SDPX_IDENTIFIER.to_string(),
63 pragma_version: PRAGMA_GROTH16_VERIFIER.to_string(),
64 template: T::default(),
65 }
66 }
67}
68
69impl<T: Template + Default> HeaderInclusionBuilder<T> {
70 pub fn sdpx<S: Into<String>>(mut self, sdpx: S) -> Self {
71 self.sdpx = sdpx.into();
72 self
73 }
74
75 pub fn pragma_version<S: Into<String>>(mut self, pragma_version: S) -> Self {
76 self.pragma_version = pragma_version.into();
77 self
78 }
79
80 pub fn template(mut self, template: impl Into<T>) -> Self {
81 self.template = template.into();
82 self
83 }
84
85 pub fn build(self) -> HeaderInclusion<T> {
86 HeaderInclusion {
87 sdpx: self.sdpx,
88 pragma_version: self.pragma_version,
89 template: self.template,
90 }
91 }
92}