-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into euclid-unnecessary-witnesses
- Loading branch information
Showing
27 changed files
with
549 additions
and
280 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use acvm::{ | ||
acir::brillig::{ForeignCallResult, Value}, | ||
pwg::ForeignCallWaitInfo, | ||
}; | ||
use iter_extended::vecmap; | ||
use noirc_abi::{decode_string_value, decode_value, input_parser::json::JsonTypes, AbiType}; | ||
|
||
use crate::errors::ForeignCallError; | ||
|
||
/// This enumeration represents the Brillig foreign calls that are natively supported by nargo. | ||
/// After resolution of a foreign call, nargo will restart execution of the ACVM | ||
pub(crate) enum ForeignCall { | ||
Println, | ||
Sequence, | ||
ReverseSequence, | ||
} | ||
|
||
impl std::fmt::Display for ForeignCall { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.name()) | ||
} | ||
} | ||
|
||
impl ForeignCall { | ||
pub(crate) fn name(&self) -> &'static str { | ||
match self { | ||
ForeignCall::Println => "println", | ||
ForeignCall::Sequence => "get_number_sequence", | ||
ForeignCall::ReverseSequence => "get_reverse_number_sequence", | ||
} | ||
} | ||
|
||
pub(crate) fn lookup(op_name: &str) -> Option<ForeignCall> { | ||
match op_name { | ||
"println" => Some(ForeignCall::Println), | ||
"get_number_sequence" => Some(ForeignCall::Sequence), | ||
"get_reverse_number_sequence" => Some(ForeignCall::ReverseSequence), | ||
_ => None, | ||
} | ||
} | ||
|
||
pub(crate) fn execute( | ||
foreign_call: &ForeignCallWaitInfo, | ||
) -> Result<ForeignCallResult, ForeignCallError> { | ||
let foreign_call_name = foreign_call.function.as_str(); | ||
match Self::lookup(foreign_call_name) { | ||
Some(ForeignCall::Println) => { | ||
Self::execute_println(&foreign_call.inputs)?; | ||
Ok(ForeignCallResult { values: vec![] }) | ||
} | ||
Some(ForeignCall::Sequence) => { | ||
let sequence_length: u128 = foreign_call.inputs[0][0].to_field().to_u128(); | ||
|
||
Ok(vecmap(0..sequence_length, Value::from).into()) | ||
} | ||
Some(ForeignCall::ReverseSequence) => { | ||
let sequence_length: u128 = foreign_call.inputs[0][0].to_field().to_u128(); | ||
|
||
Ok(vecmap((0..sequence_length).rev(), Value::from).into()) | ||
} | ||
None => panic!("unexpected foreign call {:?}", foreign_call_name), | ||
} | ||
} | ||
|
||
fn execute_println(foreign_call_inputs: &[Vec<Value>]) -> Result<(), ForeignCallError> { | ||
let (abi_type, input_values) = fetch_abi_type(foreign_call_inputs)?; | ||
|
||
// We must use a flat map here as each value in a struct will be in a separate input value | ||
let mut input_values_as_fields = | ||
input_values.iter().flat_map(|values| values.iter().map(|value| value.to_field())); | ||
let decoded_value = decode_value(&mut input_values_as_fields, &abi_type)?; | ||
|
||
let json_value = JsonTypes::try_from_input_value(&decoded_value, &abi_type)?; | ||
|
||
println!("{json_value}"); | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// Fetch the abi type from the foreign call input | ||
/// The remaining input values should hold the values to be printed | ||
fn fetch_abi_type( | ||
foreign_call_inputs: &[Vec<Value>], | ||
) -> Result<(AbiType, &[Vec<Value>]), ForeignCallError> { | ||
let (abi_type_as_values, input_values) = | ||
foreign_call_inputs.split_last().ok_or(ForeignCallError::MissingForeignCallInputs)?; | ||
let abi_type_as_fields = vecmap(abi_type_as_values, |value| value.to_field()); | ||
let abi_type_as_string = decode_string_value(&abi_type_as_fields); | ||
let abi_type: AbiType = serde_json::from_str(&abi_type_as_string) | ||
.map_err(|err| ForeignCallError::InputParserError(err.into()))?; | ||
|
||
Ok((abi_type, input_values)) | ||
} |
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
5 changes: 5 additions & 0 deletions
5
crates/nargo_cli/tests/test_data_ssa_refactor/debug_logs/Nargo.toml
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,5 @@ | ||
[package] | ||
authors = [""] | ||
compiler_version = "0.8.0" | ||
|
||
[dependencies] |
2 changes: 2 additions & 0 deletions
2
crates/nargo_cli/tests/test_data_ssa_refactor/debug_logs/Prover.toml
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,2 @@ | ||
x = "5" | ||
y = "10" |
22 changes: 22 additions & 0 deletions
22
crates/nargo_cli/tests/test_data_ssa_refactor/debug_logs/src/main.nr
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,22 @@ | ||
use dep::std; | ||
|
||
fn main(x : Field, y : pub Field) { | ||
|
||
std::println("*** println ***"); | ||
std::println(x); | ||
std::println([x, y]); | ||
|
||
let s = myStruct { y: x, x: y }; | ||
let foo = fooStruct { my_struct: s, foo: 15 }; | ||
std::println(foo); | ||
} | ||
|
||
struct myStruct { | ||
y: Field, | ||
x: Field, | ||
} | ||
|
||
struct fooStruct { | ||
my_struct: myStruct, | ||
foo: Field, | ||
} |
5 changes: 5 additions & 0 deletions
5
crates/nargo_cli/tests/test_data_ssa_refactor/regression/Nargo.toml
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,5 @@ | ||
[package] | ||
authors = [""] | ||
compiler_version = "0.1" | ||
|
||
[dependencies] |
2 changes: 2 additions & 0 deletions
2
crates/nargo_cli/tests/test_data_ssa_refactor/regression/Prover.toml
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,2 @@ | ||
x = [0x3f, 0x1c, 0xb8, 0x99, 0xab] | ||
z = 3 |
90 changes: 90 additions & 0 deletions
90
crates/nargo_cli/tests/test_data_ssa_refactor/regression/src/main.nr
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,90 @@ | ||
global NIBBLE_LENGTH: comptime Field = 16; | ||
|
||
fn compact_decode<N>(input: [u8; N], length: Field) -> ([u4; NIBBLE_LENGTH], Field) | ||
{ | ||
assert(2*input.len() as u64 <= NIBBLE_LENGTH as u64); | ||
assert(length as u64 <= input.len() as u64); | ||
|
||
let mut nibble = [0 as u4; NIBBLE_LENGTH]; | ||
|
||
let first_nibble = (input[0] >> 4) as u4; | ||
let parity = first_nibble as u1; | ||
|
||
if parity == 1 | ||
{ | ||
nibble[0] = (input[0] & 0x0f) as u4; | ||
for i in 1..input.len() | ||
{ | ||
if i as u64 < length as u64 | ||
{ | ||
let x = input[i]; | ||
nibble[2*i - 1] = (x >> 4) as u4; | ||
nibble[2*i] = (x & 0x0f) as u4; | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
for i in 0..2 | ||
{ | ||
if (i as u64) < length as u64 - 1 | ||
{ | ||
let x = input[i + 1]; | ||
nibble[2*i] = (x >> 4) as u4; | ||
nibble[2*i + 1] = (x & 0x0f) as u4; | ||
} | ||
} | ||
} | ||
|
||
let out = (nibble, 2*length + (parity as Field) - 2); | ||
|
||
out | ||
} | ||
|
||
fn enc<N>(value: [u8; N], value_length: Field) -> ([u8; 32], Field) | ||
{ | ||
assert(value.len() as u8 >= value_length as u8); | ||
let mut out_value = [0; 32]; | ||
if value_length == 0 | ||
{ | ||
let out = (out_value, value_length); | ||
out | ||
} | ||
else { if value_length as u8 < 31 | ||
{ | ||
out_value[0] = 0x80 + value_length as u8; | ||
|
||
for i in 1..value.len() | ||
{ | ||
out_value[i] = value[i-1]; | ||
} | ||
|
||
let out = (out_value, value_length + 1); | ||
|
||
out | ||
} | ||
else | ||
{ | ||
let out = (out_value, 32); | ||
out | ||
} | ||
} | ||
} | ||
|
||
fn main(x: [u8; 5], z: Field) | ||
{ | ||
//Issue 1144 | ||
let (nib, len) = compact_decode(x,z); | ||
assert(len == 5); | ||
assert([nib[0], nib[1], nib[2], nib[3], nib[4]] == [15, 1, 12, 11, 8]); | ||
|
||
// Issue 1169 | ||
let val1 = [0xb8,0x8f,0x61,0xe6,0xfb,0xda,0x83,0xfb,0xff,0xfa,0xbe,0x36,0x41,0x12,0x13,0x74,0x80,0x39,0x80,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]; | ||
let val1_length = 20; | ||
|
||
let enc_val1 = enc(val1,val1_length); | ||
|
||
assert(enc_val1.0 == [0x94,0xb8,0x8f,0x61,0xe6,0xfb,0xda,0x83,0xfb,0xff,0xfa,0xbe,0x36,0x41,0x12,0x13,0x74,0x80,0x39,0x80,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]); | ||
assert(enc_val1.1 == 21); | ||
|
||
} |
Oops, something went wrong.