Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup for add zk opcodes PR #874

Merged
merged 4 commits into from
Nov 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 56 additions & 125 deletions fuel-vm/src/interpreter/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,92 +252,48 @@ fn read_g1_point_alt_bn_128(
point_ptr: Word,
) -> SimpleResult<G1> {
// 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 py = Fq::from_slice(
memory.read(
point_ptr
.checked_add(32)
.ok_or(crate::error::PanicOrBug::Panic(
fuel_tx::PanicReason::ArithmeticOverflow,
))?,
32u64,
)?,
)
.map_err(|_| {
crate::error::PanicOrBug::Panic(fuel_tx::PanicReason::InvalidEllipticCurvePoint)
})?;
let arg_bytes: [u8; 2 * 32] = memory.read_bytes(point_ptr)?;

if px == Fq::zero() && py == Fq::zero() {
Ok(G1::zero())
let px = Fq::from_slice(&arg_bytes[..32])
.map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?;
let py = Fq::from_slice(&arg_bytes[32..64])
.map_err(|_| fuel_tx::PanicReason::InvalidEllipticCurvePoint)?;

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(
memory: &MemoryInstance,
point_ptr: Word,
) -> SimpleResult<G2> {
// 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 ax = Fq::from_slice(
memory.read(
point_ptr
.checked_add(32)
.ok_or(crate::error::PanicOrBug::Panic(
fuel_tx::PanicReason::ArithmeticOverflow,
))?,
32u64,
)?,
)
.map_err(|_| {
crate::error::PanicOrBug::Panic(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,
))?,
32u64,
)?,
)
.map_err(|_| {
crate::error::PanicOrBug::Panic(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,
))?,
32u64,
)?,
)
.map_err(|_| {
crate::error::PanicOrBug::Panic(fuel_tx::PanicReason::InvalidEllipticCurvePoint)
})?;
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 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);
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(
Expand All @@ -357,11 +313,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::MemoryOverflow)?,
)?;
let mut output = [0u8; 64];
#[allow(clippy::arithmetic_side_effects)]
Expand All @@ -374,19 +328,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::MemoryOverflow)?,
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) {
Expand All @@ -395,18 +345,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)?)
}
Expand All @@ -425,40 +367,29 @@ pub(crate) fn ec_pairing(
// Each element consists of 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::MemoryOverflow)?,
);
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::MemoryOverflow)?,
)
.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(
crate::error::PanicOrBug::Panic(
fuel_tx::PanicReason::ArithmeticOverflow,
),
)?,
start_offset
.checked_add(64)
.ok_or(fuel_tx::PanicReason::MemoryOverflow)?,
)?;
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)?)
}
Loading