diff --git a/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/kzg/KzgBlobToCommitmentTestExecutor.java b/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/kzg/KzgBlobToCommitmentTestExecutor.java index c1df15cffe7..44a425ac57b 100644 --- a/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/kzg/KzgBlobToCommitmentTestExecutor.java +++ b/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/kzg/KzgBlobToCommitmentTestExecutor.java @@ -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 { @@ -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); diff --git a/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/kzg/KzgComputeBlobProofTestExecutor.java b/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/kzg/KzgComputeBlobProofTestExecutor.java index 7e55327339d..609fd6fc0c5 100644 --- a/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/kzg/KzgComputeBlobProofTestExecutor.java +++ b/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/kzg/KzgComputeBlobProofTestExecutor.java @@ -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 { @@ -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); diff --git a/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/kzg/KzgTestExecutor.java b/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/kzg/KzgTestExecutor.java index 2f8fd433038..f89cb6c8a78 100644 --- a/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/kzg/KzgTestExecutor.java +++ b/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/kzg/KzgTestExecutor.java @@ -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; @@ -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 loadDataFile(final TestDefinition testDefinition, final Class type) diff --git a/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/kzg/KzgVerifyBlobProofBatchTestExecutor.java b/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/kzg/KzgVerifyBlobProofBatchTestExecutor.java index 22a270d0de3..d8fa474660a 100644 --- a/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/kzg/KzgVerifyBlobProofBatchTestExecutor.java +++ b/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/kzg/KzgVerifyBlobProofBatchTestExecutor.java @@ -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 { @@ -34,9 +33,8 @@ public void runTestImpl(final TestDefinition testDefinition) throws Throwable { final List blobs = data.getInput().getBlobs(); final List commitments = data.getInput().getCommitments(); final List 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); diff --git a/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/kzg/KzgVerifyBlobProofTestExecutor.java b/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/kzg/KzgVerifyBlobProofTestExecutor.java index 396b3865196..d6753b64a22 100644 --- a/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/kzg/KzgVerifyBlobProofTestExecutor.java +++ b/eth-reference-tests/src/referenceTest/java/tech/pegasys/teku/reference/phase0/kzg/KzgVerifyBlobProofTestExecutor.java @@ -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 @@ -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); diff --git a/ethereum/spec/src/main/resources/tech/pegasys/teku/spec/config/presets/minimal/deneb.yaml b/ethereum/spec/src/main/resources/tech/pegasys/teku/spec/config/presets/minimal/deneb.yaml index fe39f7f6a54..398c30e084e 100644 --- a/ethereum/spec/src/main/resources/tech/pegasys/teku/spec/config/presets/minimal/deneb.yaml +++ b/ethereum/spec/src/main/resources/tech/pegasys/teku/spec/config/presets/minimal/deneb.yaml @@ -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)` diff --git a/ethereum/spec/src/main/resources/tech/pegasys/teku/spec/config/presets/swift/deneb.yaml b/ethereum/spec/src/main/resources/tech/pegasys/teku/spec/config/presets/swift/deneb.yaml index aecefaa4640..398c30e084e 100644 --- a/ethereum/spec/src/main/resources/tech/pegasys/teku/spec/config/presets/swift/deneb.yaml +++ b/ethereum/spec/src/main/resources/tech/pegasys/teku/spec/config/presets/swift/deneb.yaml @@ -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 \ No newline at end of file +MAX_BLOB_COMMITMENTS_PER_BLOCK: 16 +# `uint64(6)` +MAX_BLOBS_PER_BLOCK: 6 \ No newline at end of file diff --git a/ethereum/spec/src/test/resources/tech/pegasys/teku/spec/config/standard/presets/mainnet/deneb.yaml b/ethereum/spec/src/test/resources/tech/pegasys/teku/spec/config/standard/presets/mainnet/deneb.yaml index 0ed6b0c9e5a..0714c4967e0 100644 --- a/ethereum/spec/src/test/resources/tech/pegasys/teku/spec/config/standard/presets/mainnet/deneb.yaml +++ b/ethereum/spec/src/test/resources/tech/pegasys/teku/spec/config/standard/presets/mainnet/deneb.yaml @@ -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 \ No newline at end of file diff --git a/ethereum/spec/src/test/resources/tech/pegasys/teku/spec/config/standard/presets/minimal/deneb.yaml b/ethereum/spec/src/test/resources/tech/pegasys/teku/spec/config/standard/presets/minimal/deneb.yaml index e49d5ee8d98..398c30e084e 100644 --- a/ethereum/spec/src/test/resources/tech/pegasys/teku/spec/config/standard/presets/minimal/deneb.yaml +++ b/ethereum/spec/src/test/resources/tech/pegasys/teku/spec/config/standard/presets/minimal/deneb.yaml @@ -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 \ No newline at end of file diff --git a/ethereum/spec/src/test/resources/tech/pegasys/teku/spec/config/standard/presets/swift/deneb.yaml b/ethereum/spec/src/test/resources/tech/pegasys/teku/spec/config/standard/presets/swift/deneb.yaml index e49d5ee8d98..398c30e084e 100644 --- a/ethereum/spec/src/test/resources/tech/pegasys/teku/spec/config/standard/presets/swift/deneb.yaml +++ b/ethereum/spec/src/test/resources/tech/pegasys/teku/spec/config/standard/presets/swift/deneb.yaml @@ -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 \ No newline at end of file diff --git a/infrastructure/kzg/src/main/java/tech/pegasys/teku/kzg/KZG.java b/infrastructure/kzg/src/main/java/tech/pegasys/teku/kzg/KZG.java index 6223d24414a..a610405622d 100644 --- a/infrastructure/kzg/src/main/java/tech/pegasys/teku/kzg/KZG.java +++ b/infrastructure/kzg/src/main/java/tech/pegasys/teku/kzg/KZG.java @@ -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 {} @@ -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; diff --git a/infrastructure/kzg/src/main/java/tech/pegasys/teku/kzg/ckzg4844/CKZG4844.java b/infrastructure/kzg/src/main/java/tech/pegasys/teku/kzg/ckzg4844/CKZG4844.java index 79be0825216..1d961011a9d 100644 --- a/infrastructure/kzg/src/main/java/tech/pegasys/teku/kzg/ckzg4844/CKZG4844.java +++ b/infrastructure/kzg/src/main/java/tech/pegasys/teku/kzg/ckzg4844/CKZG4844.java @@ -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; @@ -37,7 +36,7 @@ public final class CKZG4844 implements KZG { private static CKZG4844 instance; - private Optional loadedTrustedSetupHash = Optional.empty(); + private Optional loadedTrustedSetupFile = Optional.empty(); public static synchronized CKZG4844 createInstance() { if (instance == null) { @@ -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(); @@ -63,23 +55,14 @@ 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 g1Points = trustedSetup.g1Points(); final List g2Points = trustedSetup.g2Points(); CKZG4844JNI.loadTrustedSetup( @@ -87,10 +70,10 @@ public void loadTrustedSetup(final TrustedSetup trustedSetup) throws KZGExceptio 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); } } @@ -98,7 +81,7 @@ public void loadTrustedSetup(final TrustedSetup trustedSetup) throws KZGExceptio 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); diff --git a/infrastructure/kzg/src/main/java/tech/pegasys/teku/kzg/ckzg4844/CKZG4844Utils.java b/infrastructure/kzg/src/main/java/tech/pegasys/teku/kzg/ckzg4844/CKZG4844Utils.java index 5c9e808e796..e0ba811281e 100644 --- a/infrastructure/kzg/src/main/java/tech/pegasys/teku/kzg/ckzg4844/CKZG4844Utils.java +++ b/infrastructure/kzg/src/main/java/tech/pegasys/teku/kzg/ckzg4844/CKZG4844Utils.java @@ -59,11 +59,12 @@ public static byte[] flattenG2Points(final List 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))) { @@ -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)); } }