-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement EIP-2537 using gnark-crypto (#168)
* implement EIP-2537 and EIP-196 with gnark-crypto * shim gnark-crypt implementation for drop-in replacement for matter-labs * add additional test vectors for eip-196 and eip-2537 * bump to java21 and gradle 8.8, bump version to 0.9.0-SNAPSHOT * use gnark-crypto PR 510 until gnark-crypto is released * rebase and bump GHA to java 21 Signed-off-by: garyschulte <garyschulte@gmail.com>
- Loading branch information
1 parent
764001f
commit fd81455
Showing
56 changed files
with
4,301 additions
and
126 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
87 changes: 87 additions & 0 deletions
87
...t/java/org/hyperledger/besu/nativelib/bls12_381/AltBN128G1AddPrecompiledContractTest.java
This file contains 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,87 @@ | ||
/* | ||
* Copyright ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
package org.hyperledger.besu.nativelib.bls12_381; | ||
|
||
import com.google.common.io.CharStreams; | ||
import com.sun.jna.ptr.IntByReference; | ||
import org.apache.tuweni.bytes.Bytes; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@RunWith(Parameterized.class) | ||
public class AltBN128G1AddPrecompiledContractTest { | ||
|
||
@Parameterized.Parameter(0) | ||
public String input; | ||
@Parameterized.Parameter(1) | ||
public String expectedResult; | ||
@Parameterized.Parameter(2) | ||
public String expectedGasUsed; | ||
@Parameterized.Parameter(3) | ||
public String notes; | ||
|
||
@Parameterized.Parameters | ||
public static Iterable<String[]> parameters() throws IOException { | ||
return CharStreams.readLines( | ||
new InputStreamReader( | ||
AltBN128G1AddPrecompiledContractTest.class.getResourceAsStream("eip196_g1_add.csv"), UTF_8)) | ||
.stream() | ||
.map(line -> line.split(",", 4)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Test | ||
public void shouldCalculate() { | ||
if ("input".equals(input)) { | ||
// skip the header row | ||
return; | ||
} | ||
final byte[] input = Bytes.fromHexString(this.input).toArrayUnsafe(); | ||
|
||
final byte[] output = new byte[LibEthPairings.EIP196_PREALLOCATE_FOR_RESULT_BYTES]; | ||
final IntByReference outputLength = new IntByReference(); | ||
final byte[] error = new byte[LibEthPairings.EIP196_PREALLOCATE_FOR_ERROR_BYTES]; | ||
final IntByReference errorLength = new IntByReference(); | ||
|
||
LibEthPairings.eip196_perform_operation( | ||
LibEthPairings.EIP196_ADD_OPERATION_RAW_VALUE, | ||
input, | ||
input.length, | ||
output, | ||
outputLength, | ||
error, | ||
errorLength); | ||
|
||
final Bytes expectedComputation = | ||
expectedResult == null ? null : Bytes.fromHexString(expectedResult); | ||
if (errorLength.getValue() > 0) { | ||
assertThat(notes).isNotEmpty(); | ||
assertThat(new String(error, 0, errorLength.getValue(), UTF_8)).contains(notes); | ||
assertThat(outputLength.getValue()).isZero(); | ||
} else { | ||
final Bytes actualComputation = Bytes.wrap(output, 0, outputLength.getValue()); | ||
assertThat(actualComputation).isEqualTo(expectedComputation); | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
...t/java/org/hyperledger/besu/nativelib/bls12_381/AltBN128G1MulPrecompiledContractTest.java
This file contains 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,87 @@ | ||
/* | ||
* Copyright ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
package org.hyperledger.besu.nativelib.bls12_381; | ||
|
||
import com.google.common.io.CharStreams; | ||
import com.sun.jna.ptr.IntByReference; | ||
import org.apache.tuweni.bytes.Bytes; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@RunWith(Parameterized.class) | ||
public class AltBN128G1MulPrecompiledContractTest { | ||
|
||
@Parameterized.Parameter(0) | ||
public String input; | ||
@Parameterized.Parameter(1) | ||
public String expectedResult; | ||
@Parameterized.Parameter(2) | ||
public String expectedGasUsed; | ||
@Parameterized.Parameter(3) | ||
public String notes; | ||
|
||
@Parameterized.Parameters | ||
public static Iterable<String[]> parameters() throws IOException { | ||
return CharStreams.readLines( | ||
new InputStreamReader( | ||
AltBN128G1MulPrecompiledContractTest.class.getResourceAsStream("eip196_g1_mul.csv"), UTF_8)) | ||
.stream() | ||
.map(line -> line.split(",", 4)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Test | ||
public void shouldCalculate() { | ||
if ("input".equals(input)) { | ||
// skip the header row | ||
return; | ||
} | ||
final byte[] input = Bytes.fromHexString(this.input).toArrayUnsafe(); | ||
|
||
final byte[] output = new byte[LibEthPairings.EIP196_PREALLOCATE_FOR_RESULT_BYTES]; | ||
final IntByReference outputLength = new IntByReference(); | ||
final byte[] error = new byte[LibEthPairings.EIP196_PREALLOCATE_FOR_ERROR_BYTES]; | ||
final IntByReference errorLength = new IntByReference(); | ||
|
||
LibEthPairings.eip196_perform_operation( | ||
LibEthPairings.EIP196_MUL_OPERATION_RAW_VALUE, | ||
input, | ||
input.length, | ||
output, | ||
outputLength, | ||
error, | ||
errorLength); | ||
|
||
final Bytes expectedComputation = | ||
expectedResult == null ? null : Bytes.fromHexString(expectedResult); | ||
if (errorLength.getValue() > 0) { | ||
assertThat(notes).isNotEmpty(); | ||
assertThat(new String(error, 0, errorLength.getValue(), UTF_8)).contains(notes); | ||
assertThat(outputLength.getValue()).isZero(); | ||
} else { | ||
final Bytes actualComputation = Bytes.wrap(output, 0, outputLength.getValue()); | ||
assertThat(actualComputation).isEqualTo(expectedComputation); | ||
} | ||
} | ||
} |
Oops, something went wrong.