From 07adb046f23969b86dd99ec63e8066189025ad2a Mon Sep 17 00:00:00 2001 From: Suyash Bagad Date: Wed, 29 Mar 2023 18:54:34 +0100 Subject: [PATCH] remove ts (consulted with Adam and we're good to go). (#292) --- ts/src/crypto/index.ts | 1 - ts/src/crypto/pedersen/index.ts | 1 - ts/src/crypto/pedersen/pedersen.test.ts | 33 --------- ts/src/crypto/pedersen/pedersen.ts | 93 ------------------------- ts/src/wasm/barretenberg.wasm | 1 - ts/src/wasm/barretenberg_wasm.test.ts | 46 ------------ ts/src/wasm/index.ts | 1 - ts/src/wasm/serialize.ts | 70 ------------------- 8 files changed, 246 deletions(-) delete mode 100644 ts/src/crypto/index.ts delete mode 100644 ts/src/crypto/pedersen/index.ts delete mode 100644 ts/src/crypto/pedersen/pedersen.test.ts delete mode 100644 ts/src/crypto/pedersen/pedersen.ts delete mode 120000 ts/src/wasm/barretenberg.wasm delete mode 100644 ts/src/wasm/barretenberg_wasm.test.ts delete mode 100644 ts/src/wasm/index.ts delete mode 100644 ts/src/wasm/serialize.ts diff --git a/ts/src/crypto/index.ts b/ts/src/crypto/index.ts deleted file mode 100644 index bd41f781c3..0000000000 --- a/ts/src/crypto/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './pedersen/index.js'; diff --git a/ts/src/crypto/pedersen/index.ts b/ts/src/crypto/pedersen/index.ts deleted file mode 100644 index 41d557571b..0000000000 --- a/ts/src/crypto/pedersen/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './pedersen.js'; diff --git a/ts/src/crypto/pedersen/pedersen.test.ts b/ts/src/crypto/pedersen/pedersen.test.ts deleted file mode 100644 index 9ae59815c3..0000000000 --- a/ts/src/crypto/pedersen/pedersen.test.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { BarretenbergWasm } from '../../wasm/barretenberg_wasm.js'; -import { pedersenGetHashTree } from './pedersen.js'; - -describe('pedersen', () => { - let barretenbergWasm!: BarretenbergWasm; - const values: Buffer[] = []; - - beforeAll(async () => { - barretenbergWasm = await BarretenbergWasm.new(); - - // TODO was originally 2 ** 12 - for (let i = 0; i < 2 ** 2; ++i) { - const v = Buffer.alloc(32, 0); - v.writeUInt32LE(i, 0); - values[i] = v; - } - }); - - it('hasher_consistency_and_benchmark', () => { - const start1 = new Date().getTime(); - const result = pedersenGetHashTree(barretenbergWasm, values); - const end1 = new Date().getTime() - start1; - - console.log(`Single hasher: ~${end1 / values.length}ms / value`); - // TODO more than smoke-test this - expect(result.length).toBe(values.length * 2 - 1); - // TODO - // console.log(`Pooled hasher: ~${end2 / values.length}ms / value`); - // console.log(`Pooled improvement: ${(end1 / end2).toFixed(2)}x`); - // expect(poolResults).toEqual(singleResults); - // await pool.destroy(); - }); -}); diff --git a/ts/src/crypto/pedersen/pedersen.ts b/ts/src/crypto/pedersen/pedersen.ts deleted file mode 100644 index 5ab0533a77..0000000000 --- a/ts/src/crypto/pedersen/pedersen.ts +++ /dev/null @@ -1,93 +0,0 @@ -import { BarretenbergWasm } from '../../wasm/index.js'; -import { deserializeArrayFromVector, deserializeField, serializeBufferArrayToVector } from '../../wasm/serialize.js'; -import { Buffer } from 'buffer'; - -/** - * Combines two 32-byte hashes. - * @param wasm - The barretenberg module. - * @param lhs - The first hash. - * @param rhs - The second hash. - * @returns The new 32-byte hash. - */ -export function pedersenCompress(wasm: BarretenbergWasm, lhs: Uint8Array, rhs: Uint8Array): Buffer { - // If not done already, precompute constants. - wasm.call('pedersen__init'); - // TODO check if lhs and rhs are <= 32 bytes? - wasm.writeMemory(0, lhs); - wasm.writeMemory(32, rhs); - wasm.call('pedersen__compress_fields', 0, 32, 64); - return Buffer.from(wasm.getMemorySlice(64, 96)); -} - -/** - * Combine an array of hashes. - * @param wasm - The barretenberg module. - * @param lhs - The first hash. - * @param rhs - The second hash. - * @returns The new 32-byte hash. - */ -export function pedersenCompressInputs(wasm: BarretenbergWasm, inputs: Buffer[]): Buffer { - // If not done already, precompute constants. - wasm.call('pedersen__init'); - const inputVectors = serializeBufferArrayToVector(inputs); - wasm.writeMemory(0, inputVectors); - wasm.call('pedersen__compress', 0, 0); - return Buffer.from(wasm.getMemorySlice(0, 32)); -} - -/** - * Combine an array of hashes. - * @param wasm - The barretenberg module. - * @param lhs - The first hash. - * @param rhs - The second hash. - * @returns The new 32-byte hash. - */ -export function pedersenCompressWithHashIndex(wasm: BarretenbergWasm, inputs: Buffer[], hashIndex: number): Buffer { - // If not done already, precompute constants. - wasm.call('pedersen__init'); - const inputVectors = serializeBufferArrayToVector(inputs); - wasm.writeMemory(0, inputVectors); - wasm.call('pedersen__compress_with_hash_index', 0, 0, hashIndex); - return Buffer.from(wasm.getMemorySlice(0, 32)); -} - -/** - * Get a 32-byte pedersen hash from a buffer. - * @param wasm - The barretenberg module. - * @param data - The data buffer. - * @returns The hash buffer. - */ -export function pedersenGetHash(wasm: BarretenbergWasm, data: Buffer): Buffer { - // If not done already, precompute constants. - wasm.call('pedersen__init'); - const mem = wasm.call('bbmalloc', data.length); - wasm.writeMemory(mem, data); - wasm.call('pedersen__buffer_to_field', mem, data.length, 0); - wasm.call('bbfree', mem); - return Buffer.from(wasm.getMemorySlice(0, 32)); -} - -/** - * Given a buffer containing 32 byte pedersen leaves, return a new buffer containing the leaves and all pairs of nodes that define a merkle tree. - * E.g. - * Input: [1][2][3][4] - * Output: [1][2][3][4][compress(1,2)][compress(3,4)][compress(5,6)]. - * @param wasm - The barretenberg module. - * @param values - The 32 byte pedersen leaves. - * @returns A tree represented by an array. - */ -export function pedersenGetHashTree(wasm: BarretenbergWasm, values: Buffer[]) { - // If not done already, precompute constants. - wasm.call('pedersen__init'); - const data = serializeBufferArrayToVector(values); - const inputPtr = wasm.call('bbmalloc', data.length); - wasm.writeMemory(inputPtr, data); - - const resultPtr = wasm.call('pedersen__hash_to_tree', inputPtr); - const resultNumFields = Buffer.from(wasm.getMemorySlice(resultPtr, resultPtr + 4)).readUInt32BE(0); - const resultData = Buffer.from(wasm.getMemorySlice(resultPtr, resultPtr + 4 + resultNumFields * 32)); - wasm.call('bbfree', inputPtr); - wasm.call('bbfree', resultPtr); - - return deserializeArrayFromVector(deserializeField, resultData).elem; -} diff --git a/ts/src/wasm/barretenberg.wasm b/ts/src/wasm/barretenberg.wasm deleted file mode 120000 index f6e5fe2c73..0000000000 --- a/ts/src/wasm/barretenberg.wasm +++ /dev/null @@ -1 +0,0 @@ -../../../cpp/build-wasm/bin/barretenberg.wasm \ No newline at end of file diff --git a/ts/src/wasm/barretenberg_wasm.test.ts b/ts/src/wasm/barretenberg_wasm.test.ts deleted file mode 100644 index 03560c4002..0000000000 --- a/ts/src/wasm/barretenberg_wasm.test.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { FileCrs, SRS_DEV_PATH } from '../crs/index.js'; -import { BarretenbergWasm } from './barretenberg_wasm.js'; - -describe('basic barretenberg smoke test', () => { - const wasm: BarretenbergWasm = new BarretenbergWasm(); - - beforeAll(async () => { - await wasm.init(); - }); - - it('should new malloc, transfer and slice mem', () => { - const length = 1024; - const ptr = wasm.call('bbmalloc', length); - const buf = Buffer.alloc(length, 128); - wasm.writeMemory(ptr, buf); - wasm.call('bbfree', ptr); - const result = Buffer.from(wasm.getMemorySlice(ptr, ptr + length)); - expect(result).toStrictEqual(buf); - }); - - it('should use asyncify to do an async callback into js', async () => { - const addr1 = await wasm.asyncCall('test_async_func', 1024 * 1024, 1); - const addr2 = await wasm.asyncCall('test_async_func', 1024 * 1024 * 2, 2); - expect(wasm.getMemorySlice(addr1, addr1 + 1024 * 1024).every(v => v === 1)).toBe(true); - expect(wasm.getMemorySlice(addr2, addr2 + 1024 * 1024 * 2).every(v => v === 2)).toBe(true); - }); - - it('should correctly pass CRS data through env_load_verifier_crs', async () => { - const crs = new FileCrs(0, SRS_DEV_PATH); - await crs.init(); - const g2DataPtr = await wasm.asyncCall('test_env_load_verifier_crs'); - const g2Data = wasm.getMemorySlice(g2DataPtr, g2DataPtr + 128); - expect(Buffer.from(g2Data)).toStrictEqual(crs.getG2Data()); - wasm.call('bbfree', g2DataPtr); - }); - - it('should correctly pass CRS data through env_load_prover_crs', async () => { - const numPoints = 1024; - const crs = new FileCrs(numPoints, SRS_DEV_PATH); - await crs.init(); - const g1DataPtr = await wasm.asyncCall('test_env_load_prover_crs', numPoints); - const g1Data = wasm.getMemorySlice(g1DataPtr, g1DataPtr + numPoints * 64); - expect(Buffer.from(g1Data)).toStrictEqual(crs.getG1Data()); - wasm.call('bbfree', g1DataPtr); - }); -}); diff --git a/ts/src/wasm/index.ts b/ts/src/wasm/index.ts deleted file mode 100644 index de04d5bf87..0000000000 --- a/ts/src/wasm/index.ts +++ /dev/null @@ -1 +0,0 @@ -export { BarretenbergWasm } from './barretenberg_wasm.js'; diff --git a/ts/src/wasm/serialize.ts b/ts/src/wasm/serialize.ts deleted file mode 100644 index fce9689953..0000000000 --- a/ts/src/wasm/serialize.ts +++ /dev/null @@ -1,70 +0,0 @@ -/** - * For serializing an array of fixed length buffers. - * TODO move to foundation pkg. - * @param arr - Array of bufffers. - * @returns The serialized buffers. - */ -export function serializeBufferArrayToVector(arr: Buffer[]) { - const lengthBuf = Buffer.alloc(4); - lengthBuf.writeUInt32BE(arr.length, 0); - return Buffer.concat([lengthBuf, ...arr]); -} - -/** - * Helper function for deserializeArrayFromVector. - */ -type DeserializeFn = ( - buf: Buffer, - offset: number, -) => { - /** - * The deserialized type. - */ - elem: T; - /** - * How many bytes to advance by. - */ - adv: number; -}; - -/** - * For deserializing numbers to 32-bit little-endian form. - * TODO move to foundation pkg. - * @param n - The number. - * @returns The endian-corrected number. - */ -export function deserializeArrayFromVector(deserialize: DeserializeFn, vector: Buffer, offset = 0) { - let pos = offset; - const size = vector.readUInt32BE(pos); - pos += 4; - const arr = new Array(size); - for (let i = 0; i < size; ++i) { - const { elem, adv } = deserialize(vector, pos); - pos += adv; - arr[i] = elem; - } - return { elem: arr, adv: pos - offset }; -} - -/** - * For serializing numbers to 32 bit little-endian form. - * TODO move to foundation pkg. - * @param n - The number. - * @returns The endian-corrected number. - */ -export function numToUInt32LE(n: number, bufferSize = 4) { - const buf = Buffer.alloc(bufferSize); - buf.writeUInt32LE(n, bufferSize - 4); - return buf; -} - -/** - * Deserialize the 256-bit number at address `offset`. - * @param buf - The buffer. - * @param offset - The address. - * @returns The derserialized 256-bit field. - */ -export function deserializeField(buf: Buffer, offset = 0) { - const adv = 32; - return { elem: buf.slice(offset, offset + adv), adv }; -}