-
-
Notifications
You must be signed in to change notification settings - Fork 436
EIP-4844: Add c-kzg library and current trusted setup #4851
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import path from "node:path"; | ||
| import fs from "node:fs"; | ||
| import {fileURLToPath} from "node:url"; | ||
| import {FIELD_ELEMENTS_PER_BLOB, loadTrustedSetup} from "c-kzg"; | ||
| import {fromHex, toHex} from "@lodestar/utils"; | ||
|
|
||
| // Global variable __dirname no longer available in ES6 modules. | ||
| // Solutions: https://stackoverflow.com/questions/46745014/alternative-for-dirname-in-node-js-when-using-es6-modules | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
| export const TRUSTED_SETUP_BIN_FILEPATH = path.join(__dirname, "../../trusted_setup.bin"); | ||
| const TRUSTED_SETUP_JSON_FILEPATH = path.join(__dirname, "../../trusted_setup.json"); | ||
| const TRUSTED_SETUP_TXT_FILEPATH = path.join(__dirname, "../../trusted_setup.txt"); | ||
|
|
||
| const POINT_COUNT_BYTES = 4; | ||
| const G1POINT_BYTES = 48; | ||
| const G2POINT_BYTES = 96; | ||
| const G1POINT_COUNT = FIELD_ELEMENTS_PER_BLOB; | ||
| const G2POINT_COUNT = 65; | ||
| const TOTAL_SIZE = 2 * POINT_COUNT_BYTES + G1POINT_BYTES * G1POINT_COUNT + G2POINT_BYTES * G2POINT_COUNT; | ||
|
|
||
| /** | ||
| * Load our KZG trusted setup into C-KZG for later use. | ||
| * We persist the trusted setup as serialized bytes to save space over TXT or JSON formats. | ||
| * However the current c-kzg API **requires** to read from a file with a specific .txt format | ||
| */ | ||
| export function loadEthereumTrustedSetup(): void { | ||
| try { | ||
| const bytes = fs.readFileSync(TRUSTED_SETUP_BIN_FILEPATH); | ||
| const json = trustedSetupBinToJson(bytes); | ||
| const txt = trustedSetupJsonToTxt(json); | ||
| fs.writeFileSync(TRUSTED_SETUP_TXT_FILEPATH, txt); | ||
|
|
||
| loadTrustedSetup(TRUSTED_SETUP_TXT_FILEPATH); | ||
| } catch (e) { | ||
| (e as Error).message = `Error loading trusted setup ${TRUSTED_SETUP_JSON_FILEPATH}: ${(e as Error).message}`; | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| /* eslint-disable @typescript-eslint/naming-convention */ | ||
| export interface TrustedSetupJSON { | ||
| setup_G1: string[]; | ||
| setup_G2: string[]; | ||
| } | ||
|
|
||
| type TrustedSetupBin = Uint8Array; | ||
| type TrustedSetupTXT = string; | ||
|
|
||
| /** | ||
| * Custom format defined in https://github.com/ethereum/c-kzg-4844/issues/3 | ||
| */ | ||
| export function trustedSetupJsonToBin(data: TrustedSetupJSON): TrustedSetupBin { | ||
| const out = new Uint8Array(TOTAL_SIZE); | ||
| const dv = new DataView(out.buffer, out.byteOffset, out.byteLength); | ||
|
|
||
| dv.setUint32(0, G1POINT_COUNT); | ||
| dv.setUint32(POINT_COUNT_BYTES, G2POINT_BYTES); | ||
|
|
||
| for (let i = 0; i < G1POINT_COUNT; i++) { | ||
| const point = fromHex(data.setup_G1[i]); | ||
| if (point.length !== G1POINT_BYTES) throw Error(`g1 point size ${point.length} != ${G1POINT_BYTES}`); | ||
| out.set(point, 2 * POINT_COUNT_BYTES + i * G1POINT_BYTES); | ||
| } | ||
|
|
||
| for (let i = 0; i < G2POINT_COUNT; i++) { | ||
| const point = fromHex(data.setup_G2[i]); | ||
| if (point.length !== G2POINT_BYTES) throw Error(`g2 point size ${point.length} != ${G2POINT_BYTES}`); | ||
| out.set(point, 2 * POINT_COUNT_BYTES + G1POINT_COUNT * G1POINT_BYTES + i * G2POINT_BYTES); | ||
| } | ||
|
|
||
| return out; | ||
| } | ||
|
|
||
| export function trustedSetupBinToJson(bytes: TrustedSetupBin): TrustedSetupJSON { | ||
| const data: TrustedSetupJSON = { | ||
| setup_G1: [], | ||
| setup_G2: [], | ||
| }; | ||
|
|
||
| if (bytes.length < TOTAL_SIZE) { | ||
| throw Error(`trusted_setup size ${bytes.length} < ${TOTAL_SIZE}`); | ||
| } | ||
|
|
||
| for (let i = 0; i < G1POINT_COUNT; i++) { | ||
| const start = 2 * POINT_COUNT_BYTES + i * G1POINT_BYTES; | ||
| data.setup_G1.push(toHex(bytes.slice(start, start + G1POINT_BYTES))); | ||
| } | ||
|
|
||
| for (let i = 0; i < G2POINT_COUNT; i++) { | ||
| const start = 2 * POINT_COUNT_BYTES + G1POINT_COUNT * G1POINT_BYTES + i * G2POINT_BYTES; | ||
| data.setup_G1.push(toHex(bytes.slice(start, start + G2POINT_BYTES))); | ||
| } | ||
|
|
||
| return data; | ||
| } | ||
|
|
||
| export function trustedSetupJsonToTxt(data: TrustedSetupJSON): TrustedSetupTXT { | ||
| let out = `${FIELD_ELEMENTS_PER_BLOB}\n65\n`; | ||
|
|
||
| for (const p of data.setup_G1) out += strip0xPrefix(p) + "\n"; | ||
| for (const p of data.setup_G2) out += strip0xPrefix(p) + "\n"; | ||
|
|
||
| return out; | ||
| } | ||
|
|
||
| function strip0xPrefix(hex: string): string { | ||
| if (hex.startsWith("0x")) { | ||
| return hex.slice(2); | ||
| } else { | ||
| return hex; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import crypto from "node:crypto"; | ||
| import {expect} from "chai"; | ||
| import { | ||
| freeTrustedSetup, | ||
| blobToKzgCommitment, | ||
| computeAggregateKzgProof, | ||
| verifyAggregateKzgProof, | ||
| BYTES_PER_FIELD_ELEMENT, | ||
| FIELD_ELEMENTS_PER_BLOB, | ||
| } from "c-kzg"; | ||
| import {eip4844} from "@lodestar/types"; | ||
| import {loadEthereumTrustedSetup} from "../../../src/util/kzg.js"; | ||
|
|
||
| const BLOB_BYTE_COUNT = FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD_ELEMENT; | ||
|
|
||
| describe("C-KZG", () => { | ||
| before(async function () { | ||
| this.timeout(10000); // Loading trusted setup is slow | ||
| loadEthereumTrustedSetup(); | ||
| }); | ||
|
|
||
| after(() => { | ||
| freeTrustedSetup(); | ||
| }); | ||
|
|
||
| it("computes the correct commitments and aggregate proofs from blobs", () => { | ||
| // ==================== | ||
| // Apply this example to the test data | ||
| // ==================== | ||
| const blobs = new Array(2).fill(0).map(generateRandomBlob); | ||
| const commitments = blobs.map(blobToKzgCommitment); | ||
| const proof = computeAggregateKzgProof(blobs); | ||
| expect(verifyAggregateKzgProof(blobs, commitments, proof)).to.equal(true); | ||
| }); | ||
| }); | ||
|
|
||
| /** | ||
| * Generate random blob of sequential integers such that each element is < BLS_MODULUS | ||
| */ | ||
| function generateRandomBlob(): eip4844.Blob { | ||
| return new Uint8Array(crypto.randomBytes(BLOB_BYTE_COUNT)); | ||
| } |
16 changes: 16 additions & 0 deletions
16
packages/beacon-node/test/utils/cliTools/kzgTrustedSetupFromJson.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import fs from "node:fs"; | ||
| import {TrustedSetupJSON, trustedSetupJsonToBin, TRUSTED_SETUP_BIN_FILEPATH} from "../../../src/util/kzg.js"; | ||
|
|
||
| // CLI TOOL: Use to transform a JSON trusted setup into .ssz | ||
| // | ||
| // Note: Closer to EIP-4844 this tool may never be useful again, | ||
| // see https://github.com/ethereum/c-kzg-4844/issues/3 | ||
|
|
||
| const INPUT_FILE = process.argv[2]; | ||
| if (!INPUT_FILE) throw Error("no INPUT_FILE"); | ||
|
|
||
| const json = JSON.parse(fs.readFileSync(INPUT_FILE, "utf8")) as TrustedSetupJSON; | ||
|
|
||
| const bytes = trustedSetupJsonToBin(json); | ||
|
|
||
| fs.writeFileSync(TRUSTED_SETUP_BIN_FILEPATH, bytes); |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would this be network dependent or same setup will work for all networks (and then the question of chains like gnosis etc)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still unknown, probably it will be shared since it's only dependant on the size of blobs. I would bet big on being the same file for all eth2 networks