-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e55b5a8
commit 753e52f
Showing
10 changed files
with
102 additions
and
107 deletions.
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 |
---|---|---|
@@ -1,64 +1,60 @@ | ||
use acvm::acir::circuit::Opcode; | ||
use acvm::pwg::{solve, Blocks, PartialWitnessGeneratorStatus, UnresolvedBrilligCall}; | ||
use acvm::PartialWitnessGenerator; | ||
use acvm::acir::brillig_vm::ForeignCallResult; | ||
use acvm::pwg::{ForeignCallWaitInfo, PartialWitnessGeneratorStatus, ACVM}; | ||
use acvm::BlackBoxFunctionSolver; | ||
use acvm::{acir::circuit::Circuit, acir::native_types::WitnessMap}; | ||
|
||
use crate::NargoError; | ||
|
||
pub fn execute_circuit( | ||
backend: &impl PartialWitnessGenerator, | ||
pub fn execute_circuit<B: BlackBoxFunctionSolver + Default>( | ||
_backend: &B, | ||
circuit: Circuit, | ||
mut initial_witness: WitnessMap, | ||
initial_witness: WitnessMap, | ||
) -> Result<WitnessMap, NargoError> { | ||
let mut blocks = Blocks::default(); | ||
let solver_status = solve(backend, &mut initial_witness, &mut blocks, circuit.opcodes)?; | ||
let mut acvm = ACVM::new(B::default(), circuit.opcodes, initial_witness); | ||
|
||
// TODO(#1615): Nargo only supports "oracle_print_**_impl" functions that print a singular value or an array and nothing else | ||
// This should be expanded in a general logging refactor | ||
if let PartialWitnessGeneratorStatus::RequiresOracleData { | ||
unresolved_brillig_calls, | ||
required_oracle_data, | ||
unsolved_opcodes, | ||
} = solver_status | ||
{ | ||
if !required_oracle_data.is_empty() { | ||
unreachable!("oracles are not supported by nargo execute") | ||
} | ||
for unresolved_brillig_call in unresolved_brillig_calls { | ||
let UnresolvedBrilligCall { foreign_call_wait_info, mut brillig } = | ||
unresolved_brillig_call; | ||
loop { | ||
let solver_status = acvm.solve()?; | ||
|
||
// Execute foreign calls | ||
// TODO(#1615): "oracle_print_impl" and "oracle_print_array_impl" are just identity funcs | ||
if foreign_call_wait_info.function == "oracle_print_impl" { | ||
let values = &foreign_call_wait_info.inputs[0]; | ||
println!("{:?}", values[0].to_field().to_hex()); | ||
brillig.foreign_call_results.push(foreign_call_wait_info.inputs[0][0].into()); | ||
} else if foreign_call_wait_info.function == "oracle_print_array_impl" { | ||
let mut outputs_hex = Vec::new(); | ||
for values in foreign_call_wait_info.inputs.clone() { | ||
for value in values { | ||
outputs_hex.push(value.to_field().to_hex()); | ||
} | ||
} | ||
// Join all of the hex strings using a comma | ||
let comma_separated_elements = outputs_hex.join(", "); | ||
let output_witnesses_string = "[".to_owned() + &comma_separated_elements + "]"; | ||
println!("{output_witnesses_string}"); | ||
brillig.foreign_call_results.push(foreign_call_wait_info.inputs[0][0].into()); | ||
match solver_status { | ||
PartialWitnessGeneratorStatus::Solved => break, | ||
PartialWitnessGeneratorStatus::RequiresForeignCall => { | ||
let foreign_call = | ||
acvm.get_pending_foreign_call().expect("Should be waiting on a foreign call"); | ||
|
||
let foreign_call_result = execute_foreign_call(foreign_call); | ||
acvm.resolve_pending_foreign_call(foreign_call_result); | ||
} | ||
} | ||
} | ||
|
||
let mut next_opcodes_for_solving = vec![Opcode::Brillig(brillig)]; | ||
next_opcodes_for_solving.extend_from_slice(&unsolved_opcodes[..]); | ||
let solved_witness = acvm.finalize(); | ||
Ok(solved_witness) | ||
} | ||
|
||
let solver_status = | ||
solve(backend, &mut initial_witness, &mut blocks, next_opcodes_for_solving)?; | ||
if matches!(solver_status, PartialWitnessGeneratorStatus::RequiresOracleData { .. }) { | ||
todo!("Add multiple foreign call support to nargo execute") | ||
// TODO 1557 | ||
fn execute_foreign_call(foreign_call: &ForeignCallWaitInfo) -> ForeignCallResult { | ||
// TODO(#1615): "oracle_print_impl" and "oracle_print_array_impl" are just identity funcs | ||
match foreign_call.function.as_str() { | ||
"oracle_print_impl" => { | ||
let values = &foreign_call.inputs[0]; | ||
println!("{:?}", values[0].to_field().to_hex()); | ||
values[0].into() | ||
} | ||
"oracle_print_array_impl" => { | ||
let mut outputs_hex = Vec::new(); | ||
for values in &foreign_call.inputs { | ||
for value in values { | ||
outputs_hex.push(value.to_field().to_hex()); | ||
} | ||
} | ||
// Join all of the hex strings using a comma | ||
let comma_separated_elements = outputs_hex.join(", "); | ||
let output_witnesses_string = "[".to_owned() + &comma_separated_elements + "]"; | ||
println!("{output_witnesses_string}"); | ||
|
||
foreign_call.inputs[0][0].into() | ||
} | ||
_ => panic!("unexpected foreign call type"), | ||
} | ||
|
||
Ok(initial_witness) | ||
} |
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
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
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
Oops, something went wrong.