Git Product home page Git Product logo

galoisfields.jl's Introduction

GaloisFields.jl - finite fields for Julia

Build Status Test coverage
Coverage Status

Introduction

This module defines types representing finite fields. It supports both fields of prime order and of prime power order.

Synopsis

The easiest way to create Galois fields is with the @GaloisField and @GaloisField! macros. Typically, you use the former for a field of prime order and the latter for a field of prime power order. In the prime power case, you pass a display name / variable name for the primitive element.

using GaloisFields

const F = @GaloisField 29     # ℤ/29ℤ
const G = @GaloisField! 27 β   # degree-3 extension of ℤ/3ℤ; multiplicatively generated by β

F(2)^29 == F(2)
β^27 == β

The exclamation mark ! is intended to convey that the macro has a side-effect: for example, in the code above, it assigns a variable called β.

The macros also accept special symbols for specifying the field. This is more difficult to type (docs) but more elegant to read:

const F = @GaloisField/29const G = @GaloisField 𝔽₂₇ β

If you want to pass your own generator for the representation of a field of order q = p^n, you can:

const F = @GaloisField! 𝔽₃ β^2 + β + 2
β^2 + β + 2 == 0

Lastly, there's also function interfaces in cases where macros are not appropriate:

const F = GaloisField(29)               # ℤ/29ℤ
const G, β = GaloisField(81, )        # degree-4 extension of ℤ/3ℤ
const G, β = GaloisField(3, 4, )      # same; avoid having to factorize 81
const F, β = GaloisField(3,  => [2, 0, 0, 2, 1]) # same; pass our own custom minimum polynomial

Fast multiplications

In some cases, we make use of Zech's logarithms for faster multiplications. By default, this happens if the order of the field is less than 2^16, if the characteristic is not 2, and if the primitive element is also a multiplicative generator. However, you can override this by calling either of

GaloisFields.enable_zech_multiplication(F)
GaloisFields.disable_zech_multiplication(F)

before doing any multiplication operation. If you call this function on a field whose primitive element is not a multiplicative generator, this will throw a warning.

Conversions

If you specify your own minimum polynomial, we make no assumptions about conversions between fields. For example, when defining

const F = @GaloisField! 𝔽₂ β^2 + β + 1
const G = @GaloisField! 𝔽₂ γ^2 + γ + 1

an operation like

G(β)

will throw an error. The mathematical reason is that the fields F and G are isomorphic, but there is two different isomorphisms. ("They are not canonically isomorphic.") To choose an identification, you can use the identify function (which is not exported by default, so we use its full path):

GaloisFields.identify=> γ^2)
GaloisFields.identify=> β^2)

This allows for conversions such as

G(β)
convert(F, γ + 1)

The inner workings of this distinction are based on the symbol names. So if you define F and G with the same symbol and minimum polynomial:

const F = @GaloisField! 𝔽₂ β^2 + β + 1
const G = @GaloisField! 𝔽₂ β^2 + β + 1

then they are just considered equal and conversions work without extra work.

Conversions for the default minimum polynomials

If you do not specify a minimum polynomial, for example by using

const F = @GaloisField! 𝔽₈₁ β
const G = @GaloisField! 𝔽₉ γ

then we use Conway polynomials. They have special compatibility relations between them, allowing conversions:

β^10 == γ

This works provided F and G have the same characteristic p. If the order of either is a power of the other, we convert into the bigger field. If not, we convert both into the field of order p^N, where N is the least common multiple of the extension degrees of F and G over ℤ/pℤ.

Constructing a tower of field extensions

In some applications of finite fields it is convenient to use extensions of already defined finite field, i. e. the extensions of the type G of power q^m over F of power q where q = p^n for some integers m, n. It is possible to construct an extension of already defined finite field:

# creating field with 29 elements
F = @GaloisField 29
# the polynomial x^2 - 2 is irreducible over F29
G = @GaloisField! F x^2 - 2
# the polynomial y^3 + 2y - 2 is irreducible over G
H = @GaloisField! G   y^3 + 2y - 2
# G is a subfield of H
# H has |G|^3 elements

Acknowledgements

This package uses Frank Lübeck's database of Conway polynomials. For security, we make a copy available over https for this package. It is downloaded as part of the install process.

galoisfields.jl's People

Contributors

keno avatar kirtsar avatar pddshk avatar tkluck avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

galoisfields.jl's Issues

TagBot trigger issue

This issue is used to trigger TagBot; feel free to unsubscribe.

If you haven't already, you should update your TagBot.yml to include issue comment triggers.
Please see this post on Discourse for instructions and more details.

If you'd like for me to do this for you, comment TagBot fix on this issue.
I'll open a PR within a few hours, please be patient!

Usage report / open discussions tab?

Hi! I finished (enough) of my use-case for the GaloisFields.jl package that I thought I'd share as thanks for your time and trouble answering my vague, peculiar questions. I hope you might even find it interesting. 😄

I published as a Pluto.jl notebook on my blog: List hash as matrices over finite fields, which explores the idea of defining the hash of a list of elements with the key feature that the hash is composable with other list hashes. The definition goes something like: hash each entry of the list, interpret each hash digest as a matrix with GF(256) elements (rejecting and retrying singular matrices), define the hash of the whole list to be reduction by matrix multiplication of the matrix hashes of all the elements.

I reference a previous post where I try to do the same thing over the ring of integers mod 256, but that doesn't work because it's very likely that random matrices over such a ring are singular, and after multiplying enough of them the list hash degenerates into the zero matrix. A patient soul on crypto stackoverflow corrected me and suggested GF(256) as an alternative, which is how I found myself here (after deciding for some reason to switch from python to Julia which I'd never used before).

As far as analyzing the security of such a construction I'm quite out of my depth. That said, I'm not aware of any prior cryptographic primitive that features associativity, which I think could open up many use cases for cryptographic security of mutable lists that have thus far been under-served.

If you so choose I'd gladly welcome any feedback or criticism, but in any case thanks for the great library and for all your help.


This post might be more appropriate as a discussion, you might consider enabling the Discussions feature on the repo here on github. Anyways, since this isn't really an issue, feel free to close whenever you like.

Display confusion between PrimeField and BinaryField

I'm not sure if this is intended or a bug, but either way it's confusing.

julia> @GaloisField ℤ/256ℤ
𝔽₂₅₆

julia> @GaloisField 𝔽₂₅₆
(𝔽₂₅₆, ##423)

The former 𝔽₂₅₆ is a PrimeField (even though 256 isn't prime?) while the latter is a BinaryField. They print the same, but behave completely differently.

Mapping to/from extension fields

I've been using an extension field, but I'm finding it very awkward to convert integers to/from the field. This is the extension field I'm using:

const G = @GaloisField! 2^8 β

I can map integers to it with exponentiation syntax, and it is output after being reduced by the characteristic polynomial as expected:

julia> β^10
β^6 + β^5 + β^4 + β^2

However this doesn't work as a general interface to map integers into the field to cover its whole range. E.g. this definition fails because there's no way to map to the 0 value:

itog(i::Int) -> β^i
itog(0) == 1
itog(1) == β
...
itog(255) == 1
itog(256) == β

This is not exactly surprising (it's exponentiation after all), it's just not very complete. I don't believe any integer put into this itog can result in 0. So I'm currently using this definition of itog:

function itog(i)
	if iszero(i)
		0*β
	else
		β^i
	end
end

This works but is... lets say not very pretty.

I also tried to use G directly, but aiui G only represents the prime order field (2 in this case) and isn't useful for obtaining instances of the extension field.


Also, how do I convert extension field elements back into integers? Say, convert β^6 + β^5 + β^4 + β^2 from above back into Int(10)? I checked through the implementation and didn't see anything obvious (to me) that provides for this, so I ended up making a crude workaround by just enumerating all the extension field elements and storing the reverse mapping in a Dict like Dict(map(x -> (β^x, x), 0:255)). (This is where I discovered that I didn't map 0 correctly so it wasn't present in this Dict and caused problems until I used my itog function.)


To me these seem to be general 'API issues'. What do you think? Am I missing something?

Use GF as matrix element type?

Hi! I'm trying to use GaloisFields as an element type in matrices, but I'm running into MethodErrors when using some of the stdlib matrix functions, some examples:

using GaloisFields
using LinearAlgebra
const F = @GaloisField/257ℤ

x = convert(Matrix{F},reshape(rand(F,4),2,2)) # random elements

det(x)   # MethodError: no method matching abs(::𝔽₂₅₇)
inv(x)   # MethodError: no method matching abs(::𝔽₂₅₇)
conj(x)  # MethodError: no method matching conj(::𝔽₂₅₇)

y = convert(Matrix{F},[1 0; 0 1]) # identity matrix elements

det(y)    # ok, returns 1
inv(y)    # MethodError: no method matching abs(::𝔽₂₅₇)
conj(y)   # MethodError: no method matching abs(::𝔽₂₅₇)

I did try the obvious defining abs:

abs(x::F) = x

But that didn't have any effect.

The stack trace of one of the abs errors is:

MethodError: no method matching abs(::𝔽₂₅₇)
Closest candidates are:
  abs(::Bool) at bool.jl:79
  abs(::Unsigned) at int.jl:169
  abs(::Signed) at int.jl:170
  ...

Stacktrace:
 [1] generic_lufact!(A::Matrix{𝔽₂₅₇}, ::Val{true}; check::Bool)
   @ LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/lu.jl:143
 [2] #lu!#134
   @ /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/lu.jl:130 [inlined]
 [3] #lu#136
   @ /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/lu.jl:273 [inlined]
 [4] det(A::Matrix{𝔽₂₅₇})
   @ LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:1560

And for conj:

MethodError: no method matching conj(::𝔽₂₅₇)
Closest candidates are:
  conj(::Union{Hermitian{T, S}, Symmetric{T, S}} where {T, S}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/symmetric.jl:368
  conj(::P) where P<:Polynomials.LaurentPolynomial at /opt/julia/packages/Polynomials/1aa8e/src/polynomials/LaurentPolynomial.jl:327
  conj(::P) where P<:Polynomials.AbstractPolynomial at /opt/julia/packages/Polynomials/1aa8e/src/common.jl:301
  ...

I must admit I'm new to both Julia and GF, so it's quite possible I'm doing something wrong. 😅

Thoughts?

Nested extension continued

  1. There are no checks that the minimal polynomial is irreducible. Is it intended?
F2 = @GaloisField 2
F4 = @GaloisField! F2 x^2
@show x^(-1)
  1. Not possible to use coefficients from field in irreducible polynomial in macro?
F2 = @GaloisField 2
F4 = @GaloisField! F2 x^2 + x + 1
# t^3 + x is irreducible over F4
@GaloisField! F4 t^3 - x
# ERROR: LoadError: Polynomials must have same variable
# However, the straight construction works well:
F64 = GaloisField(F4, :t => [1, 0, 0, x])

Update Polynomials dependency to the latest

I opened an issue in Polynomials package in order to add function to get polynomial variable as symbol.

This function with a few updates in src/GaloisFields.jl (replace poly.var with getvar(poly)) allows to update dependency to the latest Polynomials version (all tests passed).

You can simply add this function inside src/GaloisFields.jl or wait for the issue mentioned to be resolved, and update dependency.

Any plans on supporting creation finite fields over finite fields?

It is not so rare in (for instance) coding theory to have the "tower" of Galois fields, that is, we theat code as linear subspace over GF(q^m), where q = p^n for some prime n. I understand that this thing is isomorphic to some GF(p^mn), but it could be more handy to treat it as GF(q^m).

Rings of polynomials over fields

I was wondering if there is an implementation of rings over Galois Fields F[x, y ...]? It seems that SemialgebraicSets are not supposed to use with custom fields other from R or C. I know about Nemo, but this is really huge and pretty "monolithic".

Release v0.4

I'd like to release v0.4, which drops support for non-x86 architectures. This allows using dedicated instructions for characteristic-2 fields: https://github.com/tkluck/GaloisFields.jl/blob/master/src/BinaryFields.jl#L82

Another feature of v0.4 is that broadcasting now merges the ring operations (+,-,*,/) in the sense that it only does a single mod operation after applying those operations. There's some careful bookkeeping that avoids integer overflows.

@b-reinke , since you contributed to this package: is dropping x86 a problem for you? If so, let me know and we'll work around it. More generally, if you have any other comments about whether this package satisfies your needs, do let me know! Also if you decided to replace it by something else.

Thanks!

Incompatible with the built-in `@profview` of VSCode

GaloisFields seems to be incompatible with the built-in @profview of VSCode.
When I run the following simple code:

using GaloisFields
@profview println("hello")

I get the error:

ERROR: LoadError: UndefVarError: F not defined
Stacktrace:
  [1] Base.Broadcast.BroadcastStyle(T::Type{Tuple{}})
    @ GaloisFields.Broadcast ~/.julia/packages/GaloisFields/41126/src/Broadcast.jl:59
  [2] combine_styles(c::Tuple{})
    @ Base.Broadcast ./broadcast.jl:435
  [3] broadcasted(::Function, ::Tuple{})
    @ Base.Broadcast ./broadcast.jl:1298
  [4] var"@profview"(__source__::LineNumberNode, __module__::Module, ex::Any, args::Vararg{Any})
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.7.5/scripts/packages/VSCodeServer/src/profiler.jl:132
  [5] eval
    @ ./boot.jl:368 [inlined]
  [6] include_string(mapexpr::typeof(identity), mod::Module, code::String, filename::String)
    @ Base ./loading.jl:1428
  [7] include_string(m::Module, txt::String, fname::String)
    @ Base ./loading.jl:1438
  [8] invokelatest(::Any, ::Any, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Base ./essentials.jl:729
  [9] invokelatest(::Any, ::Any, ::Vararg{Any})
    @ Base ./essentials.jl:726
 [10] inlineeval(m::Module, code::String, code_line::Int64, code_column::Int64, file::String; softscope::Bool)
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.7.5/scripts/packages/VSCodeServer/src/eval.jl:233
 [11] (::VSCodeServer.var"#66#70"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})()
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.7.5/scripts/packages/VSCodeServer/src/eval.jl:157
 [12] withpath(f::VSCodeServer.var"#66#70"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams}, path::String)
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.7.5/scripts/packages/VSCodeServer/src/repl.jl:249
 [13] (::VSCodeServer.var"#65#69"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})()
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.7.5/scripts/packages/VSCodeServer/src/eval.jl:155
 [14] hideprompt(f::VSCodeServer.var"#65#69"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.7.5/scripts/packages/VSCodeServer/src/repl.jl:38
 [15] (::VSCodeServer.var"#64#68"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})()
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.7.5/scripts/packages/VSCodeServer/src/eval.jl:126
 [16] with_logstate(f::Function, logstate::Any)
    @ Base.CoreLogging ./logging.jl:511
 [17] with_logger
    @ ./logging.jl:623 [inlined]
 [18] (::VSCodeServer.var"#63#67"{VSCodeServer.ReplRunCodeRequestParams})()
    @ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.7.5/scripts/packages/VSCodeServer/src/eval.jl:225
 [19] #invokelatest#2
    @ ./essentials.jl:729 [inlined]
 [20] invokelatest(::Any)
    @ Base ./essentials.jl:726
 [21] macro expansion
    @ ~/.vscode/extensions/julialang.language-julia-1.7.5/scripts/packages/VSCodeServer/src/eval.jl:34 [inlined]
 [22] (::VSCodeServer.var"#61#62")()
    @ VSCodeServer ./task.jl:484

I'd be glad if you could tell me a workaround for this issue.

tr() doesn't work over F2^p

Julia Version 1.5.3
pkg> st
Status `~/xxx/Project.toml`
  [8d0d7f98] GaloisFields v1.0.1
julia> using GaloisFields
[ Info: Precompiling GaloisFields [8d0d7f98-d412-5cd4-8397-071c807280aa]
julia> F2 = @GaloisField 2
𝔽₂
julia> F16 = @GaloisField! 2^4 α
𝔽₁₆
julia> tr(F2, α)
fish: 'julia' terminated by signal SIGSEGV (Address boundary error)

In the REPL on Visual Studio Code, I got StackOverflowError with the same syntax described above.

llvm.x86.pclmulqdq on Julia over Apple M1

On Julia over Apple M1, I am unable to perform power operations as shown below.

julia> versioninfo()
Julia Version 1.7.2
Commit bf53498635 (2022-02-06 15:21 UTC)
Platform Info:
  OS: macOS (arm64-apple-darwin21.2.0)
  CPU: Apple M1
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, cyclone)

julia> using GaloisFields
[ Info: Precompiling GaloisFields [8d0d7f98-d412-5cd4-8397-071c807280aa]

julia> const G, α = GaloisField(2, 4, )
(𝔽₁₆, α)

julia> α^2
LLVM ERROR: Cannot select: intrinsic %llvm.x86.pclmulqdq

signal (6): Abort trap: 6
in expression starting at REPL[3]:1
__pthread_kill at /usr/lib/system/libsystem_kernel.dylib (unknown line)
Allocations: 7490130 (Pool: 7486448; Big: 3682); GC: 9
fish: Job 1, 'julia' terminated by signal SIGABRT (Abort)

There seems to be an error in the following line executing pclmulqdq in BinaryFields.jl. I would appreciate it if you could provide me with a solution.

function carrylessmul(a::m128, b::m128)
    ccall("llvm.x86.pclmulqdq", llvmcall, m128, (m128, m128, UInt8), a, b, 0)
end

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.