This package provides field arithmetic, a hash function, a Merkle tree module, and Schnorr signatures suitable for use inside of SNARKs. You can install it with

npm install snarkyjs-crypto

For usage of the API, please see the example here.

API documentationa

/* Crypto over the scalar field of the BN128 curve. */
let bn128 : S;

module type S = {
  module Field: {
    /* Arithmetic and other operations on a prime order field. */
    type t;

    let equal: (t, t) => bool;

    let mul: (t, t) => t;
    let add: (t, t) => t;
    let sub: (t, t) => t;
    let div: (t, t) => t;

    let negate: t => t;
    let invert: t => t;
    let square: t => t;

    let one: t;
    let zero: t;

    let ofString: string => t;
    let ofInt: int => t;
    let ofBits: array(bool) => t;

    let toString: t => string;

    let testBit : (t, int) => bool;

  module Hash: {
    type t = Field.t;

    /* Hash an array of elements using the Poseidon hash function.
          This hash function is intended to be an ideal cryptographic hash function.
    let hash: array(Field.t) => t;

  module MerkleTree: {
    type t('a) = Merkle_tree.t('a, Hash.t);
    type merkle_tree('a) = t('a);

    module MembershipProof: {
      type t = {
        index: int,
        path: array(Hash.t),

      /* Create a membership proof for a leaf at the given index. */
      let create: (merkle_tree('a), int) => t;

      /* Check a membership proof. */
      let check:
          Hash.t, /* root hash */
        ) => /* element hash */

    /* Create a binary merkle tree with values of type 'a at the leaves.
       The first argument is for hashing the leaves.
       The second argument is a default value which will be used to padt
       the given array to a power of 2 length. */
    let ofArray: ('a => Hash.t, 'a, array('a)) => t('a);

  module Group: {
    type t

    let toAffine : t => (Field.t, Field.t);
    let ofAffine : (Field.t, Field.t) => t;

  module Schnorr: {
    module PrivateKey: {
      type t;

      /* Generate a private key using randomness from node's crypto API. */
      let create: unit => t;

      let toJSON: t => json;

    module PublicKey: {
      type t = Group.t;

      /* Derive the public key corresponding to a given private key. */
      let ofPrivateKey: PrivateKey.t => t;
      let toJSON: t => json;

    module Signature: {
      type t;

      /* Verify a signature against the given public key and message. */
      let check: (t, PublicKey.t, array(Field.t)) => bool;
      let toJSON: t => json;

    /* Sign a message. */
    let sign: (PrivateKey.t, array(Field.t)) => Signature.t;