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

browser-snark

v1.0.3

Published

A javascript library to generate SNARKs in a browser. Developers can use this library to quickly setup zk-proofs in their applications.

Downloads

9

Readme

Browser SNARK

A javascript library to generate SNARKs in a browser. Developers can use this library to quickly setup zk-proofs in their applications.

These zk-proofs can be submitted on chain. The proof verification also ensures that one proof is used exactly once on chain.

Installation

Next.js

npm install browser-snark

Add the following to your .babel.rc

{
    "presets": [
      [
        "next/babel",
        {
          "preset-env": {
            // "debug": true,
            "targets": [
              "last 2 Edge versions",
              "last 2 Opera versions",
              "last 2 Safari versions",
              "last 2 Chrome versions",
              "last 2 Firefox versions"
            ]
          }
        }
      ]
    ]
  }

Add the following to your next.config.js

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  webpack: function (config, options) {
    if (!options.isServer) {
      config.resolve.fallback.fs = false;
    }
    config.experiments = { asyncWebAssembly: true };
    return config;
  },
}

module.exports = nextConfig

Usage for SNARK Proofs

The following SNARK proofs can be generated and verified using this library

Preimage of Hash

You can prove that the user knows the preimage of a hash and verify it on a smart contract.

Smart Contract

Add the following interface to your contract

interface Verifier {
function verifyWithNonce(            
            uint[2] memory a,
            uint[2][2] memory b,
            uint[2] memory c,
            uint[3] memory input
    ) external returns(bool);
}

In your constructor instantiate the verifier;

    Verifier verifier;
    constructor() {
        verifier = Verifier(<CONTRACT ADDRESS>);
        // refer below table fot the contract address depending on the chain
    }

| Chain | address | |-------|---------| | Goerli | 0x9247fbEB6195dF258A701D5749dD21b4696c1C22 | | Optimism Mainnet | 0x7BA977e10e6f0EB05f3D6f4C1AB44626258e1d83 | | Polygon Mainnet | 0xa932B4b9eeE2B098766828aa6E0803820ad3a064 | | Ethereum Mainnet | Coming soon... |

In your function where you want to execute some logic only if proof is valid you need to do 3 steps

  1. Add 4 new parameters to your function uint[2] memory a, uint[2][2] memory b, uint[2] memory c, uint[3] memory input
  2. On the top of this function add require(verifier.verifyWithNonce(a,b,c, input), "Invalid proof");
  3. Verify that the proof was generated for the correct hash

For example

function yourFunction(uint yourParam1, uint yourParam2, uint[2] memory a, uint[2][2] memory b, uint[2] memory c, uint[3] memory input) public {
    require(verifier.verifyWithNonce(a,b,c, input), "Invalid proof");
    require(input[2] == EXPECTED_HASH, "Proof generated for wrong hash");
    
    //... Your logic
}

Javascript

Import browser-snark and create an object

import {Preimage} from 'browser-snark'
...
preimage = new Preimage();
...

Generate poseidon hash

myHash = preimage.hash(value)

Generate proof

if(myHash == EXPECTED_HASH){
    const proof = preimage.generateProof(value, EXPECTED_HASH);
    //... submit proof
}

Submit transaction with proof

    contractWithSigner.yourFunction(yourParam1, yourParam2, proof.a, proof.b, proof.c, proof.input)

For an example usage you can see the app/ directory.

Todo

  • Generate nonce from msg.sender