ZK Paris: Koh Wei Jie - Deep Dive into WebGPU
4 Aug 2023 · 15:08
Recorded at zkParis in Paris on July 20, 2023, this short talk features Koh Wei Jie giving a deep dive into WebGPU, the modern browser standard for GPU-accelerated computation. The session, part of a zkParis program backed by Geometry and ZKValidator, runs about fifteen minutes and sits alongside the event's broader lineup of zero-knowledge cryptography talks from the Zero Knowledge podcast community.
Speakers
Topics
Transcript
okay so up next we have a deep dive into web GPU from wayj at geometry thanks Chloe so hi everybody my name is weijia from geometry uh doing research and development and my topic today is web GPU for crypto so but before we begin our presentation let's generate a SDK proof in our browser because we want to do some transaction it's and it's taking a really long time that's a problem client-side ZK tends to be pretty slow even for circuits which are reasonable
in size for privacy related applications so how can we make this faster just to drive in the point about performance in applications like to know the cache depends on your machine but it takes 10 to 30 seconds on my really old machine takes like 20 seconds and for other circuits like the Persona Labs efficient ecdsa verification circuit with 16 with 163 000 constraints on the N1 MacBook Pro 39 seconds so that's not very good user experience but it's still necessary to generate ZK proofs in the client for these
applications because otherwise you would have to Outsource your proving to a third party and the third party can easily de-anomize you and that's not that defeats the purpose of doing privacy in the first place so where do we go from here I want to introduce the idea of using web GPU for crypto client-side proving so web GPU is a relatively new API for the web it's a standard by the w3c big companies in a browser space like Google Microsoft Mozilla Apple we're all involved in the creation of this API over the past few years
they started working on this in 2017 and I believe that soon it will be available in these browsers Chrome Firefox Safari but currently it's still Revenue so you have to enable this feature in your developer features in your config page and web GPU comes with its own Shader language a shaded language is basically the same it means a programming language that you send to your GPU or you you write programs that you send your GPU in a shaded language okay so all this is relatively new because it's targeting modern gpus and it provides layers of abstraction which are more developer
friendly than previous iterations of GPU for the web like webgl and um it's also much friendlier to write these programs or shaders then lower level languages like C plus with Cuda of apis and Vulcan so much of the web GPU development today is focused on Graphics there are lots of examples online you get 3D high performance High FPS graphics with GPU but this presentation is not about that because web GPU also supports general purpose compute not just graphics and the whole point of using gpus in the ZK or crypto space is because you want to
parallelize your operations and the benefits of doing that in this domain is that certain algorithms can benefit from running different computations in parallel so in noyama there's like a lot of work on gpus zprize has certain like competition tracks for to award um price prices to teams that can write code for gpus and there is also a lot of interesting speed UPS in in domains like private information retrieval where you do need to do a lot
of matrix multiplication so that's where gpus come in and so all that stuff has to do with server-side GPU and server side acceleration with gpus but what about client-side so because web GPU is so underexplored I have observed that this domain is very underexplored um I think there's a lot of space for engineers and developers and researchers to jump in and so even if you don't have like a big GPU cluster why not use your own device right you
can develop these interesting applications that speed up different algorithms on your own device um and they run on most devices most modern devices this is a really old phone uh 2019 era phone and it runs a web GPU in the developer developer version of Chrome so um so in terms of like what we want to Target for research and development into web GPU for crypto I mean the um like examples of our goals will be multi-scale multiplication and entity number automatic transforms and there has been some research on whether these Algos are
amenable to gpus or fpgas and um the answer there is a lot of like complexity in this in this analysis you also have to look into how memory access plays a role in the actual performance that you get out of this but nevertheless it's very exciting to have a new field to jump in and do this research so I just want to spend a bit of time to go into a technical overview of web GPU so the most important most important thing to remember is that it's an API and the browser does all the heavy lifting and if you can wrap your head around the
basic programming model of web GPU It's relatively easy to start to get started programming with it because it's really not as complex as something like Cuda with C plus plus for example there are abstractions over for example an adapter a GPU adapter is an abstraction of a like a particular implementation of web GPU in the browser and it provides you with an attraction over the device called GPU device GPU devices can have buffers storage buffers staging buffers these are abstractions over ways that developers can read and write data into the GPU and this is where it's also important to
understand the underlying memory models of gpus because they behave differently than with like um regular computer architecture um specifically it's important to understand how memory maps to different GPU cores and memory access is very closely tied to computation um there is the idea of a pipeline and this is abstraction over the way that the API blenders together data in buffers and shaders which is the GPU code they want to execute and sends it to the GPU so there's a lot of new terms in this in this framework and it's really really important to understand the new the
whole Paradigm and how this works but it's there's a lot of really good documentation by the w3c there are more and more developers writing code of this and writing other custom blog posts so I think it's a really good time to start here's an example of web web GPU Shader language code so this takes some value multiplies it by itself and adds three and VP assist operation 100 uh 104 000 times and if you Benchmark this on a CPU in Syria it's like half a second but if
you run this in power level in the GPU it's much faster because gpus tend to have like hundreds of calls well CPUs have much uh much lower number so and this was done on a really old GPU um and this if you run this in a like M2 micro Pro will be much faster anyway so um I also wrote an implementation of the Poseidon hash function in wgsl for the web for web GPU um so this is targeting the bn254 curve a scalar field um if you run more than 2 000 of these hash functions in parallel then the GPU is going to beat the CPU or at least on my machine
um I did a benchmark on 16 000 hash uh 16 000 hashes in Poseidon and for the CPU it's like one second from on the machine in the browser and like 300 milliseconds in GPU via web GPU so I did this after I finished my slide so I don't have my screenshots but come find me and I will show you the demo so where can we go with this so we are still really early if you spend some time on GitHub and look for web GPU for crypto you will only find a few repos so um that's I'm adapted to
um this MSM web GPU repo that same pretty uh wrote um and Aryan and I'm really grateful for the help they wrote a lot of uh they wrote uh MSM code for I think a hackathon or some kind of event as usual or something like that um so we used it I used the basic uh field arithmetic big integer arithmetic uh functions that they wrote and converted that to use to for my Poseidon hash function implementation so there are also other developers who wrote stuff with webpage GPU but not that many so if you're an engineer and you want to do
something really interesting in crypto like get in like get in the car let's go let's go shopping okay um and let's take a step back and think about what do we do in a field where it's so new that there's a lot of things to learn and research but they're not enough developer tools and not enough like uh Community groups to to support like this work so let's think about how ZK looked like in 2019 and this was when we had um the equivalent I'm thinking of is circle so in 2018 2019 silicon was really new and it was groundbreaking because it allowed like developers to
easily write circuits today people are using Halo 2 and so on but back then silicon was the go-to language for if we wanted to write any kind of circuit I mean back then it was like graph 16 and um what the zero X part group did was really awesome because they just jumped straight into it and then they wrote really useful developer tools they push it to the extreme to write extremely large circuits and like this sort of tested the boundaries push the boundaries what what they could do they got a bunch of developers excited about ZK and and some of them spinal into their own startups and many of the hardest ones you see at
today's event um so what's the equivalent for web GPU for crypto um currently most developers to write web GPU probably do so for graphics and they're not that many could write it for crypto so what can we do what kind of tools can we write so maybe like debugging tools then no debugging tools I found so far there are no higher level dsls that I've seen besides ones that Target graphics um there needs to be more research into how we can abstract good more abstractions uh that make it even easier to Target consumer devices [Music] and we also need to do research into
security because I think no one has done this is it possible to Dos clients if you provide like malicious inputs I don't know can you do timing attacks side Channel attacks to extract private key information I'm not sure but this is really important because you want to do ZK proving on a client side you need to know you need to figure out all this stuff um so moonshot goes to Engineers ZK approvers we want to implement ZK approvers in you don't have to write the whole thing in web GPU we can write like Parts in wasm or JavaScript and then write the stuff that you need to parallelize in web GPU that's that's perfectly fine um witness generation that's also one of
the slow steps in in running like uh privacy based uh client-side apps and then the more moonshot ideas like distributed proof generation networks so you don't just want to like have uh make it you don't just want people with GPU Farms to run your apps or your or your notes maybe if you can run in the browser even better and also applications to prr so um very short presentation but thanks for thanks for attention I'd love to take any questions you have thank you [Applause] thanks for the great talk again
um maybe it's a really stupid question but I've used 3js and I didn't have to enable special browser application so which which OS and browser are you on um Chrome M1 okay I'm on Linux and my laptop is really shitty so okay I don't know it's different experiences yeah okay oh cool thank you [Applause] amazing thank you AJ