Skip to content

Commit

Permalink
git subrepo pull --force --branch=master noir/noir-repo
Browse files Browse the repository at this point in the history
subrepo:
  subdir:   "noir/noir-repo"
  merged:   "bdb2bc608e"
upstream:
  origin:   "https://github.com/noir-lang/noir"
  branch:   "master"
  commit:   "bdb2bc608e"
git-subrepo:
  version:  "0.4.6"
  origin:   "???"
  commit:   "???"
  • Loading branch information
Maddiaa0 committed Jun 5, 2024
1 parent 5a10672 commit c76c4a6
Show file tree
Hide file tree
Showing 19 changed files with 78 additions and 248 deletions.
6 changes: 3 additions & 3 deletions noir/noir-repo/.gitrepo
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
;
[subrepo]
remote = https://github.com/noir-lang/noir
branch = md/restrict-bitsize-to-u32
commit = fae3be52223699ba7ccb0e1368db263ac2b091ed
parent = 9f5b11345dc5dd055442eaf7673227fe7cbaf262
branch = master
commit = bdb2bc608ea8fd52d46545a38b68dd2558b28110
parent = 5a10672830155bf03a210972c22c314a28086a0e
method = merge
cmdver = 0.4.6
108 changes: 62 additions & 46 deletions noir/noir-repo/acvm-repo/brillig_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ impl<'a, F: AcirField, B: BlackBoxFunctionSolver<F>> VM<'a, F, B> {
destination_value_types: &[HeapValueType],
foreign_call_index: usize,
) -> Result<(), String> {
let values = &self.foreign_call_results[foreign_call_index].values;
let values = std::mem::take(&mut self.foreign_call_results[foreign_call_index].values);

if destinations.len() != values.len() {
return Err(format!(
Expand All @@ -479,22 +479,13 @@ impl<'a, F: AcirField, B: BlackBoxFunctionSolver<F>> VM<'a, F, B> {
}

for ((destination, value_type), output) in
destinations.iter().zip(destination_value_types).zip(values)
destinations.iter().zip(destination_value_types).zip(&values)
{
match (destination, value_type) {
(ValueOrArray::MemoryAddress(value_index), HeapValueType::Simple(bit_size)) => {
match output {
ForeignCallParam::Single(value) => {
let memory_value = MemoryValue::new_checked(*value, *bit_size);
if let Some(memory_value) = memory_value {
self.memory.write(*value_index, memory_value);
} else {
return Err(format!(
"Foreign call result value {} does not fit in bit size {}",
value,
bit_size
));
}
self.write_value_to_memory(*value_index, value, *bit_size)?;
}
_ => return Err(format!(
"Function result size does not match brillig bytecode. Expected 1 result but got {output:?}")
Expand All @@ -506,28 +497,12 @@ impl<'a, F: AcirField, B: BlackBoxFunctionSolver<F>> VM<'a, F, B> {
HeapValueType::Array { value_types, size: type_size },
) if size == type_size => {
if HeapValueType::all_simple(value_types) {
let bit_sizes_iterator = value_types.iter().map(|typ| match typ {
HeapValueType::Simple(bit_size) => *bit_size,
_ => unreachable!("Expected simple value type"),
}).cycle();
match output {
match output {
ForeignCallParam::Array(values) => {
if values.len() != *size {
return Err("Foreign call result array doesn't match expected size".to_string());
}
// Convert the destination pointer to a usize
let destination = self.memory.read_ref(*pointer_index);
// Write to our destination memory
let memory_values: Option<Vec<_>> = values.iter().zip(bit_sizes_iterator).map(
|(value, bit_size)| MemoryValue::new_checked(*value, bit_size)).collect();
if let Some(memory_values) = memory_values {
self.memory.write_slice(destination, &memory_values);
} else {
return Err(format!(
"Foreign call result values {:?} do not match expected bit sizes",
values,
));
}
self.write_values_to_memory_slice(*pointer_index, values, value_types)?;
}
_ => {
return Err("Function result size does not match brillig bytecode size".to_string());
Expand All @@ -542,26 +517,12 @@ impl<'a, F: AcirField, B: BlackBoxFunctionSolver<F>> VM<'a, F, B> {
HeapValueType::Vector { value_types },
) => {
if HeapValueType::all_simple(value_types) {
let bit_sizes_iterator = value_types.iter().map(|typ| match typ {
HeapValueType::Simple(bit_size) => *bit_size,
_ => unreachable!("Expected simple value type"),
}).cycle();
match output {
ForeignCallParam::Array(values) => {
// Set our size in the size address
self.memory.write(*size_index, values.len().into());
// Convert the destination pointer to a usize
let destination = self.memory.read_ref(*pointer_index);
// Write to our destination memory
let memory_values: Option<Vec<_>> = values.iter().zip(bit_sizes_iterator).map(|(value, bit_size)| MemoryValue::new_checked(*value, bit_size)).collect();
if let Some(memory_values) = memory_values {
self.memory.write_slice(destination, &memory_values);
}else{
return Err(format!(
"Foreign call result values {:?} do not match expected bit sizes",
values,
));
}

self.write_values_to_memory_slice(*pointer_index, values, value_types)?;
}
_ => {
return Err("Function result size does not match brillig bytecode size".to_string());
Expand All @@ -577,6 +538,61 @@ impl<'a, F: AcirField, B: BlackBoxFunctionSolver<F>> VM<'a, F, B> {
}
}

let _ =
std::mem::replace(&mut self.foreign_call_results[foreign_call_index].values, values);

Ok(())
}

fn write_value_to_memory(
&mut self,
destination: MemoryAddress,
value: &F,
value_bit_size: u32,
) -> Result<(), String> {
let memory_value = MemoryValue::new_checked(*value, value_bit_size);

if let Some(memory_value) = memory_value {
self.memory.write(destination, memory_value);
} else {
return Err(format!(
"Foreign call result value {} does not fit in bit size {}",
value, value_bit_size
));
}
Ok(())
}

fn write_values_to_memory_slice(
&mut self,
pointer_index: MemoryAddress,
values: &[F],
value_types: &[HeapValueType],
) -> Result<(), String> {
let bit_sizes_iterator = value_types
.iter()
.map(|typ| match typ {
HeapValueType::Simple(bit_size) => *bit_size,
_ => unreachable!("Expected simple value type"),
})
.cycle();

// Convert the destination pointer to a usize
let destination = self.memory.read_ref(pointer_index);
// Write to our destination memory
let memory_values: Option<Vec<_>> = values
.iter()
.zip(bit_sizes_iterator)
.map(|(value, bit_size)| MemoryValue::new_checked(*value, bit_size))
.collect();
if let Some(memory_values) = memory_values {
self.memory.write_slice(destination, &memory_values);
} else {
return Err(format!(
"Foreign call result values {:?} do not match expected bit sizes",
values,
));
}
Ok(())
}

Expand Down
34 changes: 10 additions & 24 deletions noir/noir-repo/aztec_macros/src/transforms/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,30 +216,12 @@ pub fn export_fn_abi(
///
/// Inserts the following code at the beginning of an unconstrained function
/// ```noir
/// let context = UnconstrainedContext::new();
/// let storage = Storage::init(context);
/// let storage = Storage::init(Context::none());
/// ```
///
/// This will allow developers to access their contract' storage struct in unconstrained functions
pub fn transform_unconstrained(func: &mut NoirFunction, storage_struct_name: String) {
// let context = UnconstrainedContext::new();
let let_context = assignment(
"context", // Assigned to
call(
variable_path(chained_dep!(
"aztec",
"context",
"unconstrained_context",
"UnconstrainedContext",
"new"
)),
vec![],
),
);

// We inject the statements at the beginning, in reverse order.
func.def.body.statements.insert(0, abstract_storage(storage_struct_name, true));
func.def.body.statements.insert(0, let_context);
}

/// Helper function that returns what the private context would look like in the ast
Expand Down Expand Up @@ -615,26 +597,30 @@ fn abstract_return_values(func: &NoirFunction) -> Result<Option<Vec<Statement>>,
/// ```noir
/// #[aztec(private)]
/// fn lol() {
/// let storage = Storage::init(&mut context);
/// let storage = Storage::init(context);
/// }
/// ```
///
/// For public functions:
/// ```noir
/// #[aztec(public)]
/// fn lol() {
/// let storage = Storage::init(&mut context);
/// let storage = Storage::init(context);
/// }
/// ```
///
/// For unconstrained functions:
/// ```noir
/// unconstrained fn lol() {
/// let storage = Storage::init(context);
/// let storage = Storage::init(());
/// }
fn abstract_storage(storage_struct_name: String, unconstrained: bool) -> Statement {
let context_expr =
if unconstrained { variable("context") } else { mutable_reference("context") };
let context_expr = if unconstrained {
// Note that the literal unit type (i.e. '()') is not the same as a tuple with zero elements
expression(ExpressionKind::Literal(Literal::Unit))
} else {
mutable_reference("context")
};

assignment(
"storage", // Assigned to
Expand Down
2 changes: 0 additions & 2 deletions noir/noir-repo/examples/recursion/.gitignore

This file was deleted.

2 changes: 0 additions & 2 deletions noir/noir-repo/examples/recursion/Nargo.toml

This file was deleted.

61 changes: 0 additions & 61 deletions noir/noir-repo/examples/recursion/generate_recursive_proof.sh

This file was deleted.

7 changes: 0 additions & 7 deletions noir/noir-repo/examples/recursion/recurse_leaf/Nargo.toml

This file was deleted.

20 changes: 0 additions & 20 deletions noir/noir-repo/examples/recursion/recurse_leaf/src/main.nr

This file was deleted.

7 changes: 0 additions & 7 deletions noir/noir-repo/examples/recursion/recurse_node/Nargo.toml

This file was deleted.

17 changes: 0 additions & 17 deletions noir/noir-repo/examples/recursion/recurse_node/src/main.nr

This file was deleted.

7 changes: 0 additions & 7 deletions noir/noir-repo/examples/recursion/sum/Nargo.toml

This file was deleted.

2 changes: 0 additions & 2 deletions noir/noir-repo/examples/recursion/sum/Prover.toml

This file was deleted.

4 changes: 0 additions & 4 deletions noir/noir-repo/examples/recursion/sum/src/main.nr

This file was deleted.

8 changes: 0 additions & 8 deletions noir/noir-repo/examples/recursion/test.sh

This file was deleted.

Loading

0 comments on commit c76c4a6

Please sign in to comment.