Skip to content
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

Implement EIP-2537 using gnark-crypto #168

Merged
merged 35 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8473c93
initial gnark eip-2537 skeleton
garyschulte May 7, 2024
651d00f
working g1Add poc, with test
garyschulte May 8, 2024
9fd1a3a
cleaner version of 2537 G1Add
garyschulte May 8, 2024
8ecfeb0
test timings for G1Add
garyschulte May 9, 2024
2902557
impl and timings for G1Mul
garyschulte May 10, 2024
f110aaf
PoC implementation for G1Add G1Mul G2Add G2Mul
garyschulte May 11, 2024
b7050b6
add G1MultiExp
garyschulte May 11, 2024
8f4d3b4
G2MultiExp, FpToG1, Fp2ToG2, start on pairing
garyschulte May 11, 2024
989d53d
pairings, some csv test fixture tweaks
garyschulte May 11, 2024
2ebf7db
revert timings changes to matter-labs bls12-381
garyschulte May 11, 2024
a0486a3
test cleanup
garyschulte May 11, 2024
38a8c1a
attempt at DRY
garyschulte May 11, 2024
a4ffb7b
linux arm64 and amd64 targets
garyschulte May 11, 2024
4125f73
add 2537 lib to build.sh
garyschulte May 12, 2024
379cc71
initial timings with idiomatic jblst
garyschulte May 25, 2024
9ca4b91
g1multiExpr jblst timings
garyschulte May 25, 2024
104dff0
G1/G2 written and tested.
garyschulte May 28, 2024
ddc2f24
pairing
garyschulte May 29, 2024
2e8d33e
fp_to_g1 and fp2_to_g2 implemented, but mapping function is incorrect
garyschulte May 29, 2024
ed9bef0
built using local build of jblst 0.3.12 with P1.map_to and P2.map_to …
garyschulte May 30, 2024
ddc7ea3
SKIP_CI
garyschulte May 30, 2024
8a69a56
clean up for merge
garyschulte Jun 4, 2024
28e9794
add eip-196 to gnark bindings. add shims for drop-in-replacement for…
garyschulte Jun 4, 2024
c2e497c
shim gnark eip-2537 implementation for drop-in replacement for matter…
garyschulte Jun 6, 2024
d987074
fix pairing deserialization
garyschulte Jun 6, 2024
da003be
added additional pairing test vectors to bls12-381 for performance co…
garyschulte Jun 6, 2024
5a2fa1c
additional test cases from eip-196
garyschulte Jun 6, 2024
86469e1
PR fixup
garyschulte Jun 7, 2024
6353021
use error SetBytesCanonical for gnark bls12-381, use error array inst…
garyschulte Jun 14, 2024
2a642e7
use passed in lengths, but defer to max val
garyschulte Jun 15, 2024
35eb1bc
add nonMontogomery marshalling, add test cases found in reference tes…
garyschulte Jun 20, 2024
03cf505
fix g1Add edge case for eip196
garyschulte Jun 20, 2024
c4cb76c
fix bounds problem with eip196 ecmul, add test cases covering partial…
garyschulte Jun 20, 2024
9f694fe
bump to java21 and gradle 8.8, bump version to 0.9.0-SNAPSHOT, use gn…
garyschulte Jun 24, 2024
7686ae2
rebase and bump GHA to java 21
garyschulte Jun 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading