Skip to content

Commit

Permalink
feat(shader): add BN254 basefield modulus constant generation
Browse files Browse the repository at this point in the history
  • Loading branch information
moven0831 committed Jan 8, 2025
1 parent b515f19 commit 15aa8d5
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion mopro-msm/src/msm/metal_msm/host/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
* xcrun -sdk macosx metallib <path.ir> -o <path.metallib>
*/

use crate::msm::metal_msm::utils::limbs_conversion::ToLimbs;
use ark_bn254::Fq as BaseField;
use ark_ff::PrimeField;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
Expand Down Expand Up @@ -93,6 +96,7 @@ pub fn write_constants(
) {
let two_pow_word_size = 2u32.pow(log_limb_size);
let mask = two_pow_word_size - 1u32;
let basefield_modulus = BaseField::MODULUS.to_limbs(num_limbs, log_limb_size);

let mut data = "// THIS FILE IS AUTOGENERATED BY shader.rs\n".to_owned();
data += format!("#define NUM_LIMBS {}\n", num_limbs).as_str();
Expand All @@ -102,6 +106,14 @@ pub fn write_constants(
data += format!("#define MASK {}\n", mask).as_str();
data += format!("#define N0 {}\n", n0).as_str();
data += format!("#define NSAFE {}\n", nsafe).as_str();
data += format!("constant uint32_t BN254_BASEFIELD_MODULUS[NUM_LIMBS] = {{\n").as_str();
for i in 0..num_limbs {
if i > 0 {
data += ",\n";
}
data += format!(" {}", basefield_modulus[i]).as_str();
}
data += format!("\n}};\n").as_str();

let output_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join(filepath)
Expand All @@ -111,7 +123,7 @@ pub fn write_constants(

#[cfg(test)]
pub mod tests {
use super::compile_metal;
use super::*;

#[test]
pub fn test_compile() {
Expand All @@ -121,4 +133,9 @@ pub mod tests {
);
println!("{}", lib_filepath);
}

#[test]
pub fn test_write_constants() {
write_constants("../mopro-msm/src/msm/metal_msm/shader", 16, 16, 25481, 1);
}
}

0 comments on commit 15aa8d5

Please sign in to comment.