Skip to content

Commit

Permalink
refactor: change bigint3_pack uses for BigInt3::pack86 (lambdacla…
Browse files Browse the repository at this point in the history
…ss#1329)

* Use `Felt252::bits` instead of shift + compare

* Use `BigInt3::pack86` instead of bigint3_pack

* Rename generic size

* Update changelog

* Remove changelog entry

---------

Co-authored-by: Pedro Fontana <fontana.pedro93@gmail.com>
  • Loading branch information
2 people authored and kariy committed Jul 25, 2023
1 parent 8ca51eb commit 0515105
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 76 deletions.
3 changes: 3 additions & 0 deletions fuzzer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 12 additions & 9 deletions vm/src/hint_processor/builtin_hint_processor/ec_recover.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use num_integer::Integer;

use super::secp::bigint_utils::BigInt3;
use super::secp::secp_utils::bigint3_pack;
use crate::stdlib::{collections::HashMap, prelude::*};
use crate::{
hint_processor::hint_processor_definition::HintReference,
Expand Down Expand Up @@ -29,9 +28,13 @@ pub fn ec_recover_divmod_n_packed(
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
) -> Result<(), HintError> {
let n = bigint3_pack(BigInt3::from_var_name("n", vm, ids_data, ap_tracking)?);
let x = bigint3_pack(BigInt3::from_var_name("x", vm, ids_data, ap_tracking)?).mod_floor(&n);
let s = bigint3_pack(BigInt3::from_var_name("s", vm, ids_data, ap_tracking)?).mod_floor(&n);
let n = BigInt3::from_var_name("n", vm, ids_data, ap_tracking)?.pack86();
let x = BigInt3::from_var_name("x", vm, ids_data, ap_tracking)?
.pack86()
.mod_floor(&n);
let s = BigInt3::from_var_name("s", vm, ids_data, ap_tracking)?
.pack86()
.mod_floor(&n);

let value = div_mod(&x, &s, &n);
exec_scopes.insert_value("value", value.clone());
Expand All @@ -55,8 +58,8 @@ pub fn ec_recover_sub_a_b(
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
) -> Result<(), HintError> {
let a = bigint3_pack(BigInt3::from_var_name("a", vm, ids_data, ap_tracking)?);
let b = bigint3_pack(BigInt3::from_var_name("b", vm, ids_data, ap_tracking)?);
let a = BigInt3::from_var_name("a", vm, ids_data, ap_tracking)?.pack86();
let b = BigInt3::from_var_name("b", vm, ids_data, ap_tracking)?.pack86();

let value = a - b;
exec_scopes.insert_value("value", value.clone());
Expand All @@ -83,9 +86,9 @@ pub fn ec_recover_product_mod(
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
) -> Result<(), HintError> {
let a = bigint3_pack(BigInt3::from_var_name("a", vm, ids_data, ap_tracking)?);
let b = bigint3_pack(BigInt3::from_var_name("b", vm, ids_data, ap_tracking)?);
let m = bigint3_pack(BigInt3::from_var_name("m", vm, ids_data, ap_tracking)?);
let a = BigInt3::from_var_name("a", vm, ids_data, ap_tracking)?.pack86();
let b = BigInt3::from_var_name("b", vm, ids_data, ap_tracking)?.pack86();
let m = BigInt3::from_var_name("m", vm, ids_data, ap_tracking)?.pack86();

let product = a * b;
let value = product.mod_floor(&m);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn unsafe_keccak(
let word = vm.get_integer(word_addr)?;
let n_bytes = cmp::min(16, u64_length - byte_i);

if word.is_negative() || word.as_ref() >= &(Felt252::one() << (8 * (n_bytes as u32))) {
if word.is_negative() || word.bits() > 8 * n_bytes {
return Err(HintError::InvalidWordSize(Box::new(word.into_owned())));
}

Expand Down
27 changes: 27 additions & 0 deletions vm/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ mod tests {
use crate::vm::vm_core::VirtualMachine;

use assert_matches::assert_matches;
use felt::felt_str;
use num_traits::One;

#[cfg(target_arch = "wasm32")]
Expand Down Expand Up @@ -475,4 +476,30 @@ mod tests {
//Check hint memory inserts
check_memory![vm.segments.memory, ((1, 6), 0)];
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn u384_pack86() {
let pack_1 = Uint384 {
d0: Cow::Borrowed(&Felt252::new(10_i32)),
d1: Cow::Borrowed(&Felt252::new(10_i32)),
d2: Cow::Borrowed(&Felt252::new(10_i32)),
}
.pack86();
assert_eq!(
pack_1,
bigint_str!("59863107065073783529622931521771477038469668772249610")
);

let pack_2 = Uint384 {
d0: Cow::Borrowed(&felt_str!("773712524553362")),
d1: Cow::Borrowed(&felt_str!("57408430697461422066401280")),
d2: Cow::Borrowed(&felt_str!("1292469707114105")),
}
.pack86();
assert_eq!(
pack_2,
bigint_str!("7737125245533626718119526477371252455336267181195264773712524553362")
);
}
}
15 changes: 6 additions & 9 deletions vm/src/hint_processor/builtin_hint_processor/secp/field_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use crate::{
hint_processor::{
builtin_hint_processor::{
hint_utils::{insert_value_from_var_name, insert_value_into_ap},
secp::{
bigint_utils::Uint384,
secp_utils::{bigint3_pack, SECP_P},
},
secp::{bigint_utils::Uint384, secp_utils::SECP_P},
},
hint_processor_definition::HintReference,
},
Expand Down Expand Up @@ -38,7 +35,7 @@ pub fn verify_zero(
secp_p: &BigInt,
) -> Result<(), HintError> {
exec_scopes.insert_value("SECP_P", secp_p.clone());
let val = bigint3_pack(Uint384::from_var_name("val", vm, ids_data, ap_tracking)?);
let val = Uint384::from_var_name("val", vm, ids_data, ap_tracking)?.pack86();
let (q, r) = val.div_rem(secp_p);
if !r.is_zero() {
return Err(HintError::SecpVerifyZero(Box::new(val)));
Expand All @@ -64,7 +61,7 @@ pub fn verify_zero_with_external_const(
ap_tracking: &ApTracking,
) -> Result<(), HintError> {
let secp_p = exec_scopes.get_ref("SECP_P")?;
let val = bigint3_pack(Uint384::from_var_name("val", vm, ids_data, ap_tracking)?);
let val = Uint384::from_var_name("val", vm, ids_data, ap_tracking)?.pack86();
let (q, r) = val.div_rem(secp_p);
if !r.is_zero() {
return Err(HintError::SecpVerifyZero(Box::new(val)));
Expand All @@ -88,7 +85,7 @@ pub fn reduce(
ap_tracking: &ApTracking,
) -> Result<(), HintError> {
exec_scopes.insert_value("SECP_P", SECP_P.clone());
let value = bigint3_pack(Uint384::from_var_name("x", vm, ids_data, ap_tracking)?);
let value = Uint384::from_var_name("x", vm, ids_data, ap_tracking)?.pack86();
exec_scopes.insert_value("value", value.mod_floor(&SECP_P));
Ok(())
}
Expand All @@ -108,7 +105,7 @@ pub fn is_zero_pack(
ap_tracking: &ApTracking,
) -> Result<(), HintError> {
exec_scopes.insert_value("SECP_P", SECP_P.clone());
let x_packed = bigint3_pack(Uint384::from_var_name("x", vm, ids_data, ap_tracking)?);
let x_packed = Uint384::from_var_name("x", vm, ids_data, ap_tracking)?.pack86();
let x = x_packed.mod_floor(&SECP_P);
exec_scopes.insert_value("x", x);
Ok(())
Expand All @@ -121,7 +118,7 @@ pub fn is_zero_pack_external_secp(
ap_tracking: &ApTracking,
) -> Result<(), HintError> {
let secp_p = exec_scopes.get_ref("SECP_P")?;
let x_packed = bigint3_pack(Uint384::from_var_name("x", vm, ids_data, ap_tracking)?);
let x_packed = Uint384::from_var_name("x", vm, ids_data, ap_tracking)?.pack86();
let x = x_packed.mod_floor(secp_p);
exec_scopes.insert_value("x", x);
Ok(())
Expand Down
47 changes: 3 additions & 44 deletions vm/src/hint_processor/builtin_hint_processor/secp/secp_utils.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use core::str::FromStr;

use crate::stdlib::{boxed::Box, ops::Shl, prelude::*};
use crate::stdlib::{boxed::Box, prelude::*};

use crate::vm::errors::hint_errors::HintError;

use lazy_static::lazy_static;
use num_bigint::{BigInt, BigUint};
use num_traits::Zero;

use super::bigint_utils::Uint384;

// Constants in package "starkware.cairo.common.cairo_secp.constants".
pub const BASE_86: &str = "starkware.cairo.common.cairo_secp.constants.BASE";
pub const BETA: &str = "starkware.cairo.common.cairo_secp.constants.BETA";
Expand Down Expand Up @@ -89,28 +87,13 @@ pub fn bigint3_split(integer: &num_bigint::BigUint) -> Result<[num_bigint::BigUi
Ok(canonical_repr)
}

/*
Takes an UnreducedFelt2523 struct which represents a triple of limbs (d0, d1, d2) of field
elements and reconstructs the corresponding 256-bit integer (see split()).
Note that the limbs do not have to be in the range [0, BASE).
*/
pub(crate) fn bigint3_pack(num: Uint384) -> num_bigint::BigInt {
let limbs = [num.d0, num.d1, num.d2];
#[allow(deprecated)]
limbs
.into_iter()
.enumerate()
.map(|(idx, value)| value.to_signed_felt().shl(idx * 86))
.sum()
}

#[cfg(test)]
mod tests {
use super::*;
use crate::stdlib::{borrow::Cow, collections::HashMap, string::ToString};
use crate::stdlib::{collections::HashMap, string::ToString};
use crate::utils::test_utils::*;
use assert_matches::assert_matches;
use felt::{felt_str, Felt252};
use felt::Felt252;
use num_bigint::BigUint;

use num_traits::One;
Expand Down Expand Up @@ -184,28 +167,4 @@ mod tests {

);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn secp_pack() {
let pack_1 = bigint3_pack(Uint384 {
d0: Cow::Borrowed(&Felt252::new(10_i32)),
d1: Cow::Borrowed(&Felt252::new(10_i32)),
d2: Cow::Borrowed(&Felt252::new(10_i32)),
});
assert_eq!(
pack_1,
bigint_str!("59863107065073783529622931521771477038469668772249610")
);

let pack_2 = bigint3_pack(Uint384 {
d0: Cow::Borrowed(&felt_str!("773712524553362")),
d1: Cow::Borrowed(&felt_str!("57408430697461422066401280")),
d2: Cow::Borrowed(&felt_str!("1292469707114105")),
});
assert_eq!(
pack_2,
bigint_str!("7737125245533626718119526477371252455336267181195264773712524553362")
);
}
}
20 changes: 11 additions & 9 deletions vm/src/hint_processor/builtin_hint_processor/secp/signature.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::{
any_box,
hint_processor::{
builtin_hint_processor::{
hint_utils::get_integer_from_var_name,
secp::secp_utils::{bigint3_pack, BETA},
},
builtin_hint_processor::{hint_utils::get_integer_from_var_name, secp::secp_utils::BETA},
hint_processor_definition::HintReference,
},
math_utils::{div_mod, safe_div_bigint},
Expand Down Expand Up @@ -38,8 +35,8 @@ pub fn div_mod_n_packed(
ap_tracking: &ApTracking,
n: &BigInt,
) -> Result<(), HintError> {
let a = bigint3_pack(Uint384::from_var_name("a", vm, ids_data, ap_tracking)?);
let b = bigint3_pack(Uint384::from_var_name("b", vm, ids_data, ap_tracking)?);
let a = Uint384::from_var_name("a", vm, ids_data, ap_tracking)?.pack86();
let b = Uint384::from_var_name("b", vm, ids_data, ap_tracking)?.pack86();

let value = div_mod(&a, &b, n);
exec_scopes.insert_value("a", a);
Expand Down Expand Up @@ -118,7 +115,8 @@ pub fn get_point_from_x(
.ok_or_else(|| HintError::MissingConstant(Box::new(BETA)))?
.to_bigint();

let x_cube_int = bigint3_pack(Uint384::from_var_name("x_cube", vm, ids_data, ap_tracking)?)
let x_cube_int = Uint384::from_var_name("x_cube", vm, ids_data, ap_tracking)?
.pack86()
.mod_floor(&SECP_P);
let y_cube_int = (x_cube_int + beta).mod_floor(&SECP_P);
// Divide by 4
Expand Down Expand Up @@ -147,8 +145,12 @@ pub fn pack_modn_div_modn(
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
) -> Result<(), HintError> {
let x = bigint3_pack(Uint384::from_var_name("x", vm, ids_data, ap_tracking)?).mod_floor(&N);
let s = bigint3_pack(Uint384::from_var_name("s", vm, ids_data, ap_tracking)?).mod_floor(&N);
let x = Uint384::from_var_name("x", vm, ids_data, ap_tracking)?
.pack86()
.mod_floor(&N);
let s = Uint384::from_var_name("s", vm, ids_data, ap_tracking)?
.pack86()
.mod_floor(&N);

let value = div_mod(&x, &s, &N);
exec_scopes.insert_value("x", x);
Expand Down
8 changes: 4 additions & 4 deletions vm/src/hint_processor/builtin_hint_processor/uint_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ use felt::Felt252;
use num_bigint::BigUint;
use num_traits::One;

pub(crate) fn split<const T: usize>(num: &BigUint, num_bits_shift: u32) -> [Felt252; T] {
pub(crate) fn split<const N: usize>(num: &BigUint, num_bits_shift: u32) -> [Felt252; N] {
let mut num = num.clone();
let bitmask = &((BigUint::one() << num_bits_shift) - 1_u32);
[0; T].map(|_| {
[0; N].map(|_| {
let a = &num & bitmask;
num >>= num_bits_shift;
Felt252::from(a)
})
}

pub(crate) fn pack<const T: usize>(
limbs: [impl AsRef<Felt252>; T],
pub(crate) fn pack<const N: usize>(
limbs: [impl AsRef<Felt252>; N],
num_bits_shift: usize,
) -> BigUint {
limbs
Expand Down

0 comments on commit 0515105

Please sign in to comment.