Skip to content

Commit

Permalink
Merge branch 'master' into gd/dynamic_array2
Browse files Browse the repository at this point in the history
  • Loading branch information
guipublic committed Feb 14, 2023
2 parents d39c8bf + 766b57d commit 91b5090
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 78 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
id: release
uses: google-github-actions/release-please-action@v3
with:
token: ${{ secrets.NOIR_RELEASES_TOKEN }}
release-type: simple
package-name: noir
bump-minor-pre-major: true
Expand Down
1 change: 0 additions & 1 deletion crates/nargo/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ pub fn generate_circuit_and_witness_to_disk<P: AsRef<Path>>(
circuit_path.set_extension(ACIR_EXT);
let path = write_to_file(serialized.as_slice(), &circuit_path);
println!("Generated ACIR code into {path}");
println!("{:?}", std::fs::canonicalize(&circuit_path));

if generate_witness {
let (_, solved_witness) =
Expand Down
18 changes: 9 additions & 9 deletions crates/noirc_evaluator/src/ssa/acir_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ use internal_var_cache::InternalVarCache;
// Expose this to the crate as we need to apply range constraints when
// converting the ABI(main parameters) to Noir types
pub(crate) use constraints::range_constraint;
mod memory_map;
use memory_map::MemoryMap;
mod acir_mem;
use acir_mem::AcirMem;

#[derive(Default)]
pub struct Acir {
memory_map: MemoryMap,
memory: AcirMem,
var_cache: InternalVarCache,
}

Expand All @@ -39,12 +39,12 @@ impl Acir {
binary, condition, constrain, intrinsics, load, not, r#return, store, truncate,
};

let memory_map = &mut self.memory_map;
let acir_mem = &mut self.memory;
let var_cache = &mut self.var_cache;

let output = match &ins.operation {
Operation::Binary(binary) => {
binary::evaluate(binary, ins.res_type, var_cache, memory_map, evaluator, ctx)
binary::evaluate(binary, ins.res_type, var_cache, acir_mem, evaluator, ctx)
}
Operation::Constrain(value, ..) => {
constrain::evaluate(value, var_cache, evaluator, ctx)
Expand All @@ -57,19 +57,19 @@ impl Acir {
truncate::evaluate(value, *bit_size, *max_bit_size, var_cache, evaluator, ctx)
}
Operation::Intrinsic(opcode, args) => {
intrinsics::evaluate(args, ins, *opcode, var_cache, memory_map, ctx, evaluator)
intrinsics::evaluate(args, ins, *opcode, var_cache, acir_mem, ctx, evaluator)
}
Operation::Return(node_ids) => {
r#return::evaluate(node_ids, memory_map, var_cache, evaluator, ctx)?
r#return::evaluate(node_ids, acir_mem, var_cache, evaluator, ctx)?
}
Operation::Cond { condition, val_true: lhs, val_false: rhs } => {
condition::evaluate(*condition, *lhs, *rhs, var_cache, evaluator, ctx)
}
Operation::Load { array_id, index } => {
load::evaluate(*array_id, *index, memory_map, var_cache, evaluator, ctx)
load::evaluate(*array_id, *index, acir_mem, var_cache, evaluator, ctx)
}
Operation::Store { array_id, index, value } => {
store::evaluate(*array_id, *index, *value, memory_map, var_cache, evaluator, ctx)
store::evaluate(*array_id, *index, *value, acir_mem, var_cache, evaluator, ctx)
}
Operation::Nop => None,
i @ Operation::Jne(..)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,41 @@ use crate::ssa::{
};
use acvm::acir::native_types::Witness;
use iter_extended::vecmap;
use std::collections::HashMap;
use std::collections::BTreeMap;

// maps memory address to expression
#[derive(Default)]
pub struct MemoryMap {
inner: HashMap<u32, InternalVar>,
pub struct ArrayHeap {
// maps memory address to InternalVar
memory_map: BTreeMap<u32, InternalVar>,
}

impl MemoryMap {
/// Handle virtual memory access
#[derive(Default)]
pub struct AcirMem {
virtual_memory: BTreeMap<ArrayId, ArrayHeap>,
}

impl AcirMem {
// Returns the memory_map for the array
fn array_map_mut(&mut self, array_id: ArrayId) -> &mut BTreeMap<u32, InternalVar> {
&mut self.virtual_memory.entry(array_id).or_default().memory_map
}

// Write the value to the array's VM at the specified index
pub fn insert(&mut self, array_id: ArrayId, index: u32, value: InternalVar) {
self.array_map_mut(array_id).insert(index, value);
}

//Map the outputs into the array
pub(crate) fn map_array(&mut self, a: ArrayId, outputs: &[Witness], ctx: &SsaContext) {
let array = &ctx.mem[a];
let address = array.adr;
for i in 0..array.len {
let var = if i < outputs.len() as u32 {
InternalVar::from(outputs[i as usize])
} else {
InternalVar::zero_expr()
};
self.inner.insert(address + i, var);
self.array_map_mut(array.id).insert(i, var);
}
}

Expand Down Expand Up @@ -56,18 +71,11 @@ impl MemoryMap {
return None; // IndexOutOfBoundsError
}

let address_of_element = array.absolute_adr(offset);

// Check the memory_map to see if the element is there
let array_element = self
.inner
.get(&address_of_element)
.array_map_mut(array.id)
.get(&offset)
.expect("ICE: Could not find value at index {offset}");

Some(array_element.clone())
}

pub(crate) fn insert(&mut self, key: u32, value: InternalVar) -> Option<InternalVar> {
self.inner.insert(key, value)
}
}
4 changes: 2 additions & 2 deletions crates/noirc_evaluator/src/ssa/acir_gen/operations/binary.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
ssa::{
acir_gen::{
constraints, internal_var_cache::InternalVarCache, memory_map::MemoryMap, operations,
acir_mem::AcirMem, constraints, internal_var_cache::InternalVarCache, operations,
InternalVar,
},
context::SsaContext,
Expand Down Expand Up @@ -30,7 +30,7 @@ pub(crate) fn evaluate(
binary: &node::Binary,
res_type: ObjectType,
var_cache: &mut InternalVarCache,
memory_map: &mut MemoryMap,
memory_map: &mut AcirMem,
evaluator: &mut Evaluator,
ctx: &SsaContext,
) -> Option<InternalVar> {
Expand Down
10 changes: 5 additions & 5 deletions crates/noirc_evaluator/src/ssa/acir_gen/operations/cmp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
ssa::{
acir_gen::{constraints, memory_map::MemoryMap, InternalVar},
acir_gen::{acir_mem::AcirMem, constraints, InternalVar},
context::SsaContext,
mem::{MemArray, Memory},
node::NodeId,
Expand All @@ -26,7 +26,7 @@ use iter_extended::vecmap;
// so in reality, the NEQ instruction will be done on the fields
// of the struct
pub(crate) fn evaluate_neq(
memory_map: &mut MemoryMap,
acir_mem: &mut AcirMem,
lhs: NodeId,
rhs: NodeId,
l_c: Option<InternalVar>,
Expand Down Expand Up @@ -55,7 +55,7 @@ pub(crate) fn evaluate_neq(
)
}

let mut x = InternalVar::from(array_eq(memory_map, array_a, array_b, evaluator));
let mut x = InternalVar::from(array_eq(acir_mem, array_a, array_b, evaluator));
// TODO we need a witness because of the directive, but we should use an expression
// TODO if we change the Invert directive to take an `Expression`, then we
// TODO can get rid of this extra gate.
Expand Down Expand Up @@ -89,7 +89,7 @@ pub(crate) fn evaluate_neq(
}

pub(crate) fn evaluate_eq(
memory_map: &mut MemoryMap,
memory_map: &mut AcirMem,
lhs: NodeId,
rhs: NodeId,
l_c: Option<InternalVar>,
Expand All @@ -107,7 +107,7 @@ pub(crate) fn evaluate_eq(
//
// N.B. We assumes the lengths of a and b are the same but it is not checked inside the function.
fn array_eq(
memory_map: &mut MemoryMap,
memory_map: &mut AcirMem,
a: &MemArray,
b: &MemArray,
evaluator: &mut Evaluator,
Expand Down
17 changes: 8 additions & 9 deletions crates/noirc_evaluator/src/ssa/acir_gen/operations/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
constraints::{bound_constraint_with_offset, to_radix_base},
expression_from_witness,
operations::sort::evaluate_permutation,
InternalVar, InternalVarCache, MemoryMap,
AcirMem, InternalVar, InternalVarCache,
},
builtin,
context::SsaContext,
Expand Down Expand Up @@ -32,7 +32,7 @@ pub(crate) fn evaluate(
instruction: &Instruction,
opcode: builtin::Opcode,
var_cache: &mut InternalVarCache,
memory_map: &mut MemoryMap,
memory_map: &mut AcirMem,
ctx: &SsaContext,
evaluator: &mut Evaluator,
) -> Option<InternalVar> {
Expand Down Expand Up @@ -121,7 +121,7 @@ pub(crate) fn evaluate(
// Transform the arguments of intrinsic functions into witnesses
fn prepare_inputs(
var_cache: &mut InternalVarCache,
memory_map: &mut MemoryMap,
memory_map: &mut AcirMem,
arguments: &[NodeId],
cfg: &SsaContext,
evaluator: &mut Evaluator,
Expand All @@ -137,7 +137,7 @@ fn prepare_inputs(
fn resolve_node_id(
node_id: &NodeId,
var_cache: &mut InternalVarCache,
memory_map: &mut MemoryMap,
memory_map: &mut AcirMem,
cfg: &SsaContext,
evaluator: &mut Evaluator,
) -> Vec<FunctionInput> {
Expand Down Expand Up @@ -179,7 +179,7 @@ fn resolve_node_id(

fn resolve_array(
array_id: ArrayId,
memory_map: &mut MemoryMap,
acir_mem: &mut AcirMem,
cfg: &SsaContext,
evaluator: &mut Evaluator,
) -> Vec<FunctionInput> {
Expand All @@ -188,7 +188,7 @@ fn resolve_array(
let array = &cfg.mem[array_id];
let num_bits = array.element_type.bits();
for i in 0..array.len {
let mut arr_element = memory_map
let mut arr_element = acir_mem
.load_array_element_constant_index(array, i)
.expect("array index out of bounds");

Expand All @@ -197,8 +197,7 @@ fn resolve_array(
);
let func_input = FunctionInput { witness, num_bits };

let address = array.adr + i;
memory_map.insert(address, arr_element);
acir_mem.insert(array.id, i, arr_element);

inputs.push(func_input)
}
Expand All @@ -207,7 +206,7 @@ fn resolve_array(
}

fn prepare_outputs(
memory_map: &mut MemoryMap,
memory_map: &mut AcirMem,
pointer: NodeId,
output_nb: u32,
ctx: &SsaContext,
Expand Down
4 changes: 2 additions & 2 deletions crates/noirc_evaluator/src/ssa/acir_gen/operations/load.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
ssa::{
acir_gen::{internal_var_cache::InternalVarCache, memory_map::MemoryMap, InternalVar},
acir_gen::{acir_mem::AcirMem, internal_var_cache::InternalVarCache, InternalVar},
context::SsaContext,
mem::{self, ArrayId},
node::NodeId,
Expand All @@ -11,7 +11,7 @@ use crate::{
pub(crate) fn evaluate(
array_id: ArrayId,
index: NodeId,
memory_map: &mut MemoryMap,
memory_map: &mut AcirMem,
var_cache: &mut InternalVarCache,
evaluator: &mut Evaluator,
ctx: &SsaContext,
Expand Down
4 changes: 2 additions & 2 deletions crates/noirc_evaluator/src/ssa/acir_gen/operations/return.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
errors::RuntimeErrorKind,
ssa::{
acir_gen::{internal_var_cache::InternalVarCache, memory_map::MemoryMap, InternalVar},
acir_gen::{acir_mem::AcirMem, internal_var_cache::InternalVarCache, InternalVar},
context::SsaContext,
mem::Memory,
node::NodeId,
Expand All @@ -11,7 +11,7 @@ use crate::{

pub(crate) fn evaluate(
node_ids: &[NodeId],
memory_map: &mut MemoryMap,
memory_map: &mut AcirMem,
var_cache: &mut InternalVarCache,
evaluator: &mut Evaluator,
ctx: &SsaContext,
Expand Down
7 changes: 3 additions & 4 deletions crates/noirc_evaluator/src/ssa/acir_gen/operations/store.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
ssa::{
acir_gen::{internal_var_cache::InternalVarCache, memory_map::MemoryMap, InternalVar},
acir_gen::{acir_mem::AcirMem, internal_var_cache::InternalVarCache, InternalVar},
context::SsaContext,
mem::{self, ArrayId},
node::NodeId,
Expand All @@ -12,7 +12,7 @@ pub(crate) fn evaluate(
array_id: ArrayId,
index: NodeId,
value: NodeId,
memory_map: &mut MemoryMap,
acir_mem: &mut AcirMem,
var_cache: &mut InternalVarCache,
evaluator: &mut Evaluator,
ctx: &SsaContext,
Expand All @@ -24,8 +24,7 @@ pub(crate) fn evaluate(
match index.to_const() {
Some(index) => {
let idx = mem::Memory::as_u32(index);
let absolute_adr = ctx.mem[array_id].absolute_adr(idx);
memory_map.insert(absolute_adr, value);
acir_mem.insert(array_id, idx, value);
//we do not generate constraint, so no output.
None
}
Expand Down
29 changes: 1 addition & 28 deletions crates/noirc_evaluator/src/ssa/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,35 +157,8 @@ impl SsaContext {
fn binary_to_string(&self, binary: &node::Binary) -> String {
let lhs = self.id_to_string(binary.lhs);
let rhs = self.id_to_string(binary.rhs);
let op = match &binary.operator {
BinaryOp::Add => "add",
BinaryOp::SafeAdd => "safe_add",
BinaryOp::Sub { .. } => "sub",
BinaryOp::SafeSub { .. } => "safe_sub",
BinaryOp::Mul => "mul",
BinaryOp::SafeMul => "safe_mul",
BinaryOp::Udiv => "udiv",
BinaryOp::Sdiv => "sdiv",
BinaryOp::Urem => "urem",
BinaryOp::Srem => "srem",
BinaryOp::Div => "div",
BinaryOp::Eq => "eq",
BinaryOp::Ne => "ne",
BinaryOp::Ult => "ult",
BinaryOp::Ule => "ule",
BinaryOp::Slt => "slt",
BinaryOp::Sle => "sle",
BinaryOp::Lt => "lt",
BinaryOp::Lte => "lte",
BinaryOp::And => "and",
BinaryOp::Or => "or",
BinaryOp::Xor => "xor",
BinaryOp::Assign => "assign",
BinaryOp::Shl => "shl",
BinaryOp::Shr => "shr",
};

format!("{op} {lhs}, {rhs}")
format!("{} {lhs}, {rhs}", binary.operator)
}

pub fn operation_to_string(&self, op: &Operation) -> String {
Expand Down
Loading

0 comments on commit 91b5090

Please sign in to comment.