Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Big Number Representation and Computation Library #1

Open
7 tasks
Vilayat-Ali opened this issue Sep 21, 2024 · 0 comments
Open
7 tasks

Add Big Number Representation and Computation Library #1

Vilayat-Ali opened this issue Sep 21, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@Vilayat-Ali
Copy link
Owner

Feature Request: Implement BigNumber Representation Logic

Issue Overview

We need to add support for BigNumber representation in Oktopus to handle large numbers accurately and efficiently. Rust's standard numeric types (e.g., u64, i64, f64) have size limitations that can lead to overflows or precision loss when dealing with large numerical values. To ensure reliable handling of these numbers, especially for financial, cryptographic, or scientific calculations, we will implement custom BigNumber logic.

Motivation

  • Oktopus needs to handle extremely large numbers, which are currently beyond the capabilities of Rust's built-in data types.
  • To prevent data loss due to precision or overflow, we need a robust system to represent and manipulate these numbers.
  • Many operations such as cryptography, large financial transactions, and blockchain calculations require support for arbitrary precision.

Key Requirements

  1. Arbitrary Precision: The BigNumber should support arbitrary-length integers and optionally arbitrary precision floating-point numbers (depending on future requirements).
  2. Mathematical Operations: The BigNumber type should support all standard mathematical operations (+, -, *, /, %, etc.) and comparisons (<, >, ==, etc.).
  3. Serialization/Deserialization: The BigNumber type must be serializable and deserializable to common formats like JSON and binary, to ensure seamless integration with the rest of the Oktopus project.
  4. Performance: Since BigNumber operations can be computationally expensive, we should aim for a performant implementation, utilizing algorithms such as Karatsuba or FFT multiplication for large numbers.
  5. Error Handling: Edge cases like division by zero, underflow, overflow, etc., must be handled appropriately with meaningful error messages.

Suggested Approach

  1. Type Definition: Define a new BigNumber struct or enum that represents arbitrarily large numbers. We can initially leverage libraries like num-bigint for the core implementation, or write it from scratch if performance or customization is a priority.

    Example:

    struct BigNumber {
        int: Vec<u32>, // Integer digits stored in a vector for arbitrary precision
        frac: Vec<u32>,   // Fraction digits stored in a vector for arbitrary precision
        sign: bool,       // Sign of the number (true for positive, false for negative)

}

Mathematical Operations

Implement the Add, Sub, Mul, Div, and Rem traits for BigNumber. This will allow it to integrate seamlessly with Rust's arithmetic operators.

Example:

impl Add for BigNumber {
    type Output = BigNumber;
    
    fn add(self, other: BigNumber) -> BigNumber {
        // Addition logic here
    }
}

Serialization

Implement Serialize and Deserialize traits from the serde library to enable easy (de)serialization of BigNumbers.

Testing and Benchmarks

Write comprehensive unit tests to validate the correctness of the implementation, especially edge cases such as handling negative numbers, division by zero, and overflow scenarios. Additionally, benchmark the performance of operations on very large numbers.

Tasks

  • Design BigNumber struct with appropriate internal representation.
  • Implement basic arithmetic operations (Add, Sub, Mul, Div, Rem).
  • Implement comparison traits (PartialEq, PartialOrd, etc.).
  • Implement serialization and deserialization with serde.
  • Write unit tests for different operations and edge cases.
  • Benchmark performance and optimize for large number operations.
  • Update project documentation to include the new BigNumber type.

Additional Context

The BigNumber implementation should be flexible enough to accommodate future expansion for cryptographic purposes if needed. Existing libraries such as num-bigint can be referenced for inspiration, but we may need custom logic for more control or performance improvements.


If anyone has suggestions or would like to help, feel free to comment on this issue or submit a PR.

References

@Vilayat-Ali Vilayat-Ali added the enhancement New feature or request label Sep 21, 2024
@Vilayat-Ali Vilayat-Ali self-assigned this Sep 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant