-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added LUT provider stub and plain impl for MemOps
- Loading branch information
Showing
13 changed files
with
221 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use acir::{ | ||
circuit::opcodes::{BlockId, MemOp}, | ||
native_types::{Expression, Witness}, | ||
AcirField, | ||
}; | ||
use mpc_core::traits::NoirWitnessExtensionProtocol; | ||
|
||
use super::{CoAcvmResult, CoSolver}; | ||
|
||
impl<T, F> CoSolver<T, F> | ||
where | ||
T: NoirWitnessExtensionProtocol<F>, | ||
F: AcirField, | ||
{ | ||
pub(super) fn solve_memory_init_block( | ||
&mut self, | ||
block_id: BlockId, | ||
init: &[Witness], | ||
) -> CoAcvmResult<()> { | ||
// TODO: should we trust on the compiler here? | ||
// this should not be possible so maybe we do not need the check? | ||
if self.memory_access.get(block_id.0.into()).is_some() { | ||
//there is already a block? This should no be possible | ||
Err(eyre::eyre!( | ||
"There is already a block for id {}", | ||
block_id.0 | ||
))?; | ||
} | ||
// let get all witnesses | ||
let witness_map = self.witness(); | ||
let init = init | ||
.iter() | ||
.map(|witness| witness_map.get(witness).cloned()) | ||
.collect::<Option<Vec<_>>>() | ||
.ok_or(eyre::eyre!( | ||
"tried to write not initialized witness to memory - this is a bug" | ||
))?; | ||
let lut = self.driver.init_lut(init); | ||
self.memory_access.insert(block_id.0.into(), lut); | ||
Ok(()) | ||
} | ||
|
||
pub(super) fn solve_memory_op( | ||
&mut self, | ||
block_id: BlockId, | ||
op: &MemOp<F>, | ||
_predicate: Option<Expression<F>>, | ||
) -> CoAcvmResult<()> { | ||
let index = self.evaluate_expression(&op.index)?; | ||
let value = self.simplify_expression(&op.value)?; | ||
let witness = if value.is_degree_one_univariate() { | ||
//we can get the witness | ||
let (_coef, witness) = &value.linear_combinations[0]; | ||
let _q_c = value.q_c; | ||
Ok(*witness) | ||
//todo check if coef is one and q_c is zero! | ||
} else { | ||
Err(eyre::eyre!( | ||
"value for mem op must be a degree one univariate polynomial" | ||
)) | ||
}?; | ||
//TODO CHECK PREDICATE - do we need to cmux here? | ||
if op.operation.q_c.is_zero() { | ||
// read the value from the LUT | ||
let lut = self | ||
.memory_access | ||
.get(block_id.0.into()) | ||
.ok_or(eyre::eyre!( | ||
"tried to access block {} but not present", | ||
block_id.0 | ||
))?; | ||
let value = self.driver.get_from_lut(&index, lut); | ||
self.witness().insert(witness, value); | ||
} else if op.operation.q_c.is_one() { | ||
// write value to LUT | ||
let value = self | ||
.witness() | ||
.get(&witness) | ||
.cloned() | ||
.ok_or(eyre::eyre!("Trying to write unknown witness in mem block"))?; | ||
let lut = self | ||
.memory_access | ||
.get_mut(block_id.0.into()) | ||
.ok_or(eyre::eyre!( | ||
"tried to access block {} but not present", | ||
block_id.0 | ||
))?; | ||
self.driver.write_to_lut(index, value, lut); | ||
} else { | ||
Err(eyre::eyre!( | ||
"Got unknown operation {} for mem op - this is a bug", | ||
op.operation.q_c | ||
))? | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "slice" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.33.0" | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
vk = ["1", "2"] |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"noir_version":"0.33.0+325dac54efb6f99201de9fdeb0a507d45189607d","hash":11907287274716899065,"abi":{"parameters":[{"name":"vk","type":{"kind":"array","length":2,"type":{"kind":"field"}},"visibility":"private"}],"return_type":null,"error_types":{}},"bytecode":"H4sIAAAAAAAA/9WWbQ7CIAyGYTAXb9PyMco/ryIZ3P8IOsfizDQxDhJ5EtKGH81LWwqcLQz3dc6+zLZjTzjbs+5dstUwGhOdiqjxCsoHsmBsGAkJLdlJkdaRDDkfvAOPRkdM1usEC90mFhxEsPeUiv8pB3AMLJmDWhpFBY1VGkoWPnSLDSUb0NiX1CizwHV6zYXrNz5vsIgtTIVTSY2tFqqF2zawSuNbFBb6p935ksBfn6iYHkxzcb/9b+24AResqhe9CQAA","debug_symbols":"pZHBDoMgEET/Zc9cUBDLrzRNg4oNCQEj2KQh/nuh1aa1Xoi3nd15k00mQCeb6XZVprcO+DmAtq3wypqoAuDXyg3CJOW8GD1wTCgCabo40XpG0CstgVM2XxAUuUCZC5BcgOYC1S5Q4g9w+gbQn7UqFyfDm2R2MJmRxVkXm+R6N7lYP8EV+QGiuItRiUbL1HO6TaZda4/SP4b3JXqf","file_map":{"57":{"source":"use dep::std;\n\nfn flatten_slice(slice: [Field]) -> Field {\n slice[0] + slice[1]\n}\n\nfn main(vk: [Field; 2]) {\n assert(vk[0] + flatten_slice(vk.as_slice()) == 4);\n}\n","path":"/home/fnieddu/repos/collaborative-circom/test_vectors/noir/slice/src/main.nr"}},"names":["main"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use dep::std; | ||
|
||
fn flatten_slice(slice: [Field]) -> Field { | ||
slice[0] + slice[1] | ||
} | ||
|
||
fn main(vk: [Field; 2]) { | ||
assert(vk[0] + flatten_slice(vk.as_slice()) == 4); | ||
} |