Skip to content
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

Use prefixed hex strings in outer API input/outputs #17

Merged
merged 7 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
*
Expand All @@ -38,35 +38,35 @@ 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
}

/**
*
* @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
Expand All @@ -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'
}
/**
Expand All @@ -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
}
}

28 changes: 14 additions & 14 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReturnType<typeof loadKZG>>
beforeAll(async () => {
kzg = await loadKZG()
})
Expand All @@ -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)
const verifiedKzgProof = kzg.verifyKZGProof(precompileData.Commitment, precompileData.z, precompileData.y, precompileData.Proof)
assert.equal(verifiedKzgProof, true)
})
})