-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(metal_mont_backend): adapted from https://github.com/geometryxyz/msl-secp256k1 #13
base: main
Are you sure you want to change the base?
Conversation
…rbitrary limbs of Vec<u32>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Haven't reviewed any metal
file, bc I believe they're just moved rather modified, so I consider them out of scope this particular PR.
These build.rs
although being nice to have not a critical part of PR, so in case of tight deadlines if any we can put in a separate PR, I can even take it on my own afterwards.
let mut limb_idx = 0; | ||
|
||
for &byte in bytes.iter() { | ||
val |= (byte as u32) << bits; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd consider to operate by a whole word here rather than a single byte. However I think this optimisation should be a part of a separate PR.
pub enum MetalError { | ||
#[error("Couldn't find a system default device for Metal")] | ||
DeviceNotFound(), | ||
#[error("Couldn't create a new Metal library: {0}")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this one is used within metalcompiller.rs
, so I believe it could be removed due to reasoning we have discussed in DM
use std::string::String; | ||
|
||
pub fn compile_metal(path_from_cargo_manifest_dir: &str, input_filename: &str) -> String { | ||
let input_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just saving here all the main points of the talk we have had in DM recently.
The metal building code is pretty similar to what I've made for my internal use in #14 so it's perfectly fine to keep it as it is except for two points:
- Move the code in
build.rs
as will enable it building during the library itself build stage, allowingcargo
to handle incremental builds of metal shaders effortlessly. - It worth it to keep built library in
target
directory withenv::var("OUT_DIR")
env variable, which could be referenced in the main code base asenv!("OUT_DIR")
- It's worth it to conditionally append debug symbols generation attribute for profiling as it's made here: https://github.com/zkmopro/gpu-acceleration/pull/14/files#diff-1fb60ca492a52fb05b37909bd795664d0a6c31e77d34f28bc1baa3b7d91093a7R35-R40
- When it comes to build.rs you have to specify the list of foreign language files to follow, which is done here https://github.com/zkmopro/gpu-acceleration/pull/14/files#diff-1fb60ca492a52fb05b37909bd795664d0a6c31e77d34f28bc1baa3b7d91093a7R103-R106
Overview
This PR introduces a well-organized structure for further MSM implementation with Metal and adapts a more efficient EC metal library from Zprize 2023 winner, Tal and Koh's work of MSM with WebGPU.
Related Issue
Key Changes
Core Implementation
Refactored Metal MSM Module
metal_msm
module with host, shader, and utils submodulesmetal_msm
aims to replace the previousmetal
directory as a refactored version. Themetal
directory will be deleted once themetal_msm
is implemented.Better EC backend adapted from Zprize 2023 winner
BigInt
andBigIntWide
structs with arithmetic operationsTesting & Benchmarking
Test Suites
Benchmarking
For the BN254 curve we're targeting, the
modified mont_mul
with a15-bit
limb size appears to work best. This makes sense intuitively: with a 15-bit limb size, there are 17 limbs, resulting in 15 * 17 = 255 bits, which is closest to the 254-bit base field modulus of BN254.Benchmark result:
Utility Functions
Dependencies
Updated various dependencies including:
Next Steps
Reference