-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automated pull of development from the [noir](https://github.com/noir-lang/noir) programming language, a dependency of Aztec. BEGIN_COMMIT_OVERRIDE chore: remove unnecessary casts in `BoundedVec` (noir-lang/noir#4831) fix: issue 4682 and add solver for unconstrained bigintegers (noir-lang/noir#4729) chore(docs): fix wrong Nargo.toml workspace examples (noir-lang/noir#4822) chore: delete unnecessary Prover.toml file (noir-lang/noir#4829) END_COMMIT_OVERRIDE --------- Co-authored-by: Tom French <tom@tomfren.ch>
- Loading branch information
1 parent
df1c3c4
commit beab8c9
Showing
33 changed files
with
650 additions
and
299 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
053d5e8879865c25ac2bfcfd206a6b729d328b9c | ||
6cc105ee441e093b4fccdd5fcc3db922eb28a3fb |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,99 @@ | ||
use std::collections::HashMap; | ||
|
||
use acir::BlackBoxFunc; | ||
|
||
use num_bigint::BigUint; | ||
|
||
use crate::BlackBoxResolutionError; | ||
|
||
/// Resolve BigInt opcodes by storing BigInt values (and their moduli) by their ID in a HashMap: | ||
/// - When it encounters a bigint operation opcode, it performs the operation on the stored values | ||
/// and store the result using the provided ID. | ||
/// - When it gets a to_bytes opcode, it simply looks up the value and resolves the output witness accordingly. | ||
#[derive(Default, Debug, Clone, PartialEq, Eq)] | ||
|
||
pub struct BigIntSolver { | ||
bigint_id_to_value: HashMap<u32, BigUint>, | ||
bigint_id_to_modulus: HashMap<u32, BigUint>, | ||
} | ||
|
||
impl BigIntSolver { | ||
pub(crate) fn get_bigint( | ||
&self, | ||
id: u32, | ||
func: BlackBoxFunc, | ||
) -> Result<BigUint, BlackBoxResolutionError> { | ||
self.bigint_id_to_value | ||
.get(&id) | ||
.ok_or(BlackBoxResolutionError::Failed( | ||
func, | ||
format!("could not find bigint of id {id}"), | ||
)) | ||
.cloned() | ||
} | ||
|
||
pub(crate) fn get_modulus( | ||
&self, | ||
id: u32, | ||
func: BlackBoxFunc, | ||
) -> Result<BigUint, BlackBoxResolutionError> { | ||
self.bigint_id_to_modulus | ||
.get(&id) | ||
.ok_or(BlackBoxResolutionError::Failed( | ||
func, | ||
format!("could not find bigint of id {id}"), | ||
)) | ||
.cloned() | ||
} | ||
pub fn bigint_from_bytes( | ||
&mut self, | ||
inputs: &[u8], | ||
modulus: &[u8], | ||
output: u32, | ||
) -> Result<(), BlackBoxResolutionError> { | ||
let bigint = BigUint::from_bytes_le(inputs); | ||
self.bigint_id_to_value.insert(output, bigint); | ||
let modulus = BigUint::from_bytes_le(modulus); | ||
self.bigint_id_to_modulus.insert(output, modulus); | ||
Ok(()) | ||
} | ||
|
||
pub fn bigint_to_bytes(&self, input: u32) -> Result<Vec<u8>, BlackBoxResolutionError> { | ||
let bigint = self.get_bigint(input, BlackBoxFunc::BigIntToLeBytes)?; | ||
Ok(bigint.to_bytes_le()) | ||
} | ||
|
||
pub fn bigint_op( | ||
&mut self, | ||
lhs: u32, | ||
rhs: u32, | ||
output: u32, | ||
func: BlackBoxFunc, | ||
) -> Result<(), BlackBoxResolutionError> { | ||
let modulus = self.get_modulus(lhs, func)?; | ||
let lhs = self.get_bigint(lhs, func)?; | ||
let rhs = self.get_bigint(rhs, func)?; | ||
let mut result = match func { | ||
BlackBoxFunc::BigIntAdd => lhs + rhs, | ||
BlackBoxFunc::BigIntSub => { | ||
if lhs >= rhs { | ||
&lhs - &rhs | ||
} else { | ||
&lhs + &modulus - &rhs | ||
} | ||
} | ||
BlackBoxFunc::BigIntMul => lhs * rhs, | ||
BlackBoxFunc::BigIntDiv => { | ||
lhs * rhs.modpow(&(&modulus - BigUint::from(2_u32)), &modulus) | ||
} //TODO ensure that modulus is prime | ||
_ => unreachable!("ICE - bigint_op must be called for an operation"), | ||
}; | ||
if result > modulus { | ||
let q = &result / &modulus; | ||
result -= q * &modulus; | ||
} | ||
self.bigint_id_to_value.insert(output, result); | ||
self.bigint_id_to_modulus.insert(output, modulus); | ||
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
Oops, something went wrong.