-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Wolfram's primitive polynomials #464
Open
iyanmv
wants to merge
21
commits into
mhostetter:release/0.3.x
Choose a base branch
from
iyanmv:iyanmv.primitive-wolfram
base: release/0.3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
91d58e8
Fix typo in `pollard_p1()` docstring
mhostetter 6195d39
Update copyright
mhostetter cadc24f
Upgrade to Sphinx Immaterial 0.11.2
mhostetter 0fdd5e8
Add `pytest-benchmark` back to `[dev]` dependencies
mhostetter 3839488
Use Google docstrings instead of NumPy docstrings
mhostetter 6f8d78f
Change max line length to 120
mhostetter 08eb3f9
Clean up docstrings
mhostetter 5e45744
Parse Google docstring "See Also" sections as if they were NumPy docs…
mhostetter 1af404f
Add `terms` kwarg to irreducible poly functions
mhostetter e2b5513
Add `terms` kwarg to primitive poly functions
mhostetter c34cfe3
Make search for polynomials of fixed-term much more efficient
mhostetter 9b07c84
Add a `terms="min"` option to irreducible/primitive poly functions
mhostetter 537ae90
Move common irreducible/primitive poly functions to `_poly.py`
mhostetter 6191415
Make search for a random irreducible/primitive polynomial much more e…
mhostetter 0c4386b
Move polynomial deterministic search into `_poly.py`
mhostetter 521171d
Add Wolfram's primitive polynomials
iyanmv 6d2a2bd
Use PrimitivePolyDatabase with terms="min"
iyanmv 41486bf
Add unit tests
iyanmv 06d0ab9
Replace pytest-timeout with a simple tick-tock
iyanmv bad87d2
Fix order when LookupError
iyanmv d3c6f6d
Fix case terms == "min" and method != "min"
iyanmv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,5 +13,8 @@ | |
"editor.codeActionsOnSave": { | ||
"source.organizeImports": true | ||
} | ||
} | ||
}, | ||
"editor.rulers": [ | ||
120 | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Copyright (c) 2020-2022 Matt Hostetter <[email protected]> | ||
Copyright (c) 2020-2023 Matt Hostetter <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
""" | ||
A script to create a database of primitive polynomials | ||
|
||
Sources: | ||
- Wolfram Research, "Primitive Polynomials" from the Wolfram Data Repository (2017): https://doi.org/10.24097/wolfram.48521.data | ||
|
||
""" | ||
from __future__ import annotations | ||
|
||
import os | ||
import sqlite3 | ||
from pathlib import Path | ||
|
||
import requests | ||
import hashlib | ||
import json | ||
|
||
|
||
def main(): | ||
""" | ||
The main routine to create a database of primitive polynomials | ||
""" | ||
|
||
database_file = Path(__file__).parent.parent / "src" / "galois" / "_databases" / "primitive_polys.db" | ||
conn, cursor = create_database(database_file) | ||
|
||
_add_wolfram_2017(conn, cursor) | ||
|
||
conn.close() | ||
|
||
|
||
def create_database(file: Path) -> tuple[sqlite3.Connection, sqlite3.Cursor]: | ||
""" | ||
Deletes the old database, makes a new one, and returns the database connection. | ||
""" | ||
if file.exists(): | ||
os.remove(file) | ||
|
||
conn = sqlite3.connect(file) | ||
cursor = conn.cursor() | ||
create_table(conn, cursor) | ||
|
||
return conn, cursor | ||
|
||
|
||
def create_table(conn: sqlite3.Connection, cursor: sqlite3.Cursor): | ||
""" | ||
Creates an empty 'polys' table. | ||
""" | ||
cursor.execute( | ||
""" | ||
CREATE TABLE polys ( | ||
characteristic INTEGER NOT NULL, | ||
degree INTEGER NOT NULL, | ||
nonzero_degrees TEXT NOT NULL, | ||
nonzero_coeffs TEXT NOT NULL, | ||
PRIMARY KEY (characteristic, degree) | ||
) | ||
""" | ||
) | ||
conn.commit() | ||
|
||
|
||
def add_to_database( | ||
cursor: sqlite3.Cursor, characteristic: int, degree: int, nonzero_degrees: str, nonzero_coeffs: str | ||
): | ||
""" | ||
Adds the given primitive polynomial to the database. | ||
""" | ||
cursor.execute( | ||
""" | ||
INSERT INTO polys (characteristic, degree, nonzero_degrees, nonzero_coeffs) | ||
VALUES (?,?,?,?) | ||
""", | ||
(characteristic, degree, nonzero_degrees, nonzero_coeffs), | ||
) | ||
|
||
|
||
def _add_wolfram_2017(conn, cursor): | ||
""" | ||
Add Wolfram's primitive polynomials to the database. | ||
Up to GF(2^1_200), GF(3^660), GF(5^430) and GF(7^358) | ||
""" | ||
url = "https://www.wolframcloud.com/objects/8a6cda66-58d7-49cf-8a1b-5d4788ff6c6e" | ||
data = requests.get(url, stream=True).content | ||
sha256 = hashlib.sha256() | ||
sha256.update(data) | ||
assert sha256.hexdigest() == "38249bff65eb06d74b9188ccbf4c29fe2becd0716588bf78d418b31463d30703" | ||
|
||
data = json.loads(data) | ||
|
||
print("Parsing Wolfram's primitive polynomials (2017)...") | ||
|
||
for entry in data[1:]: | ||
characteristic = entry[1][1] | ||
degree = entry[1][2] | ||
coeffs = entry[2][1][2] | ||
nonzero_degrees = [0] | ||
nonzero_coeffs = [coeffs[1]] | ||
for term in coeffs[2:]: | ||
if term[0] == "Power": | ||
nonzero_degrees += [term[2]] | ||
nonzero_coeffs += [1] | ||
elif term[0] == "Times": | ||
if term[2][0] == "Power": | ||
nonzero_degrees += [term[2][2]] | ||
nonzero_coeffs += [term[1]] | ||
else: # Case P(x) = n * x | ||
nonzero_degrees += [1] | ||
nonzero_coeffs += [term[1]] | ||
else: # Case P(x) = x | ||
nonzero_degrees += [1] | ||
nonzero_coeffs += [1] | ||
nonzero_degrees = str(nonzero_degrees[::-1]).replace(" ", "")[1:-1] | ||
nonzero_coeffs = str(nonzero_coeffs[::-1]).replace(" ", "")[1:-1] | ||
print(f"Irreducible polynomial for GF({characteristic}^{degree})") | ||
add_to_database(cursor, characteristic, degree, nonzero_degrees, nonzero_coeffs) | ||
|
||
conn.commit() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can just do