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

fix: catch panics from EC point creation (e.g. the point is at infinity) #4790

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ examples/**/target/
examples/9
node_modules
pkg/
.idea

# Yarn
.pnp.*
Expand Down
41 changes: 36 additions & 5 deletions acvm-repo/bn254_blackbox_solver/src/fixed_base_scalar_mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,21 @@ pub fn embedded_curve_add(
input2_x: FieldElement,
input2_y: FieldElement,
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> {
let mut point1 = grumpkin::SWAffine::new(input1_x.into_repr(), input1_y.into_repr());
let point2 = grumpkin::SWAffine::new(input2_x.into_repr(), input2_y.into_repr());
fn create_point(
x: FieldElement,
y: FieldElement,
) -> Result<grumpkin::SWAffine, BlackBoxResolutionError> {
match std::panic::catch_unwind(|| grumpkin::SWAffine::new(x.into_repr(), y.into_repr())) {
Ok(point) => Ok(point),
Err(_) => Err(BlackBoxResolutionError::Failed(
BlackBoxFunc::EmbeddedCurveAdd,
format!("Point ({}, {}) is not on curve", x.to_hex(), y.to_hex()),
)),
}
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
}

let mut point1 = create_point(input1_x, input1_y)?;
let point2 = create_point(input2_x, input2_y)?;
let res = point1 + point2;
point1 = res.into();
if let Some((res_x, res_y)) = point1.xy() {
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -72,6 +85,7 @@ mod grumpkin_fixed_base_scalar_mul {
use ark_ff::BigInteger;

use super::*;

#[test]
fn smoke_test() -> Result<(), BlackBoxResolutionError> {
let input = FieldElement::one();
Expand All @@ -84,6 +98,7 @@ mod grumpkin_fixed_base_scalar_mul {
assert_eq!(y, res.1.to_hex());
Ok(())
}

#[test]
fn low_high_smoke_test() -> Result<(), BlackBoxResolutionError> {
let low = FieldElement::one();
Expand All @@ -103,9 +118,9 @@ mod grumpkin_fixed_base_scalar_mul {
let max_limb = FieldElement::from(u128::MAX);
let invalid_limb = max_limb + FieldElement::one();

let expected_error = Err(BlackBoxResolutionError::Failed(
let expected_error = Err(BlackBoxResolutionError::Failed(
BlackBoxFunc::FixedBaseScalarMul,
"Limb 0000000000000000000000000000000100000000000000000000000000000000 is not less than 2^128".into()
"Limb 0000000000000000000000000000000100000000000000000000000000000000 is not less than 2^128".into(),
));

let res = fixed_base_scalar_mul(&invalid_limb, &FieldElement::zero());
Expand All @@ -128,7 +143,23 @@ mod grumpkin_fixed_base_scalar_mul {
res,
Err(BlackBoxResolutionError::Failed(
BlackBoxFunc::FixedBaseScalarMul,
"30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47 is not a valid grumpkin scalar".into()
"30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47 is not a valid grumpkin scalar".into(),
))
);
}

#[test]
fn rejects_addition_of_points_not_in_curve() {
let x = FieldElement::from(1u128);
let y = FieldElement::from(2u128);

let res = embedded_curve_add(x, y, x, y);

assert_eq!(
res,
Err(BlackBoxResolutionError::Failed(
BlackBoxFunc::EmbeddedCurveAdd,
"Point (0000000000000000000000000000000000000000000000000000000000000001, 0000000000000000000000000000000000000000000000000000000000000002) is not on curve".into(),
))
);
}
Expand Down
Loading