Skip to content

oliveryasuna/math

Repository files navigation

math

Growing mathematics structures and components library for Java.

Abstract algebra is really cool. Right now, I have implemented group- and ring-like structures. I plan on adding more later (thinking logic operations). If the library grows large enough, I will probably split it into multiple modules.

The original goal was to provide abstractions for my crypto library, but never did so.

This is not an alternative to or replacement for commons-math.

Use Cases

Cryptography:

  • Elliptic Curve Cryptography: Define elliptic curve groups for ECDSA/ECDH (see EllipticCurveGroup)
  • RSA: Use multiplicative group modulo n for key generation and operations
  • Diffie-Hellman: Cyclic groups for key exchange protocols
  • Digital Signatures: Group-based signature schemes (Schnorr, DSA)
  • Homomorphic Encryption: Ring structures for operations on encrypted data

Number Theory:

  • Modular Arithmetic: Already have IntegersModuloNAdditiveGroup for congruence computations
  • Prime Field Operations: Field arithmetic for prime moduli
  • Chinese Remainder Theorem: Combining ring homomorphisms
  • Quadratic Residues: Group structure of squares modulo p

Coding Theory:

  • Error-Correcting Codes: Finite field arithmetic (Reed-Solomon, BCH codes)
  • Linear Codes: Vector spaces over finite fields
  • Cyclic Codes: Polynomial rings and ideals

Logic Theory:

  • Logic Operations: Boolean rings (already have BooleanRing interface)
  • Propositional Logic: Algebraic structures for logical equivalences
  • Modal Logic: Algebraic semantics for modal operators

Computer Algebra Systems:

  • Symbolic Computation: Ring operations on polynomials, matrices
  • Algebraic Simplification: Using ring axioms for expression rewriting
  • Abstract Algebra Education: Teaching tool for group/ring theory concepts

Discrete Mathematics:

  • Permutation Groups: Symmetry operations, Rubik's cube solvers (see SymmetricGroup)
  • Graph Automorphisms: Symmetry groups of graphs
  • Combinatorial Structures: Group actions on sets

Blockchain/Cryptocurrency:

  • Digital Signatures: secp256k1 elliptic curve group (Bitcoin/Ethereum)
  • Zero-Knowledge Proofs: Cyclic group operations (zk-SNARKs, Bulletproofs)
  • Multi-Party Computation: Secret sharing in finite fields

Security Protocols:

  • Key Derivation: Group operations for key stretching
  • Password-Authenticated Key Exchange: PAKE protocols using groups
  • Threshold Cryptography: Shamir secret sharing over finite fields

Example Usage

Right now, the focus is on group- and ring-like structures.

Here is a minimalistic naive implementation of the multiplicative group of integers modulo n, .

public class Zn
    extends CommonAlgebraicStructure<Zn, Zn.ZnElement, BigInteger>
    implements CommutativeGroup<Zn, Zn.ZnElement>, FiniteGroup<Zn, Zn.ZnElement> {

  public Zn(final BigInteger n) {
    super();

    this.n = n;
  }

  protected final BigInteger n;

  @Override
  public ZnElement uniformRandomElement() throws UnsupportedOperationException {
    return getElement(BigIntegerUtils.random(getN(), new SecureRandom()));
  }

  @Override
  public ZnElement getElementSafe(final BigInteger value) {
    return new ZnElement(value);
  }

  @Override
  public boolean hasElementSafe(final BigInteger value) {
    return BigIntegerUtils.isInRange(value, BigInteger.ZERO, getN()); // [0,n)
  }

  @Override
  public CommutativeGroupOperation<ZnElement> operation() {
    return new MultiplicationOperation();
  }

  @Override
  public Stream<ZnElement> elements() {
    return Stream.iterate(BigInteger.ZERO, i -> i.compareTo(getN()) < 0, BigIntegerUtils::increment)
        .map(this::getElement);
  }

  @Override
  public BigInteger elementCount() {
    return getN();
  }

  public final BigInteger getN() {
    return n;
  }

  public class ZnElement
      extends AbstractAlgebraicElement<ZnElement, Zn>
      implements CommutativeGroupElement<ZnElement, Zn>, MultiplicativeMagmaElement<ZnElement, Zn> {

    protected ZnElement(final BigInteger value) {
      super(Zn.this);

      this.value = value;
    }

    protected final BigInteger value;

    public BigInteger getValue() {
      return value;
    }

  }

  public class MultiplicationOperation implements CommutativeGroupOperation<ZnElement> {

    @Override
    public ZnElement perform(final ZnElement multiplier, final ZnElement multiplicand) {
      return getElement(multiplier.getValue().multiply(multiplicand.getValue()).mod(getN()));
    }

    @Override
    public ZnElement identity() {
      return getElement(BigInteger.ONE);
    }

    @Override
    public ZnElement inverse(final ZnElement element) {
      return getElement(element.getValue().modInverse(getN()));
    }

  }

}

About

Mathematics structures and components for Java.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Languages