field

package
v0.0.0-...-0a71033 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2026 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package field implements finite field arithmetic.

Example
package main

import (
	"fmt"

	"github.com/fumin/nag/field"
)

func main() {
	// This example checks the freshman's dream identity for finite fields:
	//
	//   (x + y)^p = x^p + y^p
	//
	// where p is the characteristic of the field.
	// Create the Galois field GF(11^3), and pick any two elements x and y.
	p, n := 11, 3
	irr := field.NewIrreduciblePoly(p, n)
	x, y := irr.Ext(153), irr.Ext(749)

	// Compute (x + y)^p.
	xPlusY := irr.Ext(0).Add(x, y)
	xyp := irr.Ext(1)
	for range p {
		xyp.Mul(xyp, xPlusY)
	}

	// Compute x^p + y^p.
	xp, yp := irr.Ext(1), irr.Ext(1)
	for range p {
		xp.Mul(xp, x)
		yp.Mul(yp, y)
	}
	xpPlusyp := irr.Ext(0).Add(xp, yp)

	fmt.Println("(x + y)^p == x^p + y^p")
	fmt.Println(xyp, "==", xpPlusyp)

}
Output:

(x + y)^p == x^p + y^p
655 == 655

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IrreduciblePoly

type IrreduciblePoly struct {
	*nag.Polynomial[*prime]
}

An IrreduciblePoly is an irreducible polynomial for the construction of a prime field extension.

func NewIrreduciblePoly

func NewIrreduciblePoly(p, n int) *IrreduciblePoly

NewIrreduciblePoly returns an irreducible polynomial for the finite field GF(p^n), where p is a prime number and n >= 1.

func (*IrreduciblePoly) Ext

func (irr *IrreduciblePoly) Ext(i int) *PrimeExt

Ext returns the i'th element in the finite field GF(p^n).

type PrimeExt

type PrimeExt struct {
	// contains filtered or unexported fields
}

A PrimeExt is an element in the finite field GF(p^n), where p is a prime number and n >= 1.

func (*PrimeExt) Add

func (z *PrimeExt) Add(x, y *PrimeExt) *PrimeExt

Add sets z to the sum x+y and returns z.

func (*PrimeExt) Div

func (z *PrimeExt) Div(x, y *PrimeExt) *PrimeExt

Div sets z to the quotient x/y and returns z.

func (*PrimeExt) Equal

func (x *PrimeExt) Equal(y *PrimeExt) bool

Equal reports whether x and y are equal.

func (*PrimeExt) Inv

func (z *PrimeExt) Inv(x *PrimeExt) *PrimeExt

Inv sets z to 1/x and returns z.

func (*PrimeExt) Mul

func (z *PrimeExt) Mul(x, y *PrimeExt) *PrimeExt

Mul sets z to the product x*y and returns z.

func (*PrimeExt) NewOne

func (x *PrimeExt) NewOne() *PrimeExt

NewOne returns the multiplicative identity 1.

func (*PrimeExt) NewZero

func (x *PrimeExt) NewZero() *PrimeExt

NewZero returns the additive identity 0.

func (*PrimeExt) String

func (x *PrimeExt) String() string

String returns the integer representation of x.

func (*PrimeExt) Sub

func (z *PrimeExt) Sub(x, y *PrimeExt) *PrimeExt

Sub sets z to the difference x-y and returns z.

Jump to

Keyboard shortcuts

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