Astra-Num
Astra-Num is a Rust library providing an easy-to-use wrapper around [num-bigint], [num-integer], and [num-traits]. It offers additional utilities for handling incredibly large (astronomical) values, cryptographic operations (optional), and more. This library is suitable for projects that require arbitrary-precision arithmetic or advanced integer manipulation.
Table of Contents
Features
- Arbitrary-precision arithmetic based on [
num_bigint::BigInt]. - Trait implementations for [
num_traits]—allows you to use familiar methods likeZero,One,Num, andSigned. - Additional arithmetic methods:
mod_powfor efficient modular exponentiation.sqrt,sqrt_precisefor integer square roots.- Bitwise operations like AND, OR, XOR, NOT.
- Optional cryptographic utilities (enabled via the
crypto_utilsfeature) including:- Miller-Rabin primality check for probable prime testing.
- Random prime generation of a given bit size.
- Serde support for easy serialization/deserialization of
BigNumas decimal strings.
Installation
Add the following to your Cargo.toml:
[]
= "0.1.0"
If you want the cryptographic utilities (prime checking, prime generation), enable the crypto_utils feature:
[]
= { = "0.1.0", = ["crypto_utils"] }
Then import in your Rust code:
use BigNum;
Usage
Basic Example
use BigNum;
Arithmetic Operations
BigNum supports common operations through both method calls and Rust’s operator overloads:
use BigNum;
// addition: operator
let a = from;
let b = from;
let sum = a.clone + b.clone; // 125
// subtraction: method
let diff = a.sub; // 75
// multiplication: operator
let product = a * b; // 2500
// division & remainder
let quotient = from / from; // 6
let remainder = from % from;// 1
println!;
println!;
println!;
println!;
println!;
Conversion Examples
use BigNum;
use Sign;
// from decimal string
let bn_decimal = from_str.expect;
println!; // 999999999
// from/to little-endian bytes
let data = &; // example
let bn_le = from_bytes_le;
let = bn_le.to_bytes_le;
assert_eq!;
assert_eq!;
// from f64
let approximate = from_f64.unwrap;
println!; // 12345
// to f64 (returns an Option<f64>)
let maybe_float = approximate.to_f64;
if let Some = maybe_float else
Crypto Utilities (Feature: crypto_utils)
// In Cargo.toml:
// [dependencies]
// astra-num = { version = "0.1.0", features = ["crypto_utils"] }
use BigNum;
Security Notice: While these utilities are educational and convenient, cryptography requires careful review and best practices (e.g., secure random number generation, safe prime generation parameters, etc.). For production-grade cryptographic applications, consider established, audited libraries.
Documentation
Comprehensive documentation for astra-num is hosted on docs.rs.
To build it locally, run:
This command will generate and open documentation in your web browser.
Testing
I included a suite of unit tests in the lib.rs file. You can run them with:
This will:
- Build the library in test mode, and
- Execute all the tests, reporting any failures or errors.
License
This project is licensed under the MIT license. You are free to use, modify, and distribute this software, subject to the terms of the license.