diff --git a/package-lock.json b/package-lock.json index 20c6a8c..b2be947 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "kzg-wasm", - "version": "0.3.1", + "version": "0.4.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "kzg-wasm", - "version": "0.3.1", + "version": "0.4.0", "license": "MIT", "devDependencies": { "@babel/cli": "^7.23.9", diff --git a/src/index.ts b/src/index.ts index 0777fe1..3166c4e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,10 +19,10 @@ export const loadKZG = async (trustedSetup: TrustedSetup = mainnetTrustedSetup) const loadTrustedSetupWasm = module.cwrap('load_trusted_setup_from_wasm', 'number', ['string', 'number','string', 'number']) as (g1: string, n1: number, g2: string, n2: number) => number const freeTrustedSetup = module.cwrap('free_trusted_setup_wasm', null, []) as () => void - const blobToKzgCommitmentWasm = module.cwrap('blob_to_kzg_commitment_wasm', 'string', ['array']) as (blob: Uint8Array) => string - const computeBlobKzgProofWasm = module.cwrap('compute_blob_kzg_proof_wasm', 'string', ['array', 'array']) as (blob: Uint8Array, commitment: Uint8Array) => string - const verifyBlobKzgProofWasm = module.cwrap('verify_blob_kzg_proof_wasm', 'string', ['array', 'array', 'array']) as (blob: Uint8Array, commitment: Uint8Array, proof: Uint8Array) => string - const verifyKzgProofWasm = module.cwrap('verify_kzg_proof_wasm', 'string', ['array', 'array', 'array', 'array']) + const blobToKZGCommitmentWasm = module.cwrap('blob_to_kzg_commitment_wasm', 'string', ['array']) as (blob: Uint8Array) => string + const computeBlobKZGProofWasm = module.cwrap('compute_blob_kzg_proof_wasm', 'string', ['array', 'array']) as (blob: Uint8Array, commitment: Uint8Array) => string + const verifyBlobKZGProofWasm = module.cwrap('verify_blob_kzg_proof_wasm', 'string', ['array', 'array', 'array']) as (blob: Uint8Array, commitment: Uint8Array, proof: Uint8Array) => string + const verifyKZGProofWasm = module.cwrap('verify_kzg_proof_wasm', 'string', ['array', 'array', 'array', 'array']) as (commitment: Uint8Array, z: Uint8Array, y: Uint8Array, proof: Uint8Array) => string /** * @@ -38,20 +38,20 @@ export const loadKZG = async (trustedSetup: TrustedSetup = mainnetTrustedSetup) * @param blob - a blob of data formatted as a flattened Uint8Array of 4096 big endian KZG field elements * @returns a KZG commitment corresponding to the input blob formatted as a 48 byte Uint8Array */ - const blobToKzgCommitment = (blob: Uint8Array) => { - const blobHex = '0x' + blobToKzgCommitmentWasm(blob) - return hexToBytes(blobHex) + const blobToKZGCommitment = (blob: string) => { + const blobHex = '0x' + blobToKZGCommitmentWasm(hexToBytes(blob)) + return blobHex } /** * * @param blob - a blob of data formatted as a flattened Uint8Array of 4096 big endian KZG field elements * @param commitment - a KZG commitment corresponding to a blob formatted as a 48 byte Uint8Array - * @returns a 48 byte KZG proof corresponding to the blob and KZG commitment + * @returns a 48 byte KZG proof as prefixed hex string corresponding to the blob and KZG commitment */ - const computeBlobKzgProof = (blob: Uint8Array, commitment: Uint8Array) => { - const proofHex = '0x' + computeBlobKzgProofWasm(blob, commitment) - return hexToBytes(proofHex) + const computeBlobKZGProof = (blob: string, commitment: string) => { + const proofHex = '0x' + computeBlobKZGProofWasm(hexToBytes(blob), hexToBytes(commitment)) + return proofHex } /** @@ -59,14 +59,14 @@ export const loadKZG = async (trustedSetup: TrustedSetup = mainnetTrustedSetup) * @param blobs - an array of blobs * @param commitments - an array of corresponding commitments * @param proofs - an array of corresponding KZG proofs - * @returns returns true if all proofs are verified against their blobs and commitments; false otherise + * @returns returns true if all proofs are verified against their blobs and commitments; false otherwise */ - const verifyBlobKzgProofBatch = (blobs: Uint8Array[], commitments: Uint8Array[], proofs: Uint8Array[]) => { + const verifyBlobKZGProofBatch = (blobs: string[], commitments: string[], proofs: string[]) => { if (blobs.length !== commitments.length && commitments.length !== proofs.length) { throw new Error('number of blobs, commitments, and proofs, must match') } for (let x = 0; x < blobs.length; x++) { - const res = verifyBlobKzgProofWasm(blobs[x], commitments[x], proofs[x]) + const res = verifyBlobKZGProofWasm(hexToBytes(blobs[x]), hexToBytes(commitments[x]), hexToBytes(proofs[x])) if (res !== 'true') return false } return true @@ -79,8 +79,8 @@ export const loadKZG = async (trustedSetup: TrustedSetup = mainnetTrustedSetup) * @param proof - a 48 byte KZG proof corresponding to the blob and commitment * @returns true if proof is verified; false otherwise */ - const verifyBlobKzgProof = (blob: Uint8Array, commitment: Uint8Array, proof: Uint8Array) => { - const res = verifyBlobKzgProofWasm(blob, commitment, proof) + const verifyBlobKZGProof = (blob: string, commitment: string, proof: string) => { + const res = verifyBlobKZGProofWasm(hexToBytes(blob), hexToBytes(commitment), hexToBytes(proof)) return res === 'true' } /** @@ -91,15 +91,15 @@ export const loadKZG = async (trustedSetup: TrustedSetup = mainnetTrustedSetup) * @param proof * @returns true if proof is verified; false otherwise */ - const verifyKzgProof = (commitment: Uint8Array, z: Uint8Array, y: Uint8Array, proof: Uint8Array) => { - const res = verifyKzgProofWasm(commitment, z, y, proof) + const verifyKZGProof = (commitment: string, z: string, y: string, proof: string) => { + const res = verifyKZGProofWasm(hexToBytes(commitment), hexToBytes(z), hexToBytes(y), hexToBytes(proof)) return res === 'true' } loadTrustedSetup(trustedSetup) return { - loadTrustedSetup, freeTrustedSetup, blobToKzgCommitment, computeBlobKzgProof, verifyBlobKzgProofBatch, verifyKzgProof, verifyBlobKzgProof + loadTrustedSetup, freeTrustedSetup, blobToKZGCommitment, computeBlobKZGProof, verifyBlobKZGProofBatch, verifyKZGProof, verifyBlobKZGProof } } diff --git a/test/index.spec.ts b/test/index.spec.ts index 33d58d7..dc34427 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -13,17 +13,17 @@ describe('kzg initialization', () => { }) it('should initialize', async () => { - assert.typeOf(kzg.computeBlobKzgProof, 'function' , 'initialized KZG object') + assert.typeOf(kzg.computeBlobKZGProof, 'function', 'initialized KZG object') kzg.freeTrustedSetup() }) it('should return nonzero when invalid trusted setup is provided', () => { - const res = kzg.loadTrustedSetup({ g1: 'x12', n1: -1, g2: 'bad coordinates', n2: 0}) - assert.notOk(res === 0) + const res = kzg.loadTrustedSetup({ g1: 'x12', n1: -1, g2: 'bad coordinates', n2: 0 }) + assert.notOk(res === 0) }) }) describe('kzg API tests', () => { - let kzg + let kzg: Awaited> beforeAll(async () => { kzg = await loadKZG() }) @@ -32,31 +32,31 @@ describe('kzg API tests', () => { const blob = new Uint8Array(BYTES_PER_BLOB) blob[0] = 0x01 blob[1] = 0x02 - const commitment = kzg.blobToKzgCommitment(blob) - assert.equal(bytesToHex(commitment).slice(2), 'ab87358a111c3cd9da8aadf4b414e9f6be5ac83d923fb70d8d27fef1e2690b4cad015b23b8c058881da78a05c62b1173') - const proof = kzg.computeBlobKzgProof(blob, commitment) - assert.equal(bytesToHex(proof).slice(2), '8dd951edb4e0df1779c29d28b835a2cc8b26ebf69a38d7d9afadd0eb8a4cbffd9db1025fd253e91e00a9904f109e81e3') - const proofVerified = kzg.verifyBlobKzgProofBatch([blob], [commitment], [proof]) + const commitment = kzg.blobToKZGCommitment(bytesToHex(blob)) + assert.equal(commitment.slice(2).toLowerCase(), 'ab87358a111c3cd9da8aadf4b414e9f6be5ac83d923fb70d8d27fef1e2690b4cad015b23b8c058881da78a05c62b1173') + const proof = kzg.computeBlobKZGProof(bytesToHex(blob), (commitment)) + assert.equal(proof.toLowerCase(), '0x8dd951edb4e0df1779c29d28b835a2cc8b26ebf69a38d7d9afadd0eb8a4cbffd9db1025fd253e91e00a9904f109e81e3') + const proofVerified = kzg.verifyBlobKZGProofBatch([bytesToHex(blob)], [(commitment)], [proof]) assert.equal(proofVerified, true) }) it('should verify kzg proofs with points', async () => { const precompileData = { - Proof: hexToBytes( + Proof: ( '0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' ), - Commitment: hexToBytes( + Commitment: ( '0xc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' ), - z: hexToBytes( + z: ( '0x623ce31cf9759a5c8daf3a357992f9f3dd7f9339d8998bc8e68373e54f00b75e' ), - y: hexToBytes( + y: ( '0x0000000000000000000000000000000000000000000000000000000000000000' ), } - const verifiedKzgProof = kzg.verifyKzgProof(precompileData.Commitment, precompileData.z, precompileData.y, precompileData.Proof) - assert.equal(verifiedKzgProof, true) + const verifiedKZGProof = kzg.verifyKZGProof(precompileData.Commitment, precompileData.z, precompileData.y, precompileData.Proof) + assert.equal(verifiedKZGProof, true) }) }) \ No newline at end of file