Skip to main content

flagrs

This is an experimental project parallel to my PyNTL project where I attempt to reimplement parts from my flagmining library as CPython extension code in Rust.

Is designed to be fast. Uses the rug library as a lightweight wrapper around GMP.

Due to some lacking functionality in rust-cpython==0.6.0 it depends on my own fork of said project, so you probably won't be able to compile it.

ZZ : ℤ

Normal Integer Operations

x+y -> n, x-y -> n, x*y -> n

Normal addition, subtraction, and multiplication.

x//y -> q, x%y -> r, divmod(x,y) -> (q,r)

Integers act Euclidean.

x**e, pow(x,e)

Normal exponentiation. e must be a natural number (i.e. non-negative integer).

bool(x), x.__bool__() -> bool

Numbers are true if they're non-zero.

n.sqrt() -> (w,r)

Floored integer square root with remainder. w*w + r == n.

n.root(d) -> (w,r)

Floored integer dth root with remainder. w**d + r == n.

-x

Negation.

abs(x)

Absolute value.

x.sign() -> s

The sign (also called signum) of x (-1, 0, or 1).

Bitwise Operations

x|y, x&y, x^y

Bitwise OR, AND, and XOR.

~x

Bitwise negation, acting as if the integer had infinite width. Equivalent to -x-1.

x<<i, x>>i

Bit shifts (can be negative).

n.nbits() -> c

Number of bits needed to represent the absolute value of n.

A synonym for n.bit_length() or len(n).

n.weight() -> c

Number of bits set in the absolute value of n.

n.truncate(bits, [signed=False])

Truncate n to the given number of bits. Negative numbers are treated as if they're in two's-complement form for the given bit width.

If signed is True the resulting bits will be re-interpreted as a signed value and so the result might be negative.

n.next_bit() -> b

Next power-of-two bigger than n.

list(n) -> [bool...].

Integers also function implicitly as a list of bits, from least significant to most significant.

>>> list(ZZ(256+16+1))
[True, False, False, False, True, False, False, False, True]

n[i] -> bool

Checks bit i (0-indexed).

n[i:j] -> v

Returns a number with the bits set in the slice.

Morally equivalent to (n>>i) % (1<<j-i) but supports full slice syntax, including negative numbers.

TODO: rust-cpython doesn't have PySlice objects!

Representation

str(x) -> str, x.__repr__()

Number in base-10 as a text string.

x.nbytes() -> l

Number of bytes needed to represent the number. For positive numbers this is equivalent to (x.nbits() + 7)//8. Negative numbers might require an extra bit (see x.bytes()).

x.bytes([order='big']) -> bytes

Interprets the number as base-256 and returns the digits as a bytestring.

Negative numbers are treated as if they're in two's complement representation of the minimum bit width that will successfully represent them, so -128 gives b'\x80' and -129 gives b'\xff\x7f'.

x.digits(base) -> [d...]

Yields a list of digits in base base. The base can be negative, but must have a magnitude of 2 or more.

Modular Arithmetic

n.inv_mod(m) -> r

Returns $1/n \pmod m$.

pow(x,e,m)

Exponentiation under a modulus. e can be negative if gcd(x,m) == 1.

a.kronecker(n) -> k

Kronecker symbol $$(\frac{a}{n})$$. For primes it corresponds to the Legendre symbol.

m.M -> <ZMod type>

Access to the modular numbers using the given number as modulus. See below for ZMod.

>>> F = ZZ(13).M
>>> F(3) * 5
(2)

Factors

n.gcd(m...) -> g

Returns the GCD of n with all arguments.

n.egcd(m) -> (g,x,y)

Extended GCD yielding Bézout coefficients. x*n + m*y == g.

n.lcm(m...) -> m

Returns the LCM of n with all arguments.

n.is_prime([reps=25]) -> bool

Trivial divisors, then Baille-PSW, then $(reps-24)$ rounds of Miller-Rabin.

n.next_prime() -> p

Returns the next prime larger than n.

n.make_odd() -> (q,e)

Returns the odd part and exponent of 2 in n. 2**e * q == n

n.small_factors([upto=0x100000]) -> (q,[(p,e)...])

Factors out all primes smaller than upto.

Returns the remaining factor q and a list of primes and their multiplicity.

x.factor_pollard(upto)

...

x.factor_fermat(s, e)

...

ZMod : ℤ_n

Represents the residue ring (or field) of integers under a given modulus. The normal arithmetic works as expected. In cases where a field is required for the operation to be well-defined, the code will simply assume the user knows what they're doing and operate under the assumption that it is (i.e. that the modulus is prime).

x/y -> z

Normal division works as expected:

>>> F = ZMod(17)
>>> F(5)/2
(11)
>>> F(11)*2
(5)

x**e -> y

Exponentiation also works as expected:

>>> ZMod(17179)(2)**0xdeadcafe
(14537)

p.EC(a,b) -> <EC type>

Access to the additive group of an Elliptic Curve using this modulus.

EC : Ellipic Curves

Release files for flagrs 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for flagrs 0.1.4
File Interpreter ABI Platform
flagrs-0.1.4-cp39-cp39-manylinux_2_24_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.24+ x86-64 Details
flagrs-0.1.4-cp38-cp38-manylinux_2_24_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.24+ x86-64 Details
flagrs-0.1.4-cp37-cp37m-manylinux_2_24_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.24+ x86-64 Details

Total release size: 1.2 MB

Release files / flagrs-0.1.4-cp39-cp39-manylinux_2_24_x86_64.whl

Download URL flagrs-0.1.4-cp39-cp39-manylinux_2_24_x86_64.whl
Size 414.9 kB
Tags CPython 3.9 Linux glibc 2.24+ x86-64
SHA-256 checksum
How to use checksums
959e34f890bae63624880b94dd62a12afc68070d155c71ea5ec07b72419b77d7
BLAKE2b-256 checksum
How to use checksums
0b66ed51494a0b0e68f1b6ffaf59cc83755f936f0d7ca836c454a4dd59a19c27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.11.3

Release files / flagrs-0.1.4-cp38-cp38-manylinux_2_24_x86_64.whl

Download URL flagrs-0.1.4-cp38-cp38-manylinux_2_24_x86_64.whl
Size 414.8 kB
Tags CPython 3.8 Linux glibc 2.24+ x86-64
SHA-256 checksum
How to use checksums
001550bd5e2be348b238eb9be733f1a03598e33fdd5dc2defbcad887c0332dda
BLAKE2b-256 checksum
How to use checksums
d838fe6ba7e35e1ced27a7906af166c3a6066dd8bc4c0f2d0e7c346796259c0b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.11.3

Release files / flagrs-0.1.4-cp37-cp37m-manylinux_2_24_x86_64.whl

Download URL flagrs-0.1.4-cp37-cp37m-manylinux_2_24_x86_64.whl
Size 414.8 kB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.24+ x86-64
SHA-256 checksum
How to use checksums
7be1530e6597233aac6c9b37d7ea5520f068902b1157681e7492c13605aa504e
BLAKE2b-256 checksum
How to use checksums
610a790e6adf47aac99f322b96881f29cb929015a3b43ea13635716eaa22e734
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/0.11.3

Release history Release notifications | RSS feed

This release

0.1.4 This release

3 release files

0.1.2

3 release files

0.1.0

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page