-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: issue 4682 and add solver for unconstrained bigintegers (#4729)
# Description ## Problem\* Managing bigintegers as ID makes it difficult to handle them in conditionals and also when passing them around unconstrained functions. As a matter of fact these two use cases do not work, the first one being documented in issue #4682, while the second one was never implemented. ## Summary\* In order to solve this two issues, I modified the bigint representation so that it stores its byte array instead of an id, allowing it to work directly with conditional and unconstrained functions. ## Additional Context For simplicity, I also made the byte arrays of hardcoded length 32. Since the moduli we support are of this length and since custom bigintegers will be implemented differently, I do not expect this length to change. The barrentenberg interface has not changed and we still use id for it. The Id is constructed when required by doing a call to from_bytes. ## Documentation\* The traits have not changed, I have simply added a from_byte_32 method which takes a 32-bytes array as input. Check one: - [] No documentation needed. - [X] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [X] I have tested the changes locally. - [X] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --------- Co-authored-by: Tom French <tom@tomfren.ch>
- Loading branch information
1 parent
23e1f3b
commit e4d33c1
Showing
16 changed files
with
484 additions
and
158 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.