forked from visoftsolutions/noir_rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement bigint in Noir, using bigint opcodes (AztecProtocol#4198
) This PR enables big integers in Noir using the bigint opcodes. It does not implement bigints in ACVM which is required to solve the opcodes. It also does not implement bigints opcodes in Barretenberg, which is required to generate the constraints. However it provides a BigInt structure in the stdlib so you can write code like this in Noir, which will be compiled into ACIR using the bigint opcodes. ``` let a = bigint::BigInt::from_le_bytes([3], [23]); let b = bigint::BigInt::from_le_bytes([5], [23]); let c = a+b; let bytes = c.to_le_bytes(); std::println(bytes[0]); ``` I did not add a test yet because ACMV solver is not yet implemented. --------- Co-authored-by: kevaundray <kevtheappdev@gmail.com>
- Loading branch information
1 parent
e34b703
commit 1372fb6
Showing
8 changed files
with
271 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub(crate) mod acir_variable; | ||
pub(crate) mod big_int; | ||
pub(crate) mod generated_acir; | ||
pub(crate) mod sort; |
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
57 changes: 57 additions & 0 deletions
57
noir/compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/big_int.rs
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,57 @@ | ||
use acvm::FieldElement; | ||
use num_bigint::BigUint; | ||
|
||
/// Represents a bigint value in the form (id, modulus) where | ||
/// id is the identifier of the big integer number, and | ||
/// modulus is the identifier of the big integer size | ||
#[derive(Default, Clone, Copy, Debug)] | ||
pub(crate) struct BigIntId { | ||
pub(crate) bigint_id: u32, | ||
pub(crate) modulus_id: u32, | ||
} | ||
|
||
impl BigIntId { | ||
pub(crate) fn bigint_id(&self) -> FieldElement { | ||
FieldElement::from(self.bigint_id as u128) | ||
} | ||
|
||
pub(crate) fn modulus_id(&self) -> FieldElement { | ||
FieldElement::from(self.modulus_id as u128) | ||
} | ||
} | ||
|
||
/// BigIntContext is used to generate identifiers for big integers and their modulus | ||
#[derive(Default, Debug)] | ||
pub(crate) struct BigIntContext { | ||
modulus: Vec<BigUint>, | ||
big_integers: Vec<BigIntId>, | ||
} | ||
|
||
impl BigIntContext { | ||
/// Creates a new BigIntId for the given modulus identifier and returns it. | ||
pub(crate) fn new_big_int(&mut self, modulus_id: FieldElement) -> BigIntId { | ||
let id = self.big_integers.len() as u32; | ||
let result = BigIntId { bigint_id: id, modulus_id: modulus_id.to_u128() as u32 }; | ||
self.big_integers.push(result); | ||
result | ||
} | ||
|
||
/// Returns the modulus corresponding to the given modulus index | ||
pub(crate) fn modulus(&self, idx: FieldElement) -> BigUint { | ||
self.modulus[idx.to_u128() as usize].clone() | ||
} | ||
|
||
/// Returns the BigIntId corresponding to the given identifier | ||
pub(crate) fn get(&self, id: FieldElement) -> BigIntId { | ||
self.big_integers[id.to_u128() as usize] | ||
} | ||
|
||
/// Adds a modulus to the context (if it is not already present) | ||
pub(crate) fn get_or_insert_modulus(&mut self, modulus: BigUint) -> u32 { | ||
if let Some(pos) = self.modulus.iter().position(|x| x == &modulus) { | ||
return pos as u32; | ||
} | ||
self.modulus.push(modulus); | ||
(self.modulus.len() - 1) as u32 | ||
} | ||
} |
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.