-
Notifications
You must be signed in to change notification settings - Fork 992
feat: Add a wrapper for matter-labs-eip1962 for EIP196 #2266
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
Closed
Closed
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
34e7f7c
add read_scalar, point_add and point_mul into the wrapper
kevaundray d3aba76
modify bn128.rs to use new api methods
kevaundray ec1a3b4
preserve previous semantics
kevaundray 1dc9404
initial commit to add matter-labs wrapper
kevaundray 99edba1
feature gate matter-labs impl -- make substrate impl still default
kevaundray 45e13e3
update revm and precompile cargo.toml file
kevaundray 398910c
use cfg_if
kevaundray 526103b
make bn an optional dependency
kevaundray 8dfda37
cfg else -> else_if so that there is no silent fallback
kevaundray 5b2fe79
follow same cfg_if pattern as other precompiles
kevaundray 98b6488
fix optimism
kevaundray 5becac4
add back `self` import
kevaundray f8be131
Push empty commit to trigger CI
kevaundray f4c8595
cargo fmt
kevaundray df64b70
clippy fix
kevaundray 570d449
make `bn` the default with revm and revme
kevaundray e38b6e1
fix typo
kevaundray 6b2a765
Merge branch 'main' into kw/add-matter-labs
kevaundray d672f23
Update crates/revm/Cargo.toml
kevaundray cfda7c4
Update crates/revm/Cargo.toml
kevaundray 88ed53c
Update crates/precompile/src/lib.rs
kevaundray 50f45cf
Update crates/optimism/Cargo.toml
kevaundray 0fb0fdd
revert Cargo.toml formatting
kevaundray 6d5348c
multi:
kevaundray b889f1c
revert crates/optimism/src/precompiles.rs
kevaundray 7713162
revert crates/optimism/src/evm.rs
kevaundray 6004b6c
revert unnecessary changes in crates/precompile/src/lib.rs
kevaundray a792687
revert Cargo.toml changes
kevaundray b840176
revert automatic Cargo.toml formatting
kevaundray a57920e
revert formatting on secp256k1 in Cargo.toml
kevaundray 498502e
revert c-kzg formatting in Cargo.toml
kevaundray 184151c
revert dev key in Cargo.toml
kevaundray f80171d
revert c-kzg feature in Cargo.toml
kevaundray b3e5bd1
Merge branch 'main' into kw/add-matter-labs
kevaundray f747766
Update crates/precompile/Cargo.toml
kevaundray 1b20177
Apply suggestions from code review
kevaundray d56d192
Merge branch 'main' into kw/add-matter-labs
kevaundray File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| use super::{PrecompileError, FQ_LEN, G1_LEN, G2_LEN, SCALAR_LEN}; | ||
| use eth_pairings::{ | ||
| engines::bn254::*, | ||
| extension_towers::fp12_as_2_over3_over_2::Fp12, | ||
| field::U256Repr, | ||
| integers::MaxGroupSizeUint, | ||
| pairings::PairingEngine, | ||
| public_interface::{decode_g1, decode_g2}, | ||
| traits::{Group, ZeroAndOne}, | ||
| }; | ||
|
|
||
| /// G1Point is the concrete representation of a G1 element | ||
| pub(super) type G1Point = eth_pairings::weierstrass::curve::CurvePoint< | ||
| 'static, | ||
| eth_pairings::weierstrass::CurveOverFpParameters< | ||
| 'static, | ||
| U256Repr, | ||
| eth_pairings::field::PrimeField<U256Repr>, | ||
| >, | ||
| >; | ||
|
|
||
| /// G2Point is the concrete representation of a G2 element | ||
| pub(super) type G2Point = eth_pairings::weierstrass::curve::CurvePoint< | ||
| 'static, | ||
| eth_pairings::weierstrass::CurveOverFp2Parameters< | ||
| 'static, | ||
| U256Repr, | ||
| eth_pairings::field::PrimeField<U256Repr>, | ||
| >, | ||
| >; | ||
kevaundray marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// Fr is the concrete representation of an element in the scalar field. | ||
| pub(super) type Fr = MaxGroupSizeUint; | ||
|
|
||
| /// Reads a G1 point from the input slice. | ||
| /// | ||
| /// Parses a G1 point from a byte slice by reading two consecutive field elements | ||
| /// representing the x and y coordinates. | ||
| #[inline] | ||
| pub(super) fn read_g1_point(input: &[u8]) -> Result<G1Point, PrecompileError> { | ||
| let (point, _) = decode_g1::decode_g1_point_from_xy_oversized(input, FQ_LEN, &*BN254_G1_CURVE) | ||
| .map_err(|_| PrecompileError::Bn128AffineGFailedToCreate)?; | ||
|
|
||
| if !point.is_on_curve() { | ||
| return Err(PrecompileError::Bn128AffineGFailedToCreate); | ||
| } | ||
|
|
||
| // We can skip the subgroup check since G1 is prime ordered. | ||
|
|
||
| Ok(point) | ||
| } | ||
|
|
||
| /// Encodes a G1 point into a byte array. | ||
| /// | ||
| /// Serializes a G1 point into its x and y coordinates as a byte array. | ||
| #[inline] | ||
| pub(super) fn encode_g1_point(point: G1Point) -> [u8; G1_LEN] { | ||
| let mut output = [0u8; G1_LEN]; | ||
|
|
||
| if !point.is_zero() { | ||
| let as_vec = decode_g1::serialize_g1_point(FQ_LEN, &point).unwrap(); | ||
| output.copy_from_slice(&as_vec[..]); | ||
| } | ||
|
|
||
| output | ||
| } | ||
|
|
||
| /// Reads a G2 point from the input slice. | ||
| /// | ||
| /// Parses a G2 point from a byte slice by reading four consecutive field elements | ||
| /// representing the two coordinates (x and y) of the G2 point. | ||
| #[inline] | ||
| pub(super) fn read_g2_point(input: &[u8]) -> Result<G2Point, PrecompileError> { | ||
| // G2 encoding in EIP 196/197 is non-standard: Fp2 element c0 + v*c1 where v is non-residue is | ||
| // encoded as (c1, c0) instead of usual (c0, c1) | ||
| let mut swapped_encoding = [0u8; G2_LEN]; | ||
|
|
||
| let x_0 = &input[0..FQ_LEN]; | ||
| let x_1 = &input[FQ_LEN..(FQ_LEN * 2)]; | ||
| let y_0 = &input[(FQ_LEN * 2)..(FQ_LEN * 3)]; | ||
| let y_1 = &input[(FQ_LEN * 3)..(FQ_LEN * 4)]; | ||
|
|
||
| // swap for x coordinate | ||
| swapped_encoding[0..FQ_LEN].copy_from_slice(x_1); | ||
| swapped_encoding[FQ_LEN..(FQ_LEN * 2)].copy_from_slice(x_0); | ||
|
|
||
| // swap for y coordinate | ||
| swapped_encoding[(FQ_LEN * 2)..(FQ_LEN * 3)].copy_from_slice(y_1); | ||
| swapped_encoding[(FQ_LEN * 3)..(FQ_LEN * 4)].copy_from_slice(y_0); | ||
|
|
||
| let (g2_point, _) = decode_g2::decode_g2_point_from_xy_in_fp2_oversized( | ||
| &swapped_encoding, | ||
| FQ_LEN, | ||
| &*BN254_G2_CURVE, | ||
| ) | ||
| .map_err(|_| PrecompileError::Bn128AffineGFailedToCreate)?; | ||
|
|
||
| if !g2_point.is_on_curve() { | ||
| return Err(PrecompileError::Bn128FieldPointNotAMember); | ||
| } | ||
|
|
||
| // The zero point is on the curve and in the subgroup | ||
| if g2_point.is_zero() { | ||
| return Ok(g2_point); | ||
| } | ||
| // Check G2 point is in the correct subgroup | ||
| let is_in_subgroup = g2_point | ||
| .wnaf_mul_with_window_size(&BN254_SUBGROUP_ORDER[..], 5) | ||
| .is_zero(); | ||
| if !is_in_subgroup { | ||
| return Err(PrecompileError::Bn128FieldPointNotAMember); | ||
| } | ||
|
|
||
| Ok(g2_point) | ||
| } | ||
|
|
||
| /// Reads a scalar from the input slice | ||
| /// | ||
| /// Note: The scalar does not need to be canonical. | ||
| #[inline] | ||
| pub(super) fn read_scalar(input: &[u8]) -> Fr { | ||
| assert_eq!( | ||
| input.len(), | ||
| SCALAR_LEN, | ||
| "unexpected scalar length. got {}, expected {SCALAR_LEN}", | ||
| input.len() | ||
| ); | ||
| let (scalar, _) = decode_g1::decode_scalar_representation(input, SCALAR_LEN).unwrap(); | ||
|
|
||
| scalar | ||
| } | ||
|
|
||
| /// Performs point addition on two G1 points. | ||
| #[inline] | ||
| pub(super) fn g1_point_add(p1: G1Point, p2: G1Point) -> G1Point { | ||
| let mut result = p1.clone(); | ||
| result.add_assign(&p2); | ||
| result | ||
| } | ||
|
|
||
| /// Performs point multiplication. | ||
| /// | ||
| /// Takes a G1 point and a scalar representation, and returns the result of the multiplication. | ||
| #[inline] | ||
| pub(super) fn g1_point_mul(p: G1Point, scalar: Fr) -> G1Point { | ||
| p.mul(scalar) | ||
| } | ||
|
|
||
| /// pairing_check performs a pairing check on a list of G1 and G2 point pairs. | ||
| /// | ||
| /// Returns true if the result of the pairing is equal to the identity element. | ||
| #[inline] | ||
| pub(super) fn pairing_check(pairs: &[(G1Point, G2Point)]) -> bool { | ||
| if pairs.is_empty() { | ||
| return true; | ||
| } | ||
|
|
||
| let engine = &*BN254_PAIRING_ENGINE; | ||
|
|
||
| // Convert to vectors as required by Matter Labs implementation | ||
| let g1_points: Vec<_> = pairs.iter().map(|(g1, _)| g1.clone()).collect(); | ||
| let g2_points: Vec<_> = pairs.iter().map(|(_, g2)| g2.clone()).collect(); | ||
|
|
||
| let pairing_result = engine.pair(&g1_points, &g2_points); | ||
|
|
||
| // This returns None under two conditions: | ||
| // | ||
| // - g1_points.len() != g2_points.len() | ||
| // - The final_exponentiation value is 0 | ||
| // | ||
| // - The first case is not possible by construction | ||
| // - In the second case, we want to return false because the | ||
| // result is not 1 | ||
| let pairing_result = match pairing_result { | ||
| Some(pr) => pr, | ||
| None => return false, | ||
| }; | ||
|
|
||
| let one_fp12 = Fp12::one(&*BN254_EXT12_FIELD); | ||
| pairing_result == one_fp12 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.