npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

emp-wasm

v0.1.5

Published

Wasm build of authenticated garbling from [emp-toolkit/emp-ag2pc](https://github.com/emp-toolkit/emp-ag2pc).

Downloads

39

Readme

emp-wasm

Wasm build of authenticated garbling from emp-toolkit/emp-ag2pc.

(If you're not familiar with garbled circuits, you might find this video helpful.)

Usage

Take a look at mpc-framework for a nicer API which utilizes emp-wasm as a dependency. In particular, it allows you to write your circuits in a TypeScript-like language called summon.

Read on if you're more interested in what's going on under the hood and are prepared to use/write circuits like these directly.

npm install emp-wasm
import { secure2PC, BufferedIO, IO } from 'emp-wasm';

import circuit from './circuit.ts';

async function main() {
  // emp-wasm has no opinion about how you do your IO, websockets are just used
  // for this example
  const io = await makeWebSocketIO('wss://somehow-connect-to-bob');

  const output = await secure2PC(
    'alice', // use 'bob' in the other client
    circuit, // a string defining the circuit, see circuits/*.txt for examples
    Uint8Array.from([/* 0s and 1s defining your input bits */]),
    io,
  );

  // the output bits from the circuit as a Uint8Array
  console.log(output);
}

async function makeWebSocketIO(url: string) {
  const sock = new WebSocket(url);
  sock.binaryType = 'arraybuffer';

  await new Promise((resolve, reject) => {
    sock.onopen = resolve;
    sock.onerror = reject;
  });

  // You don't have to use BufferedIO, but it's a bit easier to use, otherwise
  // you need to implement io.recv(len) returning a promise to exactly len
  // bytes
  const io = new BufferedIO(
    data => sock.send(data),
    () => sock.close(),
  );

  sock.onmessage = (event: MessageEvent) => {
    if (!(event.data instanceof ArrayBuffer)) {
      console.error('Unrecognized event.data');
      return;
    }

    // Pass Uint8Arrays to io.accept
    io.accept(new Uint8Array(event.data));
  };

  sock.onerror = (e) => {
    io.emit('error', new Error(`WebSocket error: ${e}`));
  };

  sock.onclose = () => io.close();

  return io;
}

main().catch(console.error);

For a concrete example, see wsDemo in demo.ts (usage instructions further down in readme).

Demo

npm install
npm run build
npm run demo

Requirements:

internalDemo

If you don't want to juggle multiple pages, you can do await internalDemo(3, 5) in the console, which will run two instances in the same page communicating internally.

consoleDemo

Open the url in the console in two tabs and run consoleDemo('alice', 3) in one and consoleDemo('bob', 5) in the other. This will begin a back-and-forth where each page prints write(...) to the console, which you can paste into the other console to send that data to the other instance (note: sometimes there are multiple writes, make sure to copy them over in order). After about 15 rounds you'll get an alert showing 8 (== 3 + 5).

wsDemo

Open the url in the console in two tabs and run await wsDemo('alice', 3) in one and await wsDemo('bob', 5) in the other. This uses a websocket relay included in npm run demo and defined in scripts/relayServer.ts.

rtcDemo

Open the url in the console in two tabs and run await rtcDemo('pair-id', 'alice', 3) in one and await rtcDemo('pair-id', 'bob', 5) in the other. This one uses WebRTC (via peerjs) and should work across networks, locating each other via pair-id.

Regular C++ Compile

This library started out as a stripped down version of the original C++ project. You can compile this for your local system and test it like this:

./scripts/build_local_test.sh
./scripts/local_test.sh

This will calculate sha1("")==da39a3ee5e6b4b0d3255bfef95601890afd80709. It proves to Alice that Bob knows the preimage of this hash. Each side is run in a separate process and they communicate over a local socket.

Requirements:

  • clang
  • mbedtls (on macos: brew install mbedtls)
    • this version of mbedtls is actually not needed for the wasm version, since we need to compile a wasm-specific version ourselves

Uncertain Changes

For most of the changes I'm reasonably confident that I preserved behavior, but there some things I'm less confident about:

  • send and recv swapped for bob in Fpre::generate (see TODO comment)
  • after removing threading, I also moved everything to a single io channel
  • rewrite of simd code to not use simd and updated aes usage written by chatgpt o1 preview
  • used chatgpt o1 preview to migrate from openssl to mbedtls

Regardless, this version of emp-toolkit needs some checking.