Skip to content

Commit

Permalink
Use official trusted setup
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Oct 19, 2023
1 parent 7fb8eb1 commit bb00284
Show file tree
Hide file tree
Showing 18 changed files with 24,984 additions and 8,957 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,17 @@
import tech.pegasys.teku.spec.config.SpecConfigDeneb;

public abstract class KzgTestExecutor implements TestExecutor {
// We don't support config reloading and all tests are currently for mainnet
private static final String CONFIG_NAME = "mainnet";

@Override
public final void runTest(TestDefinition testDefinition) throws Throwable {
final Eth2NetworkConfiguration networkConfig =
Eth2NetworkConfiguration.builder(CONFIG_NAME).build();
Eth2NetworkConfiguration.builder(testDefinition.getConfigName()).build();
final SpecConfigDeneb specConfigDeneb =
SpecConfigDeneb.required(networkConfig.getSpec().getGenesisSpecConfig());

KZG kzg = null;
try {
kzg = CKZG4844.createInstance(specConfigDeneb.getFieldElementsPerBlob());
kzg = CKZG4844.createInstance();
kzg.loadTrustedSetup(specConfigDeneb.getTrustedSetupPath().orElseThrow());
runTestImpl(testDefinition);
} finally {
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public MiscHelpersDeneb(final SpecConfigDeneb specConfig) {
private static KZG initKZG(final SpecConfigDeneb config) {
final KZG kzg;
if (!config.getDenebForkEpoch().equals(SpecConfig.FAR_FUTURE_EPOCH) && !config.isKZGNoop()) {
kzg = CKZG4844.createInstance(config.getFieldElementsPerBlob());
kzg = CKZG4844.createInstance();
kzg.loadTrustedSetup(config.getTrustedSetupPath().orElseThrow());
} else {
kzg = KZG.NOOP;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package tech.pegasys.teku.spec.logic.versions.deneb.helpers;

import com.google.common.io.Resources;
import ethereum.ckzg4844.CKZG4844JNI;
import java.util.Optional;
import net.jqwik.api.Tuple;
import net.jqwik.api.lifecycle.LifecycleContext;
Expand Down Expand Up @@ -56,9 +55,8 @@ private KZG getKzgWithTrustedSetup() {

private static class KzgAutoLoadFree implements Store.CloseOnReset {
private static final String TRUSTED_SETUP =
Resources.getResource(TrustedSetups.class, "mainnet/trusted_setup.txt").toExternalForm();
private final KZG kzg =
CKZG4844.createInstance(CKZG4844JNI.Preset.MAINNET.fieldElementsPerBlob);
Resources.getResource(TrustedSetups.class, "trusted_setup.txt").toExternalForm();
private final KZG kzg = CKZG4844.createInstance();

private KzgAutoLoadFree() {
kzg.loadTrustedSetup(TRUSTED_SETUP);
Expand Down
2 changes: 1 addition & 1 deletion gradle/versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencyManagement {

dependency 'io.libp2p:jvm-libp2p:1.0.1-RELEASE'
dependency 'tech.pegasys:jblst:0.3.11'
dependency 'tech.pegasys:jc-kzg-4844:0.7.2'
dependency 'tech.pegasys:jc-kzg-4844:0.8.0'

dependency 'org.hdrhistogram:HdrHistogram:2.1.12'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
package tech.pegasys.teku.kzg.ckzg4844;

import ethereum.ckzg4844.CKZG4844JNI;
import ethereum.ckzg4844.CKZG4844JNI.Preset;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.apache.logging.log4j.LogManager;
Expand All @@ -39,47 +37,26 @@ public final class CKZG4844 implements KZG {

private static CKZG4844 instance;

private static int initializedFieldElementsPerBlob = -1;

private Optional<Integer> loadedTrustedSetupHash = Optional.empty();

public static synchronized CKZG4844 createInstance(final int fieldElementsPerBlob) {
public static synchronized CKZG4844 createInstance() {
if (instance == null) {
final Preset preset = getPreset(fieldElementsPerBlob);
instance = new CKZG4844(preset);
initializedFieldElementsPerBlob = fieldElementsPerBlob;
return instance;
}
if (fieldElementsPerBlob != initializedFieldElementsPerBlob) {
throw new KZGException(
"Can't reinitialize C-KZG-4844 library with a different value for fieldElementsPerBlob.");
instance = new CKZG4844();
}
return instance;
}

public static CKZG4844 getInstance() {
if (instance == null) {
throw new KZGException("C-KZG-4844 library hasn't been initialized.");
throw new KZGException("C-KZG-4844 library hasn't been initialized");
}
return instance;
}

private static Preset getPreset(final int fieldElementsPerBlob) {
return Arrays.stream(Preset.values())
.filter(preset -> preset.fieldElementsPerBlob == fieldElementsPerBlob)
.findFirst()
.orElseThrow(
() ->
new KZGException(
String.format(
"C-KZG-4844 library can't be initialized with %d fieldElementsPerBlob.",
fieldElementsPerBlob)));
}

private CKZG4844(final Preset preset) {
private CKZG4844() {
try {
CKZG4844JNI.loadNativeLibrary(preset);
LOG.debug("Loaded C-KZG-4844 with {} preset", preset);
CKZG4844JNI.loadNativeLibrary();
LOG.debug("Loaded C-KZG-4844 library");
} catch (final Exception ex) {
throw new KZGException("Failed to load C-KZG-4844 library", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class CKZG4844Utils {
private static final int MAX_BYTES_TO_FLATTEN = 100_663_296; // ~100.66 MB or 768 blobs

public static byte[] flattenBlobs(final List<Bytes> blobs) {
return flattenBytes(blobs, CKZG4844JNI.getBytesPerBlob() * blobs.size());
return flattenBytes(blobs, CKZG4844JNI.BYTES_PER_BLOB * blobs.size());
}

public static byte[] flattenCommitments(final List<KZGCommitment> commitments) {
Expand Down
Loading

0 comments on commit bb00284

Please sign in to comment.