Skip to content

Commit

Permalink
fix reference tests
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Oct 20, 2023
1 parent 1e0cde4 commit 26c1872
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.apache.tuweni.bytes.Bytes;
import tech.pegasys.teku.ethtests.finder.TestDefinition;
import tech.pegasys.teku.kzg.KZGCommitment;
import tech.pegasys.teku.kzg.ckzg4844.CKZG4844;

public class KzgBlobToCommitmentTestExecutor extends KzgTestExecutor {

Expand All @@ -30,8 +29,8 @@ public void runTestImpl(final TestDefinition testDefinition) throws Throwable {
KZGCommitment actualKzgCommitment;
try {
final Bytes blob = data.getInput().getBlob();
actualKzgCommitment = CKZG4844.getInstance().blobToKzgCommitment(blob);
} catch (RuntimeException e) {
actualKzgCommitment = kzg.blobToKzgCommitment(blob);
} catch (final RuntimeException ex) {
actualKzgCommitment = null;
}
assertThat(actualKzgCommitment).isEqualTo(expectedKzgCommitment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import tech.pegasys.teku.ethtests.finder.TestDefinition;
import tech.pegasys.teku.kzg.KZGCommitment;
import tech.pegasys.teku.kzg.KZGProof;
import tech.pegasys.teku.kzg.ckzg4844.CKZG4844;

public class KzgComputeBlobProofTestExecutor extends KzgTestExecutor {

Expand All @@ -32,8 +31,8 @@ public void runTestImpl(final TestDefinition testDefinition) throws Throwable {
try {
final Bytes blob = data.getInput().getBlob();
final KZGCommitment commitment = data.getInput().getCommitment();
actualKzgProof = CKZG4844.getInstance().computeBlobKzgProof(blob, commitment);
} catch (RuntimeException e) {
actualKzgProof = kzg.computeBlobKzgProof(blob, commitment);
} catch (final RuntimeException ex) {
actualKzgProof = null;
}
assertThat(actualKzgProof).isEqualTo(expectedKzgProof);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import static tech.pegasys.teku.ethtests.finder.KzgTestFinder.KZG_DATA_FILE;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import tech.pegasys.teku.ethtests.finder.TestDefinition;
import tech.pegasys.teku.kzg.KZG;
import tech.pegasys.teku.kzg.ckzg4844.CKZG4844;
Expand All @@ -26,23 +28,32 @@

public abstract class KzgTestExecutor implements TestExecutor {

private static final Pattern TEST_NAME_PATTERN = Pattern.compile("kzg-(.+)/.+");

protected final KZG kzg = CKZG4844.createInstance();

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

KZG kzg = null;
try {
kzg = CKZG4844.createInstance();
kzg.loadTrustedSetup(specConfigDeneb.getTrustedSetupPath().orElseThrow());
runTestImpl(testDefinition);
} finally {
if (kzg != null) {
kzg.freeTrustedSetup();
}
kzg.freeTrustedSetup();
}
}

private String extractNetwork(final String testName) {
final Matcher matcher = TEST_NAME_PATTERN.matcher(testName);
if (matcher.find()) {
return matcher.group(1);
}
throw new IllegalArgumentException("Can't extract network from " + testName);
}

protected <T> T loadDataFile(final TestDefinition testDefinition, final Class<T> type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import tech.pegasys.teku.ethtests.finder.TestDefinition;
import tech.pegasys.teku.kzg.KZGCommitment;
import tech.pegasys.teku.kzg.KZGProof;
import tech.pegasys.teku.kzg.ckzg4844.CKZG4844;

public class KzgVerifyBlobProofBatchTestExecutor extends KzgTestExecutor {

Expand All @@ -34,9 +33,8 @@ public void runTestImpl(final TestDefinition testDefinition) throws Throwable {
final List<Bytes> blobs = data.getInput().getBlobs();
final List<KZGCommitment> commitments = data.getInput().getCommitments();
final List<KZGProof> proofs = data.getInput().getProofs();
actualVerificationResult =
CKZG4844.getInstance().verifyBlobKzgProofBatch(blobs, commitments, proofs);
} catch (RuntimeException e) {
actualVerificationResult = kzg.verifyBlobKzgProofBatch(blobs, commitments, proofs);
} catch (final RuntimeException ex) {
actualVerificationResult = null;
}
assertThat(actualVerificationResult).isEqualTo(expectedVerificationResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import tech.pegasys.teku.ethtests.finder.TestDefinition;
import tech.pegasys.teku.kzg.KZGCommitment;
import tech.pegasys.teku.kzg.KZGProof;
import tech.pegasys.teku.kzg.ckzg4844.CKZG4844;

/**
* Actually uses {@link tech.pegasys.teku.kzg.KZG#verifyBlobKzgProofBatch(List, List, List)} KZG
Expand All @@ -40,10 +39,8 @@ public void runTestImpl(final TestDefinition testDefinition) throws Throwable {
final KZGCommitment commitment = data.getInput().getCommitment();
final KZGProof proof = data.getInput().getProof();
actualVerificationResult =
CKZG4844
.getInstance()
.verifyBlobKzgProofBatch(List.of(blob), List.of(commitment), List.of(proof));
} catch (RuntimeException e) {
kzg.verifyBlobKzgProofBatch(List.of(blob), List.of(commitment), List.of(proof));
} catch (final RuntimeException ex) {
actualVerificationResult = null;
}
assertThat(actualVerificationResult).isEqualTo(expectedVerificationResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

# Misc
# ---------------------------------------------------------------
# [customized]
FIELD_ELEMENTS_PER_BLOB: 4
# `uint64(4096)`
FIELD_ELEMENTS_PER_BLOB: 4096
# [customized]
MAX_BLOB_COMMITMENTS_PER_BLOCK: 16
# `uint64(6)`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

# Misc
# ---------------------------------------------------------------
# `uint64(4096)`
FIELD_ELEMENTS_PER_BLOB: 4096
# [customized]
FIELD_ELEMENTS_PER_BLOB: 4
# `uint64(2**2)` (= 4)
MAX_BLOBS_PER_BLOCK: 4
MAX_BLOB_COMMITMENTS_PER_BLOCK: 16
# `uint64(6)`
MAX_BLOBS_PER_BLOCK: 6
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
# ---------------------------------------------------------------
# `uint64(4096)`
FIELD_ELEMENTS_PER_BLOB: 4096
# `uint64(2**12)` (= 4096)
MAX_BLOB_COMMITMENTS_PER_BLOCK: 4096
# `uint64(6)`
MAX_BLOBS_PER_BLOCK: 6
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

# Misc
# ---------------------------------------------------------------
# `uint64(4096)`
FIELD_ELEMENTS_PER_BLOB: 4096
# [customized]
FIELD_ELEMENTS_PER_BLOB: 4
MAX_BLOB_COMMITMENTS_PER_BLOCK: 16
# `uint64(6)`
MAX_BLOBS_PER_BLOCK: 6
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

# Misc
# ---------------------------------------------------------------
# `uint64(4096)`
FIELD_ELEMENTS_PER_BLOB: 4096
# [customized]
FIELD_ELEMENTS_PER_BLOB: 4
MAX_BLOB_COMMITMENTS_PER_BLOCK: 16
# `uint64(6)`
MAX_BLOBS_PER_BLOCK: 6
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ public interface KZG {
KZG NOOP =
new KZG() {
@Override
public void loadTrustedSetup(final String trustedSetup) throws KZGException {}

@Override
public void loadTrustedSetup(final TrustedSetup trustedSetup) throws KZGException {}
public void loadTrustedSetup(final String trustedSetupFile) throws KZGException {}

@Override
public void freeTrustedSetup() throws KZGException {}
Expand All @@ -54,9 +51,7 @@ public KZGProof computeBlobKzgProof(final Bytes blob, final KZGCommitment kzgCom
}
};

void loadTrustedSetup(String trustedSetup) throws KZGException;

void loadTrustedSetup(TrustedSetup trustedSetup) throws KZGException;
void loadTrustedSetup(String trustedSetupFile) throws KZGException;

void freeTrustedSetup() throws KZGException;

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

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

private static CKZG4844 instance;

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

public static synchronized CKZG4844 createInstance() {
if (instance == null) {
Expand All @@ -46,13 +45,6 @@ public static synchronized CKZG4844 createInstance() {
return instance;
}

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

private CKZG4844() {
try {
CKZG4844JNI.loadNativeLibrary();
Expand All @@ -63,42 +55,33 @@ private CKZG4844() {
}

@Override
public synchronized void loadTrustedSetup(final String trustedSetupFilePath) throws KZGException {
try {
final TrustedSetup trustedSetup = CKZG4844Utils.parseTrustedSetupFile(trustedSetupFilePath);
loadTrustedSetup(trustedSetup);
} catch (final IOException ex) {
throw new KZGException("Failed to load trusted setup from file: " + trustedSetupFilePath, ex);
}
}

@Override
public void loadTrustedSetup(final TrustedSetup trustedSetup) throws KZGException {
if (loadedTrustedSetupHash.isPresent()
&& loadedTrustedSetupHash.get().equals(trustedSetup.hashCode())) {
LOG.trace("Trusted setup {} is already loaded.", trustedSetup);
public synchronized void loadTrustedSetup(final String trustedSetupFile) throws KZGException {
if (loadedTrustedSetupFile.isPresent()
&& loadedTrustedSetupFile.get().equals(trustedSetupFile)) {
LOG.trace("Trusted setup from file {} is already loaded.", trustedSetupFile);
return;
}
try {
final TrustedSetup trustedSetup = CKZG4844Utils.parseTrustedSetupFile(trustedSetupFile);
final List<Bytes> g1Points = trustedSetup.g1Points();
final List<Bytes> g2Points = trustedSetup.g2Points();
CKZG4844JNI.loadTrustedSetup(
CKZG4844Utils.flattenG1Points(g1Points),
g1Points.size(),
CKZG4844Utils.flattenG2Points(g2Points),
g2Points.size());
loadedTrustedSetupHash = Optional.of(trustedSetup.hashCode());
LOG.debug("Loaded trusted setup: {}", trustedSetup);
LOG.debug("Loaded trusted setup from file {}", trustedSetupFile);
loadedTrustedSetupFile = Optional.of(trustedSetupFile);
} catch (final Exception ex) {
throw new KZGException("Failed to load trusted setup: " + trustedSetup, ex);
throw new KZGException("Failed to load trusted setup from file " + trustedSetupFile, ex);
}
}

@Override
public synchronized void freeTrustedSetup() throws KZGException {
try {
CKZG4844JNI.freeTrustedSetup();
loadedTrustedSetupHash = Optional.empty();
loadedTrustedSetupFile = Optional.empty();
LOG.debug("Trusted setup was freed");
} catch (final Exception ex) {
throw new KZGException("Failed to free trusted setup", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ public static byte[] flattenG2Points(final List<Bytes> g2Points) {
return flattenBytes(g2Points, CKZG4844JNI.BYTES_PER_G2 * g2Points.size());
}

public static TrustedSetup parseTrustedSetupFile(final String filePath) throws IOException {
final String sanitizedTrustedSetup = UrlSanitizer.sanitizePotentialUrl(filePath);
public static TrustedSetup parseTrustedSetupFile(final String trustedSetupFile)
throws IOException {
final String sanitizedTrustedSetup = UrlSanitizer.sanitizePotentialUrl(trustedSetupFile);
final InputStream resource =
ResourceLoader.urlOrFile("application/octet-stream")
.load(filePath)
.load(trustedSetupFile)
.orElseThrow(() -> new FileNotFoundException(sanitizedTrustedSetup + " is not found"));
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(resource, StandardCharsets.UTF_8))) {
Expand All @@ -87,7 +88,8 @@ public static TrustedSetup parseTrustedSetupFile(final String filePath) throws I

return new TrustedSetup(g1Points, g2Points);
} catch (final Exception ex) {
throw new IOException(String.format("Failed to parse trusted setup file\n: %s", filePath));
throw new IOException(
String.format("Failed to parse trusted setup file\n: %s", trustedSetupFile));
}
}

Expand Down

0 comments on commit 26c1872

Please sign in to comment.