From 1775559e91bfb11a4121ffbc6e71ddeae490274c Mon Sep 17 00:00:00 2001 From: Marco Granelli Date: Tue, 30 Jan 2024 20:15:06 +0100 Subject: [PATCH 1/3] Bumps `masp` version --- Cargo.lock | 7 ++++--- Cargo.toml | 4 ++-- wasm/Cargo.lock | 7 ++++--- wasm_for_tests/wasm_source/Cargo.lock | 7 ++++--- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 008530945b..e793f8e5b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3897,7 +3897,7 @@ dependencies = [ [[package]] name = "masp_note_encryption" version = "1.0.0" -source = "git+https://github.com/anoma/masp?tag=v1.0.0#c3c6047a9c9da54058afc71219b913ac9f79e48b" +source = "git+https://github.com/anoma/masp?tag=v1.1.0#f24691c0eb76909e3c15ae03aef294dccebd2df3" dependencies = [ "borsh", "chacha20", @@ -3910,7 +3910,7 @@ dependencies = [ [[package]] name = "masp_primitives" version = "1.0.0" -source = "git+https://github.com/anoma/masp?tag=v1.0.0#c3c6047a9c9da54058afc71219b913ac9f79e48b" +source = "git+https://github.com/anoma/masp?tag=v1.1.0#f24691c0eb76909e3c15ae03aef294dccebd2df3" dependencies = [ "aes", "bip0039", @@ -3942,7 +3942,7 @@ dependencies = [ [[package]] name = "masp_proofs" version = "1.0.0" -source = "git+https://github.com/anoma/masp?tag=v1.0.0#c3c6047a9c9da54058afc71219b913ac9f79e48b" +source = "git+https://github.com/anoma/masp?tag=v1.1.0#f24691c0eb76909e3c15ae03aef294dccebd2df3" dependencies = [ "bellman", "blake2b_simd", @@ -4162,6 +4162,7 @@ dependencies = [ "namada_tx_env", "namada_vote_ext", "namada_vp_env", + "num-traits 0.2.17", "num256", "orion", "owo-colors", diff --git a/Cargo.toml b/Cargo.toml index de1555a20a..df4e7dec1c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -114,8 +114,8 @@ ledger-transport-hid = "0.10.0" libc = "0.2.97" libloading = "0.7.2" # branch = "murisi/namada-integration" -masp_primitives = { git = "https://github.com/anoma/masp", tag = "v1.0.0" } -masp_proofs = { git = "https://github.com/anoma/masp", tag = "v1.0.0", default-features = false, features = ["local-prover"] } +masp_primitives = { git = "https://github.com/anoma/masp", tag = "v1.1.0" } +masp_proofs = { git = "https://github.com/anoma/masp", tag = "v1.1.0", default-features = false, features = ["local-prover"] } num256 = "0.3.5" num_cpus = "1.13.0" num-derive = "0.3.3" diff --git a/wasm/Cargo.lock b/wasm/Cargo.lock index 83de29e525..180d11da1b 100644 --- a/wasm/Cargo.lock +++ b/wasm/Cargo.lock @@ -3134,7 +3134,7 @@ dependencies = [ [[package]] name = "masp_note_encryption" version = "1.0.0" -source = "git+https://github.com/anoma/masp?tag=v1.0.0#c3c6047a9c9da54058afc71219b913ac9f79e48b" +source = "git+https://github.com/anoma/masp?tag=v1.1.0#f24691c0eb76909e3c15ae03aef294dccebd2df3" dependencies = [ "borsh", "chacha20", @@ -3147,7 +3147,7 @@ dependencies = [ [[package]] name = "masp_primitives" version = "1.0.0" -source = "git+https://github.com/anoma/masp?tag=v1.0.0#c3c6047a9c9da54058afc71219b913ac9f79e48b" +source = "git+https://github.com/anoma/masp?tag=v1.1.0#f24691c0eb76909e3c15ae03aef294dccebd2df3" dependencies = [ "aes", "bip0039", @@ -3179,7 +3179,7 @@ dependencies = [ [[package]] name = "masp_proofs" version = "1.0.0" -source = "git+https://github.com/anoma/masp?tag=v1.0.0#c3c6047a9c9da54058afc71219b913ac9f79e48b" +source = "git+https://github.com/anoma/masp?tag=v1.1.0#f24691c0eb76909e3c15ae03aef294dccebd2df3" dependencies = [ "bellman", "blake2b_simd", @@ -3350,6 +3350,7 @@ dependencies = [ "namada_tx_env", "namada_vote_ext", "namada_vp_env", + "num-traits", "num256", "orion", "owo-colors", diff --git a/wasm_for_tests/wasm_source/Cargo.lock b/wasm_for_tests/wasm_source/Cargo.lock index 2744978c64..ad94e56a74 100644 --- a/wasm_for_tests/wasm_source/Cargo.lock +++ b/wasm_for_tests/wasm_source/Cargo.lock @@ -3134,7 +3134,7 @@ dependencies = [ [[package]] name = "masp_note_encryption" version = "1.0.0" -source = "git+https://github.com/anoma/masp?tag=v1.0.0#c3c6047a9c9da54058afc71219b913ac9f79e48b" +source = "git+https://github.com/anoma/masp?tag=v1.1.0#f24691c0eb76909e3c15ae03aef294dccebd2df3" dependencies = [ "borsh", "chacha20", @@ -3147,7 +3147,7 @@ dependencies = [ [[package]] name = "masp_primitives" version = "1.0.0" -source = "git+https://github.com/anoma/masp?tag=v1.0.0#c3c6047a9c9da54058afc71219b913ac9f79e48b" +source = "git+https://github.com/anoma/masp?tag=v1.1.0#f24691c0eb76909e3c15ae03aef294dccebd2df3" dependencies = [ "aes", "bip0039", @@ -3179,7 +3179,7 @@ dependencies = [ [[package]] name = "masp_proofs" version = "1.0.0" -source = "git+https://github.com/anoma/masp?tag=v1.0.0#c3c6047a9c9da54058afc71219b913ac9f79e48b" +source = "git+https://github.com/anoma/masp?tag=v1.1.0#f24691c0eb76909e3c15ae03aef294dccebd2df3" dependencies = [ "bellman", "blake2b_simd", @@ -3350,6 +3350,7 @@ dependencies = [ "namada_tx_env", "namada_vote_ext", "namada_vp_env", + "num-traits", "num256", "orion", "owo-colors", From 89cf6598bd2984dd93cd4a94c4d82c536838c750 Mon Sep 17 00:00:00 2001 From: Marco Granelli Date: Tue, 30 Jan 2024 20:16:09 +0100 Subject: [PATCH 2/3] Checked operations on `I128Sum` in masp vp --- crates/namada/Cargo.toml | 1 + crates/namada/src/ledger/native_vp/masp.rs | 37 +++++++++++++++------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/crates/namada/Cargo.toml b/crates/namada/Cargo.toml index 0180115988..3026aff756 100644 --- a/crates/namada/Cargo.toml +++ b/crates/namada/Cargo.toml @@ -109,6 +109,7 @@ loupe = { version = "0.1.3", optional = true } masp_primitives.workspace = true masp_proofs.workspace = true num256.workspace = true +num-traits.workspace = true orion.workspace = true owo-colors = "3.5.0" parity-wasm = { version = "0.45.0", features = ["sign_ext"], optional = true } diff --git a/crates/namada/src/ledger/native_vp/masp.rs b/crates/namada/src/ledger/native_vp/masp.rs index 1b9d3b4d90..b222aeaf9b 100644 --- a/crates/namada/src/ledger/native_vp/masp.rs +++ b/crates/namada/src/ledger/native_vp/masp.rs @@ -19,6 +19,7 @@ use namada_state::{OptionExt, ResultExt}; use namada_token::read_denom; use namada_tx::Tx; use namada_vp_env::VpEnv; +use num_traits::ops::checked::{CheckedAdd, CheckedSub}; use ripemd::Digest as RipemdDigest; use sha2::Digest as Sha2Digest; use thiserror::Error; @@ -462,12 +463,18 @@ where // 3. Public key must be the hash of the source for vin in &transp_bundle.vin { // Non-masp sources add to the transparent tx pool - transparent_tx_pool += I128Sum::from_nonnegative( - vin.asset_type, - vin.value as i128, - ) - .ok() - .ok_or_err_msg("invalid value or asset type for amount")?; + transparent_tx_pool = transparent_tx_pool + .checked_add( + &I128Sum::from_nonnegative( + vin.asset_type, + vin.value as i128, + ) + .ok() + .ok_or_err_msg( + "invalid value or asset type for amount", + )?, + ) + .ok_or_err_msg("Overflow in input sum")?; // Satisfies 3. if <[u8; 20]>::from(hash) != vin.address.0 { @@ -608,12 +615,18 @@ where for out in &transp_bundle.vout { // Non-masp destinations subtract from transparent tx // pool - transparent_tx_pool -= I128Sum::from_nonnegative( - out.asset_type, - out.value as i128, - ) - .ok() - .ok_or_err_msg("invalid value or asset type for amount")?; + transparent_tx_pool = transparent_tx_pool + .checked_sub( + &I128Sum::from_nonnegative( + out.asset_type, + out.value as i128, + ) + .ok() + .ok_or_err_msg( + "invalid value or asset type for amount", + )?, + ) + .ok_or_err_msg("Underflow in output subtraction")?; // Satisfies 3. if <[u8; 20]>::from(hash) != out.address.0 { From 017250c505a4346f408fd276b0ba265d27eb69a2 Mon Sep 17 00:00:00 2001 From: Marco Granelli Date: Tue, 30 Jan 2024 20:20:06 +0100 Subject: [PATCH 3/3] Changelog #2476 --- .changelog/unreleased/improvements/2476-masp-checked-ops.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changelog/unreleased/improvements/2476-masp-checked-ops.md diff --git a/.changelog/unreleased/improvements/2476-masp-checked-ops.md b/.changelog/unreleased/improvements/2476-masp-checked-ops.md new file mode 100644 index 0000000000..c7898a8b44 --- /dev/null +++ b/.changelog/unreleased/improvements/2476-masp-checked-ops.md @@ -0,0 +1,2 @@ +- Removed possible over/under-flow of `I128Sum` operations in the masp vp. + ([\#2476](https://github.com/anoma/namada/pull/2476)) \ No newline at end of file