Git Product home page Git Product logo

wasm-multi-party-ecdsa's Introduction

WASM Multi Party ECDSA

This library provides a secure implementation of the Elliptic Curve Digital Signature Algorithm (ECDSA) in WebAssembly (WASM) implemented entirely in Rust. It enables parties to securely generate keys and sign messages using a threshold scheme without revealing their private keys. This solution outperforms previous hybrid approaches and is the first pure-wasm MPC solution.

CoinFabrik is the Web3 solutions company behind this implementation.

Usage

Installation

In order to use the library, you will need to install the following dependencies:

npm install @mpc-framework/wasm-multi-party-ecdsa comlink

Preparation

As this library needs a web worker to perform the calculations, you'll need to create a worker instance. You can do this by creating a file called worker.ts and adding the following code:

import init, { initThreadPool, MultiPartyEcdsa } from "wasm-multi-party-ecdsa";
import * as Comlink from "comlink";

void (async function () {
  // Needed for wasm-bindgen-rayon
  await init();
  await initThreadPool(1);

  // In case we want to add a hook to listen when it's ready
  self.postMessage({ ready: true });
})();

const worker = { MultiPartyEcdsa };
export type IWorker = typeof worker;

Comlink.expose(worker);

We'll be using Comlink to communicate with the worker. This is a library that allows us to use web workers as if they were regular functions.

Finally, a temporary hack is needed for the crypto.getRandomValues() function to work with our library. More information can be found here. This code must be added to the same file worker.ts:

const getRandomValues = crypto.getRandomValues;
crypto.getRandomValues = function <T extends ArrayBufferView | null>(array: T) {
  const buffer = new Uint8Array(array as unknown as Uint8Array);
  const value = getRandomValues.call(crypto, buffer);
  (array as unknown as Uint8Array).set(value as unknown as Uint8Array);
  return array;
};

Now we're ready to start using the library.

Keygen

Using the library is pretty straightforward. First we'll need to understand the concepts of groups, sessions and parties.

  • Groups are a collection of parties that will hold a key. A group can be reused to generate multiple keys or sign multiple messages.

  • Sessions are subgroups created by members of a group with the sole purpose of generating a key or signing a message. A session should not be reused.

  • Parties are the members that will hold a key. Each party is identified by a unique number.

To generate our first set of keys, we'll define the number of parties and the threshold. The threshold is the minimum number of parties that need to be present in order to sign a message, minus one. The number of parties is the amount of parties that will hold a key.

E.g.: Given a threshold of 1 and a number of parties of 3, we need at least 2 parties to be present in order to sign a message.

We can start by instantiating the library and connecting it to our MPC-Manager instance:

import * as Comlink from "comlink";
import { IWorker } from "./worker";

// We need to create a new worker instance and wrap it with Comlink
const innerWorker = new Worker(new URL("./worker.ts", import.meta.url));
const worker = Comlink.wrap<IWorker>(innerWorker);

// Then we can instantiate the library. At this point we'll be connected
// to our manager.
const multiPartyEcdsa = await new worker.MultiPartyEcdsa("ws://localhost:8080");

Now we can create a new group and session, which we'll use to generate a new key:

const NUMBER_OF_PARTIES = 3;
const THRESHOLD = 1;

const { group } = await multiPartyEcdsa.groupCreate(
  NUMBER_OF_PARTIES,
  THRESHOLD
);
const { session } = await multiPartyEcdsa.sessionCreate(
  group.id,
  "keygen",
  null
);
const { partyNumber } = await multiPartyEcdsa.sessionSignup(
  group.id,
  session.id
);
// And use it to create a new key
const { localKey, publicKey } = await multiPartyEcdsa.keygen(
  group.id,
  session.id,
  partyNumber,
  NUMBER_OF_PARTIES,
  THRESHOLD
);

In the other clients:

// ... rest of the code
const { group } = await multiPartyEcdsa.groupJoin(groupId);
const { session, partyNumber } = await multiPartyEcdsa.sessionSignup(
  groupId,
  sessionId
);
const { localKey, publicKey } = await multiPartyEcdsa.keygen(
  group.id,
  session.id,
  partyNumber,
  NUMBER_OF_PARTIES,
  THRESHOLD
);

And that's it! You now have a new multi-party key that can be used to sign messages.

Signing

In order to sign a message, we'll need to create a new session:

// ... rest of the code
const message = new Uint8Array([1, 2, 3]);
const parties = [1, 2];

const { session } = await multiPartyEcdsa.sessionCreate(
  groupId,
  "sign",
  message
);

// In this case we don't need to sign up, as we already got a party number
// assigned to us at the moment of the keygen.
await multiPartyEcdsa.sessionLogin(groupId, session.id, localKey.i);
const signature = await multiPartyEcdsa.sign(
  groupId,
  session.id,
  localKey,
  parties,
  message
);

In the other clients we must only login to the created session and wait for the signature to be generated:

// ... rest of the code
const parties = [1, 2];

const { session } = await multiPartyEcdsa.sessionLogin(
  groupId,
  sessionId,
  localKey.i
);
const signature = await multiPartyEcdsa.sign(
  groupId,
  session.id,
  localKey,
  parties,
  message
);

And that's it! You have now signed your first message with a multi party threshold scheme.

Contributing

If you'd like to contribute to the library, please open an issue or submit a pull request. We welcome any contributions, including bug fixes, feature requests, and documentation improvements.

Acknowledgments

This project is based on the following projects:

License

This project is licensed under the MIT License. Please see the LICENSE file for more information.

wasm-multi-party-ecdsa's People

Contributors

aon avatar nflorescf avatar srw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

wasm-multi-party-ecdsa's Issues

Is it because I am not using the right way?

I ran mpc-manager, then compiled wasm-multi-party-ecdsa with wasm build, and then executed npm start in the www directory, and I could click some buttons in the localhost:8081 interface, but after these buttons were clicked, the mpc-manager I can click some buttons on the localhost:8081 interface, but after clicking these buttons, some of them are not displayed correctly in the logs of mpc-manager. Is this normal? Is it because I am not using the right way?

npm ERR

npm install @mpc-framework/wasm-multi-party-ecdsa comlink

npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/@mpc-framework%2fwasm-multi-party-ecdsa - Not found
npm ERR! 404
npm ERR! 404 '@mpc-framework/wasm-multi-party-ecdsa@*' is not in this registry.
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! A complete log of this run can be found in: /Users/zhangshuguang/.npm/_logs/2023-06-08T07_08_24_295Z-debug-0.log

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.