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.
chore: add bigint solver in ACVM and add a unit test for bigints in N…
…oir (AztecProtocol#4415) The PR enable bigints in ACVM, i.e you can now compile and execute Noir programs using bigint opcodes. This is demonstrated in the added unit test. --------- Co-authored-by: kevaundray <kevtheappdev@gmail.com>
- Loading branch information
1 parent
f09e8fc
commit e4a2fe9
Showing
9 changed files
with
213 additions
and
18 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 |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use std::collections::HashMap; | ||
|
||
use acir::{ | ||
circuit::opcodes::FunctionInput, | ||
native_types::{Witness, WitnessMap}, | ||
BlackBoxFunc, FieldElement, | ||
}; | ||
|
||
use num_bigint::BigUint; | ||
|
||
use crate::pwg::OpcodeResolutionError; | ||
|
||
/// 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)] | ||
pub(crate) 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, OpcodeResolutionError> { | ||
self.bigint_id_to_value | ||
.get(&id) | ||
.ok_or(OpcodeResolutionError::BlackBoxFunctionFailed( | ||
func, | ||
format!("could not find bigint of id {id}"), | ||
)) | ||
.cloned() | ||
} | ||
|
||
pub(crate) fn get_modulus( | ||
&self, | ||
id: u32, | ||
func: BlackBoxFunc, | ||
) -> Result<BigUint, OpcodeResolutionError> { | ||
self.bigint_id_to_modulus | ||
.get(&id) | ||
.ok_or(OpcodeResolutionError::BlackBoxFunctionFailed( | ||
func, | ||
format!("could not find bigint of id {id}"), | ||
)) | ||
.cloned() | ||
} | ||
pub(crate) fn bigint_from_bytes( | ||
&mut self, | ||
inputs: &[FunctionInput], | ||
modulus: &[u8], | ||
output: u32, | ||
initial_witness: &mut WitnessMap, | ||
) -> Result<(), OpcodeResolutionError> { | ||
let bytes = inputs | ||
.iter() | ||
.map(|input| initial_witness.get(&input.witness).unwrap().to_u128() as u8) | ||
.collect::<Vec<u8>>(); | ||
let bigint = BigUint::from_bytes_le(&bytes); | ||
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(crate) fn bigint_to_bytes( | ||
&self, | ||
input: u32, | ||
outputs: &Vec<Witness>, | ||
initial_witness: &mut WitnessMap, | ||
) -> Result<(), OpcodeResolutionError> { | ||
let bigint = self.get_bigint(input, BlackBoxFunc::BigIntToLeBytes)?; | ||
|
||
let mut bytes = bigint.to_bytes_le(); | ||
while bytes.len() < outputs.len() { | ||
bytes.push(0); | ||
} | ||
bytes.iter().zip(outputs.iter()).for_each(|(byte, output)| { | ||
initial_witness.insert(*output, FieldElement::from(*byte as u128)); | ||
}); | ||
Ok(()) | ||
} | ||
|
||
pub(crate) fn bigint_op( | ||
&mut self, | ||
lhs: u32, | ||
rhs: u32, | ||
output: u32, | ||
func: BlackBoxFunc, | ||
) -> Result<(), OpcodeResolutionError> { | ||
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::BigIntNeg => { | ||
if lhs >= rhs { | ||
&lhs - &rhs | ||
} else { | ||
&lhs + &modulus - &rhs | ||
} | ||
} | ||
BlackBoxFunc::BigIntMul => lhs * rhs, | ||
BlackBoxFunc::BigIntDiv => { | ||
lhs * rhs.modpow(&(&modulus - BigUint::from(1_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
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,6 @@ | ||
[package] | ||
name = "bigint" | ||
type = "bin" | ||
authors = [""] | ||
|
||
[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,2 @@ | ||
x = [34,3,5,8,4] | ||
y = [44,7,1,8,8] |
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,21 @@ | ||
use dep::std::bigint; | ||
|
||
fn main(mut x: [u8;5], y: [u8;5]) { | ||
let a = bigint::BigInt::secpk1_fq_from_le_bytes([x[0],x[1],x[2],x[3],x[4]]); | ||
let b = bigint::BigInt::secpk1_fq_from_le_bytes([y[0],y[1],y[2],y[3],y[4]]); | ||
|
||
let a_bytes = a.to_le_bytes(); | ||
let b_bytes = b.to_le_bytes(); | ||
for i in 0..5 { | ||
assert(a_bytes[i] == x[i]); | ||
assert(b_bytes[i] == y[i]); | ||
} | ||
|
||
let d = a*b - b; | ||
let d_bytes = d.to_le_bytes(); | ||
let d1 = bigint::BigInt::secpk1_fq_from_le_bytes(597243850900842442924.to_le_bytes(10)); | ||
let d1_bytes = d1.to_le_bytes(); | ||
for i in 0..32 { | ||
assert(d_bytes[i] == d1_bytes[i]); | ||
} | ||
} |