-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into aztec-packages
* master: chore: remove unnecessary casts in `BoundedVec` (#4831) fix: issue 4682 and add solver for unconstrained bigintegers (#4729) chore(docs): fix wrong Nargo.toml workspace examples (#4822) chore: delete unnecessary Prover.toml file (#4829) chore: fix alerts on rust msrv (#4817) chore(ci): fix alerts on msrv issues (#4816) chore: run clippy (#4810) chore: optimize poseidon2 implementation (#4807) fix: catch panics from EC point creation (e.g. the point is at infinity) (#4790)
- Loading branch information
Showing
31 changed files
with
648 additions
and
301 deletions.
There are no files selected for viewing
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.