Skip to content

Commit dbec6f2

Browse files
authored
feat!: remove concept of noir fallbacks for foreign functions (#1371)
1 parent 5d1efd5 commit dbec6f2

File tree

9 files changed

+9
-79
lines changed

9 files changed

+9
-79
lines changed

crates/noirc_driver/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ pub use program::CompiledProgram;
2929
pub struct Driver {
3030
context: Context,
3131
language: Language,
32+
// We retain this as we need to pass this into `create_circuit` once signature is updated to allow.
33+
#[allow(dead_code)]
34+
is_opcode_supported: Box<dyn Fn(&Opcode) -> bool>,
3235
}
3336

3437
#[derive(Args, Clone, Debug, Serialize, Deserialize)]
@@ -68,9 +71,7 @@ impl Default for CompileOptions {
6871

6972
impl Driver {
7073
pub fn new(language: &Language, is_opcode_supported: Box<dyn Fn(&Opcode) -> bool>) -> Self {
71-
let mut driver = Driver { context: Context::default(), language: language.clone() };
72-
driver.context.def_interner.set_opcode_support(is_opcode_supported);
73-
driver
74+
Driver { context: Context::default(), language: language.clone(), is_opcode_supported }
7475
}
7576

7677
// This is here for backwards compatibility

crates/noirc_frontend/src/ast/function.rs

-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ impl From<FunctionDefinition> for NoirFunction {
7676
let kind = match fd.attribute {
7777
Some(Attribute::Builtin(_)) => FunctionKind::Builtin,
7878
Some(Attribute::Foreign(_)) => FunctionKind::LowLevel,
79-
Some(Attribute::Alternative(_)) => FunctionKind::Normal,
8079
Some(Attribute::Test) => FunctionKind::Normal,
8180
None => FunctionKind::Normal,
8281
};

crates/noirc_frontend/src/hir/mod.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ pub mod type_check;
66

77
use crate::graph::{CrateGraph, CrateId};
88
use crate::node_interner::NodeInterner;
9-
use acvm::acir::circuit::Opcode;
109
use def_map::CrateDefMap;
1110
use fm::FileManager;
1211
use std::collections::HashMap;
@@ -29,20 +28,14 @@ pub struct Context {
2928
pub type StorageSlot = u32;
3029

3130
impl Context {
32-
pub fn new(
33-
file_manager: FileManager,
34-
crate_graph: CrateGraph,
35-
is_opcode_supported: Box<dyn Fn(&Opcode) -> bool>,
36-
) -> Context {
37-
let mut ctx = Context {
31+
pub fn new(file_manager: FileManager, crate_graph: CrateGraph) -> Context {
32+
Context {
3833
def_interner: NodeInterner::default(),
3934
def_maps: HashMap::new(),
4035
crate_graph,
4136
file_manager,
4237
storage_slots: HashMap::new(),
43-
};
44-
ctx.def_interner.set_opcode_support(is_opcode_supported);
45-
ctx
38+
}
4639
}
4740

4841
/// Returns the CrateDefMap for a given CrateId.

crates/noirc_frontend/src/hir/resolution/resolver.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -1149,23 +1149,7 @@ impl<'a> Resolver<'a> {
11491149
let span = path.span();
11501150
let id = self.resolve_path(path)?;
11511151

1152-
if let Some(mut function) = TryFromModuleDefId::try_from(id) {
1153-
// Check if this is an unsupported low level opcode. If so, replace it with
1154-
// an alternative in the stdlib.
1155-
if let Some(meta) = self.interner.try_function_meta(&function) {
1156-
if meta.kind == crate::FunctionKind::LowLevel {
1157-
let attribute = meta.attributes.expect("all low level functions must contain an attribute which contains the opcode which it links to");
1158-
let opcode = attribute.foreign().expect(
1159-
"ice: function marked as foreign, but attribute kind does not match this",
1160-
);
1161-
if !self.interner.foreign(&opcode) {
1162-
if let Some(new_id) = self.interner.get_alt(opcode) {
1163-
function = new_id;
1164-
}
1165-
}
1166-
}
1167-
}
1168-
1152+
if let Some(function) = TryFromModuleDefId::try_from(id) {
11691153
return Ok(self.interner.function_definition_id(function));
11701154
}
11711155

crates/noirc_frontend/src/lexer/token.rs

-4
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,6 @@ impl IntType {
324324
pub enum Attribute {
325325
Foreign(String),
326326
Builtin(String),
327-
Alternative(String),
328327
Test,
329328
}
330329

@@ -333,7 +332,6 @@ impl fmt::Display for Attribute {
333332
match *self {
334333
Attribute::Foreign(ref k) => write!(f, "#[foreign({k})]"),
335334
Attribute::Builtin(ref k) => write!(f, "#[builtin({k})]"),
336-
Attribute::Alternative(ref k) => write!(f, "#[alternative({k})]"),
337335
Attribute::Test => write!(f, "#[test]"),
338336
}
339337
}
@@ -365,7 +363,6 @@ impl Attribute {
365363
let tok = match attribute_type {
366364
"foreign" => Token::Attribute(Attribute::Foreign(attribute_name.to_string())),
367365
"builtin" => Token::Attribute(Attribute::Builtin(attribute_name.to_string())),
368-
"alternative" => Token::Attribute(Attribute::Alternative(attribute_name.to_string())),
369366
_ => {
370367
return Err(LexerErrorKind::MalformedFuncAttribute { span, found: word.to_owned() })
371368
}
@@ -401,7 +398,6 @@ impl AsRef<str> for Attribute {
401398
match self {
402399
Attribute::Foreign(string) => string,
403400
Attribute::Builtin(string) => string,
404-
Attribute::Alternative(string) => string,
405401
Attribute::Test => "",
406402
}
407403
}

crates/noirc_frontend/src/main.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,7 @@ fn main() {
2727
let crate_id = crate_graph.add_crate_root(CrateType::Library, root_file_id);
2828

2929
// initiate context with file manager and crate graph
30-
let mut context = Context::new(
31-
fm,
32-
crate_graph,
33-
#[allow(deprecated)]
34-
Box::new(acvm::default_is_opcode_supported(acvm::Language::R1CS)),
35-
);
30+
let mut context = Context::new(fm, crate_graph);
3631

3732
// Now create the CrateDefMap
3833
// This is preamble for analysis

crates/noirc_frontend/src/node_interner.rs

-36
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use std::collections::{BTreeMap, HashMap};
22

3-
use acvm::acir::circuit::opcodes::BlackBoxFuncCall;
4-
use acvm::acir::circuit::Opcode;
5-
use acvm::Language;
63
use arena::{Arena, Index};
74
use fm::FileId;
85
use iter_extended::vecmap;
@@ -69,9 +66,6 @@ pub struct NodeInterner {
6966

7067
next_type_variable_id: usize,
7168

72-
//used for fallback mechanism
73-
is_opcode_supported: Box<dyn Fn(&Opcode) -> bool>,
74-
7569
delayed_type_checks: Vec<TypeCheckFn>,
7670

7771
/// A map from a struct type and method name to a function id for the method.
@@ -258,8 +252,6 @@ impl Default for NodeInterner {
258252
field_indices: HashMap::new(),
259253
next_type_variable_id: 0,
260254
globals: HashMap::new(),
261-
#[allow(deprecated)]
262-
is_opcode_supported: Box::new(acvm::default_is_opcode_supported(Language::R1CS)),
263255
delayed_type_checks: vec![],
264256
struct_methods: HashMap::new(),
265257
primitive_methods: HashMap::new(),
@@ -396,17 +388,6 @@ impl NodeInterner {
396388
self.func_meta.insert(func_id, func_data);
397389
}
398390

399-
pub fn get_alt(&self, opcode: String) -> Option<FuncId> {
400-
for (func_id, meta) in &self.func_meta {
401-
if let Some(crate::token::Attribute::Alternative(name)) = &meta.attributes {
402-
if *name == opcode {
403-
return Some(*func_id);
404-
}
405-
}
406-
}
407-
None
408-
}
409-
410391
pub fn push_definition(
411392
&mut self,
412393
name: String,
@@ -580,23 +561,6 @@ impl NodeInterner {
580561
self.function_definition_ids[&function]
581562
}
582563

583-
pub fn set_opcode_support(&mut self, is_opcode_supported: Box<dyn Fn(&Opcode) -> bool>) {
584-
self.is_opcode_supported = is_opcode_supported;
585-
}
586-
587-
#[allow(deprecated)]
588-
pub fn foreign(&self, opcode: &str) -> bool {
589-
let black_box_func = match acvm::acir::BlackBoxFunc::lookup(opcode) {
590-
Some(black_box_func) => black_box_func,
591-
None => return false,
592-
};
593-
(self.is_opcode_supported)(&Opcode::BlackBoxFuncCall(BlackBoxFuncCall {
594-
name: black_box_func,
595-
inputs: Vec::new(),
596-
outputs: Vec::new(),
597-
}))
598-
}
599-
600564
pub fn push_delayed_type_check(&mut self, f: TypeCheckFn) {
601565
self.delayed_type_checks.push(f);
602566
}

noir_stdlib/src/merkle.nr

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ fn check_membership(_root : Field, _leaf : Field, _index : Field, _hash_path: [F
1313
fn compute_merkle_root(_leaf : Field, _index : Field, _hash_path: [Field]) -> Field {}
1414

1515
// Returns the root of the tree from the provided leaf and its hashpath, using pedersen hash
16-
#[alternative(compute_merkle_root)]
1716
fn compute_root_from_leaf(leaf : Field, index : Field, hash_path: [Field]) -> Field {
1817
let n = hash_path.len();
1918
let index_bits = index.to_le_bits(n as u32);

noir_stdlib/src/sha256.nr

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ fn msg_u8_to_u32(msg: [u8; 64]) -> [u32; 16]
9999
}
100100

101101
// SHA-256 hash function
102-
#[alternative(sha256)]
103102
fn digest<N>(msg: [u8; N]) -> [u8; 32] {
104103
let mut msg_block: [u8; 64] = [0; 64];
105104
let mut h: [u32; 8] = [1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]; // Intermediate hash, starting with the canonical initial value

0 commit comments

Comments
 (0)