From 207e0099737772464c09d8daa8c9938ac4e207f5 Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Thu, 28 Nov 2024 15:09:24 +0200 Subject: [PATCH 1/3] Simplify error types --- fuel-vm/src/interpreter/crypto.rs | 148 ++++++++++-------------------- 1 file changed, 51 insertions(+), 97 deletions(-) diff --git a/fuel-vm/src/interpreter/crypto.rs b/fuel-vm/src/interpreter/crypto.rs index b384aed6e..4ba63ca23 100644 --- a/fuel-vm/src/interpreter/crypto.rs +++ b/fuel-vm/src/interpreter/crypto.rs @@ -252,32 +252,25 @@ fn read_g1_point_alt_bn_128( point_ptr: Word, ) -> SimpleResult { // Big endian required by the library - let px = Fq::from_slice(memory.read(point_ptr, 32u64)?).map_err(|_| { - crate::error::PanicOrBug::Panic(fuel_tx::PanicReason::InvalidEllipticCurvePoint) - })?; + let px = Fq::from_slice(memory.read(point_ptr, 32u64)?) + .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?; let py = Fq::from_slice( memory.read( point_ptr .checked_add(32) - .ok_or(crate::error::PanicOrBug::Panic( - fuel_tx::PanicReason::ArithmeticOverflow, - ))?, + .ok_or(fuel_tx::PanicReason::ArithmeticOverflow)?, 32u64, )?, ) - .map_err(|_| { - crate::error::PanicOrBug::Panic(fuel_tx::PanicReason::InvalidEllipticCurvePoint) - })?; + .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?; - if px == Fq::zero() && py == Fq::zero() { - Ok(G1::zero()) + Ok(if px == Fq::zero() && py == Fq::zero() { + G1::zero() } else { - AffineG1::new(px, py).map(Into::into).map_err(|_| { - crate::error::PanicOrBug::Panic( - fuel_tx::PanicReason::InvalidEllipticCurvePoint, - ) - }) - } + AffineG1::new(px, py) + .map(Into::into) + .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)? + }) } fn read_g2_point_alt_bn_128( @@ -285,59 +278,45 @@ fn read_g2_point_alt_bn_128( point_ptr: Word, ) -> SimpleResult { // Big endian required by the library - let ay = Fq::from_slice(memory.read(point_ptr, 32u64)?).map_err(|_| { - crate::error::PanicOrBug::Panic(fuel_tx::PanicReason::InvalidEllipticCurvePoint) - })?; + let ay = Fq::from_slice(memory.read(point_ptr, 32u64)?) + .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?; let ax = Fq::from_slice( memory.read( point_ptr .checked_add(32) - .ok_or(crate::error::PanicOrBug::Panic( - fuel_tx::PanicReason::ArithmeticOverflow, - ))?, + .ok_or(fuel_tx::PanicReason::ArithmeticOverflow)?, 32u64, )?, ) - .map_err(|_| { - crate::error::PanicOrBug::Panic(fuel_tx::PanicReason::InvalidEllipticCurvePoint) - })?; + .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?; let by = Fq::from_slice( memory.read( point_ptr .checked_add(64) - .ok_or(crate::error::PanicOrBug::Panic( - fuel_tx::PanicReason::ArithmeticOverflow, - ))?, + .ok_or(fuel_tx::PanicReason::ArithmeticOverflow)?, 32u64, )?, ) - .map_err(|_| { - crate::error::PanicOrBug::Panic(fuel_tx::PanicReason::InvalidEllipticCurvePoint) - })?; + .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?; let bx = Fq::from_slice( memory.read( point_ptr .checked_add(96) - .ok_or(crate::error::PanicOrBug::Panic( - fuel_tx::PanicReason::ArithmeticOverflow, - ))?, + .ok_or(fuel_tx::PanicReason::ArithmeticOverflow)?, 32u64, )?, ) - .map_err(|_| { - crate::error::PanicOrBug::Panic(fuel_tx::PanicReason::InvalidEllipticCurvePoint) - })?; + .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?; let a = Fq2::new(ax, ay); let b = Fq2::new(bx, by); - if a.is_zero() && b.is_zero() { - Ok(G2::zero()) + Ok(if a.is_zero() && b.is_zero() { + G2::zero() } else { - Ok(G2::from(AffineG2::new(a, b).map_err(|_| { - crate::error::PanicOrBug::Panic( - fuel_tx::PanicReason::InvalidEllipticCurvePoint, - ) - })?)) - } + G2::from( + AffineG2::new(a, b) + .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?, + ) + }) } pub(crate) fn ec_operation( @@ -357,11 +336,9 @@ pub(crate) fn ec_operation( let point1 = read_g1_point_alt_bn_128(memory, points_ptr)?; let point2 = read_g1_point_alt_bn_128( memory, - points_ptr.checked_add(64).ok_or( - crate::error::PanicOrBug::Panic( - fuel_tx::PanicReason::ArithmeticOverflow, - ), - )?, + points_ptr + .checked_add(64) + .ok_or(fuel_tx::PanicReason::ArithmeticOverflow)?, )?; let mut output = [0u8; 64]; #[allow(clippy::arithmetic_side_effects)] @@ -374,19 +351,15 @@ pub(crate) fn ec_operation( // Scalar multiplication 1 => { let point = read_g1_point_alt_bn_128(memory, points_ptr)?; - let scalar = Fr::from_slice(memory.read( - points_ptr.checked_add(64).ok_or( - crate::error::PanicOrBug::Panic( - fuel_tx::PanicReason::ArithmeticOverflow, - ), + let scalar = Fr::from_slice( + memory.read( + points_ptr + .checked_add(64) + .ok_or(fuel_tx::PanicReason::ArithmeticOverflow)?, + 32u64, )?, - 32u64, - )?) - .map_err(|_| { - crate::error::PanicOrBug::Panic( - fuel_tx::PanicReason::InvalidEllipticCurvePoint, - ) - })?; + ) + .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?; let mut output = [0u8; 64]; #[allow(clippy::arithmetic_side_effects)] if let Some(product) = AffineG1::from_jacobian(point * scalar) { @@ -395,18 +368,10 @@ pub(crate) fn ec_operation( } memory.write_bytes(owner, dst, output)?; } - _ => { - return Err(crate::error::PanicOrBug::Panic( - fuel_tx::PanicReason::UnsupportedOperationType, - )) - } + _ => return Err(fuel_tx::PanicReason::UnsupportedOperationType.into()), } } - _ => { - return Err(crate::error::PanicOrBug::Panic( - fuel_tx::PanicReason::UnsupportedCurveId, - )) - } + _ => return Err(fuel_tx::PanicReason::UnsupportedCurveId.into()), } Ok(inc_pc(pc)?) } @@ -425,40 +390,29 @@ pub(crate) fn ec_pairing( // Each element consistsof an uncompressed G1 point (64 bytes) and an // uncompressed G2 point (128 bytes). let element_size = 128 + 64; - let mut elements = - Vec::with_capacity(usize::try_from(number_elements).map_err(|_| { - crate::error::PanicOrBug::Panic( - fuel_tx::PanicReason::ArithmeticOverflow, - ) - })?); + let mut elements = Vec::with_capacity( + usize::try_from(number_elements) + .map_err(|_| fuel_tx::PanicReason::ArithmeticOverflow)?, + ); for idx in 0..number_elements { let start_offset = elements_ptr - .checked_add(idx.checked_mul(element_size).ok_or( - crate::error::PanicOrBug::Panic( - fuel_tx::PanicReason::ArithmeticOverflow, - ), - )?) - .ok_or(crate::error::PanicOrBug::Panic( - fuel_tx::PanicReason::ArithmeticOverflow, - ))?; + .checked_add( + idx.checked_mul(element_size) + .ok_or(fuel_tx::PanicReason::ArithmeticOverflow)?, + ) + .ok_or(fuel_tx::PanicReason::ArithmeticOverflow)?; let a = read_g1_point_alt_bn_128(memory, start_offset)?; let b = read_g2_point_alt_bn_128( memory, - start_offset.checked_add(64).ok_or( - crate::error::PanicOrBug::Panic( - fuel_tx::PanicReason::ArithmeticOverflow, - ), - )?, + start_offset + .checked_add(64) + .ok_or(fuel_tx::PanicReason::ArithmeticOverflow)?, )?; elements.push((a, b)); } *success = (bn::pairing_batch(&elements) == Gt::one()) as u64; } - _ => { - return Err(crate::error::PanicOrBug::Panic( - fuel_tx::PanicReason::UnsupportedOperationType, - )) - } + _ => return Err(fuel_tx::PanicReason::UnsupportedOperationType.into()), } Ok(inc_pc(pc)?) } From a36fecc5204f56e3a3c672270a2634d0e3018768 Mon Sep 17 00:00:00 2001 From: Hannes Karppila Date: Thu, 28 Nov 2024 15:21:06 +0200 Subject: [PATCH 2/3] Use MemoryOverflow instead of ArithmeticOverflow --- fuel-vm/src/interpreter/crypto.rs | 65 ++++++++++--------------------- 1 file changed, 21 insertions(+), 44 deletions(-) diff --git a/fuel-vm/src/interpreter/crypto.rs b/fuel-vm/src/interpreter/crypto.rs index 4ba63ca23..324791e70 100644 --- a/fuel-vm/src/interpreter/crypto.rs +++ b/fuel-vm/src/interpreter/crypto.rs @@ -252,17 +252,12 @@ fn read_g1_point_alt_bn_128( point_ptr: Word, ) -> SimpleResult { // Big endian required by the library - let px = Fq::from_slice(memory.read(point_ptr, 32u64)?) + let arg_bytes: [u8; 2 * 32] = memory.read_bytes(point_ptr)?; + + let py = Fq::from_slice(&arg_bytes[..32]) + .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?; + let px = Fq::from_slice(&arg_bytes[32..64]) .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?; - let py = Fq::from_slice( - memory.read( - point_ptr - .checked_add(32) - .ok_or(fuel_tx::PanicReason::ArithmeticOverflow)?, - 32u64, - )?, - ) - .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?; Ok(if px == Fq::zero() && py == Fq::zero() { G1::zero() @@ -278,35 +273,17 @@ fn read_g2_point_alt_bn_128( point_ptr: Word, ) -> SimpleResult { // Big endian required by the library - let ay = Fq::from_slice(memory.read(point_ptr, 32u64)?) + let arg_bytes: [u8; 4 * 32] = memory.read_bytes(point_ptr)?; + + let ay = Fq::from_slice(&arg_bytes[..32]) + .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?; + let ax = Fq::from_slice(&arg_bytes[32..64]) .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?; - let ax = Fq::from_slice( - memory.read( - point_ptr - .checked_add(32) - .ok_or(fuel_tx::PanicReason::ArithmeticOverflow)?, - 32u64, - )?, - ) - .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?; - let by = Fq::from_slice( - memory.read( - point_ptr - .checked_add(64) - .ok_or(fuel_tx::PanicReason::ArithmeticOverflow)?, - 32u64, - )?, - ) - .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?; - let bx = Fq::from_slice( - memory.read( - point_ptr - .checked_add(96) - .ok_or(fuel_tx::PanicReason::ArithmeticOverflow)?, - 32u64, - )?, - ) - .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?; + let by = Fq::from_slice(&arg_bytes[64..96]) + .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?; + let bx = Fq::from_slice(&arg_bytes[96..128]) + .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?; + let a = Fq2::new(ax, ay); let b = Fq2::new(bx, by); Ok(if a.is_zero() && b.is_zero() { @@ -338,7 +315,7 @@ pub(crate) fn ec_operation( memory, points_ptr .checked_add(64) - .ok_or(fuel_tx::PanicReason::ArithmeticOverflow)?, + .ok_or(fuel_tx::PanicReason::MemoryOverflow)?, )?; let mut output = [0u8; 64]; #[allow(clippy::arithmetic_side_effects)] @@ -355,7 +332,7 @@ pub(crate) fn ec_operation( memory.read( points_ptr .checked_add(64) - .ok_or(fuel_tx::PanicReason::ArithmeticOverflow)?, + .ok_or(fuel_tx::PanicReason::MemoryOverflow)?, 32u64, )?, ) @@ -392,21 +369,21 @@ pub(crate) fn ec_pairing( let element_size = 128 + 64; let mut elements = Vec::with_capacity( usize::try_from(number_elements) - .map_err(|_| fuel_tx::PanicReason::ArithmeticOverflow)?, + .map_err(|_| fuel_tx::PanicReason::MemoryOverflow)?, ); for idx in 0..number_elements { let start_offset = elements_ptr .checked_add( idx.checked_mul(element_size) - .ok_or(fuel_tx::PanicReason::ArithmeticOverflow)?, + .ok_or(fuel_tx::PanicReason::MemoryOverflow)?, ) - .ok_or(fuel_tx::PanicReason::ArithmeticOverflow)?; + .ok_or(fuel_tx::PanicReason::MemoryOverflow)?; let a = read_g1_point_alt_bn_128(memory, start_offset)?; let b = read_g2_point_alt_bn_128( memory, start_offset .checked_add(64) - .ok_or(fuel_tx::PanicReason::ArithmeticOverflow)?, + .ok_or(fuel_tx::PanicReason::MemoryOverflow)?, )?; elements.push((a, b)); } From 21323ec4e126cde5d077aa6c87b2a4ac98b7f273 Mon Sep 17 00:00:00 2001 From: AurelienFT Date: Thu, 28 Nov 2024 14:59:04 +0100 Subject: [PATCH 3/3] Fix inversion letter --- fuel-vm/src/interpreter/crypto.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fuel-vm/src/interpreter/crypto.rs b/fuel-vm/src/interpreter/crypto.rs index d46cd81fe..1ac5f451f 100644 --- a/fuel-vm/src/interpreter/crypto.rs +++ b/fuel-vm/src/interpreter/crypto.rs @@ -254,9 +254,9 @@ fn read_g1_point_alt_bn_128( // Big endian required by the library let arg_bytes: [u8; 2 * 32] = memory.read_bytes(point_ptr)?; - let py = Fq::from_slice(&arg_bytes[..32]) + let px = Fq::from_slice(&arg_bytes[..32]) .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?; - let px = Fq::from_slice(&arg_bytes[32..64]) + let py = Fq::from_slice(&arg_bytes[32..64]) .map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?; Ok(if px == Fq::zero() && py == Fq::zero() {