c-kzg-4844/bindings/node.js/test.ts

81 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-11-03 19:57:46 +00:00
import { randomBytes } from 'crypto';
import {
loadTrustedSetup,
freeTrustedSetup,
verifyKzgProof,
2022-11-03 00:17:17 +00:00
blobToKzgCommitment,
ReturnValue,
SetupHandle,
2022-11-03 00:17:17 +00:00
Blob,
BLOB_SIZE,
NUMBER_OF_FIELDS,
2022-11-03 19:57:46 +00:00
computeAggregateKzgProof,
} from './kzg';
2022-11-02 20:45:29 +00:00
2022-11-03 19:57:46 +00:00
const SETUP_FILE_PATH = '../../src/trusted_setup.txt';
const COMMITMENT_BYTE_LENGTH = 48;
2022-11-03 00:17:17 +00:00
function generateRandomBlob(): Blob {
return new Uint8Array(randomBytes(NUMBER_OF_FIELDS * BLOB_SIZE));
}
2022-11-02 20:45:29 +00:00
describe('C-KZG', () => {
2022-11-03 19:57:46 +00:00
let sharedSetupHandle: SetupHandle;
beforeAll(() => {
2022-11-03 19:57:46 +00:00
sharedSetupHandle = loadTrustedSetup(SETUP_FILE_PATH);
});
describe('setup', () => {
2022-11-02 20:45:29 +00:00
it('can both load and free', () => {
2022-11-03 19:57:46 +00:00
expect(
freeTrustedSetup(loadTrustedSetup(SETUP_FILE_PATH)),
).toBeUndefined();
});
});
describe('computing a KZG commitment from a blob', () => {
2022-11-03 19:57:46 +00:00
it('returns data with the correct length', () => {
2022-11-03 00:17:17 +00:00
const blob = generateRandomBlob();
2022-11-03 19:57:46 +00:00
const commitment = blobToKzgCommitment(blob, sharedSetupHandle);
expect(commitment.length).toBe(COMMITMENT_BYTE_LENGTH);
});
});
describe('verifying a KZG proof', () => {
2022-11-03 19:57:46 +00:00
it.only('returns the expected value', () => {
const byteEncoder = new TextEncoder();
2022-11-03 19:57:46 +00:00
const blob = generateRandomBlob();
const commitment = blobToKzgCommitment(blob, sharedSetupHandle);
const proof = computeAggregateKzgProof([blob], sharedSetupHandle);
const x = byteEncoder.encode(
'0345f802a75a6c0d9cc5b8a1e71642b8fa80b0a78938edc6da1e591149578d1a',
);
const y = byteEncoder.encode(
'3b17cab634c3795d311380f3bc93ce8e768efc0e2b9e79496cfc8f351594b472',
);
2022-11-03 19:57:46 +00:00
const result = verifyKzgProof(commitment, y, x, proof, sharedSetupHandle);
console.log({ result });
expect(result).toBe(ReturnValue.OK);
});
});
describe('computing an aggregate KZG proof', () => {
it('returns the expected value', () => {
2022-11-03 19:57:46 +00:00
const blob = generateRandomBlob();
const commitment = blobToKzgCommitment(blob, sharedSetupHandle);
const proof = computeAggregateKzgProof([blob], sharedSetupHandle);
});
});
describe('verifying an aggregate KZG proof', () => {
it('returns the expected value', () => {
expect(true).toBe(false);
2022-11-02 20:45:29 +00:00
});
});
});