Skip to content

Commit

Permalink
feat(meta): Comptime keccak (noir-lang/noir#5854)
Browse files Browse the repository at this point in the history
feat(optimization): Avoid merging identical (by ID) arrays (noir-lang/noir#5853)
feat: add `FunctionDef::body` (noir-lang/noir#5825)
fix(sha256): Fix upper bound when building msg block and delay final block compression under certain cases  (noir-lang/noir#5838)
feat: remove unnecessary copying of vector size during reversal (noir-lang/noir#5852)
chore: Add missing cases to arithmetic generics (noir-lang/noir#5841)
feat: warn on unused imports (noir-lang/noir#5847)
chore: add documentation to `to_be_bytes`, etc. (noir-lang/noir#5843)
feat: simplify constant calls to `poseidon2_permutation`, `schnorr_verify` and `embedded_curve_add` (noir-lang/noir#5140)
chore: don't require empty `Prover.toml` for programs with zero arguments but a return value (noir-lang/noir#5845)
fix!: Check unused generics are bound (noir-lang/noir#5840)
chore(perf): Simplify poseidon2 algorithm  (noir-lang/noir#5811)
chore: redo typo PR by nnsW3 (noir-lang/noir#5834)
fix(sha256): Perform compression per block and utilize ROM instead of RAM when setting up the message block (noir-lang/noir#5760)
chore(perf): Update to stdlib keccak for reduced Brillig code size (noir-lang/noir#5827)
  • Loading branch information
AztecBot committed Aug 28, 2024
2 parents b3770d3 + 5a6289c commit 7bd6240
Show file tree
Hide file tree
Showing 66 changed files with 2,286 additions and 398 deletions.
2 changes: 1 addition & 1 deletion .noir-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
c7473c6fcc6cbfd118b0352229ff86001cde3a64
0e8becc7bccee2ae4e4e3ef373df08c3e9ef88c9
12 changes: 10 additions & 2 deletions noir/noir-repo/aztec_macros/src/utils/parse_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,10 @@ fn empty_statement(statement: &mut Statement) {
StatementKind::For(for_loop_statement) => empty_for_loop_statement(for_loop_statement),
StatementKind::Comptime(statement) => empty_statement(statement),
StatementKind::Semi(expression) => empty_expression(expression),
StatementKind::Break | StatementKind::Continue | StatementKind::Error => (),
StatementKind::Break
| StatementKind::Continue
| StatementKind::Interned(_)
| StatementKind::Error => (),
}
}

Expand Down Expand Up @@ -271,12 +274,15 @@ fn empty_expression(expression: &mut Expression) {
ExpressionKind::Unsafe(block_expression, _span) => {
empty_block_expression(block_expression);
}
ExpressionKind::Quote(..) | ExpressionKind::Resolved(_) | ExpressionKind::Error => (),
ExpressionKind::AsTraitPath(path) => {
empty_unresolved_type(&mut path.typ);
empty_path(&mut path.trait_path);
empty_ident(&mut path.impl_item);
}
ExpressionKind::Quote(..)
| ExpressionKind::Resolved(_)
| ExpressionKind::Interned(_)
| ExpressionKind::Error => (),
}
}

Expand Down Expand Up @@ -353,6 +359,7 @@ fn empty_unresolved_type(unresolved_type: &mut UnresolvedType) {
| UnresolvedTypeData::Unit
| UnresolvedTypeData::Quoted(_)
| UnresolvedTypeData::Resolved(_)
| UnresolvedTypeData::Interned(_)
| UnresolvedTypeData::Unspecified
| UnresolvedTypeData::Error => (),
}
Expand Down Expand Up @@ -531,6 +538,7 @@ fn empty_lvalue(lvalue: &mut LValue) {
empty_expression(index);
}
LValue::Dereference(lvalue, _) => empty_lvalue(lvalue),
LValue::Interned(..) => (),
}
}

Expand Down
26 changes: 23 additions & 3 deletions noir/noir-repo/compiler/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ pub struct CompileOptions {
pub skip_underconstrained_check: bool,
}

#[derive(Clone, Debug, Default)]
pub struct CheckOptions {
pub compile_options: CompileOptions,
pub error_on_unused_imports: bool,
}

impl CheckOptions {
pub fn new(compile_options: &CompileOptions, error_on_unused_imports: bool) -> Self {
Self { compile_options: compile_options.clone(), error_on_unused_imports }
}
}

pub fn parse_expression_width(input: &str) -> Result<ExpressionWidth, std::io::Error> {
use std::io::{Error, ErrorKind};
let width = input
Expand Down Expand Up @@ -278,8 +290,10 @@ pub fn add_dep(
pub fn check_crate(
context: &mut Context,
crate_id: CrateId,
options: &CompileOptions,
check_options: &CheckOptions,
) -> CompilationResult<()> {
let options = &check_options.compile_options;

let macros: &[&dyn MacroProcessor] =
if options.disable_macros { &[] } else { &[&aztec_macros::AztecMacro] };

Expand All @@ -289,6 +303,7 @@ pub fn check_crate(
context,
options.debug_comptime_in_file.as_deref(),
options.arithmetic_generics,
check_options.error_on_unused_imports,
macros,
);
errors.extend(diagnostics.into_iter().map(|(error, file_id)| {
Expand Down Expand Up @@ -322,7 +337,10 @@ pub fn compile_main(
options: &CompileOptions,
cached_program: Option<CompiledProgram>,
) -> CompilationResult<CompiledProgram> {
let (_, mut warnings) = check_crate(context, crate_id, options)?;
let error_on_unused_imports = true;
let check_options = CheckOptions::new(options, error_on_unused_imports);

let (_, mut warnings) = check_crate(context, crate_id, &check_options)?;

let main = context.get_main_function(&crate_id).ok_or_else(|| {
// TODO(#2155): This error might be a better to exist in Nargo
Expand Down Expand Up @@ -357,7 +375,9 @@ pub fn compile_contract(
crate_id: CrateId,
options: &CompileOptions,
) -> CompilationResult<CompiledContract> {
let (_, warnings) = check_crate(context, crate_id, options)?;
let error_on_unused_imports = true;
let check_options = CheckOptions::new(options, error_on_unused_imports);
let (_, warnings) = check_crate(context, crate_id, &check_options)?;

// TODO: We probably want to error if contracts is empty
let contracts = context.get_all_contracts(&crate_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,20 +276,14 @@ impl<F: AcirField + DebugToString, Registers: RegisterAllocator> BrilligContext<
let index_at_end_of_array = self.allocate_register();
let end_value_register = self.allocate_register();

self.codegen_loop(iteration_count, |ctx, iterator_register| {
// Load both values
ctx.codegen_array_get(pointer, iterator_register, start_value_register);
self.mov_instruction(index_at_end_of_array, vector.size);

self.codegen_loop(iteration_count, |ctx, iterator_register| {
// The index at the end of array is size - 1 - iterator
ctx.mov_instruction(index_at_end_of_array, size);
ctx.codegen_usize_op_in_place(index_at_end_of_array, BrilligBinaryOp::Sub, 1);
ctx.memory_op_instruction(
index_at_end_of_array,
iterator_register.address,
index_at_end_of_array,
BrilligBinaryOp::Sub,
);

// Load both values
ctx.codegen_array_get(vector.pointer, iterator_register, start_value_register);
ctx.codegen_array_get(
pointer,
SingleAddrVariable::new_usize(index_at_end_of_array),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,6 @@ impl<F: AcirField> AcirContext<F> {
name,
BlackBoxFunc::MultiScalarMul
| BlackBoxFunc::Keccakf1600
| BlackBoxFunc::Sha256Compression
| BlackBoxFunc::Blake2s
| BlackBoxFunc::Blake3
| BlackBoxFunc::AND
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -733,11 +733,15 @@ impl Instruction {
}
}

let then_value = dfg.resolve(*then_value);
let else_value = dfg.resolve(*else_value);
if then_value == else_value {
return SimplifiedTo(then_value);
}

if matches!(&typ, Type::Numeric(_)) {
let then_condition = *then_condition;
let then_value = *then_value;
let else_condition = *else_condition;
let else_value = *else_value;

let result = ValueMerger::merge_numeric_values(
dfg,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ impl<'a> ValueMerger<'a> {
then_value: ValueId,
else_value: ValueId,
) -> ValueId {
let then_value = self.dfg.resolve(then_value);
let else_value = self.dfg.resolve(else_value);

if then_value == else_value {
return then_value;
}

match self.dfg.type_of_value(then_value) {
Type::Numeric(_) => Self::merge_numeric_values(
self.dfg,
Expand Down
8 changes: 7 additions & 1 deletion noir/noir-repo/compiler/noirc_frontend/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::ast::{
};
use crate::hir::def_collector::errors::DefCollectorErrorKind;
use crate::macros_api::StructId;
use crate::node_interner::{ExprId, QuotedTypeId};
use crate::node_interner::{ExprId, InternedExpressionKind, QuotedTypeId};
use crate::token::{Attributes, FunctionAttribute, Token, Tokens};
use crate::{Kind, Type};
use acvm::{acir::AcirField, FieldElement};
Expand Down Expand Up @@ -43,6 +43,11 @@ pub enum ExpressionKind {
// code. It is used to translate function values back into the AST while
// guaranteeing they have the same instantiated type and definition id without resolving again.
Resolved(ExprId),

// This is an interned ExpressionKind during comptime code.
// The actual ExpressionKind can be retrieved with a NodeInterner.
Interned(InternedExpressionKind),

Error,
}

Expand Down Expand Up @@ -603,6 +608,7 @@ impl Display for ExpressionKind {
Unsafe(block, _) => write!(f, "unsafe {block}"),
Error => write!(f, "Error"),
Resolved(_) => write!(f, "?Resolved"),
Interned(_) => write!(f, "?Interned"),
Unquote(expr) => write!(f, "$({expr})"),
Quote(tokens) => {
let tokens = vecmap(&tokens.0, ToString::to_string);
Expand Down
7 changes: 6 additions & 1 deletion noir/noir-repo/compiler/noirc_frontend/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub use traits::*;
pub use type_alias::*;

use crate::{
node_interner::QuotedTypeId,
node_interner::{InternedUnresolvedTypeData, QuotedTypeId},
parser::{ParserError, ParserErrorReason},
token::IntType,
BinaryTypeOperator,
Expand Down Expand Up @@ -141,6 +141,10 @@ pub enum UnresolvedTypeData {
/// as a result of being spliced into a macro's token stream input.
Resolved(QuotedTypeId),

// This is an interned UnresolvedTypeData during comptime code.
// The actual UnresolvedTypeData can be retrieved with a NodeInterner.
Interned(InternedUnresolvedTypeData),

Unspecified, // This is for when the user declares a variable without specifying it's type
Error,
}
Expand Down Expand Up @@ -297,6 +301,7 @@ impl std::fmt::Display for UnresolvedTypeData {
Unspecified => write!(f, "unspecified"),
Parenthesized(typ) => write!(f, "({typ})"),
Resolved(_) => write!(f, "(resolved type)"),
Interned(_) => write!(f, "?Interned"),
AsTraitPath(path) => write!(f, "{path}"),
}
}
Expand Down
48 changes: 47 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 @@ -13,6 +13,7 @@ use super::{
use crate::elaborator::types::SELF_TYPE_NAME;
use crate::lexer::token::SpannedToken;
use crate::macros_api::{SecondaryAttribute, UnresolvedTypeData};
use crate::node_interner::{InternedExpressionKind, InternedStatementKind};
use crate::parser::{ParserError, ParserErrorReason};
use crate::token::Token;

Expand Down Expand Up @@ -45,6 +46,9 @@ pub enum StatementKind {
Comptime(Box<Statement>),
// This is an expression with a trailing semi-colon
Semi(Expression),
// This is an interned StatementKind during comptime code.
// The actual StatementKind can be retrieved with a NodeInterner.
Interned(InternedStatementKind),
// This statement is the result of a recovered parse error.
// To avoid issuing multiple errors in later steps, it should
// be skipped in any future analysis if possible.
Expand Down Expand Up @@ -97,6 +101,9 @@ impl StatementKind {
// A semicolon on a for loop is optional and does nothing
StatementKind::For(_) => self,

// No semicolon needed for a resolved statement
StatementKind::Interned(_) => self,

StatementKind::Expression(expr) => {
match (&expr.kind, semi, last_statement_in_block) {
// Semicolons are optional for these expressions
Expand Down Expand Up @@ -534,6 +541,7 @@ pub enum LValue {
MemberAccess { object: Box<LValue>, field_name: Ident, span: Span },
Index { array: Box<LValue>, index: Expression, span: Span },
Dereference(Box<LValue>, Span),
Interned(InternedExpressionKind, Span),
}

#[derive(Debug, PartialEq, Eq, Clone)]
Expand Down Expand Up @@ -591,7 +599,7 @@ impl Recoverable for Pattern {
}

impl LValue {
fn as_expression(&self) -> Expression {
pub fn as_expression(&self) -> Expression {
let kind = match self {
LValue::Ident(ident) => ExpressionKind::Variable(Path::from_ident(ident.clone())),
LValue::MemberAccess { object, field_name, span: _ } => {
Expand All @@ -612,17 +620,53 @@ impl LValue {
rhs: lvalue.as_expression(),
}))
}
LValue::Interned(id, _) => ExpressionKind::Interned(*id),
};
let span = self.span();
Expression::new(kind, span)
}

pub fn from_expression(expr: Expression) -> LValue {
LValue::from_expression_kind(expr.kind, expr.span)
}

pub fn from_expression_kind(expr: ExpressionKind, span: Span) -> LValue {
match expr {
ExpressionKind::Variable(path) => LValue::Ident(path.as_ident().unwrap().clone()),
ExpressionKind::MemberAccess(member_access) => LValue::MemberAccess {
object: Box::new(LValue::from_expression(member_access.lhs)),
field_name: member_access.rhs,
span,
},
ExpressionKind::Index(index) => LValue::Index {
array: Box::new(LValue::from_expression(index.collection)),
index: index.index,
span,
},
ExpressionKind::Prefix(prefix) => {
if matches!(
prefix.operator,
crate::ast::UnaryOp::Dereference { implicitly_added: false }
) {
LValue::Dereference(Box::new(LValue::from_expression(prefix.rhs)), span)
} else {
panic!("Called LValue::from_expression with an invalid prefix operator")
}
}
ExpressionKind::Interned(id) => LValue::Interned(id, span),
_ => {
panic!("Called LValue::from_expression with an invalid expression")
}
}
}

pub fn span(&self) -> Span {
match self {
LValue::Ident(ident) => ident.span(),
LValue::MemberAccess { span, .. }
| LValue::Index { span, .. }
| LValue::Dereference(_, span) => *span,
LValue::Interned(_, span) => *span,
}
}
}
Expand Down Expand Up @@ -777,6 +821,7 @@ impl Display for StatementKind {
StatementKind::Continue => write!(f, "continue"),
StatementKind::Comptime(statement) => write!(f, "comptime {}", statement.kind),
StatementKind::Semi(semi) => write!(f, "{semi};"),
StatementKind::Interned(_) => write!(f, "(resolved);"),
StatementKind::Error => write!(f, "Error"),
}
}
Expand Down Expand Up @@ -809,6 +854,7 @@ impl Display for LValue {
}
LValue::Index { array, index, span: _ } => write!(f, "{array}[{index}]"),
LValue::Dereference(lvalue, _span) => write!(f, "*{lvalue}"),
LValue::Interned(_, _) => write!(f, "?Interned"),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions noir/noir-repo/compiler/noirc_frontend/src/debug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ impl DebugInstrumenter {
ast::LValue::Dereference(_ref, _span) => {
unimplemented![]
}
ast::LValue::Interned(..) => {
unimplemented![]
}
}
}
build_assign_member_stmt(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ impl<'context> Elaborator<'context> {
self.elaborate_unsafe_block(block_expression)
}
ExpressionKind::Resolved(id) => return (id, self.interner.id_type(id)),
ExpressionKind::Interned(id) => {
let expr_kind = self.interner.get_expression_kind(id);
let expr = Expression::new(expr_kind.clone(), expr.span);
return self.elaborate_expression(expr);
}
ExpressionKind::Error => (HirExpression::Error, Type::Error),
ExpressionKind::Unquote(_) => {
self.push_err(ResolverError::UnquoteUsedOutsideQuote { span: expr.span });
Expand Down
Loading

0 comments on commit 7bd6240

Please sign in to comment.