Skip to content

Commit

Permalink
feat: Sync from noir (#8701)
Browse files Browse the repository at this point in the history
Automated pull of development from the
[noir](https://github.com/noir-lang/noir) programming language, a
dependency of Aztec.
BEGIN_COMMIT_OVERRIDE
feat: swap endianness in-place in keccak implementation
(noir-lang/noir#6128)
feat: (LSP) remove unused imports
(noir-lang/noir#6129)
fix: handle parenthesized expressions in array length
(noir-lang/noir#6132)
chore: remove bubble_up_constrains
(noir-lang/noir#6127)
fix: Consider constants as used values to keep their rc ops
(noir-lang/noir#6122)
END_COMMIT_OVERRIDE

---------

Co-authored-by: TomAFrench <[email protected]>
Co-authored-by: ludamad <[email protected]>
Co-authored-by: Tom French <[email protected]>
  • Loading branch information
4 people authored Sep 24, 2024
1 parent 9a1b5b5 commit 4522c4f
Show file tree
Hide file tree
Showing 19 changed files with 427 additions and 200 deletions.
2 changes: 1 addition & 1 deletion .noir-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5598059576c6cbc72474aff4b18bc5e4bb9f08e1
e3cdebe515e4dc4ee6e16e01bd8af25135939798

This file was deleted.

4 changes: 4 additions & 0 deletions noir/noir-repo/compiler/noirc_evaluator/src/ssa/opt/die.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,17 @@ impl Context {
self.used_values.insert(value_id);
}
Value::Array { array, .. } => {
self.used_values.insert(value_id);
for elem in array {
self.mark_used_instruction_results(dfg, *elem);
}
}
Value::Param { .. } => {
self.used_values.insert(value_id);
}
Value::NumericConstant { .. } => {
self.used_values.insert(value_id);
}
_ => {
// Does not comprise of any instruction results
}
Expand Down
1 change: 0 additions & 1 deletion noir/noir-repo/compiler/noirc_evaluator/src/ssa/opt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
mod array_set;
mod as_slice_length;
mod assert_constant;
mod bubble_up_constrains;
mod constant_folding;
mod defunctionalize;
mod die;
Expand Down
1 change: 1 addition & 0 deletions noir/noir-repo/compiler/noirc_frontend/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ impl UnresolvedTypeExpression {
ExpressionKind::AsTraitPath(path) => {
Ok(UnresolvedTypeExpression::AsTraitPath(Box::new(path)))
}
ExpressionKind::Parenthesized(expr) => Self::from_expr_helper(*expr),
_ => Err(expr),
}
}
Expand Down
6 changes: 5 additions & 1 deletion noir/noir-repo/compiler/noirc_frontend/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,13 @@ impl Display for UseTree {

match &self.kind {
UseTreeKind::Path(name, alias) => {
if !(self.prefix.segments.is_empty() && self.prefix.kind == PathKind::Plain) {
write!(f, "::")?;
}

write!(f, "{name}")?;

while let Some(alias) = alias {
if let Some(alias) = alias {
write!(f, " as {alias}")?;
}

Expand Down
4 changes: 2 additions & 2 deletions noir/noir-repo/compiler/noirc_frontend/src/ast/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ pub trait Visitor {
true
}

fn visit_import(&mut self, _: &UseTree, _visibility: ItemVisibility) -> bool {
fn visit_import(&mut self, _: &UseTree, _: Span, _visibility: ItemVisibility) -> bool {
true
}

Expand Down Expand Up @@ -506,7 +506,7 @@ impl Item {
}
ItemKind::Trait(noir_trait) => noir_trait.accept(self.span, visitor),
ItemKind::Import(use_tree, visibility) => {
if visitor.visit_import(use_tree, *visibility) {
if visitor.visit_import(use_tree, self.span, *visibility) {
use_tree.accept(visitor);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ impl DefCollector {
crate_id: CrateId,
errors: &mut Vec<(CompilationError, FileId)>,
) {
let unused_imports = context.def_interner.usage_tracker.unused_items().iter();
let unused_imports = context.def_interner.unused_items().iter();
let unused_imports = unused_imports.filter(|(module_id, _)| module_id.krate == crate_id);

errors.extend(unused_imports.flat_map(|(module_id, usage_tracker)| {
Expand Down
7 changes: 7 additions & 0 deletions noir/noir-repo/compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::hir::type_check::generics::TraitGenerics;
use crate::hir_def::traits::NamedType;
use crate::macros_api::ModuleDefId;
use crate::macros_api::UnaryOp;
use crate::usage_tracker::UnusedItem;
use crate::usage_tracker::UsageTracker;
use crate::QuotedType;

Expand Down Expand Up @@ -2249,6 +2250,12 @@ impl NodeInterner {
pub fn doc_comments(&self, id: ReferenceId) -> Option<&Vec<String>> {
self.doc_comments.get(&id)
}

pub fn unused_items(
&self,
) -> &std::collections::HashMap<ModuleId, std::collections::HashMap<Ident, UnusedItem>> {
self.usage_tracker.unused_items()
}
}

impl Methods {
Expand Down
26 changes: 6 additions & 20 deletions noir/noir-repo/noir_stdlib/src/hash/keccak.nr
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,27 @@ pub(crate) fn keccak256<let N: u32>(input: [u8; N], message_size: u32) -> [u8; 3
block_bytes[message_size] = 1;
block_bytes[real_blocks_bytes - 1] = 0x80;

// keccak lanes interpret memory as little-endian integers,
// means we need to swap our byte ordering
let num_limbs = max_blocks * LIMBS_PER_BLOCK; //max_blocks_length / WORD_SIZE;
for i in 0..num_limbs {
let mut temp = [0; WORD_SIZE];
let word_size_times_i = WORD_SIZE * i;
for j in 0..WORD_SIZE {
temp[j] = block_bytes[word_size_times_i+j];
}
for j in 0..WORD_SIZE {
block_bytes[word_size_times_i + j] = temp[7 - j];
}
}

let mut sliced_buffer = Vec::new();
// populate a vector of 64-bit limbs from our byte array
for i in 0..num_limbs {
let word_size_times_i = i * WORD_SIZE;
let ws_times_i_plus_7 = word_size_times_i + 7;
let limb_start = WORD_SIZE * i;

let mut sliced = 0;
if (word_size_times_i + WORD_SIZE > max_blocks_length) {
let slice_size = max_blocks_length - word_size_times_i;
if (limb_start + WORD_SIZE > max_blocks_length) {
let slice_size = max_blocks_length - limb_start;
let byte_shift = (WORD_SIZE - slice_size) * 8;
let mut v = 1;
for k in 0..slice_size {
sliced += v * (block_bytes[ws_times_i_plus_7-k] as Field);
sliced += v * (block_bytes[limb_start+k] as Field);
v *= 256;
}
let w = 1 << (byte_shift as u8);
sliced *= w as Field;
} else {
let mut v = 1;
for k in 0..WORD_SIZE {
sliced += v * (block_bytes[ws_times_i_plus_7-k] as Field);
sliced += v * (block_bytes[limb_start+k] as Field);
v *= 256;
}
}
Expand Down Expand Up @@ -156,4 +143,3 @@ mod tests {
assert_eq(keccak256(input, 13), result);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "parenthesized_expression_in_array_length"
type = "bin"
authors = [""]
compiler_version = ">=0.32.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
global N = 100;
global BLOCK_SIZE = 10;

fn main() {
let _: [Field; 110] = [0; ((N + BLOCK_SIZE) * BLOCK_SIZE) / BLOCK_SIZE];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "brillig_constant_reference_regression"
type = "bin"
authors = [""]

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sorted_index = ["1", "0"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
unconstrained fn main(sorted_index: [u32; 2]) {
let original = [
55,
11
];

let mut sorted = original; // Stores the constant "original" into the sorted reference

for i in 0..2 {
let index = sorted_index[i];
let value = original[index];
sorted[i] = value; // On first iteration, we should not mutate the original constant array, RC should be > 1
}

assert_eq(sorted[1], 55);
}
Loading

0 comments on commit 4522c4f

Please sign in to comment.