Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore!: remove merkle module from stdlib #7582

Merged
merged 5 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@
interned_statement_kinds: noirc_arena::Arena<StatementKind>,

// Interned `UnresolvedTypeData`s during comptime code.
interned_unresolved_type_datas: noirc_arena::Arena<UnresolvedTypeData>,

Check warning on line 219 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (datas)

// Interned `Pattern`s during comptime code.
interned_patterns: noirc_arena::Arena<Pattern>,

/// Determins whether to run in LSP mode. In LSP mode references are tracked.

Check warning on line 224 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Determins)
pub(crate) lsp_mode: bool,

/// Store the location of the references in the graph.
Expand Down Expand Up @@ -697,7 +697,7 @@
quoted_types: Default::default(),
interned_expression_kinds: Default::default(),
interned_statement_kinds: Default::default(),
interned_unresolved_type_datas: Default::default(),

Check warning on line 700 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (datas)
interned_patterns: Default::default(),
lsp_mode: false,
location_indices: LocationIndices::default(),
Expand Down Expand Up @@ -1289,7 +1289,11 @@

/// Returns the type of an item stored in the Interner or Error if it was not found.
pub fn id_type(&self, index: impl Into<Index>) -> Type {
self.id_to_type.get(&index.into()).cloned().unwrap_or(Type::Error)
self.try_id_type(index).cloned().unwrap_or(Type::Error)
}

pub fn try_id_type(&self, index: impl Into<Index>) -> Option<&Type> {
self.id_to_type.get(&index.into())
}

/// Returns the type of the definition or `Type::Error` if it was not found.
Expand Down Expand Up @@ -2180,11 +2184,11 @@
&mut self,
typ: UnresolvedTypeData,
) -> InternedUnresolvedTypeData {
InternedUnresolvedTypeData(self.interned_unresolved_type_datas.insert(typ))

Check warning on line 2187 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (datas)
}

pub fn get_unresolved_type_data(&self, id: InternedUnresolvedTypeData) -> &UnresolvedTypeData {
&self.interned_unresolved_type_datas[id.0]

Check warning on line 2191 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (datas)
}

/// Returns the type of an operator (which is always a function), along with its return type.
Expand Down
21 changes: 18 additions & 3 deletions compiler/noirc_frontend/src/resolve_locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,24 @@
}

/// Returns the Type of the expression that exists at the given location.
pub fn type_at_location(&self, location: Location) -> Option<Type> {
let index = self.find_location_index(location)?;
Some(self.id_type(index))
pub fn type_at_location(&self, location: Location) -> Option<&Type> {
// This is similar to `find_location_index` except that we skip indexes for which there is no type
let mut location_candidate: Option<(&Index, &Location, &Type)> = None;

for (index, interned_location) in self.id_to_location.iter() {
if interned_location.contains(&location) {
if let Some(typ) = self.try_id_type(*index) {
if let Some(current_location) = location_candidate {
if interned_location.span.is_smaller(&current_location.1.span) {
location_candidate = Some((index, interned_location, typ));
}
} else {
location_candidate = Some((index, interned_location, typ));
}
}
}
}
location_candidate.map(|(_index, _location, typ)| typ)
}

/// Returns the [Location] of the definition of the given Ident found at [Span] of the given [FileId].
Expand Down Expand Up @@ -186,7 +201,7 @@
///
/// ### Example:
/// ```nr
/// trait Fieldable {

Check warning on line 204 in compiler/noirc_frontend/src/resolve_locations.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Fieldable)
/// fn to_field(self) -> Field;
/// ^------------------------------\
/// } |
Expand Down
48 changes: 0 additions & 48 deletions docs/docs/how_to/merkle-proof.mdx

This file was deleted.

58 changes: 0 additions & 58 deletions docs/docs/noir/standard_library/merkle_trees.md

This file was deleted.

1 change: 0 additions & 1 deletion noir_stdlib/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub mod hash;
pub mod aes128;
pub mod array;
pub mod slice;
pub mod merkle;
pub mod ecdsa_secp256k1;
pub mod ecdsa_secp256r1;
pub mod embedded_curve_ops;
Expand Down
19 changes: 0 additions & 19 deletions noir_stdlib/src/merkle.nr

This file was deleted.

19 changes: 17 additions & 2 deletions test_programs/execution_success/merkle_insert/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,23 @@ fn main(
leaf: Field,
index: Field,
) {
assert(old_root == std::merkle::compute_merkle_root(old_leaf, index, old_hash_path));
assert(old_root == compute_merkle_root(old_leaf, index, old_hash_path));

let calculated_root = std::merkle::compute_merkle_root(leaf, index, old_hash_path);
let calculated_root = compute_merkle_root(leaf, index, old_hash_path);
assert(new_root == calculated_root);
}

fn compute_merkle_root<let N: u32>(leaf: Field, index: Field, hash_path: [Field; N]) -> Field {
let index_bits: [u1; N] = index.to_le_bits();
let mut current = leaf;
for i in 0..N {
let path_bit = index_bits[i] as bool;
let (hash_left, hash_right) = if path_bit {
(hash_path[i], current)
} else {
(current, hash_path[i])
};
current = std::hash::pedersen_hash([hash_left, hash_right]);
}
current
}
17 changes: 16 additions & 1 deletion test_programs/execution_success/simple_shield/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,22 @@ fn main(
// Compute output note nullifier
let receiver_note_commitment = std::hash::pedersen_commitment([to_pubkey_x, to_pubkey_y]);
// Check that the input note nullifier is in the root
assert(note_root == std::merkle::compute_merkle_root(note_commitment.x, index, note_hash_path));
assert(note_root == compute_merkle_root(note_commitment.x, index, note_hash_path));

[nullifier.x, receiver_note_commitment.x]
}

fn compute_merkle_root<let N: u32>(leaf: Field, index: Field, hash_path: [Field; N]) -> Field {
let index_bits: [u1; N] = index.to_le_bits();
let mut current = leaf;
for i in 0..N {
let path_bit = index_bits[i] as bool;
let (hash_left, hash_right) = if path_bit {
(hash_path[i], current)
} else {
(current, hash_path[i])
};
current = std::hash::pedersen_hash([hash_left, hash_right]);
}
current
}
2 changes: 1 addition & 1 deletion tooling/lsp/src/requests/code_action/import_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl CodeActionFinder<'_> {
};

let trait_methods =
self.interner.lookup_trait_methods(&typ, &method_call.method_name.0.contents, true);
self.interner.lookup_trait_methods(typ, &method_call.method_name.0.contents, true);
let trait_ids: HashSet<_> = trait_methods.iter().map(|(_, trait_id)| *trait_id).collect();

for trait_id in trait_ids {
Expand Down
8 changes: 4 additions & 4 deletions tooling/lsp/src/requests/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
use super::{TraitReexport, process_request};

mod auto_import;
mod builtins;

Check warning on line 51 in tooling/lsp/src/requests/completion.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (builtins)
mod completion_items;
mod kinds;
mod sort_text;
Expand Down Expand Up @@ -1448,7 +1448,7 @@
let prefix = "";
let self_prefix = false;
self.complete_type_fields_and_methods(
&typ,
typ,
prefix,
FunctionCompletionKind::Name,
self_prefix,
Expand Down Expand Up @@ -1481,7 +1481,7 @@
let prefix = prefix[0..offset].to_string();
let self_prefix = false;
self.complete_type_fields_and_methods(
&typ,
typ,
&prefix,
FunctionCompletionKind::Name,
self_prefix,
Expand Down Expand Up @@ -1658,7 +1658,7 @@
let prefix = "";
let self_prefix = false;
self.complete_type_fields_and_methods(
&typ,
typ,
prefix,
FunctionCompletionKind::NameAndParameters,
self_prefix,
Expand Down Expand Up @@ -1731,7 +1731,7 @@
let prefix = ident.to_string().to_case(Case::Snake);
let self_prefix = false;
self.complete_type_fields_and_methods(
&typ,
typ,
&prefix,
FunctionCompletionKind::NameAndParameters,
self_prefix,
Expand Down Expand Up @@ -1949,8 +1949,8 @@
///
/// For example:
///
/// // "merk" and "ro" match "merkle" and "root" and are in order

Check warning on line 1952 in tooling/lsp/src/requests/completion.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (merk)
/// name_matches("compute_merkle_root", "merk_ro") == true

Check warning on line 1953 in tooling/lsp/src/requests/completion.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (merk)
///
/// // "ro" matches "root", but "merkle" comes before it, so no match
/// name_matches("compute_merkle_root", "ro_mer") == false
Expand Down
6 changes: 3 additions & 3 deletions tooling/lsp/src/requests/completion/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
#[test]
async fn test_use_first_segment() {
let src = r#"
mod foobaz {}

Check warning on line 133 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foobaz)
mod foobar {}
use foob>|<
"#;
Expand Down Expand Up @@ -2115,17 +2115,17 @@
async fn test_auto_import_from_std() {
let src = r#"
fn main() {
compute_merkle_roo>|<
zeroe>|<
}
"#;
let items = get_completions(src).await;
assert_eq!(items.len(), 1);

let item = &items[0];
assert_eq!(item.label, "compute_merkle_root(…)");
assert_eq!(item.label, "zeroed()");
assert_eq!(
item.label_details.as_ref().unwrap().detail,
Some("(use std::merkle::compute_merkle_root)".to_string()),
Some("(use std::mem::zeroed)".to_string()),
);
}

Expand Down
8 changes: 4 additions & 4 deletions tooling/lsp/src/requests/hover/from_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Visitor for HoverFinder<'_> {
}
}

fn format_integer(typ: Type, value: SignedField) -> String {
fn format_integer(typ: &Type, value: SignedField) -> String {
let value_base_10 = value.field.to_string();

// For simplicity we parse the value as a BigInt to convert it to hex
Expand Down Expand Up @@ -98,22 +98,22 @@ mod tests {
let typ = Type::FieldElement;
let value = SignedField::positive(0_u128);
let expected = " Field\n---\nvalue of literal: `0 (0x00)`";
assert_eq!(format_integer(typ, value), expected);
assert_eq!(format_integer(&typ, value), expected);
}

#[test]
fn format_positive_integer() {
let typ = Type::Integer(Signedness::Unsigned, IntegerBitSize::ThirtyTwo);
let value = SignedField::positive(123456_u128);
let expected = " u32\n---\nvalue of literal: `123456 (0x1e240)`";
assert_eq!(format_integer(typ, value), expected);
assert_eq!(format_integer(&typ, value), expected);
}

#[test]
fn format_negative_integer() {
let typ = Type::Integer(Signedness::Signed, IntegerBitSize::SixtyFour);
let value = SignedField::new(987654_u128.into(), true);
let expected = " i64\n---\nvalue of literal: `-987654 (-0xf1206)`";
assert_eq!(format_integer(typ, value), expected);
assert_eq!(format_integer(&typ, value), expected);
}
}
2 changes: 1 addition & 1 deletion tooling/lsp/src/requests/inlay_hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl<'a> InlayHintCollector<'a> {

self.push_type_hint(
object_lsp_location,
&typ,
typ,
false, // not editable
false, // don't include colon
);
Expand Down
4 changes: 2 additions & 2 deletions tooling/lsp/src/requests/signature_help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ impl<'a> SignatureFinder<'a> {
}

// Otherwise, the call must be a reference to an fn type
if let Some(mut typ) = self.interner.type_at_location(location) {
typ = typ.follow_bindings();
if let Some(typ) = self.interner.type_at_location(location) {
let mut typ = typ.follow_bindings();
if let Type::Forall(_, forall_typ) = typ {
typ = *forall_typ;
}
Expand Down
Loading