Skip to content
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
78 changes: 29 additions & 49 deletions actors/evm/src/interpreter/precompiles/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,13 @@ pub(super) fn modexp<RT: Runtime>(
Ok(output)
}

pub(super) fn curve_to_vec(curve: G1) -> Result<Vec<u8>, PrecompileError> {
AffineG1::from_jacobian(curve)
.map(|product| {
let mut output = vec![0; 64];
product.x().to_big_endian(&mut output[0..32]).unwrap();
product.y().to_big_endian(&mut output[32..64]).unwrap();
output
})
.ok_or(PrecompileError::EcErr(substrate_bn::CurveError::InvalidEncoding))
pub(super) fn curve_to_vec(curve: G1) -> Vec<u8> {
let mut output = vec![0; 64];
if let Some(product) = AffineG1::from_jacobian(curve) {
product.x().to_big_endian(&mut output[0..32]).unwrap();
product.y().to_big_endian(&mut output[32..64]).unwrap();
}
output
}

/// add 2 points together on an elliptic curve
Expand All @@ -165,7 +163,7 @@ pub(super) fn ec_add<RT: Runtime>(
let point1: G1 = input_params.read_value()?;
let point2: G1 = input_params.read_value()?;

curve_to_vec(point1 + point2)
Ok(curve_to_vec(point1 + point2))
}

/// multiply a point on an elliptic curve by a scalar value
Expand All @@ -178,7 +176,7 @@ pub(super) fn ec_mul<RT: Runtime>(
let point: G1 = input_params.read_value()?;
let scalar: Fr = input_params.read_value()?;

curve_to_vec(point * scalar)
Ok(curve_to_vec(point * scalar))
}

/// pairs multple groups of twisted bn curves
Expand Down Expand Up @@ -467,27 +465,14 @@ mod tests {
let res = ec_add(&mut system, &input, PrecompileContext::default()).unwrap();
assert_eq!(res, expected);
// zero sum test
let input = hex::decode(
"\
0000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000",
)
.unwrap();
let res = ec_add(&mut system, &input, PrecompileContext::default());
assert!(matches!(
res,
Err(PrecompileError::EcErr(substrate_bn::CurveError::InvalidEncoding))
));
let input = vec![0; 32 * 4];
let res = ec_add(&mut system, &input, PrecompileContext::default()).unwrap();
assert_eq!(res, vec![0; 64]);

// no input test
// no input test (auto zero extend)
let input = [];
let res = ec_add(&mut system, &input, PrecompileContext::default());
assert!(matches!(
res,
Err(PrecompileError::EcErr(substrate_bn::CurveError::InvalidEncoding))
));
let res = ec_add(&mut system, &input, PrecompileContext::default()).unwrap();
assert_eq!(res, vec![0; 64]);

// point not on curve fail
let input = hex::decode(
Expand Down Expand Up @@ -526,27 +511,10 @@ mod tests {
let res = ec_mul(&mut system, &input, PrecompileContext::default()).unwrap();
assert_eq!(res, expected);

// out of gas test
let input = hex::decode(
"\
0000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000\
0200000000000000000000000000000000000000000000000000000000000000",
)
.unwrap();
let res = ec_mul(&mut system, &input, PrecompileContext::default());
assert!(matches!(
res,
Err(PrecompileError::EcErr(substrate_bn::CurveError::InvalidEncoding))
));

// no input test
let input = [0u8; 0];
let res = ec_mul(&mut system, &input, PrecompileContext::default());
assert!(matches!(
res,
Err(PrecompileError::EcErr(substrate_bn::CurveError::InvalidEncoding))
));
let res = ec_mul(&mut system, &input, PrecompileContext::default()).unwrap();
assert_eq!(res, vec![0u8; 64]);

// point not on curve fail
let input = hex::decode(
Expand All @@ -561,6 +529,18 @@ mod tests {
res,
Err(PrecompileError::EcErr(substrate_bn::CurveError::NotMember))
));

let input = hex::decode(
"\
035cf447ec2f8f21e6ea3d49d80a4a823834b1a776ab1733731587613f5065f8\
21c972c4e0c8eb2430171599b1f4900601fdb8f4b2d248d22ebefe3d5368a800\
0000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000\
",
)
.unwrap();
let res = ec_mul(&mut system, &input, PrecompileContext::default());
assert_eq!(vec![0u8; 64], res.unwrap());
}

#[test]
Expand Down