# diffsquare
[](https://crates.io/crates/diffsquare)
[](https://crates.io/crates/diffsquare)
[](https://docs.rs/diffsquare)
[](./LICENSE)
---
## From the Author
Created by [Abhrankan Chakrabarti](https://github.com/Abhrankan-Chakrabarti), this project implements an efficient version of Fermatโs Difference of Squares method for factoring large integers. Written in Rust, it leverages the [`malachite`](https://docs.rs/malachite/) crate for high-performance arbitrary-precision arithmetic.
---
## Recent Update โ v0.7.0
๐ **New in v0.7.0:**
* โ
**`--input <FILE>` flag**
* Allows reading newline-separated numbers from a specified file.
* Supports batch factorization of large input sets via file input.
* Fully compatible with `--json`, `--csv`, `--quiet`, `--time-only`, and `--output` modes.
* Can be combined with `--threads` to enable parallel factorization.
* โ
**Thread pool configuration for batch input**
* `--threads N` lets you control the number of Rayon threads for file or stdin input.
* Improves performance tuning for large workloads.
* ๐ **Progress bar improvements**
* Displays progress for file and stdin input when not in `--quiet`, `--json`, `--csv`, or `--time-only` mode.
* Cleaner and more informative progress display using `indicatif`.
---
## Key Features
`diffsquare` is a fast and lightweight CLI utility for factoring large integers using Fermatโs Difference of Squares method.
* Efficient Fermat's Difference of Squares factorization.
* Parallelized using [`rayon`](https://docs.rs/rayon) for faster factorization on multi-core systems (since v0.5.0).
* Optional file input for batch factorization (`--input`) (new in v0.7.0).
* Optional file output with `--output` to save results (since v0.6.0).
* Control thread count for batch processing with `--threads` (new in v0.7.0).
* Progress bar for file and stdin batch input (new in v0.7.0).
* Optional `--timeout N` to limit maximum time per factorization in milliseconds (since v0.6.1).
* CSV output support via `--csv` flag (since v0.6.1).
* JSON output mode (`--json`) for scripting and automation (since v0.3.0).
* Quiet mode (`-q`) disables prompts and hides intermediate output โ ideal for scripting.
* `--time-only` flag for showing only execution time โ useful for benchmarking (since v0.3.1).
* Batch factorization via `--stdin` (since v0.4.0).
* Support for decimal, hexadecimal, and scientific notation input.
* Command-line interface with interactive fallback.
* Optional control over iteration starting point (`--iter`) and precision (`--prec`).
* Scientific notation used in verbose mode for large integer readability.
* Execution time displayed after successful factorization.
> GitHub Repository: [`diffsquare`](https://github.com/Abhrankan-Chakrabarti/diffsquare)
---
## Installation
Install via Cargo:
```bash
cargo install diffsquare
```
Ensure `$HOME/.cargo/bin` is in your `PATH`:
```bash
export PATH="$HOME/.cargo/bin:$PATH"
```
---
## Build Instructions
To build from source manually:
```bash
git clone https://github.com/Abhrankan-Chakrabarti/diffsquare.git
cd diffsquare
cargo build --release
```
---
## ๐ง Usage Examples
Run `diffsquare` interactively or use flags for automation:
```bash
# ๐น Fully interactive: prompts for modulus
diffsquare
# ๐น Provide a decimal modulus
diffsquare -n 17976931348623159077293051907890247336179769789423065727343008115
# ๐น Provide a hexadecimal modulus
diffsquare -n 0xDEADBEEFCAFEBABE1234567890
# ๐น Specify starting iteration
diffsquare -n 0xC0FFEE123456789 -i 1000000
# ๐น Use custom precision for verbose scientific output
diffsquare -n 0xABCD1234 -p 30
# ๐น Quiet mode, suppress intermediate output
diffsquare -n 0xCAFED00D1234 -q
# ๐น JSON output for scripting
diffsquare -n 0xC0FFEE123456789 --json
# ๐น Show only execution time (for benchmarking)
diffsquare -n 0xCAFED00D1234 --time-only
# ๐น Batch factorization from stdin
echo -e "2761929023323646159\n3189046231347719467" | diffsquare --stdin
# ๐น Batch factorization from file input
diffsquare --input numbers.txt
# ๐น Batch with threads and JSON output
diffsquare --input numbers.txt --threads 4 --json
# ๐น Batch with CSV output and save to file
diffsquare --input numbers.txt --csv --output results.csv
```
---
### Command-Line Flags
| `-n` | `--mod` | Number to factor (supports `0x` for hex or scientific notation) |
| `-i` | `--iter` | Starting iteration value |
| `-p` | `--prec` | Precision for verbose scientific output |
| `-q` | `--quiet` | Suppress prompts and intermediate output |
| | `--json` | Output result in JSON format |
| | `--csv` | Output result as CSV |
| | `--time-only` | Show only execution time |
| | `--stdin` | Read newline-separated input from stdin |
| | `--input` | Read newline-separated input from file |
| | `--threads` | Number of threads to use (default: 1) |
| | `--output` | Output results to file |
| | `--timeout` | Timeout in milliseconds for each factorization |
| `-h` | `--help` | Show usage help |
| `-v` | `--version` | Show version |
---
## Tags
**Tags**
`#RustLang` `#NumberTheory` `#OpenSource` `#BigInteger` `#Cryptography` `#Fermat` `#AbhrankanChakrabarti` `#Malachite`