mathutils

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 3, 2026 License: MIT Imports: 2 Imported by: 0

README

Mathutils

Matrix operations and modular arithmetic utilities for classical cipher implementations.

Documentation

Overview

mathutils provides functions and structs to perform mathematical operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BinaryExponentiation

func BinaryExponentiation(number uint64, power uint64) uint64

BinaryExponentiation computes number raised to the given power using fast exponentiation.

func ExtendedGCD

func ExtendedGCD(numberA int, numberB int) (int, int, int)

ExtendedGCD returns the GCD of numberA and numberB along with the coefficients x and y such that numberA*x + numberB*y = GCD.

func GCD

func GCD(numberA int, numberB int) int

GCD returns the greatest common divisor of numberA and numberB using the Euclidean algorithm.

func IsPrime

func IsPrime(numberA int) bool

IsPrime returns true if the given number is prime, false otherwise.

func Mod

func Mod(numberA int, modulo int) (int, error)

Mod returns the result of numberA modulo modulo in the range [0, modulo).

Returns an error if modulo is less than or equal to zero.

func Mod26

func Mod26(numberA int) int

Mod26 returns the result of numberA modulo 26 in the range [0, 26).

func ModularInverse

func ModularInverse(numberA int, modulo int) (int, bool, error)

ModularInverse returns the modular inverse of numberA modulo modulo.

Returns (0, false, nil) if the inverse does not exist.

Types

type Matrix

type Matrix[T any] struct {
	Data [][]T
}

Matrix is a generic 2D matrix with elements of type T.

func MatrixInverseModuloPrime

func MatrixInverseModuloPrime(matrix *Matrix[int], moduloPrime int) (*Matrix[int], error)

MatrixInverseModuloPrime computes the modular inverse of a matrix over a prime field.

Returns an error if moduloPrime is not prime, the matrix is not square, or the matrix is not invertible.

func NewMatrixFromData

func NewMatrixFromData[T any](data [][]T) (*Matrix[T], error)

NewMatrixFromData creates a new matrix from a 2D slice.

Returns an error if data is empty, contains empty rows, or has inconsistent row lengths.

func NewMatrixZero

func NewMatrixZero[T any](rows int, columns int) (*Matrix[T], error)

NewMatrixZero creates a new matrix with all elements initialized to the zero value of T.

Returns an error if rows or columns is less than or equal to zero.

func (*Matrix[T]) Columns

func (m *Matrix[T]) Columns() int

Columns returns the number of columns in the matrix.

func (*Matrix[T]) Rows

func (m *Matrix[T]) Rows() int

Rows returns the number of rows in the matrix.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL