Skip to content

Commit

Permalink
Upgrade jc-kzg-4844 to latest version (#8651)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov authored Sep 27, 2024
1 parent 600b34e commit 080b5a5
Show file tree
Hide file tree
Showing 14 changed files with 37,104 additions and 16,684 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions gradle/versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencyManagement {

dependency 'io.libp2p:jvm-libp2p:1.2.0-RELEASE'
dependency 'tech.pegasys:jblst:0.3.12'
dependency 'tech.pegasys:jc-kzg-4844:1.0.0'
dependency 'io.consensys.protocols:jc-kzg-4844:2.0.0'

dependency 'org.hdrhistogram:HdrHistogram:2.2.2'

Expand Down Expand Up @@ -130,7 +130,7 @@ dependencyManagement {
dependency 'org.fusesource.leveldbjni:leveldbjni-win32:1.8'
dependency 'tech.pegasys:leveldb-native:0.3.1'

dependencySet(group: "org.web3j", version: "4.12.0") {
dependencySet(group: "org.web3j", version: "4.12.2") {
entry 'core'
entry 'abi'
entry 'crypto'
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/kzg/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dependencies {

implementation 'org.apache.tuweni:tuweni-bytes'
implementation 'org.apache.tuweni:tuweni-ssz'
implementation 'tech.pegasys:jc-kzg-4844'
implementation 'io.consensys.protocols:jc-kzg-4844'
implementation 'commons-io:commons-io'

testFixturesImplementation 'com.google.guava:guava'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
final class CKZG4844 implements KZG {

private static final Logger LOG = LogManager.getLogger();
// used for FK20 proof computations (PeerDAS) so can default to 0 for now
private static final int PRECOMPUTE_DEFAULT = 0;

private static CKZG4844 instance;

Expand Down Expand Up @@ -67,13 +69,14 @@ public synchronized void loadTrustedSetup(final String trustedSetupFile) throws
freeTrustedSetup();
});
final TrustedSetup trustedSetup = CKZG4844Utils.parseTrustedSetupFile(trustedSetupFile);
final List<Bytes> g1Points = trustedSetup.g1Points();
final List<Bytes> g2Points = trustedSetup.g2Points();
final List<Bytes> g1PointsLagrange = trustedSetup.g1Lagrange();
final List<Bytes> g2PointsMonomial = trustedSetup.g2Monomial();
final List<Bytes> g1PointsMonomial = trustedSetup.g1Monomial();
CKZG4844JNI.loadTrustedSetup(
CKZG4844Utils.flattenG1Points(g1Points),
g1Points.size(),
CKZG4844Utils.flattenG2Points(g2Points),
g2Points.size());
CKZG4844Utils.flattenG1Points(g1PointsMonomial),
CKZG4844Utils.flattenG1Points(g1PointsLagrange),
CKZG4844Utils.flattenG2Points(g2PointsMonomial),
PRECOMPUTE_DEFAULT);
LOG.debug("Loaded trusted setup from {}", trustedSetupFile);
loadedTrustedSetupFile = Optional.of(trustedSetupFile);
} catch (final Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

package tech.pegasys.teku.kzg;

import static tech.pegasys.teku.kzg.KZG.BYTES_PER_G1;
import static tech.pegasys.teku.kzg.KZG.BYTES_PER_G2;

import com.google.common.base.Preconditions;
import ethereum.ckzg4844.CKZG4844JNI;
import java.io.BufferedReader;
Expand Down Expand Up @@ -49,11 +52,11 @@ public static byte[] flattenProofs(final List<KZGProof> kzgProofs) {
}

public static byte[] flattenG1Points(final List<Bytes> g1Points) {
return flattenBytes(g1Points, CKZG4844JNI.BYTES_PER_G1 * g1Points.size());
return flattenBytes(g1Points, BYTES_PER_G1 * g1Points.size());
}

public static byte[] flattenG2Points(final List<Bytes> g2Points) {
return flattenBytes(g2Points, CKZG4844JNI.BYTES_PER_G2 * g2Points.size());
return flattenBytes(g2Points, BYTES_PER_G2 * g2Points.size());
}

public static TrustedSetup parseTrustedSetupFile(final String trustedSetupFile)
Expand All @@ -70,20 +73,26 @@ public static TrustedSetup parseTrustedSetupFile(final String trustedSetupFile)
final int g1Size = Integer.parseInt(reader.readLine());
// Number of G2 points
final int g2Size = Integer.parseInt(reader.readLine());
// List of G1 points, one on each new line
final List<Bytes> g1Points = new ArrayList<>();
// List of G1 Lagrange points, one on each new line
final List<Bytes> g1PointsLagrange = new ArrayList<>();
for (int i = 0; i < g1Size; i++) {
final Bytes g1Point = Bytes.fromHexString(reader.readLine(), CKZG4844JNI.BYTES_PER_G1);
g1Points.add(g1Point);
final Bytes g1Point = Bytes.fromHexString(reader.readLine(), BYTES_PER_G1);
g1PointsLagrange.add(g1Point);
}
// List of G2 points, one on each new line
final List<Bytes> g2Points = new ArrayList<>();
// List of G2 Monomial points, one on each new line
final List<Bytes> g2PointsMonomial = new ArrayList<>();
for (int i = 0; i < g2Size; i++) {
final Bytes g2Point = Bytes.fromHexString(reader.readLine(), CKZG4844JNI.BYTES_PER_G2);
g2Points.add(g2Point);
final Bytes g2Point = Bytes.fromHexString(reader.readLine(), BYTES_PER_G2);
g2PointsMonomial.add(g2Point);
}
// List of G1 Monomial points, one on each new line
final List<Bytes> g1PointsMonomial = new ArrayList<>();
for (int i = 0; i < g1Size; i++) {
final Bytes g1Point = Bytes.fromHexString(reader.readLine(), BYTES_PER_G1);
g1PointsMonomial.add(g1Point);
}

return new TrustedSetup(g1Points, g2Points);
return new TrustedSetup(g1PointsLagrange, g2PointsMonomial, g1PointsMonomial);
} catch (final Exception ex) {
throw new IOException(
String.format("Failed to parse trusted setup file\n: %s", trustedSetupFile));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
public interface KZG {

int BYTES_PER_G1 = 48;
int BYTES_PER_G2 = 96;

static KZG getInstance() {
return CKZG4844.getInstance();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@
package tech.pegasys.teku.kzg;

import static com.google.common.base.Preconditions.checkArgument;
import static ethereum.ckzg4844.CKZG4844JNI.BYTES_PER_G1;
import static ethereum.ckzg4844.CKZG4844JNI.BYTES_PER_G2;
import static tech.pegasys.teku.kzg.KZG.BYTES_PER_G1;
import static tech.pegasys.teku.kzg.KZG.BYTES_PER_G2;

import java.util.List;
import org.apache.tuweni.bytes.Bytes;

record TrustedSetup(List<Bytes> g1Points, List<Bytes> g2Points) {
TrustedSetup(final List<Bytes> g1Points, final List<Bytes> g2Points) {
this.g1Points = g1Points;
this.g2Points = g2Points;
g1Points.forEach(this::validateG1Point);
g2Points.forEach(this::validateG2Point);
record TrustedSetup(List<Bytes> g1Lagrange, List<Bytes> g2Monomial, List<Bytes> g1Monomial) {

public TrustedSetup(
final List<Bytes> g1Lagrange, final List<Bytes> g2Monomial, final List<Bytes> g1Monomial) {
g1Lagrange.forEach(this::validateG1Point);
this.g1Lagrange = g1Lagrange;
g2Monomial.forEach(this::validateG2Point);
this.g2Monomial = g2Monomial;
g1Monomial.forEach(this::validateG1Point);
this.g1Monomial = g1Monomial;
}

private void validateG1Point(final Bytes g1Point) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,22 +251,10 @@ public void incorrectTrustedSetupFilesShouldThrow(final String filename) {
assertThat(cause.getMessage()).contains("Failed to parse trusted setup file");
}

@Test
public void monomialTrustedSetupFilesShouldThrow() {
final KZGException kzgException =
assertThrows(
KZGException.class,
() ->
CKZG.loadTrustedSetup(
TrustedSetupLoader.getTrustedSetupFile("trusted_setup_monomial.txt")));
assertThat(kzgException.getMessage()).contains("Failed to load trusted setup");
assertThat(kzgException.getCause().getMessage())
.contains("There was an error while loading the Trusted Setup. (C_KZG_BADARGS)");
}

@Test
public void testInvalidLengthG2PointInNewTrustedSetup() {
assertThatThrownBy(() -> new TrustedSetup(List.of(), List.of(Bytes.fromHexString(""))))
assertThatThrownBy(
() -> new TrustedSetup(List.of(), List.of(Bytes.fromHexString("")), List.of()))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Expected G2 point to be 96 bytes");
}
Expand Down
Loading

0 comments on commit 080b5a5

Please sign in to comment.