Skip to content

Commit

Permalink
Implement EIP-2537 using gnark-crypto (#168)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
garyschulte authored Jun 25, 2024
1 parent 764001f commit fd81455
Show file tree
Hide file tree
Showing 56 changed files with 4,301 additions and 126 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ jobs:
uses: actions/setup-java@v4
with:
distribution: adopt
java-version: 17
java-version: 21
cache: gradle
- name: Set up Go
uses: actions/setup-go@v5
Expand Down Expand Up @@ -299,7 +299,7 @@ jobs:
uses: actions/setup-java@v4
with:
distribution: adopt
java-version: 17
java-version: 21
cache: gradle
- name: gradle build
uses: gradle/[email protected]
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

# 0.9.0-SNAPSHOT
* implement EIP-196 and EIP-2537 using gnark-crypto, bump to java 21 [#168](https://github.com/hyperledger/besu-native/pull/168)

# 0.8.5
* ipa-multipoint : add verkle proof verification (#169)

Expand Down
4 changes: 2 additions & 2 deletions altbn128/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ jar {

task sourcesJar(type: Jar, dependsOn: classes) {
archiveBaseName = 'besu-native-altbn128'
classifier = 'sources'
archiveClassifier = 'sources'
from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: javadoc) {
archiveBaseName = 'besu-native-altbn128'
classifier = 'javadoc'
archiveClassifier = 'javadoc'
from javadoc.destinationDir
}

Expand Down
4 changes: 2 additions & 2 deletions arithmetic/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ test {

task sourcesJar(type: Jar, dependsOn: classes) {
archiveBaseName = 'besu-native-arithmetic'
classifier = 'sources'
archiveClassifier = 'sources'
from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: javadoc) {
archiveBaseName = 'besu-native-arithmetic'
classifier = 'javadoc'
archiveClassifier = 'javadoc'
from javadoc.destinationDir
}

Expand Down
4 changes: 2 additions & 2 deletions blake2bf/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ jar {

task sourcesJar(type: Jar, dependsOn: classes) {
archiveBaseName = 'besu-native-blake2bf'
classifier = 'sources'
archiveClassifier = 'sources'
from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: javadoc) {
archiveBaseName = 'besu-native-blake2bf'
classifier = 'javadoc'
archiveClassifier = 'javadoc'
from javadoc.destinationDir
}

Expand Down
4 changes: 2 additions & 2 deletions bls12-381/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ jar {

task sourcesJar(type: Jar, dependsOn: classes) {
archiveBaseName = 'besu-native-bls12-381'
classifier = 'sources'
archiveClassifier = 'sources'
from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: javadoc) {
archiveBaseName = 'besu-native-bls12-381'
classifier = 'javadoc'
archiveClassifier = 'javadoc'
from javadoc.destinationDir
}

Expand Down
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);
}
}
}
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);
}
}
}
Loading

0 comments on commit fd81455

Please sign in to comment.