Skip to content

Commit

Permalink
fix: Remove unnecessary integrity check in envoke tx api
Browse files Browse the repository at this point in the history
  • Loading branch information
datactor committed Feb 8, 2024
1 parent bf114d3 commit 08802c5
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 65 deletions.
10 changes: 0 additions & 10 deletions crates/client/rpc/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,3 @@
pub const MAX_EVENTS_KEYS: usize = 100;
/// Maximum number of events that can be fetched in a single chunk for the `get_events` RPC.
pub const MAX_EVENTS_CHUNK_SIZE: usize = 1000;

/// Constant representing the modulus
/// q = 0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001
pub(crate) const MODULUS: [u64; 4] = [0xffffffff00000001, 0x53bda402fffe5bfe, 0x3339d80809a1d805, 0x73eda753299d7d48];

/// Compute a - (b + borrow), returning the result and the new borrow.
pub(crate) const fn sbb(a: u64, b: u64, borrow: u64) -> (u64, u64) {
let ret = (a as u128).wrapping_sub((b as u128) + ((borrow >> 63) as u128));
(ret as u64, (ret >> 64) as u64)
}
7 changes: 1 addition & 6 deletions crates/client/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ use starknet_core::types::{
TransactionExecutionStatus, TransactionFinalityStatus, TransactionReceipt,
};
use starknet_core::utils::get_selector_from_name;
use utils::{is_message_valid, sign_message, verify_sign};
use utils::{sign_message, verify_sign};

use crate::constants::{MAX_EVENTS_CHUNK_SIZE, MAX_EVENTS_KEYS};
use crate::types::RpcEventFilter;
Expand Down Expand Up @@ -612,11 +612,6 @@ where
}
};

if !is_message_valid(invoke_tx_str.as_bytes()) {
log::error!("Invalid invoke transaction");
return Err(StarknetRpcApiError::InternalServerError.into());
}

let encryption_key = SequencerPoseidonEncryption::calculate_secret_key(y.as_bytes());
let (encrypted_data, nonce, _, _) = SequencerPoseidonEncryption::new().encrypt(invoke_tx_str, encryption_key);
Ok(EncryptedInvokeTransactionResult {
Expand Down
49 changes: 0 additions & 49 deletions crates/client/rpc/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,8 @@ use sp_runtime::BoundedVec;
use starknet_core::types::FieldElement;
use starknet_crypto::{get_public_key, sign, verify};

use crate::constants::{sbb, MODULUS};
use crate::StarknetRpcApiError;

/// Attempts to convert a little-endian byte representation of
/// a scalar into a `Scalar`, failing if the input is not canonical.
fn is_bytes_valid(buf: &[u8]) -> bool {
let mut chunks = buf.chunks_exact(8);
let mut s = [0u64; 4];

for (s_item, chunk) in s.iter_mut().zip(&mut chunks) {
if let Ok(b) = <[u8; 8]>::try_from(chunk) {
*s_item = u64::from_le_bytes(b);
} else {
return false;
}
}

// Checked by comparison with modular values
let (_, borrow) = sbb(s[0], MODULUS[0], 0);
let (_, borrow) = sbb(s[1], MODULUS[1], borrow);
let (_, borrow) = sbb(s[2], MODULUS[2], borrow);
let (_, borrow) = sbb(s[3], MODULUS[3], borrow);

(borrow as u8) & 1 == 1
}

/// This function is used in the context of attempting to convert a scalar
/// from its little-endian byte representation into a `Scalar` type.
/// It is utilized in the `encrypt` function to preemptively prevent failure
/// in cases where the input is not in canonical form.
/// This function checks if the provided byte array meets specific conditions
/// (e.g., being less than a certain modulus value).
pub fn is_message_valid(message_bytes: &[u8]) -> bool {
let mut message_vecs: Vec<Vec<u8>> = message_bytes.to_vec().chunks(32).map(|s| s.into()).collect();

for message_vec in message_vecs.iter_mut() {
message_vec.resize(32, 0);
let temp = &*message_vec;
let bytes: [u8; 32] = match temp.as_slice().try_into() {
Ok(bytes) => bytes,
_ => return false,
};

if !is_bytes_valid(&bytes) {
return false;
}
}

true
}

pub fn sign_message(message: String) -> Result<BoundedVec<Felt252Wrapper, MaxArraySize>, StarknetRpcApiError> {
// Generate commitment
// 1. Get sequencer private key
Expand Down

0 comments on commit 08802c5

Please sign in to comment.