Giter Site home page Giter Site logo

wgsl-poseidon's Introduction

Poseidon in WGSL

This repository contains code which allows you to compute Poseidon hashes in your GPU. The code is written in WGSL, a shader programming language that works with WebGPU.

This implementation of the Poseidon hash targets the BN254 scalar field, with the following parameters:

  • Number of inputs: 1
  • t = 2
  • n_rounds_f = 8
  • n_rounds_p = 56

The results from this implementation should match those of the circomlibjs implementation on BN254.

Credits

Much of the big integer and finite field code was adapted from msm-webgpu by Sampriti Panda, Adhyyan Sekhsaria, and Nalin Bhardwaj.

The structure of the Poseidon WGSL code was inspired by poseidon-ark by arnaucube.

Getting started

Clone this repository, navigate to the project directory, and run:

cargo test test_poseidon -- --nocapture

You should see output like this:

Computing 16384 Poseidon hashes in Rust / WebGPU
CPU took 416ms
AdapterInfo { name: "Quadro P520", vendor: 4318, device: 7476, device_type: DiscreteGpu, driver: "NVIDIA", driver_info: "535.54.03", backend: Vulkan }
GPU took 88ms
test poseidon::test_poseidon ... ok

The time taken by the GPU to compute a hash the first time you run this command may be slower than the time it takes during subsequent runs. This may be due to the GPU caching the shader code.

This code has been successfully tested with an Nvidia Quadro P520 with 2GB memory on a Ubuntu Linux machine with version 535.54.03 of the Nvidia driver.

The code also runs on the same machine's Intel(R) UHD Graphics 620 (WHL GT2) integrated GPU with the Mesa v22.2.5 driver, but takes about 1100ms.

Poseidon hash using WebGPU in the browser

The following was tested with Firefox Nightly 117.0a1 (2023-07-15) (64-bit).

Enter about:config in the address bar and set the following to true:

  • dom.webgpu.enabled
  • gfx.webgpu.force-enabled

Next, in the command line, navigate to the web subdirectory:

cd web

Install dependencies:

npm i

Run the web server:

npx parcel index.html

Navigate to the URL that appears and viola! You should see something like the following:

Computing 16384 Poseidon hashes in the browser / WebGPU
CPU took 1267 ms
GPU took 103 ms

wgsl-poseidon's People

Contributors

weijiekoh 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  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.