moma 0.1.9

Moving Origin Modular Arithmetic (MOMA), a library for modeling complex systems
Documentation

MOMA: Moving Origin Modular Arithmetic

MOMA is a Rust framework for exploring number theory and cryptography through the lens of Moving Origin Modular Arithmetic.

The crate is designed for researchers and developers working in number theory, cryptography, and algorithmic data analysis who are interested in a novel, relational framework for analyzing the properties of integers.


The Core Idea: A Barycenter for Numbers

The inspiration for this crate comes from the concept of a barycenter in astrophysics. Just as the Earth and Moon orbit a common center of mass that is not the exact center of the Earth, MOMA treats modular arithmetic as a system where the "zero point" or "origin" is not fixed.

This origin shifts dynamically based on a contextual value—typically a prime number p—and the chosen OriginStrategy. This provides a novel relational framework for analyzing the properties of integers, much like understanding the true orbital mechanics of our solar system.

The original inspiration came from this NASA article: What Is a Barycenter?


Core Concepts

The MOMA framework is built on a few simple but powerful concepts:

  • MomaRing: The primary object for all calculations. A ring is defined by a modulus and a chosen OriginStrategy.

  • OriginStrategy: A trait that defines how the origin moves. This makes the framework highly extensible.

  • Analysis Tools: A suite of modular tools for deeper analysis:

    • MassField: Maps primes to the "composite mass" in the gap immediately following them.
    • OriginDrift: Measures the volatility or "drift" of MOMA signatures for any given strategy.
    • CompositeInfluence: Models the "gravitational" influence of nearby composite numbers.
    • GoldbachProjector: An efficient tool for finding prime pairs for even numbers.
    • Entropy: A generic calculator for the Shannon entropy of a sequence.

Features

  • Flexible Core: A powerful and extensible system based on the MomaRing and OriginStrategy trait.
  • Advanced Analysis Tools: A suite of high-level structs for statistical and number-theoretic analysis.
  • Cryptographic Primitives: Demonstrates how MOMA can be used to build components like a Key Derivation Function.
  • Prime Number Utilities: A helper primes module for primality testing and prime generation.
  • Pure Rust: Built with safe, idiomatic Rust.

Installation

Add MOMA to your Cargo.toml:

[dependencies]

moma = "0.1.9" # Replace with the latest version


Quick Start

The easiest way to get started is to create a MomaRing and calculate the "signature" of a prime.

use moma::core::MomaRing;
use moma::primes;
use moma::strategy;

// 1. Create a MOMA ring with modulus 37 and the PrimeGap strategy.
let ring = MomaRing::new(37, strategy::PrimeGap);

// 2. Let's analyze the prime p = 29.
let p = 29;

// 3. Calculate the MOMA signature.
//    The origin for p=29 is (29 - 23) = 6.
//    The value is p + p_prev = 29 + 23 = 52.
//    The residue is (52 + 6) % 37 = 21.
let signature = ring.signature(p);

println!("For p={}, the MOMA signature is: {}", p, signature);
assert_eq!(signature, 21);

Exploring Further

MOMA is more than just a simple calculator; it's a toolkit for exploration.

Example 1: Finding Goldbach Pairs

Use the GoldbachProjector to efficiently find prime pairs for an even number.

use moma::goldbach::GoldbachProjector;

// Create a projector with a database of primes up to 1000.
let projector = GoldbachProjector::new(1000);

// Find all pairs for the number 96.
let pairs = projector.project(96);

println!("Found {} Goldbach pairs for 96.", pairs.len());
// Example pair: (7, 89)
assert!(pairs.contains(&(7, 89)));

Example 2: Measuring Strategy Volatility

Use OriginDrift to compare the stability of different OriginStrategy implementations.

use moma::origin_drift::OriginDrift;
use moma::strategy;
use moma::primes;

// Create a drift analyzer for the PrimeGap strategy.
let mut drift_analyzer = OriginDrift::new(100, strategy::PrimeGap);

// Feed it a sequence of primes.
let mut p = 3;
for _ in 0..10 {
    drift_analyzer.next(p);
    p = primes::next_prime(p);
}

// A higher drift magnitude means the strategy is more volatile.
println!(
    "Drift magnitude for PrimeGap strategy: {:.2}",
    drift_analyzer.drift_magnitude()
);

Example 3: Analyzing Composite Mass

Use MassField to analyze the distribution of composite matter between primes.

use moma::massfield::MassField;

// Create a field to analyze the range from 1 to 50.
let field = MassField::new(1, 50);
let mass_map = field.generate_mass_map();

// For p=13, p_next=17. Composites are 14, 15, 16.
// Mass = mass(14) + mass(15) + mass(16) = 2 + 2 + 4 = 8.
if let Some((prime, mass)) = mass_map.iter().find(|(p, _)| *p == 13) {
    println!("The composite mass after prime {} is {}", prime, mass);
    assert_eq!(*mass, 8);
}

Example 4: Measuring Strategy Volatility

Use OriginDrift to compare the stability of different OriginStrategy implementations.

use moma::origin_drift::OriginDrift;
use moma::strategy;
use moma::primes

// Create a drift analyzer for the PrimeGap strategy.
let mut drift_analyzer = OriginDrift::new(100, strategy::PrimeGap);

// Feed it a sequence of primes.
let mut p = 3;
for _ in 0..10 {
    drift_analyzer.next(p);
    p = primes::next_prime(p);
}

// A higher drift magnitude means the strategy is more volatile.
println!(
    "Drift magnitude for PrimeGap strategy: {:.2}",
    drift_analyzer.drift_magnitude()
);

Contributing

Contributions are welcome! If you have an idea for a new OriginStrategy, an analysis tool, or find a bug, please feel free to open an issue or submit a pull request.

License

This project is licensed under either of:

at your option.