Skip to content

Commit

Permalink
BN128 Bounds Checks (paritytech#394)
Browse files Browse the repository at this point in the history
* Check bounds on bn128 inputs

* Whitespace--
  • Loading branch information
notlesh authored May 20, 2021
1 parent f37fc91 commit 8ccd335
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions frame/evm/precompile/bn128/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,20 @@ use fp_evm::Precompile;
use evm::{ExitSucceed, ExitError, Context};

fn read_fr(input: &[u8], start_inx: usize) -> Result<bn::Fr, ExitError> {
if input.len() < start_inx + 32 {
return Err(ExitError::Other("Input not long enough".into()));
}

bn::Fr::from_slice(&input[start_inx..(start_inx + 32)]).map_err(|_| ExitError::Other("Invalid field element".into()))
}

fn read_point(input: &[u8], start_inx: usize) -> Result<bn::G1, ExitError> {
use bn::{Fq, AffineG1, G1, Group};

if input.len() < start_inx + 64 {
return Err(ExitError::Other("Input not long enough".into()));
}

let px = Fq::from_slice(&input[start_inx..(start_inx + 32)]).map_err(|_| ExitError::Other("Invalid point x coordinate".into()))?;
let py = Fq::from_slice(&input[(start_inx + 32)..(start_inx + 64)]).map_err(|_| ExitError::Other("Invalid point y coordinate".into()))?;
Ok(
Expand Down

0 comments on commit 8ccd335

Please sign in to comment.