Add ability to parse JSON trusted setup to text format expected by C-KZG

This commit is contained in:
dancoffman 2022-11-16 18:31:30 -08:00
parent ebd4051a7d
commit 242ba0adf9
No known key found for this signature in database
GPG Key ID: 47B1F53E36A9B3CC
8 changed files with 44 additions and 7 deletions

View File

@ -2,3 +2,4 @@ build
dist dist
*.node *.node
node_modules node_modules
testing_trusted_setups.txt

View File

@ -18,3 +18,4 @@ babel.config.js
Dockerfile Dockerfile
binding.dist.gyp binding.dist.gyp
Makefile Makefile
testing_trusted_setups.txt

View File

@ -4,7 +4,7 @@ FROM node:16
COPY ./dist/ /app/dist/ COPY ./dist/ /app/dist/
COPY test.ts /app COPY test.ts /app
COPY trusted_setup.txt /app COPY testing_trusted_setups.json /app
COPY kzg.ts /app COPY kzg.ts /app
COPY kzg.cxx /app COPY kzg.cxx /app
COPY package.json /app COPY package.json /app

View File

@ -32,7 +32,5 @@ publish: bundle
linux-test: bundle linux-test: bundle
cp ../../src/trusted_setup.txt .
docker build -t "linux-test" . docker build -t "linux-test" .
rm trusted_setup.txt
docker logs --follow `docker run -d linux-test` docker logs --follow `docker run -d linux-test`

View File

@ -86,7 +86,7 @@ Napi::Value LoadTrustedSetup(const Napi::CallbackInfo& info) {
if (f == NULL) { if (f == NULL) {
free(kzg_settings); free(kzg_settings);
Napi::Error::New(env, "Error opening trusted setup file").ThrowAsJavaScriptException(); Napi::Error::New(env, "Error opening trusted setup file: " + file_path).ThrowAsJavaScriptException();
return env.Null(); return env.Null();
} }

View File

@ -3,6 +3,7 @@
* https://github.com/ethereum/consensus-specs/blob/dev/specs/eip4844/polynomial-commitments.md#kzg * https://github.com/ethereum/consensus-specs/blob/dev/specs/eip4844/polynomial-commitments.md#kzg
*/ */
const kzg: KZG = require("./kzg.node"); const kzg: KZG = require("./kzg.node");
const fs = require("fs");
export type BLSFieldElement = Uint8Array; // 32 bytes export type BLSFieldElement = Uint8Array; // 32 bytes
export type KZGProof = Uint8Array; // 48 bytes export type KZGProof = Uint8Array; // 48 bytes
@ -44,6 +45,13 @@ type KZG = {
) => boolean; ) => boolean;
}; };
type TrustedSetupJSON = {
setup_G1: string[];
setup_G2: string[];
setup_G1_lagrange: string[];
roots_of_unity: string[];
};
export const FIELD_ELEMENTS_PER_BLOB = kzg.FIELD_ELEMENTS_PER_BLOB; export const FIELD_ELEMENTS_PER_BLOB = kzg.FIELD_ELEMENTS_PER_BLOB;
export const BYTES_PER_FIELD_ELEMENT = kzg.BYTES_PER_FIELD_ELEMENT; export const BYTES_PER_FIELD_ELEMENT = kzg.BYTES_PER_FIELD_ELEMENT;
@ -57,12 +65,38 @@ function requireSetupHandle(): SetupHandle {
return setupHandle; return setupHandle;
} }
export async function transformTrustedSetupJSON(
filePath: string,
): Promise<string> {
const data: TrustedSetupJSON = JSON.parse(fs.readFileSync(filePath));
const textFilePath = filePath.replace(".json", "") + ".txt";
try {
fs.unlinkSync(textFilePath);
} catch {}
const file = fs.createWriteStream(textFilePath);
file.write(`${FIELD_ELEMENTS_PER_BLOB}\n65\n`);
file.write(data.setup_G1.map((p) => p.replace("0x", "")).join("\n"));
file.write(data.setup_G2.map((p) => p.replace("0x", "")).join("\n"));
file.end();
const p = new Promise((resolve) => {
file.close(resolve);
});
await p;
return textFilePath;
}
export function loadTrustedSetup(filePath: string): void { export function loadTrustedSetup(filePath: string): void {
if (setupHandle) { if (setupHandle) {
throw new Error( throw new Error(
"Call freeTrustedSetup before loading a new trusted setup.", "Call freeTrustedSetup before loading a new trusted setup.",
); );
} }
setupHandle = kzg.loadTrustedSetup(filePath); setupHandle = kzg.loadTrustedSetup(filePath);
} }

View File

@ -9,9 +9,10 @@ import {
verifyAggregateKzgProof, verifyAggregateKzgProof,
BYTES_PER_FIELD_ELEMENT, BYTES_PER_FIELD_ELEMENT,
FIELD_ELEMENTS_PER_BLOB, FIELD_ELEMENTS_PER_BLOB,
transformTrustedSetupJSON,
} from "./kzg"; } from "./kzg";
const setupFileName = "trusted_setup.txt"; const setupFileName = "testing_trusted_setups.json";
const SETUP_FILE_PATH = existsSync(setupFileName) const SETUP_FILE_PATH = existsSync(setupFileName)
? setupFileName ? setupFileName
@ -22,8 +23,9 @@ const BLOB_BYTE_COUNT = FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD_ELEMENT;
const generateRandomBlob = () => new Uint8Array(randomBytes(BLOB_BYTE_COUNT)); const generateRandomBlob = () => new Uint8Array(randomBytes(BLOB_BYTE_COUNT));
describe("C-KZG", () => { describe("C-KZG", () => {
beforeAll(() => { beforeAll(async () => {
loadTrustedSetup(SETUP_FILE_PATH); const file = await transformTrustedSetupJSON(SETUP_FILE_PATH);
loadTrustedSetup(file);
}); });
afterAll(() => { afterAll(() => {

File diff suppressed because one or more lines are too long