ExamplesBy LevelBy TopicLearning Paths
829 Fundamental

Chinese Remainder Theorem

Functional Programming

Tutorial Video

Text description (accessibility)

This video demonstrates the "Chinese Remainder Theorem" functional Rust example. Difficulty level: Fundamental. Key concepts covered: Functional Programming. The Chinese Remainder Theorem (CRT) solves systems of simultaneous congruences: given `x ≡ a1 (mod m1)`, `x ≡ a2 (mod m2)`, ..., find x mod (m1 * m2 * ...) when the moduli are pairwise coprime. Key difference from OCaml: | Aspect | Rust | OCaml |

Tutorial

The Problem

The Chinese Remainder Theorem (CRT) solves systems of simultaneous congruences: given x ≡ a1 (mod m1), x ≡ a2 (mod m2), ..., find x mod (m1 m2 ...) when the moduli are pairwise coprime. This has applications in: cryptography (RSA-CRT speedup), number theory, calendar calculations, and distributed hashing. CRT also enables working with multiple small moduli instead of one large modulus, which is the key technique in Number Theoretic Transform (NTT) for polynomial multiplication. In competitive programming, CRT reconstructs a number from its remainders under several moduli, enabling range queries on large integers.

🎯 Learning Outcomes

  • • State the CRT: system x ≡ ai (mod mi) with pairwise coprime mi has unique solution mod M = m1m2...*mk
  • • Implement the constructive proof: for each i, compute Mi = M/mi, then yi = Mi^(-1) mod mi via extended GCD
  • • Combine: x = sum(ai * Mi * yi) mod M
  • • Handle the case of non-coprime moduli using the generalized CRT
  • • Apply CRT to RSA decryption speedup: compute separately mod p and mod q, combine with CRT
  • Code Example

    #![allow(clippy::all)]
    //! Placeholder

    Key Differences

    AspectRustOCaml
    Modular inverseExtended GCD returning Option<i64>Returns (int * int * int)
    Accumulationzip iterator loopList.fold_left2
    Overflowi128 for large product MZarith for arbitrary precision
    Error handling? operator for None propagationOption.bind chain
    Non-negative result((x % m) + m) % mSame idiom
    Return typeOption<i64>int option

    OCaml Approach

    OCaml's CRT uses List.fold_left to accumulate the sum: List.fold_left2 (fun acc a mi -> ...) 0 remainders moduli. The modular inverse uses the extended Euclidean algorithm returning int * int. OCaml's Int64 or Zarith handles large products. The Option type signals failure when moduli are not coprime. OCaml's pattern matching on the extended GCD result tuple is clean: let (g, x, _) = extended_gcd mi m_i in if g <> 1 then None else Some x.

    Full Source

    #![allow(clippy::all)]
    //! Placeholder

    Deep Comparison

    OCaml vs Rust: Chinese Remainder Theorem

    Overview

    See the example.rs and example.ml files for detailed implementations.

    Key Differences

    AspectOCamlRust
    Type systemHindley-MilnerOwnership + traits
    MemoryGCZero-cost abstractions
    MutabilityExplicit refmut keyword
    Error handlingOption/ResultResult<T, E>

    See README.md for detailed comparison.

    Exercises

  • Implement CRT for two congruences iteratively and verify on the classic example: x ≡ 2 (mod 3), x ≡ 3 (mod 5), x ≡ 2 (mod 7).
  • Extend to handle non-coprime moduli using the generalized CRT with GCD-based compatibility checking.
  • Implement RSA-CRT decryption: given ciphertext c, p, q, d, compute p-component and q-component separately, then combine.
  • Use CRT with NTT-friendly primes to multiply polynomials with large coefficients.
  • Benchmark CRT-based RSA decryption against naive modular exponentiation with the full modulus.
  • Open Source Repos