Skip to content

Commit

Permalink
Merge pull request #20 from MystenLabs/mh--add-typesafe-wasm-bindings
Browse files Browse the repository at this point in the history
Add typesafe interface over wasm bindings
  • Loading branch information
hayes-mysten authored Jan 17, 2025
2 parents b7c6eda + 0b17b55 commit 63f03b5
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 34 deletions.
2 changes: 1 addition & 1 deletion packages/walrus-wasm/walrus_wasm.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* tslint:disable */
/* eslint-disable */
export function from_signed_messages_and_indices(message_type: MessageType, signed_messages: any, signer_indices: Uint16Array): any;
export function bls12381_min_pk_verify(signature: Uint8Array, public_key: Uint8Array, msg: Uint8Array): boolean;
/**
* Aggregate a list of signatures.
Expand All @@ -12,6 +11,7 @@ export function bls12381_min_pk_aggregate(signatures: any): Uint8Array;
*/
export function bls12381_min_pk_verify_aggregate(public_keys: any, msg: Uint8Array, signature: Uint8Array): boolean;
export function blake2b256(msg: Uint8Array): Uint8Array;
export function from_signed_messages_and_indices(message_type: MessageType, signed_messages: any, signer_indices: Uint16Array): any;
/**
* The message type for the [ProtocolMessageCertificate].
*/
Expand Down
63 changes: 31 additions & 32 deletions packages/walrus-wasm/walrus_wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,38 +183,6 @@ function passArray8ToWasm0(arg, malloc) {
WASM_VECTOR_LEN = arg.length;
return ptr;
}

let cachedUint16ArrayMemory0 = null;

function getUint16ArrayMemory0() {
if (cachedUint16ArrayMemory0 === null || cachedUint16ArrayMemory0.byteLength === 0) {
cachedUint16ArrayMemory0 = new Uint16Array(wasm.memory.buffer);
}
return cachedUint16ArrayMemory0;
}

function passArray16ToWasm0(arg, malloc) {
const ptr = malloc(arg.length * 2, 2) >>> 0;
getUint16ArrayMemory0().set(arg, ptr / 2);
WASM_VECTOR_LEN = arg.length;
return ptr;
}
/**
* @param {MessageType} message_type
* @param {any} signed_messages
* @param {Uint16Array} signer_indices
* @returns {any}
*/
module.exports.from_signed_messages_and_indices = function(message_type, signed_messages, signer_indices) {
const ptr0 = passArray16ToWasm0(signer_indices, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.from_signed_messages_and_indices(message_type, signed_messages, ptr0, len0);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return takeFromExternrefTable0(ret[0]);
};

/**
* @param {Uint8Array} signature
* @param {Uint8Array} public_key
Expand Down Expand Up @@ -287,6 +255,37 @@ module.exports.blake2b256 = function(msg) {
return v2;
};

let cachedUint16ArrayMemory0 = null;

function getUint16ArrayMemory0() {
if (cachedUint16ArrayMemory0 === null || cachedUint16ArrayMemory0.byteLength === 0) {
cachedUint16ArrayMemory0 = new Uint16Array(wasm.memory.buffer);
}
return cachedUint16ArrayMemory0;
}

function passArray16ToWasm0(arg, malloc) {
const ptr = malloc(arg.length * 2, 2) >>> 0;
getUint16ArrayMemory0().set(arg, ptr / 2);
WASM_VECTOR_LEN = arg.length;
return ptr;
}
/**
* @param {MessageType} message_type
* @param {any} signed_messages
* @param {Uint16Array} signer_indices
* @returns {any}
*/
module.exports.from_signed_messages_and_indices = function(message_type, signed_messages, signer_indices) {
const ptr0 = passArray16ToWasm0(signer_indices, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.from_signed_messages_and_indices(message_type, signed_messages, ptr0, len0);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
}
return takeFromExternrefTable0(ret[0]);
};

/**
* The message type for the [ProtocolMessageCertificate].
* @enum {0 | 1}
Expand Down
Binary file modified packages/walrus-wasm/walrus_wasm_bg.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/walrus-wasm/walrus_wasm_bg.wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export const blobencoder_encode_with_metadata: (a: number, b: number, c: number)
export const blobencoder_compute_metadata: (a: number, b: number, c: number) => [number, number, number];
export const blobencoder_decode_primary: (a: number, b: bigint, c: any) => [number, number, number];
export const blobencoder_decode_secondary: (a: number, b: bigint, c: any) => [number, number, number];
export const from_signed_messages_and_indices: (a: number, b: any, c: number, d: number) => [number, number, number];
export const bls12381_min_pk_verify: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number];
export const bls12381_min_pk_aggregate: (a: any) => [number, number, number, number];
export const bls12381_min_pk_verify_aggregate: (a: any, b: number, c: number, d: number, e: number) => [number, number, number];
export const blake2b256: (a: number, b: number) => [number, number];
export const from_signed_messages_and_indices: (a: number, b: any, c: number, d: number) => [number, number, number];
export const rustsecp256k1_v0_8_1_context_create: (a: number) => number;
export const rustsecp256k1_v0_8_1_context_destroy: (a: number) => void;
export const rustsecp256k1_v0_8_1_default_illegal_callback_fn: (a: number, b: number) => void;
Expand Down
5 changes: 5 additions & 0 deletions packages/walrus/src/utils/bcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,8 @@ export const Sliver = bcs.enum('Sliver', {
Primary: SliverData,
Secondary: SliverData,
});

export const SliverPair = bcs.struct('SliverPair', {
primary: SliverData,
secondary: SliverData,
});
83 changes: 83 additions & 0 deletions packages/walrus/src/wasm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { BlobEncoder, from_signed_messages_and_indices, MessageType } from '@mysten/walrus-wasm';

import { blobIdFromBytes } from './utils/bcs.js';
import type { BlobMetadataWithId, SliverPair } from './utils/bcs.js';

export interface EncodedBlob {
sliverPairs: (typeof SliverPair.$inferInput)[];
metadata: typeof BlobMetadataWithId.$inferInput & { blob_id: string };
rootHash: Uint8Array;
}

export function encodeBlob(nShards: number, bytes: Uint8Array): EncodedBlob {
const encoder = new BlobEncoder(nShards);

const [sliverPairs, metadata, rootHash] = encoder.encode_with_metadata(bytes);

return {
sliverPairs,
metadata: {
...metadata,
blob_id: blobIdFromBytes(new Uint8Array(metadata.blob_id)),
},
rootHash: new Uint8Array(rootHash.Digest),
};
}

export interface CombinedSignature {
signers: number[];
serializedMessage: Uint8Array;
signature: string;
}

export interface Confirmation {
serializedMessage: string;
signature: string;
}

export function combineSignatures(
confirmations: Confirmation[],
signerIndices: number[],
): CombinedSignature {
const combined = from_signed_messages_and_indices(
MessageType.Confirmation,
confirmations,
new Uint16Array(signerIndices),
) as {
signers: number[];
serialized_message: number[];
signature: string;
};

return {
signers: signerIndices,
serializedMessage: new Uint8Array(combined.serialized_message),
signature: combined.signature,
};
}

export function decodePrimarySlivers(
nShards: number,
size: number | bigint,
sliverPairs: (typeof SliverPair.$inferInput)[],
): Uint8Array {
const encoder = new BlobEncoder(nShards);
const bytes = encoder.decode_primary(BigInt(size), sliverPairs);
return new Uint8Array(bytes);
}

export function computeMetadata(
nShards: number,
bytes: Uint8Array,
): typeof BlobMetadataWithId.$inferInput & { blob_id: string } {
const encoder = new BlobEncoder(nShards);
const metadata = encoder.compute_metadata(bytes);

return {
...metadata,
blob_id: blobIdFromBytes(new Uint8Array(metadata.blob_id)),
};
}

0 comments on commit 63f03b5

Please sign in to comment.