|
| 1 | +#![cfg_attr(not(feature = "std"), no_std, no_main)] |
| 2 | +#![allow(clippy::new_without_default)] |
| 3 | + |
| 4 | +#[ink::contract] |
| 5 | +mod builtin_precompiles { |
| 6 | + use ink::U256; |
| 7 | + |
| 8 | + #[ink(storage)] |
| 9 | + pub struct BuiltinPrecompiles {} |
| 10 | + |
| 11 | + impl BuiltinPrecompiles { |
| 12 | + #[ink(constructor)] |
| 13 | + pub fn new() -> Self { |
| 14 | + Self {} |
| 15 | + } |
| 16 | + |
| 17 | + /// Tests the `bn128_add` functionality. |
| 18 | + #[ink(message)] |
| 19 | + pub fn bn128_add(&self) -> (U256, U256) { |
| 20 | + self.env().bn128_add(1.into(), 2.into(), 1.into(), 2.into()) |
| 21 | + } |
| 22 | + |
| 23 | + /// Tests the `bn128_mul` functionality. |
| 24 | + #[ink(message)] |
| 25 | + pub fn bn128_mul(&self) -> (U256, U256) { |
| 26 | + self.env().bn128_mul(1.into(), 2.into(), 3.into()) |
| 27 | + } |
| 28 | + |
| 29 | + /// Tests the `bn128_pairing` functionality. |
| 30 | + #[ink(message)] |
| 31 | + pub fn bn128_pairing(&self, input: ink::prelude::vec::Vec<u8>) -> bool { |
| 32 | + self.env().bn128_pairing(input.as_ref()) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + #[cfg(all(test, feature = "e2e-tests"))] |
| 37 | + mod e2e_tests { |
| 38 | + use super::*; |
| 39 | + use impl_serde::serialize as serde_hex; |
| 40 | + use ink_e2e::ContractsBackend; |
| 41 | + |
| 42 | + type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>; |
| 43 | + |
| 44 | + #[ink_e2e::test] |
| 45 | + async fn bn128_add_works<Client: E2EBackend>( |
| 46 | + mut client: Client, |
| 47 | + ) -> E2EResult<()> { |
| 48 | + // given |
| 49 | + let mut constructor = BuiltinPrecompilesRef::new(); |
| 50 | + let contract = client |
| 51 | + .instantiate("builtin-precompiles", &ink_e2e::eve(), &mut constructor) |
| 52 | + .submit() |
| 53 | + .await |
| 54 | + .expect("instantiate failed"); |
| 55 | + let call_builder = contract.call_builder::<BuiltinPrecompiles>(); |
| 56 | + |
| 57 | + // when |
| 58 | + let (x3, y3) = client |
| 59 | + .call(&ink_e2e::eve(), &call_builder.bn128_add()) |
| 60 | + .dry_run() |
| 61 | + .await? |
| 62 | + .return_value(); |
| 63 | + |
| 64 | + // then |
| 65 | + // taken from https://github.com/polkadot-developers/polkavm-hardhat-examples/blob/v0.0.3/precompiles-hardhat/test/BN128Add.js |
| 66 | + assert_eq!( |
| 67 | + format!("{}", x3), |
| 68 | + "1368015179489954701390400359078579693043519447331113978918064868415326638035" |
| 69 | + ); |
| 70 | + assert_eq!( |
| 71 | + format!("{}", y3), |
| 72 | + "9918110051302171585080402603319702774565515993150576347155970296011118125764" |
| 73 | + ); |
| 74 | + |
| 75 | + Ok(()) |
| 76 | + } |
| 77 | + |
| 78 | + #[ink_e2e::test] |
| 79 | + async fn bn128_mul_works<Client: E2EBackend>( |
| 80 | + mut client: Client, |
| 81 | + ) -> E2EResult<()> { |
| 82 | + // given |
| 83 | + let mut constructor = BuiltinPrecompilesRef::new(); |
| 84 | + let contract = client |
| 85 | + .instantiate("builtin-precompiles", &ink_e2e::eve(), &mut constructor) |
| 86 | + .submit() |
| 87 | + .await |
| 88 | + .expect("instantiate failed"); |
| 89 | + let call_builder = contract.call_builder::<BuiltinPrecompiles>(); |
| 90 | + |
| 91 | + // when |
| 92 | + let (x2, y2) = client |
| 93 | + .call(&ink_e2e::eve(), &call_builder.bn128_mul()) |
| 94 | + .dry_run() |
| 95 | + .await? |
| 96 | + .return_value(); |
| 97 | + |
| 98 | + // then |
| 99 | + // taken from https://github.com/polkadot-developers/polkavm-hardhat-examples/blob/v0.0.3/precompiles-hardhat/test/BN128Mul.js |
| 100 | + assert_eq!( |
| 101 | + format!("{}", x2), |
| 102 | + "3353031288059533942658390886683067124040920775575537747144343083137631628272" |
| 103 | + ); |
| 104 | + assert_eq!( |
| 105 | + format!("{}", y2), |
| 106 | + "19321533766552368860946552437480515441416830039777911637913418824951667761761" |
| 107 | + ); |
| 108 | + |
| 109 | + Ok(()) |
| 110 | + } |
| 111 | + |
| 112 | + #[ink_e2e::test] |
| 113 | + async fn bn128_pairing_works<Client: E2EBackend>( |
| 114 | + mut client: Client, |
| 115 | + ) -> E2EResult<()> { |
| 116 | + // given |
| 117 | + let mut constructor = BuiltinPrecompilesRef::new(); |
| 118 | + let contract = client |
| 119 | + .instantiate("builtin-precompiles", &ink_e2e::eve(), &mut constructor) |
| 120 | + .submit() |
| 121 | + .await |
| 122 | + .expect("instantiate failed"); |
| 123 | + let call_builder = contract.call_builder::<BuiltinPrecompiles>(); |
| 124 | + |
| 125 | + // when |
| 126 | + // Taken from https://github.com/polkadot-developers/polkavm-hardhat-examples/blob/v0.0.3/precompiles-hardhat/test/BN128Pairing.js |
| 127 | + // |
| 128 | + // Using the "two_point_match_2" test vector from your data |
| 129 | + // This is a known valid pairing that should return true. |
| 130 | + let input: [u8; 384] = hex_literal::hex!( |
| 131 | + "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c21800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c21800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed275dc4a288d1afb3cbb1ac09187524c7db36395df7be3b99e673b13a075a65ec1d9befcd05a5323e6da4d435f3b617cdb3af83285c2df711ef39c01571827f9d" |
| 132 | + ); |
| 133 | + let res = client |
| 134 | + .call(&ink_e2e::eve(), &call_builder.bn128_pairing(input.to_vec())) |
| 135 | + .dry_run() |
| 136 | + .await? |
| 137 | + .return_value(); |
| 138 | + |
| 139 | + // then |
| 140 | + assert!(res); |
| 141 | + |
| 142 | + Ok(()) |
| 143 | + } |
| 144 | + |
| 145 | + #[ink_e2e::test] |
| 146 | + async fn bn128_pairing_zero_points_works<Client: E2EBackend>( |
| 147 | + mut client: Client, |
| 148 | + ) -> E2EResult<()> { |
| 149 | + // given |
| 150 | + let mut constructor = BuiltinPrecompilesRef::new(); |
| 151 | + let contract = client |
| 152 | + .instantiate("builtin-precompiles", &ink_e2e::eve(), &mut constructor) |
| 153 | + .submit() |
| 154 | + .await |
| 155 | + .expect("instantiate failed"); |
| 156 | + let call_builder = contract.call_builder::<BuiltinPrecompiles>(); |
| 157 | + |
| 158 | + // when |
| 159 | + // Taken from https://github.com/polkadot-developers/polkavm-hardhat-examples/blob/v0.0.3/precompiles-hardhat/test/BN128Pairing.js |
| 160 | + // |
| 161 | + // Pairing with zero points: |
| 162 | + // The zero point in G1 is (0, 0) and in G2 is ((0, 0), (0, 0)) |
| 163 | + // Pairing of zero points should return true |
| 164 | + let pairing = format!( |
| 165 | + "{}{}{}{}{}{}", |
| 166 | + // G1 zero point |
| 167 | + "0000000000000000000000000000000000000000000000000000000000000000", /* G1.x = 0 */ |
| 168 | + "0000000000000000000000000000000000000000000000000000000000000000", /* G1.y = 0 */ |
| 169 | + // G2 zero point |
| 170 | + "0000000000000000000000000000000000000000000000000000000000000000", /* G2.x imaginary = 0 */ |
| 171 | + "0000000000000000000000000000000000000000000000000000000000000000", /* G2.x real = 0 */ |
| 172 | + "0000000000000000000000000000000000000000000000000000000000000000", /* G2.y imaginary = 0 */ |
| 173 | + "0000000000000000000000000000000000000000000000000000000000000000" |
| 174 | + ); // G2.y real = 0 |
| 175 | + let input = serde_hex::from_hex(&pairing).expect("parsing hex failed"); |
| 176 | + let res = client |
| 177 | + .call(&ink_e2e::eve(), &call_builder.bn128_pairing(input)) |
| 178 | + .dry_run() |
| 179 | + .await? |
| 180 | + .return_value(); |
| 181 | + |
| 182 | + // then |
| 183 | + assert!(res); |
| 184 | + |
| 185 | + Ok(()) |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments