Skip to content

Commit

Permalink
feat: Sync from noir (#9569)
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
chore: use array instead of Vec in keccak256
(noir-lang/noir#6395)
fix: make keccak256 work with input lengths greater than 136 bytes
(noir-lang/noir#6393)
feat: support specifying generics on a struct when calling an associated
function (noir-lang/noir#6306)
fix: Display every bit in integer tokens
(noir-lang/noir#6360)
feat: better LSP hover for functions
(noir-lang/noir#6376)
feat: Add capacities to brillig vectors and use them in slice ops
(noir-lang/noir#6332)
feat: suggest removing `!` from macro call that doesn't return Quoted
(noir-lang/noir#6384)
fix: (formatter) correctly format quote delimiters
(noir-lang/noir#6377)
fix: allow globals in format strings
(noir-lang/noir#6382)
END_COMMIT_OVERRIDE

---------

Co-authored-by: sirasistant <[email protected]>
Co-authored-by: Tom French <[email protected]>
  • Loading branch information
3 people authored Oct 30, 2024
1 parent 9a5dce3 commit b82f3d1
Show file tree
Hide file tree
Showing 46 changed files with 1,711 additions and 655 deletions.
2 changes: 1 addition & 1 deletion .noir-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
60c770f5f2594eea31ac75c852980edefa40d9eb
075c3d32481314d900cbdea0d277de83444747ab
71 changes: 1 addition & 70 deletions noir/noir-repo/acvm-repo/acir_field/src/field_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign};
use crate::AcirField;

// XXX: Switch out for a trait and proper implementations
// This implementation is in-efficient, can definitely remove hex usage and Iterator instances for trivial functionality
// This implementation is inefficient, can definitely remove hex usage and Iterator instances for trivial functionality
#[derive(Default, Clone, Copy, Eq, PartialOrd, Ord)]
pub struct FieldElement<F: PrimeField>(F);

Expand All @@ -33,46 +33,6 @@ impl<F: PrimeField> std::fmt::Display for FieldElement<F> {
write!(f, "-")?;
}

// Number of bits needed to represent the smaller representation
let num_bits = smaller_repr.bits();

// Check if the number represents a power of 2
if smaller_repr.count_ones() == 1 {
let mut bit_index = 0;
for i in 0..num_bits {
if smaller_repr.bit(i) {
bit_index = i;
break;
}
}
return match bit_index {
0 => write!(f, "1"),
1 => write!(f, "2"),
2 => write!(f, "4"),
3 => write!(f, "8"),
_ => write!(f, "2{}", superscript(bit_index)),
};
}

// Check if number is a multiple of a power of 2.
// This is used because when computing the quotient
// we usually have numbers in the form 2^t * q + r
// We focus on 2^64, 2^32, 2^16, 2^8, 2^4 because
// they are common. We could extend this to a more
// general factorization strategy, but we pay in terms of CPU time
let mul_sign = "×";
for power in [64, 32, 16, 8, 4] {
let power_of_two = BigUint::from(2_u128).pow(power);
if &smaller_repr % &power_of_two == BigUint::zero() {
return write!(
f,
"2{}{}{}",
superscript(power as u64),
mul_sign,
smaller_repr / &power_of_two,
);
}
}
write!(f, "{smaller_repr}")
}
}
Expand Down Expand Up @@ -409,35 +369,6 @@ impl<F: PrimeField> SubAssign for FieldElement<F> {
}
}

// For pretty printing powers
fn superscript(n: u64) -> String {
if n == 0 {
"⁰".to_owned()
} else if n == 1 {
"¹".to_owned()
} else if n == 2 {
"²".to_owned()
} else if n == 3 {
"³".to_owned()
} else if n == 4 {
"⁴".to_owned()
} else if n == 5 {
"⁵".to_owned()
} else if n == 6 {
"⁶".to_owned()
} else if n == 7 {
"⁷".to_owned()
} else if n == 8 {
"⁸".to_owned()
} else if n == 9 {
"⁹".to_owned()
} else if n >= 10 {
superscript(n / 10) + &superscript(n % 10)
} else {
panic!("{}", n.to_string() + " can't be converted to superscript.");
}
}

#[cfg(test)]
mod tests {
use super::{AcirField, FieldElement};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use acvm::{

use crate::brillig::brillig_ir::{
brillig_variable::BrilligVariable, debug_show::DebugToString, registers::RegisterAllocator,
BrilligBinaryOp, BrilligContext,
BrilligContext,
};

/// Transforms SSA's black box function calls into the corresponding brillig instructions
Expand Down Expand Up @@ -395,19 +395,8 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString, Registers: Re

brillig_context.mov_instruction(out_len.address, outputs_vector.size);
// Returns slice, so we need to allocate memory for it after the fact
brillig_context.codegen_usize_op_in_place(
outputs_vector.size,
BrilligBinaryOp::Add,
2_usize,
);
brillig_context.increase_free_memory_pointer_instruction(outputs_vector.size);
// We also need to write the size of the vector to the memory
brillig_context.codegen_usize_op_in_place(
outputs_vector.pointer,
BrilligBinaryOp::Sub,
1_usize,
);
brillig_context.store_instruction(outputs_vector.pointer, out_len.address);

brillig_context.initialize_externally_returned_vector(*outputs, outputs_vector);

brillig_context.deallocate_heap_vector(inputs);
brillig_context.deallocate_heap_vector(outputs_vector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,34 +374,10 @@ impl<'block> BrilligBlock<'block> {
match output_register {
// Returned vectors need to emit some bytecode to format the result as a BrilligVector
ValueOrArray::HeapVector(heap_vector) => {
// Update the stack pointer so that we do not overwrite
// dynamic memory returned from other external calls
// Single values and allocation of fixed sized arrays has already been handled
// inside of `allocate_external_call_result`
let total_size = self.brillig_context.allocate_register();
self.brillig_context.codegen_usize_op(
heap_vector.size,
total_size,
BrilligBinaryOp::Add,
2, // RC and Length
self.brillig_context.initialize_externally_returned_vector(
output_variable.extract_vector(),
*heap_vector,
);

self.brillig_context
.increase_free_memory_pointer_instruction(total_size);
let brillig_vector = output_variable.extract_vector();
let size_pointer = self.brillig_context.allocate_register();

self.brillig_context.codegen_usize_op(
brillig_vector.pointer,
size_pointer,
BrilligBinaryOp::Add,
1_usize, // Slices are [RC, Size, ...items]
);
self.brillig_context
.store_instruction(size_pointer, heap_vector.size);
self.brillig_context.deallocate_register(size_pointer);
self.brillig_context.deallocate_register(total_size);

// Update the dynamic slice length maintained in SSA
if let ValueOrArray::MemoryAddress(len_index) = output_values[i - 1]
{
Expand Down Expand Up @@ -515,8 +491,11 @@ impl<'block> BrilligBlock<'block> {
element_size,
);

self.brillig_context
.codegen_initialize_vector(destination_vector, source_size_register);
self.brillig_context.codegen_initialize_vector(
destination_vector,
source_size_register,
None,
);

// Items
let vector_items_pointer =
Expand Down Expand Up @@ -1575,7 +1554,7 @@ impl<'block> BrilligBlock<'block> {
let size = self
.brillig_context
.make_usize_constant_instruction(array.len().into());
self.brillig_context.codegen_initialize_vector(vector, size);
self.brillig_context.codegen_initialize_vector(vector, size, None);
self.brillig_context.deallocate_single_addr(size);
}
_ => unreachable!(
Expand Down Expand Up @@ -1821,11 +1800,6 @@ impl<'block> BrilligBlock<'block> {
// The stack pointer will then be updated by the caller of this method
// once the external call is resolved and the array size is known
self.brillig_context.load_free_memory_pointer_instruction(vector.pointer);
self.brillig_context.indirect_const_instruction(
vector.pointer,
BRILLIG_MEMORY_ADDRESSING_BIT_SIZE,
1_usize.into(),
);

variable
}
Expand Down
Loading

0 comments on commit b82f3d1

Please sign in to comment.