-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: pull
Language
and Opcode
support from backend
- Loading branch information
1 parent
710132b
commit bec3cb2
Showing
7 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use acvm::acir::circuit::opcodes::Opcode; | ||
use acvm::Language; | ||
use serde::Deserialize; | ||
use std::collections::HashSet; | ||
use std::path::Path; | ||
|
||
use crate::BackendError; | ||
|
||
pub(crate) struct InfoCommand; | ||
|
||
#[derive(Deserialize)] | ||
struct InfoResponse { | ||
language: LanguageResponse, | ||
opcodes_supported: Vec<String>, | ||
black_box_functions_supported: Vec<String>, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct LanguageResponse { | ||
name: String, | ||
width: Option<usize>, | ||
} | ||
|
||
impl InfoCommand { | ||
pub(crate) fn run( | ||
self, | ||
binary_path: &Path, | ||
) -> Result<(Language, Box<impl Fn(&Opcode) -> bool>), BackendError> { | ||
let mut command = std::process::Command::new(binary_path); | ||
|
||
command.arg("info"); | ||
|
||
let output = command.output().expect("Failed to execute command"); | ||
|
||
if !output.status.success() { | ||
return Err(BackendError(String::from_utf8(output.stderr).unwrap())); | ||
} | ||
|
||
let backend_info: InfoResponse = | ||
serde_json::from_slice(&output.stdout).expect("Backend should return valid json"); | ||
let language: Language = match backend_info.language.name.as_str() { | ||
"PLONK-CSAT" => { | ||
let width = backend_info.language.width.unwrap(); | ||
Language::PLONKCSat { width } | ||
} | ||
"R1CS" => Language::R1CS, | ||
_ => panic!("Unknown langauge"), | ||
}; | ||
|
||
let opcodes_set: HashSet<String> = backend_info.opcodes_supported.into_iter().collect(); | ||
let black_box_functions_set: HashSet<String> = | ||
backend_info.black_box_functions_supported.into_iter().collect(); | ||
|
||
let is_opcode_supported = move |opcode: &Opcode| -> bool { | ||
match opcode { | ||
Opcode::Arithmetic(_) => opcodes_set.contains("arithmetic"), | ||
Opcode::Directive(_) => opcodes_set.contains("directive"), | ||
Opcode::Brillig(_) => opcodes_set.contains("brillig"), | ||
Opcode::MemoryInit { .. } => opcodes_set.contains("memory_init"), | ||
Opcode::MemoryOp { .. } => opcodes_set.contains("memory_op"), | ||
Opcode::BlackBoxFuncCall(func) => { | ||
black_box_functions_set.contains(func.get_black_box_func().name()) | ||
} | ||
} | ||
}; | ||
|
||
return Ok((language, Box::new(is_opcode_supported))); | ||
} | ||
} | ||
|
||
#[test] | ||
#[serial_test::serial] | ||
fn info_command() { | ||
use acvm::acir::native_types::Expression; | ||
|
||
let backend = crate::get_mock_backend(); | ||
|
||
let (language, is_opcode_supported) = InfoCommand.run(&backend.binary_path()).unwrap(); | ||
|
||
assert!(matches!(language, Language::PLONKCSat { width: 3 })); | ||
assert!(is_opcode_supported(&Opcode::Arithmetic(Expression::default()))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
crates/acvm_backend_barretenberg/test-binaries/mock_backend/src/info_cmd.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use clap::Args; | ||
use std::io::Write; | ||
|
||
const INFO_RESPONSE: &str = r#"{ | ||
"language": { | ||
"name": "PLONK-CSAT", | ||
"width": 3 | ||
}, | ||
"opcodes_supported": ["arithmetic", "directive", "brillig", "memory_init", "memory_op"], | ||
"black_box_functions_supported": [ | ||
"and", | ||
"xor", | ||
"range", | ||
"sha256", | ||
"blake2s", | ||
"keccak256", | ||
"schnorr_verify", | ||
"pedersen", | ||
"hash_to_field_128_security", | ||
"ecdsa_secp256k1", | ||
"ecdsa_secp256r1", | ||
"fixed_base_scalar_mul", | ||
"recursive_aggregation" | ||
] | ||
}"#; | ||
|
||
#[derive(Debug, Clone, Args)] | ||
pub(crate) struct InfoCommand; | ||
|
||
pub(crate) fn run(_args: InfoCommand) { | ||
std::io::stdout().write_all(INFO_RESPONSE.as_bytes()).unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters