-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathlib.rs
198 lines (166 loc) · 6.47 KB
/
lib.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
#![warn(unused_crate_dependencies, unused_extern_crates)]
#![warn(unreachable_pub)]
use std::{collections::HashSet, path::PathBuf};
mod cli;
mod download;
mod proof_system;
mod smart_contract;
use acvm::acir::circuit::Opcode;
use bb_abstraction_leaks::ACVM_BACKEND_BARRETENBERG;
use bb_abstraction_leaks::BB_VERSION;
use cli::VersionCommand;
pub use download::download_backend;
const BACKENDS_DIR: &str = ".nargo/backends";
pub fn backends_directory() -> PathBuf {
let home_directory = dirs::home_dir().unwrap();
home_directory.join(BACKENDS_DIR)
}
#[cfg(test)]
test_binary::build_test_binary_once!(mock_backend, "test-binaries");
#[cfg(test)]
fn get_mock_backend() -> Result<Backend, BackendError> {
std::env::set_var("NARGO_BACKEND_PATH", path_to_mock_backend());
let mock_backend = Backend::new("mock_backend".to_string());
mock_backend.assert_binary_exists()?;
Ok(mock_backend)
}
#[derive(Debug, thiserror::Error)]
pub enum BackendError {
#[error(transparent)]
IoError(#[from] std::io::Error),
#[error("Backend binary does not exist")]
MissingBinary,
#[error("The backend responded with a malformed UTF8 byte vector: {0:?}")]
InvalidUTF8Vector(Vec<u8>),
#[error(
"The backend responded with a unexpected number of bytes. Expected: {0} but got {} ({1:?})", .1.len()
)]
UnexpectedNumberOfBytes(usize, Vec<u8>),
#[error("The backend encountered an error: {0:?}")]
CommandFailed(String),
}
#[derive(Debug)]
pub struct Backend {
name: String,
binary_path: PathBuf,
}
impl Backend {
pub fn new(name: String) -> Backend {
let binary_path = if let Some(binary_path) = std::env::var_os("NARGO_BACKEND_PATH") {
PathBuf::from(binary_path)
} else {
const BINARY_NAME: &str = "backend_binary";
backends_directory().join(&name).join(BINARY_NAME)
};
Backend { name, binary_path }
}
pub fn name(&self) -> &str {
&self.name
}
fn binary_path(&self) -> &PathBuf {
&self.binary_path
}
fn assert_binary_exists(&self) -> Result<&PathBuf, BackendError> {
let binary_path = self.binary_path();
if binary_path.is_file() {
Ok(binary_path)
} else {
if self.name == ACVM_BACKEND_BARRETENBERG {
// If we're trying to use barretenberg, automatically go and install it.
let bb_url = std::env::var("BB_BINARY_URL")
.unwrap_or_else(|_| bb_abstraction_leaks::BB_DOWNLOAD_URL.to_owned());
download_backend(&bb_url, binary_path)?;
return Ok(binary_path);
}
Err(BackendError::MissingBinary)
}
}
fn backend_directory(&self) -> PathBuf {
self.binary_path()
.parent()
.expect("backend binary should have a parent directory")
.to_path_buf()
}
fn crs_directory(&self) -> PathBuf {
self.backend_directory().join("crs")
}
fn assert_correct_version(&self) -> Result<&PathBuf, BackendError> {
let binary_path = self.binary_path();
if binary_path.to_string_lossy().contains(ACVM_BACKEND_BARRETENBERG) {
match VersionCommand.run(binary_path) {
// If version matches then do nothing.
Ok(version_string) if version_string == BB_VERSION => (),
// If version doesn't match then download the correct version.
Ok(version_string) => {
println!("`{ACVM_BACKEND_BARRETENBERG}` version `{version_string}` is different from expected `{BB_VERSION}`. Downloading expected version...");
let bb_url = std::env::var("BB_BINARY_URL")
.unwrap_or_else(|_| bb_abstraction_leaks::BB_DOWNLOAD_URL.to_owned());
download_backend(&bb_url, binary_path)?;
}
// If `bb` fails to report its version, then attempt to fix it by re-downloading the binary.
Err(_) => {
println!("Could not determine version of `{ACVM_BACKEND_BARRETENBERG}`. Downloading expected version...");
let bb_url = std::env::var("BB_BINARY_URL")
.unwrap_or_else(|_| bb_abstraction_leaks::BB_DOWNLOAD_URL.to_owned());
download_backend(&bb_url, binary_path)?;
}
}
}
Ok(binary_path)
}
}
pub struct BackendOpcodeSupport {
opcodes: HashSet<String>,
black_box_functions: HashSet<String>,
}
impl BackendOpcodeSupport {
pub fn is_opcode_supported(&self, opcode: &Opcode) -> bool {
match opcode {
Opcode::Arithmetic(_) => self.opcodes.contains("arithmetic"),
Opcode::Directive(_) => self.opcodes.contains("directive"),
Opcode::Brillig(_) => self.opcodes.contains("brillig"),
Opcode::MemoryInit { .. } => self.opcodes.contains("memory_init"),
Opcode::MemoryOp { .. } => self.opcodes.contains("memory_op"),
Opcode::BlackBoxFuncCall(func) => {
self.black_box_functions.contains(func.get_black_box_func().name())
}
}
}
pub fn all() -> BackendOpcodeSupport {
BackendOpcodeSupport {
opcodes: HashSet::from([
"arithmetic".to_string(),
"directive".to_string(),
"brillig".to_string(),
"memory_init".to_string(),
"memory_op".to_string(),
]),
black_box_functions: HashSet::from([
"sha256".to_string(),
"schnorr_verify".to_string(),
"blake2s".to_string(),
"pedersen".to_string(),
"pedersen_hash".to_string(),
"hash_to_field_128_security".to_string(),
"ecdsa_secp256k1".to_string(),
"fixed_base_scalar_mul".to_string(),
"and".to_string(),
"xor".to_string(),
"range".to_string(),
"keccak256".to_string(),
"recursive_aggregation".to_string(),
"ecdsa_secp256r1".to_string(),
]),
}
}
}
#[cfg(test)]
mod backend {
use crate::{Backend, BackendError};
#[test]
fn raises_error_on_missing_binary() {
let bad_backend = Backend::new("i_dont_exist".to_string());
let binary_path = bad_backend.assert_binary_exists();
assert!(matches!(binary_path, Err(BackendError::MissingBinary)));
}
}