Repository: aptos-labs/aptos-core
File: aptos-move/framework/src/natives/cryptography/ristretto255_point.rs (Rust)

Code before:

        let (sw, a, b) = match Ord::cmp(&a, &b) {
            Ordering::Less => (false, a.0 as usize, b.0 as usize),
            Ordering::Greater => (true, b.0 as usize, a.0 as usize),
            Ordering::Equal => panic!("attempted to exclusive-borrow one element twice"),
        };

        let (left, right) = self.points.split_at_mut(a + 1);
        let (a_ref, b_ref) = (&mut left[a], &mut right[b - (a + 1)]);

        if sw {
            (b_ref, a_ref)
        } else {
            (a_ref, b_ref)
        }
    }

    /// Adds the point to the store and returns its RistrettoPointHandle ID.
    /// Aborts if the number of points has exceeded a limit.
    fn safe_add_point(&mut self, point: RistrettoPoint) -> SafeNativeResult<u64> {
        let id = self.points.len();
        if id >= NUM_POINTS_LIMIT {
            Err(SafeNativeError::abort_with_message(
                E_TOO_MANY_POINTS_CREATED,
                format!(
                    "Too many points created: current count={}, limit={}",
                    id, NUM_POINTS_LIMIT
                ),
            ))
        } else {
            self.points.push(point);
            Ok(id as u64)
        }
    }
}

//
// Partial implementation of GasParameters for point operations
//

/// If 'bytes' canonically-encode a valid RistrettoPoint, returns the point.  Otherwise, returns None.
fn decompress_maybe_non_canonical_point_bytes(
    context: &mut SafeNativeContext,
    bytes: Vec<u8>,
) -> SafeNativeResult<Option<RistrettoPoint>> {
    context.charge(RISTRETTO255_POINT_DECOMPRESS * NumArgs::one())?;

    let compressed = match compressed_point_from_bytes(bytes) {
        Some(point) => point,
        None => return Ok(None),
    };

Review comment (suggestion):
```
"Too many points created: {}. Limit is {}."
```

Code after:

        let (sw, a, b) = match Ord::cmp(&a, &b) {
            Ordering::Less => (false, a.0 as usize, b.0 as usize),
            Ordering::Greater => (true, b.0 as usize, a.0 as usize),
            Ordering::Equal => panic!("attempted to exclusive-borrow one element twice"),
        };

        let (left, right) = self.points.split_at_mut(a + 1);
        let (a_ref, b_ref) = (&mut left[a], &mut right[b - (a + 1)]);

        if sw {
            (b_ref, a_ref)
        } else {
            (a_ref, b_ref)
        }
    }

    /// Adds the point to the store and returns its RistrettoPointHandle ID.
    /// Aborts if the number of points has exceeded a limit.
    fn safe_add_point(&mut self, point: RistrettoPoint) -> SafeNativeResult<u64> {
        let id = self.points.len();
        if id >= NUM_POINTS_LIMIT {
            Err(SafeNativeError::abort_with_message(
                E_TOO_MANY_POINTS_CREATED,
                format!(
                    "Too many points created: {}, limit is {}",
                    id, NUM_POINTS_LIMIT
                ),
            ))
        } else {
            self.points.push(point);
            Ok(id as u64)
        }
    }
}

//
// Partial implementation of GasParameters for point operations
//

/// If 'bytes' canonically-encode a valid RistrettoPoint, returns the point.  Otherwise, returns None.
fn decompress_maybe_non_canonical_point_bytes(
    context: &mut SafeNativeContext,
    bytes: Vec<u8>,
) -> SafeNativeResult<Option<RistrettoPoint>> {
    context.charge(RISTRETTO255_POINT_DECOMPRESS * NumArgs::one())?;

    let compressed = match compressed_point_from_bytes(bytes) {
        Some(point) => point,
        None => return Ok(None),
    };