Selaa lähdekoodia

Fix ALT_BN128_MULTIPLICATION_INPUT_LEN constant (#3686)

* Fix ALT_BN128_MULTIPLICATION_INPUT_LEN constant

* Add feature

* Apply suggestions from code review

Co-authored-by: samkim-crypto <skim13@cs.stanford.edu>

* Add SIMD

---------

Co-authored-by: samkim-crypto <skim13@cs.stanford.edu>
Stanislav Ladyzhenskiy 10 kuukautta sitten
vanhempi
sitoutus
cb9cc49307
3 muutettua tiedostoa jossa 33 lisäystä ja 7 poistoa
  1. 14 3
      curves/bn254/src/lib.rs
  2. 14 4
      programs/bpf_loader/src/syscalls/mod.rs
  3. 5 0
      sdk/feature-set/src/lib.rs

+ 14 - 3
curves/bn254/src/lib.rs

@@ -14,7 +14,7 @@ mod consts {
     pub const ALT_BN128_ADDITION_INPUT_LEN: usize = 128;
 
     /// Input length for the multiplication operation.
-    pub const ALT_BN128_MULTIPLICATION_INPUT_LEN: usize = 128;
+    pub const ALT_BN128_MULTIPLICATION_INPUT_LEN: usize = 96;
 
     /// Pair element length.
     pub const ALT_BN128_PAIRING_ELEMENT_LEN: usize = 192;
@@ -275,12 +275,23 @@ mod target_arch {
     }
 
     pub fn alt_bn128_multiplication(input: &[u8]) -> Result<Vec<u8>, AltBn128Error> {
-        if input.len() > ALT_BN128_MULTIPLICATION_INPUT_LEN {
+        alt_bn128_apply_multiplication(input, ALT_BN128_MULTIPLICATION_INPUT_LEN)
+    }
+
+    pub fn alt_bn128_multiplication_128(input: &[u8]) -> Result<Vec<u8>, AltBn128Error> {
+        alt_bn128_apply_multiplication(input, 128) // hard-code length; we will remove this function in the future
+    }
+
+    fn alt_bn128_apply_multiplication(
+        input: &[u8],
+        expected_length: usize,
+    ) -> Result<Vec<u8>, AltBn128Error> {
+        if input.len() > expected_length {
             return Err(AltBn128Error::InvalidInputData);
         }
 
         let mut input = input.to_vec();
-        input.resize(ALT_BN128_MULTIPLICATION_INPUT_LEN, 0);
+        input.resize(expected_length, 0);
 
         let p: G1 = PodG1::from_be_bytes(&input[..64])?.try_into()?;
         let mut fr_bytes = [0u8; 32];

+ 14 - 4
programs/bpf_loader/src/syscalls/mod.rs

@@ -14,9 +14,10 @@ pub use self::{
 use {
     solana_account_info::AccountInfo,
     solana_bn254::prelude::{
-        alt_bn128_addition, alt_bn128_multiplication, alt_bn128_pairing, AltBn128Error,
-        ALT_BN128_ADDITION_OUTPUT_LEN, ALT_BN128_MULTIPLICATION_OUTPUT_LEN,
-        ALT_BN128_PAIRING_ELEMENT_LEN, ALT_BN128_PAIRING_OUTPUT_LEN,
+        alt_bn128_addition, alt_bn128_multiplication, alt_bn128_multiplication_128,
+        alt_bn128_pairing, AltBn128Error, ALT_BN128_ADDITION_OUTPUT_LEN,
+        ALT_BN128_MULTIPLICATION_OUTPUT_LEN, ALT_BN128_PAIRING_ELEMENT_LEN,
+        ALT_BN128_PAIRING_OUTPUT_LEN,
     },
     solana_compute_budget::compute_budget::ComputeBudget,
     solana_cpi::MAX_RETURN_DATA,
@@ -1728,7 +1729,16 @@ declare_builtin_function!(
 
         let calculation = match group_op {
             ALT_BN128_ADD => alt_bn128_addition,
-            ALT_BN128_MUL => alt_bn128_multiplication,
+            ALT_BN128_MUL => {
+                let fix_alt_bn128_multiplication_input_length = invoke_context
+                    .get_feature_set()
+                    .is_active(&feature_set::fix_alt_bn128_multiplication_input_length::id());
+                if fix_alt_bn128_multiplication_input_length {
+                    alt_bn128_multiplication
+                } else {
+                    alt_bn128_multiplication_128
+                }
+            }
             ALT_BN128_PAIRING => alt_bn128_pairing,
             _ => {
                 return Err(SyscallError::InvalidAttribute.into());

+ 5 - 0
sdk/feature-set/src/lib.rs

@@ -561,6 +561,10 @@ pub mod enable_alt_bn128_compression_syscall {
     solana_pubkey::declare_id!("EJJewYSddEEtSZHiqugnvhQHiWyZKjkFDQASd7oKSagn");
 }
 
+pub mod fix_alt_bn128_multiplication_input_length {
+    solana_pubkey::declare_id!("bn2puAyxUx6JUabAxYdKdJ5QHbNNmKw8dCGuGCyRrFN");
+}
+
 pub mod enable_program_redeployment_cooldown {
     solana_pubkey::declare_id!("J4HFT8usBxpcF63y46t1upYobJgChmKyZPm5uTBRg25Z");
 }
@@ -1145,6 +1149,7 @@ lazy_static! {
         (deplete_cu_meter_on_vm_failure::id(), "Deplete compute meter for vm errors SIMD-0182 #3993"),
         (reserve_minimal_cus_for_builtin_instructions::id(), "Reserve minimal CUs for builtin instructions SIMD-170 #2562"),
         (raise_block_limits_to_50m::id(), "Raise block limit to 50M SIMD-0207"),
+        (fix_alt_bn128_multiplication_input_length::id(), "fix alt_bn128 multiplication input length SIMD-0222 #3686"),
         /*************** ADD NEW FEATURES HERE ***************/
     ]
     .iter()