Skip to content

Commit

Permalink
release/v0.8.2 hotfix1 (#1509)
Browse files Browse the repository at this point in the history
* Backport REDUCE_V2 hint (#1420)

* Release v0.8.2-hotfix1

* ci: run for all PRs

* ci: try to make some space

---------

Co-authored-by: Pedro Fontana <[email protected]>
  • Loading branch information
Oppen and pefontana committed Dec 7, 2023
1 parent 0511b71 commit 3141058
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 18 deletions.
22 changes: 21 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
push:
branches: [ main ]
pull_request:
branches: [ '*' ]
branches: [ '**' ]

env:
CARGO_TERM_COLOR: always
Expand Down Expand Up @@ -198,6 +198,26 @@ jobs:
name: Run tests
runs-on: ubuntu-22.04
steps:
- name: check and free hdd space left
run: |
echo "Listing 20 largest packages"
dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n | tail -n 20
df -h
sudo apt-get update
sudo apt-get remove -y '^llvm-.*'
sudo apt-get remove -y 'php.*'
sudo apt-get remove -y '^dotnet-.*'
sudo apt-get remove -y '^temurin-.*'
sudo apt-get remove -y azure-cli google-cloud-cli microsoft-edge-stable google-chrome-stable firefox powershell mono-devel
sudo apt-get autoremove -y
sudo apt-get clean
df -h
echo "Removing large directories"
# deleting 15GB
sudo rm -rf /usr/share/dotnet/
sudo rm -rf /usr/local/lib/android
df -h
- name: Install Rust
uses: dtolnay/[email protected]
with:
Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## Cairo-VM Changelog

#### Upcoming Changes
#### [0.8.2-hotfix1] - 2023-12-07

* Add REDUCE_V2 hint [#1420](https://github.com/lambdaclass/cairo-vm/pull/1420):
* Implement REDUCE_V2 hint
* Rename hint REDUCE -> REDUCE_V1

#### [0.8.2] - 2023-7-10

Expand Down
10 changes: 5 additions & 5 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ exclude = ["ensure-no_std"]
resolver = "2"

[workspace.package]
version = "0.8.2"
version = "0.8.2-hotfix1"
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/lambdaclass/cairo-vm/"
readme = "README.md"

[workspace.dependencies]
felt = { package = "cairo-felt", path = "./felt", version = "0.8.2", default-features = false, features = [
felt = { package = "cairo-felt", path = "./felt", version = "0.8.2-hotfix1", default-features = false, features = [
"alloc",
] }
cairo-vm = { path = "./vm", version = "0.8.2", default-features = false }
cairo-vm = { path = "./vm", version = "0.8.2-hotfix1", default-features = false }
mimalloc = { version = "0.1.37", default-features = false }
num-bigint = { version = "0.4", default-features = false, features = [
"serde",
Expand Down
38 changes: 38 additions & 0 deletions cairo_programs/reduce.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,45 @@ func test_reduce_ed25519{range_check_ptr}() {
return ();
}

func reduce_v2{range_check_ptr}(x: UnreducedBigInt3) -> (reduced_x: BigInt3) {
let orig_x = x;
%{ from starkware.cairo.common.cairo_secp.secp256r1_utils import SECP256R1_P as SECP_P %}
%{
from starkware.cairo.common.cairo_secp.secp_utils import pack
value = pack(ids.x, PRIME) % SECP_P
%}
let (reduced_x: BigInt3) = nondet_bigint3();
verify_zero(
UnreducedBigInt3(
d0=orig_x.d0 - reduced_x.d0,
d1=orig_x.d1 - reduced_x.d1,
d2=orig_x.d2 - reduced_x.d2
)
);
return (reduced_x=reduced_x);
}
func main{range_check_ptr}() {
test_reduce_ed25519();
// reduce_v2 tests
let x = UnreducedBigInt3(0, 0, 0);
let (reduce_v2_a) = reduce_v2(x);
assert reduce_v2_a = BigInt3(
0, 0, 0
);
let y = UnreducedBigInt3(12354, 745634534, 81298789312879123);
let (reduce_v2_b) = reduce_v2(y);
assert reduce_v2_b = BigInt3(
12354, 745634534, 81298789312879123
);
let z = UnreducedBigInt3(12354812987893128791212331231233, 7453123123123123312634534, 8129224990312325879);
let (reduce_v2_c) = reduce_v2(z);
assert reduce_v2_c = BigInt3(
16653320122975184709085185, 7453123123123123312794216, 8129224990312325879
);
return ();
}
2 changes: 1 addition & 1 deletion felt/src/lib_bigint_felt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ impl Add<&Felt252> for u64 {
};
// A single digit means this is effectively the sum of two `u64` numbers.
let Some(h0) = rhs_digits.next() else {
return self.checked_add(low)
return self.checked_add(low);
};
// Now we need to compare the 3 most significant digits.
// There are two relevant cases from now on, either `rhs` behaves like a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ use crate::{
},
field_utils::{
is_zero_assign_scope_variables, is_zero_assign_scope_variables_external_const,
is_zero_nondet, is_zero_pack, is_zero_pack_external_secp, reduce, verify_zero,
verify_zero_with_external_const,
is_zero_nondet, is_zero_pack, is_zero_pack_external_secp, reduce_v1, reduce_v2,
verify_zero, verify_zero_with_external_const,
},
signature::{
div_mod_n_packed_divmod, div_mod_n_packed_external_n, div_mod_n_safe_div,
Expand Down Expand Up @@ -327,8 +327,11 @@ impl HintProcessorLogic for BuiltinHintProcessor {
hint_code::NONDET_BIGINT3_V1 | hint_code::NONDET_BIGINT3_V2 => {
nondet_bigint3(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::REDUCE => {
reduce(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking)
hint_code::REDUCE_V1 => {
reduce_v1(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::REDUCE_V2 => {
reduce_v2(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking)
}
hint_code::REDUCE_ED25519 => {
ed25519_reduce(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking)
Expand Down
5 changes: 4 additions & 1 deletion vm/src/hint_processor/builtin_hint_processor/hint_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,13 @@ q, r = divmod(pack(ids.val, PRIME), SECP_P)
assert r == 0, f"verify_zero: Invalid input {ids.val.d0, ids.val.d1, ids.val.d2}."
ids.q = q % PRIME"#;

pub const REDUCE: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
pub const REDUCE_V1: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack
value = pack(ids.x, PRIME) % SECP_P"#;

pub const REDUCE_V2: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack
value = pack(ids.x, PRIME) % SECP_P"#;

pub const REDUCE_ED25519: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack
SECP_P=2**255-19
Expand Down
78 changes: 76 additions & 2 deletions vm/src/hint_processor/builtin_hint_processor/secp/field_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Implements hint:
value = pack(ids.x, PRIME) % SECP_P
%}
*/
pub fn reduce(
pub fn reduce_v1(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
Expand All @@ -93,6 +93,25 @@ pub fn reduce(
Ok(())
}

/*
Implements hint:
%{
from starkware.cairo.common.cairo_secp.secp_utils import pack
value = pack(ids.x, PRIME) % SECP_P
%}
*/
pub fn reduce_v2(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
) -> Result<(), HintError> {
let secp_p = exec_scopes.get_ref("SECP_P")?;
let value = Uint384::from_var_name("x", vm, ids_data, ap_tracking)?.pack86();
exec_scopes.insert_value("value", value.mod_floor(secp_p));
Ok(())
}

/*
Implements hint:
%{
Expand Down Expand Up @@ -194,7 +213,7 @@ pub fn is_zero_assign_scope_variables_external_const(
#[cfg(test)]
mod tests {
use super::*;
use crate::hint_processor::builtin_hint_processor::hint_code;
use crate::hint_processor::builtin_hint_processor::hint_code::{self, REDUCE_V2};
use crate::stdlib::string::ToString;

use crate::{
Expand Down Expand Up @@ -398,6 +417,61 @@ mod tests {
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_reduce_v2_ok() {
let hint_code = REDUCE_V2;
let mut vm = vm_with_range_check!();
add_segments!(vm, 3);

//Initialize fp
vm.run_context.fp = 25;

//Create hint data
let ids_data = non_continuous_ids_data![("x", -5)];

vm.segments = segments![
((1, 20), ("132181232131231239112312312313213083892150", 10)),
((1, 21), ("12354812987893128791212331231233", 10)),
((1, 22), ("654867675805132187", 10))
];

let mut exec_scopes = ExecutionScopes::new();
exec_scopes.insert_value("SECP_P", SECP_P.clone());
//Execute the hint
assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Ok(()));

//Check 'value' is defined in the vm scope
assert_matches!(
exec_scopes.get::<BigInt>("value"),
Ok(x) if x == bigint_str!(
"3920241379018821570896271640300310233395357371896837069219347149797814"
)
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_reduce_v2_with_no_secp() {
let hint_code = REDUCE_V2;
let mut vm = vm_with_range_check!();
add_segments!(vm, 3);

//Initialize fp
vm.run_context.fp = 25;

//Create hint data
let ids_data = non_continuous_ids_data![("x", -5)];

vm.segments = segments![((1, 20), 1), ((1, 21), 1), ((1, 22), 1)];

let mut exec_scopes = ExecutionScopes::new();
// Skip the SECP definition, so the hint fails
// exec_scopes.insert_value("SECP_P", SECP_P.clone());
//Execute the hint
assert_matches!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes), Err(_));
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn run_is_zero_pack_ok() {
Expand Down

0 comments on commit 3141058

Please sign in to comment.