|
| 1 | +// adapted from: https://github.com/geometryxyz/msl-secp256k1 |
| 2 | + |
| 3 | +use crate::msm::metal::abstraction::limbs_conversion::{FromLimbs, ToLimbs}; |
| 4 | +use crate::msm::metal_msm::host::gpu::{ |
| 5 | + create_buffer, create_empty_buffer, get_default_device, read_buffer, |
| 6 | +}; |
| 7 | +use crate::msm::metal_msm::host::shader::{compile_metal, write_constants}; |
| 8 | +use ark_bn254::Fr as ScalarField; |
| 9 | +use ark_ff::{BigInt, BigInteger, PrimeField}; |
| 10 | +use metal::*; |
| 11 | + |
| 12 | +#[test] |
| 13 | +#[serial_test::serial] |
| 14 | +pub fn test_ff_sub() { |
| 15 | + let log_limb_size = 13; |
| 16 | + let num_limbs = 20; |
| 17 | + |
| 18 | + // Scalar field modulus for bn254 |
| 19 | + let p = BigInt::new([ |
| 20 | + 0x43E1F593F0000001, |
| 21 | + 0x2833E84879B97091, |
| 22 | + 0xB85045B68181585D, |
| 23 | + 0x30644E72E131A029, |
| 24 | + ]); |
| 25 | + assert!(p == ScalarField::MODULUS); |
| 26 | + |
| 27 | + let a = BigInt::new([ |
| 28 | + 0x43E1F593F0000001, |
| 29 | + 0x2833E84879B97091, |
| 30 | + 0xB85045B68181585D, |
| 31 | + 0x30644E72E131A028, |
| 32 | + ]); |
| 33 | + let b = BigInt::new([ |
| 34 | + 0xAAAAAAAAF0000001, |
| 35 | + 0x2833E84879B97091, |
| 36 | + 0xB85045B68181585D, |
| 37 | + 0x30644E7200000000, |
| 38 | + ]); |
| 39 | + |
| 40 | + let device = get_default_device(); |
| 41 | + let a_buf = create_buffer(&device, &a.to_limbs(num_limbs, log_limb_size)); |
| 42 | + let b_buf = create_buffer(&device, &b.to_limbs(num_limbs, log_limb_size)); |
| 43 | + let p_buf = create_buffer(&device, &p.to_limbs(num_limbs, log_limb_size)); |
| 44 | + let result_buf = create_empty_buffer(&device, num_limbs); |
| 45 | + |
| 46 | + // Perform (a - b) % p |
| 47 | + let mut expected = a.clone(); |
| 48 | + expected.sub_with_borrow(&b); |
| 49 | + |
| 50 | + // If result is negative, add p until it's positive |
| 51 | + while expected < BigInt::zero() { |
| 52 | + expected.add_with_carry(&p); |
| 53 | + } |
| 54 | + let expected_limbs = expected.to_limbs(num_limbs, log_limb_size); |
| 55 | + |
| 56 | + let command_queue = device.new_command_queue(); |
| 57 | + let command_buffer = command_queue.new_command_buffer(); |
| 58 | + |
| 59 | + let compute_pass_descriptor = ComputePassDescriptor::new(); |
| 60 | + let encoder = command_buffer.compute_command_encoder_with_descriptor(compute_pass_descriptor); |
| 61 | + |
| 62 | + write_constants( |
| 63 | + "../mopro-msm/src/msm/metal_msm/shader", |
| 64 | + num_limbs, |
| 65 | + log_limb_size, |
| 66 | + 0, |
| 67 | + 0, |
| 68 | + ); |
| 69 | + let library_path = compile_metal( |
| 70 | + "../mopro-msm/src/msm/metal_msm/shader/field", |
| 71 | + "ff_sub.metal", |
| 72 | + ); |
| 73 | + let library = device.new_library_with_file(library_path).unwrap(); |
| 74 | + let kernel = library.get_function("run", None).unwrap(); |
| 75 | + |
| 76 | + let pipeline_state_descriptor = ComputePipelineDescriptor::new(); |
| 77 | + pipeline_state_descriptor.set_compute_function(Some(&kernel)); |
| 78 | + |
| 79 | + let pipeline_state = device |
| 80 | + .new_compute_pipeline_state_with_function( |
| 81 | + pipeline_state_descriptor.compute_function().unwrap(), |
| 82 | + ) |
| 83 | + .unwrap(); |
| 84 | + |
| 85 | + encoder.set_compute_pipeline_state(&pipeline_state); |
| 86 | + encoder.set_buffer(0, Some(&a_buf), 0); |
| 87 | + encoder.set_buffer(1, Some(&b_buf), 0); |
| 88 | + encoder.set_buffer(2, Some(&p_buf), 0); |
| 89 | + encoder.set_buffer(3, Some(&result_buf), 0); |
| 90 | + |
| 91 | + let thread_group_count = MTLSize { |
| 92 | + width: 1, |
| 93 | + height: 1, |
| 94 | + depth: 1, |
| 95 | + }; |
| 96 | + |
| 97 | + let thread_group_size = MTLSize { |
| 98 | + width: 1, |
| 99 | + height: 1, |
| 100 | + depth: 1, |
| 101 | + }; |
| 102 | + |
| 103 | + encoder.dispatch_thread_groups(thread_group_count, thread_group_size); |
| 104 | + encoder.end_encoding(); |
| 105 | + |
| 106 | + command_buffer.commit(); |
| 107 | + command_buffer.wait_until_completed(); |
| 108 | + |
| 109 | + let result_limbs: Vec<u32> = read_buffer(&result_buf, num_limbs); |
| 110 | + let result = BigInt::from_limbs(&result_limbs, log_limb_size); |
| 111 | + |
| 112 | + assert!(result == expected); |
| 113 | + assert!(result_limbs == expected_limbs); |
| 114 | +} |
0 commit comments