Skip to content

Commit

Permalink
rewrote point_evaluation_run()
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Jul 15, 2023
1 parent 0ad5c0c commit e3a4142
Showing 1 changed file with 9 additions and 21 deletions.
30 changes: 9 additions & 21 deletions crates/precompile/src/point_evaluation.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use core::str::FromStr;

use alloc::vec::Vec;
use num::{BigUint, FromPrimitive};
use revm_primitives::{PrecompileResult, StandardPrecompileFn, uint};
use revm_primitives::{PrecompileResult, StandardPrecompileFn, uint, U256};
use sha2::{Digest, Sha256};

use crate::{Precompile, PrecompileAddress};
Expand All @@ -12,13 +11,13 @@ pub const POINT_EVALUATION_PRECOMPILE: PrecompileAddress = PrecompileAddress(
Precompile::Standard(point_evaluation_run as StandardPrecompileFn),
);

const FIELD_ELEMENTS_PER_BLOB: u32 = 4096;
// Modulus is 381 bits which is greater than 256 bits so need to find better type
// `FIELD_ELEMENTS_PER_BLOB = 4096`
// TODO: make FIELD_ELEMENTS_PER_BLOB and BLS_MODULUS a constant slice so we can easily append them
const FIELD_ELEMENTS_PER_BLOB: [u8;4] = [0,0,16,0];
// const BLS_MODULUS: uint<> =
// "52435875175126190479447740508185965837690552500527637822603658699938581184513";
const BLS_MODULUS: &str =
"52435875175126190479447740508185965837690552500527637822603658699938581184513";
const BLOB_COMMITMENT_VERSION_KZG: u8 = 0x01;
const BLS_MODULUS: [u8;32] = [115, 237, 167, 83, 41, 157, 125, 72, 51, 57, 216, 8, 9, 161, 216, 5, 83, 189, 164, 2, 255, 254, 91, 254, 255, 255, 255, 255, 0, 0, 0, 1];
const BLOB_COMMITMENT_VERSION_KZG: u8 = 1;

pub fn point_evaluation_run(input: &[u8], gas_limit: u64) -> PrecompileResult {
// The data is encoded as follows: versioned_hash | z | y | commitment | proof | with z and y being padded 32 byte big endian values
Expand All @@ -35,21 +34,10 @@ pub fn point_evaluation_run(input: &[u8], gas_limit: u64) -> PrecompileResult {
// Verify KZG proof with z and y in big endian format
assert!(_verify_kzg_proof(commitment, z, y, proof));

// Convert FIELD_ELEMENTS_PER_BLOB to BigUint
let field_elements_big_uint = BigUint::from_u32(FIELD_ELEMENTS_PER_BLOB).unwrap();
let mut field_elements_bytes = vec![0; 32];
let bytes_be = field_elements_big_uint.to_bytes_be();
field_elements_bytes[32 - bytes_be.len()..].copy_from_slice(&bytes_be);
let mut result = Vec::from(FIELD_ELEMENTS_PER_BLOB); // The first bytes of the result are the FIELD_ELEMENTS_PER_BLOB
let bls_modulus_bytes = Vec::from(BLS_MODULUS);
result.extend(bls_modulus_bytes); // Concatenate the BLS_MODULUS to the result

// Convert BLS_MODULUS to BigUint
let bls_modulus_big_uint = BigUint::from_str(BLS_MODULUS).expect("Failed to parse BLS_MODULUS");
let mut bls_modulus_bytes = vec![0; 32];
let bytes_be = bls_modulus_big_uint.to_bytes_be();
bls_modulus_bytes[32 - bytes_be.len()..].copy_from_slice(&bytes_be);

// Concatenate both byte arrays
let mut result = field_elements_bytes;
result.extend(bls_modulus_bytes);
Ok((gas_limit, result))
}

Expand Down

0 comments on commit e3a4142

Please sign in to comment.