chiark / gitweb /
catacomb-python
4 years ago@@@ py_buffer/freebin wip mdw/freebin
Mark Wooding [Sat, 25 Apr 2020 12:55:09 +0000 (13:55 +0100)]
@@@ py_buffer/freebin wip

4 years agoalgorithms.c (gprp_pynew): Don't double-bump the new object's refcount.
Mark Wooding [Sat, 25 Apr 2020 12:54:16 +0000 (13:54 +0100)]
algorithms.c (gprp_pynew): Don't double-bump the new object's refcount.

4 years agoalgorithms.c: Add bindings for STROBE. mdw/python3
Mark Wooding [Tue, 26 Nov 2019 22:23:10 +0000 (22:23 +0000)]
algorithms.c: Add bindings for STROBE.

4 years agot/t-algorithms.py (HashBufferTestMixin): Assemble method names dynamically.
Mark Wooding [Tue, 26 Nov 2019 22:23:50 +0000 (22:23 +0000)]
t/t-algorithms.py (HashBufferTestMixin): Assemble method names dynamically.

Not everything I want to test with this machinery calls its methods
`hashXXX'.

4 years agoSplit 'pyke/' into commit '6c2569879c220eeac35957cdba9a043e8a5ea9ed'
Mark Wooding [Sat, 11 Apr 2020 11:56:31 +0000 (12:56 +0100)]
Split 'pyke/' into commit '6c2569879c220eeac35957cdba9a043e8a5ea9ed'

git-subtree-dir: pyke
git-subtree-mainline: df0bd0f9f9230d0248eaecc3bc6595183a0b9453
git-subtree-split: 6c2569879c220eeac35957cdba9a043e8a5ea9ed

* commit '6c2569879c220eeac35957cdba9a043e8a5ea9ed':
  pyke/pyke-mLib.c: Raise `OverflowError' on out-of-range inputs.

4 years agoalgorithms.c: Add a Keccak `set' method now there's upstream support.
Mark Wooding [Mon, 14 Oct 2019 00:01:05 +0000 (01:01 +0100)]
algorithms.c: Add a Keccak `set' method now there's upstream support.

Bump dependency on Catacomb.

4 years agomp.c: Accept arbitrary iterables as inputs to `MPCRT'.
Mark Wooding [Mon, 25 Nov 2019 13:30:44 +0000 (13:30 +0000)]
mp.c: Accept arbitrary iterables as inputs to `MPCRT'.

Both the constructor and the `solve' method.

4 years agoec.c (ecptxl_1): Accept any iterable of coordinates.
Mark Wooding [Mon, 25 Nov 2019 13:29:39 +0000 (13:29 +0000)]
ec.c (ecptxl_1): Accept any iterable of coordinates.

4 years agocatacomb.c (mexp_common): Accept an arbitrary iterable.
Mark Wooding [Mon, 25 Nov 2019 13:19:45 +0000 (13:19 +0000)]
catacomb.c (mexp_common): Accept an arbitrary iterable.

This means that any iterable producing either (BASE, EXP) pair objects
or alternating BASE/EXP pairs can be used as input to any of the
multiple-exponentiation functions.  Of course, expecting an object with
unpredictable iteration order to produce a useful sequence of BASE/EXP
pairs will probably cause disappointment, but, for example, a dictionary
mapping bases to exponents seems like a reasonable thing to maintain.

4 years agocatacomb.c, ec.c, group.c, mp.c: Count base/exponent pairs with `size_t'.
Mark Wooding [Fri, 18 Oct 2019 20:24:23 +0000 (21:24 +0100)]
catacomb.c, ec.c, group.c, mp.c: Count base/exponent pairs with `size_t'.

Admittedly, if you get even slightly close to the limits of an `int',
the calculation will take an infinite amount of time.

4 years agorand.c: Implement the `passes' property of the `DSARand' generator.
Mark Wooding [Mon, 25 Nov 2019 13:07:34 +0000 (13:07 +0000)]
rand.c: Implement the `passes' property of the `DSARand' generator.

This has always been present in the C library, but wasn't surfaced in
Python, which seems like a poor show.

4 years agopyke/pyke-mLib.c: Raise `OverflowError' on out-of-range inputs.
Mark Wooding [Sat, 23 Nov 2019 15:05:39 +0000 (15:05 +0000)]
pyke/pyke-mLib.c: Raise `OverflowError' on out-of-range inputs.

This seems to match better what Python does under these conditions.

4 years agorand.c: Make nonce/IV arguments to cipher-based random generators optional.
Mark Wooding [Mon, 25 Nov 2019 13:04:35 +0000 (13:04 +0000)]
rand.c: Make nonce/IV arguments to cipher-based random generators optional.

In all cases, this defaults to zero.

4 years agocatacomb/__init__.py: Rewrite `kcdsaprime' to return the correct length.
Mark Wooding [Mon, 25 Nov 2019 13:12:43 +0000 (13:12 +0000)]
catacomb/__init__.py: Rewrite `kcdsaprime' to return the correct length.

The old version wouldn't return numbers of the correct length reliably.
This new one, written to follow Catacomb's own algorithm, will.

4 years agopgen.c: Add a binding for Catacomb's `primeiter'.
Mark Wooding [Mon, 25 Nov 2019 12:57:55 +0000 (12:57 +0000)]
pgen.c: Add a binding for Catacomb's `primeiter'.

4 years agopgen.c: Delete pointless global capture of the null event handler.
Mark Wooding [Thu, 28 Nov 2019 18:34:01 +0000 (18:34 +0000)]
pgen.c: Delete pointless global capture of the null event handler.

I think this must have been ancient debugging code that never got
removed.

4 years agomp.c: Simplify final cleanup in `MPCRT' operations.
Mark Wooding [Mon, 25 Nov 2019 13:25:34 +0000 (13:25 +0000)]
mp.c: Simplify final cleanup in `MPCRT' operations.

4 years agomp.c: Fix overly ambitious section header.
Mark Wooding [Sun, 20 Oct 2019 15:14:18 +0000 (16:14 +0100)]
mp.c: Fix overly ambitious section header.

4 years agomp.c: Accept `0x', `0o' and `0b' prefixes on strings with explicit base.
Mark Wooding [Mon, 25 Nov 2019 12:49:34 +0000 (12:49 +0000)]
mp.c: Accept `0x', `0o' and `0b' prefixes on strings with explicit base.

Python itself does this, so it seems like a good idea.

4 years agokey.c: Make the `KeyFile' key-lookup methods behave consistently.
Mark Wooding [Mon, 25 Nov 2019 12:00:52 +0000 (12:00 +0000)]
key.c: Make the `KeyFile' key-lookup methods behave consistently.

Annoyingly, `byid' and `bytag' would raise `KERR_NOTFOUND', while
`bytype' would give you `None'.  Resolve this: add a `fail' keyword
argument to each method, which defaults to true: if `fail' is true then
a failed lookup raises `KERR_NOTFOUND'; if false, then you get `None'.

4 years agokey.c: Complain about duplicate subkeys passed to `KeyDataStructured'.
Mark Wooding [Mon, 25 Nov 2019 11:54:12 +0000 (11:54 +0000)]
key.c: Complain about duplicate subkeys passed to `KeyDataStructured'.

This wasn't previously possible (without complicated subterfuge), but
now a keyword argument can duplicate a mapping set in the positional
argument.

4 years agokey.c: Populate `KeyDataStructured' from the keywords.
Mark Wooding [Mon, 25 Nov 2019 11:51:44 +0000 (11:51 +0000)]
key.c: Populate `KeyDataStructured' from the keywords.

Process the keyword arguments as additional mappings to install in the
new object.  In particular, `subkeys' is not special any more.

4 years agokey.c: Factor out populating a `KeyDataStructured' from a mapping.
Mark Wooding [Mon, 25 Nov 2019 11:48:39 +0000 (11:48 +0000)]
key.c: Factor out populating a `KeyDataStructured' from a mapping.

Introduce a new function `populate_struct' which does the gruntwork of
iterating over a given mapping and setting the corresponding labels in a
`key_data' object.

No functional change.

4 years agokey.c: Make `KeyDataStructured' constructor cleanup less bizarre.
Mark Wooding [Mon, 25 Nov 2019 11:44:52 +0000 (11:44 +0000)]
key.c: Make `KeyDataStructured' constructor cleanup less bizarre.

Drop the reference to `kd' if it's non-null, rather than inspecting the
apparently unrelated `me'.

4 years agoalgorithms.c: Use `RETURN_ME' rather than doing the job by hand.
Mark Wooding [Sun, 24 Nov 2019 23:08:14 +0000 (23:08 +0000)]
algorithms.c: Use `RETURN_ME' rather than doing the job by hand.

4 years ago*.c: Delete some pointless refcount twiddling.
Mark Wooding [Sun, 24 Nov 2019 23:07:59 +0000 (23:07 +0000)]
*.c: Delete some pointless refcount twiddling.

4 years agoec.c, catacomb/__init__.py: Return tail from `parse'; provide `fromstring'.
Mark Wooding [Sun, 24 Nov 2019 22:50:56 +0000 (22:50 +0000)]
ec.c, catacomb/__init__.py: Return tail from `parse'; provide `fromstring'.

`ECCurve.parse' was inconsistent with the established nomenclature.
Generally, `parse' reads a prefix of some given string, returning the
denoted value and the unprocessed remainder of the input string;
`fromstring' does the same, except that it returns only the value, and
raises an exception if the remainder of the string is not only
whitespace.

Fix `ECCurve.parse' (which unfortunately is an incompatible change), and
add missing `fromstring' methods to `ECCurve' and `ECInfo'.

4 years agoec.c: Accept and discard parentheses around a point when parsing.
Mark Wooding [Sun, 24 Nov 2019 22:46:33 +0000 (22:46 +0000)]
ec.c: Accept and discard parentheses around a point when parsing.

The `str' function returns parentheses around the coordinates, so it
seems unfair to reject parentheses on input.

4 years agoec.c (ecptxl_1): Preparatory reformatting.
Mark Wooding [Sun, 24 Nov 2019 22:45:14 +0000 (22:45 +0000)]
ec.c (ecptxl_1): Preparatory reformatting.

Gentle vertical compression.

4 years agoec.c: Dispatch to `ecptxl_3' whether or not we have a curve.
Mark Wooding [Sun, 24 Nov 2019 22:42:10 +0000 (22:42 +0000)]
ec.c: Dispatch to `ecptxl_3' whether or not we have a curve.

The `ecptxl_3' function has a better error message in the case where
there is no curve.  Indeed, `want sequence of two or three items' is
downright unfair in the case where you did in fact supply a sequence of
three items, and the real problem is that you're trying to construct a
curveless point.

4 years agoec.c: Reject strings with trailing junk in the curve-point constructor.
Mark Wooding [Sun, 24 Nov 2019 21:54:44 +0000 (21:54 +0000)]
ec.c: Reject strings with trailing junk in the curve-point constructor.

4 years agoec.c: Accept field elements in the fast path from `samep' fields.
Mark Wooding [Sun, 24 Nov 2019 21:45:37 +0000 (21:45 +0000)]
ec.c: Accept field elements in the fast path from `samep' fields.

... as well as from the same field object.

4 years agocatacomb/__init__.py, mp.c: Use implicit-conversion rules for rationals.
Mark Wooding [Sun, 24 Nov 2019 00:50:18 +0000 (00:50 +0000)]
catacomb/__init__.py, mp.c: Use implicit-conversion rules for rationals.

Export the implicit conversions for `MP' and `GF' to Python, and use
them in the `BaseRat' constructor.

4 years agoec.c, field.c, group.c: Allow exponents and scalars from prime fields.
Mark Wooding [Sun, 24 Nov 2019 00:41:52 +0000 (00:41 +0000)]
ec.c, field.c, group.c: Allow exponents and scalars from prime fields.

The idea is that the unit group of a finite field, and the point group
of an elliptic curve, can usefully be viewed as modules over a suitable
integer quotient ring; and, even more so, prime-order subgroups of these
can be considered as one-dimensional vector spaces over prime-order
fields.  It's therefore helpful to allow elements of the appropriate
field as exponents or scalar multipliers as appropriate.  Unfortunately
it's annoying to check whether we have the /right/ scalar field; we
could check, but that does a lot of computation for little benefit.  So
allow any prime-field element in these positions.

I don't (currently) allow a prime-field element as the exponent in `pow'
applied to `MP' or `GF'; I'm open to representation on whether this is a
good idea.  They are permitted in the exponentiation functions attached
to the various modular-reduction classes, because the exponent operand
in those cases is subject to explicit conversion.

4 years agomp.c: Tighten up the `MP' and `GF' implicit conversions.
Mark Wooding [Sun, 24 Nov 2019 00:28:14 +0000 (00:28 +0000)]
mp.c: Tighten up the `MP' and `GF' implicit conversions.

Allow implicit conversions to `MP' from `PrimeFilter' (for old time's
sake), and anything /other than/ field elements and polynomials which
implements the `__index__' implicit conversion to integer.

Don't allow implicit conversions to `GF' at all.

4 years agomp.c: Use implicit-conversion rules in `coerce'.
Mark Wooding [Sun, 24 Nov 2019 00:24:57 +0000 (00:24 +0000)]
mp.c: Use implicit-conversion rules in `coerce'.

Coercion is only used (in `Py_TPFLAGS_CHECKTYPES' classes) in the three-
way comparison function; but that's still relevant in Python 2.

4 years agomp.c: Convert shift amounts via `index' to prevent `float' and `str'.
Mark Wooding [Sun, 24 Nov 2019 00:21:44 +0000 (00:21 +0000)]
mp.c: Convert shift amounts via `index' to prevent `float' and `str'.

It obviously makes no sense to calculate `MP(4) << "4"' or `MP(9) >>
2.8'.

4 years agoec.c, field.c, group.c, mp.c: Fix conversions in `pow' and scalar mul.
Mark Wooding [Sun, 24 Nov 2019 00:18:43 +0000 (00:18 +0000)]
ec.c, field.c, group.c, mp.c: Fix conversions in `pow' and scalar mul.

The base and modulus (if present) should belong to the base ring; the
exponent or scalar should be an integer; and we should use implicit-
conversion rules for all three.  (Of course, the modulus can also be
`None'.)

This tightening temporarily breaks a test multiplying an elliptic curve
point by a prime-field element Q n.  That used to work, but not for any
especially principled reason; and this wasn't commutative (n Q would
multiply n by the x-coordinate of Q coerced into the scalar field).  We
shall fix this soon.

4 years agofield.c: Replace `tofe' by `implicitfe', calling `implicit{mp,gf}'.
Mark Wooding [Sat, 23 Nov 2019 23:48:52 +0000 (23:48 +0000)]
field.c: Replace `tofe' by `implicitfe', calling `implicit{mp,gf}'.

If something converts to an integer, we allow converting it to a
prime-field element too; similarly, things that convert to polynomials
can convert to binary-field elements.  (Obviously, these conversions use
the canonical homomorphisms.)

4 years agocatacomb/__init__.py: Prepare rational classes for upcoming changes.
Mark Wooding [Sat, 23 Nov 2019 23:39:21 +0000 (23:39 +0000)]
catacomb/__init__.py: Prepare rational classes for upcoming changes.

In the near future, we won't be able to guarantee that all rings allow
implicit conversions of (integer) 0 and 1 to the additive and
multiplicative ring identities, so:

  * define `ZERO' and `ONE' class attributes on the concrete classes to
    hold the respective ring identities;

  * make the `_split_rat' function into a private method of the
    `BaseRat' class and adjust callers to match;

  * compare against `ZERO' to detect an exact element of the base ring;
    and

  * return `ONE' as the implicit denominator for a base-ring element or
    foreign object to be converted.

No functional change.

4 years agomp.c: Export `implicitmp' and `implicitgf'.
Mark Wooding [Sat, 23 Nov 2019 22:38:04 +0000 (22:38 +0000)]
mp.c: Export `implicitmp' and `implicitgf'.

The next few commits are going to be about shoring up implicit
conversions in generic arithmetic operators, and, in particular, in
forbidding obviously silly conversions, e.g., from floating-point
numbers of strings to integers or polynomials.  This is a first step
towards that.

No new callers yet, and no functional change.

4 years agoalgorithms.c: Hide the `_Poly1305Class' type a bit better.
Mark Wooding [Sun, 13 Oct 2019 23:58:57 +0000 (00:58 +0100)]
algorithms.c: Hide the `_Poly1305Class' type a bit better.

4 years agopyke/pyke-mLib.c: Raise `OverflowError' on out-of-range inputs.
Mark Wooding [Sat, 23 Nov 2019 15:05:39 +0000 (15:05 +0000)]
pyke/pyke-mLib.c: Raise `OverflowError' on out-of-range inputs.

This seems to match better what Python does under these conditions.

4 years agoalgorithms.c (FOO.hashbufN): Consistently raise `ValueError' if too big.
Mark Wooding [Sun, 13 Oct 2019 23:53:56 +0000 (00:53 +0100)]
algorithms.c (FOO.hashbufN): Consistently raise `ValueError' if too big.

4 years agoalgorithms.c: Implement KMAC in C.
Mark Wooding [Mon, 14 Oct 2019 15:27:51 +0000 (16:27 +0100)]
algorithms.c: Implement KMAC in C.

It's longer this way, but significantly less ugly, and it works
consistently with the other algorithms.

4 years agoalgorithms.c: Set `hashsz', `tagsz', and `name' properties on MAC keys.
Mark Wooding [Sun, 13 Oct 2019 23:56:07 +0000 (00:56 +0100)]
algorithms.c: Set `hashsz', `tagsz', and `name' properties on MAC keys.

The generic MAC objects, and Poly1305.

4 years agoalgorithms.c (KeySZSet): Represent the set as an actual (frozen) set.
Mark Wooding [Sun, 13 Oct 2019 23:52:23 +0000 (00:52 +0100)]
algorithms.c (KeySZSet): Represent the set as an actual (frozen) set.

4 years agoalgorithms.c: Set `KSZ.max' to `None' to indicate no bound.
Mark Wooding [Sun, 13 Oct 2019 23:50:51 +0000 (00:50 +0100)]
algorithms.c: Set `KSZ.max' to `None' to indicate no bound.

4 years agoalgorithms.c (ShakeNN.done): Offer a (sensible) default hash size.
Mark Wooding [Mon, 14 Oct 2019 00:03:25 +0000 (01:03 +0100)]
algorithms.c (ShakeNN.done): Offer a (sensible) default hash size.

4 years agoalgorithms.c (ShakeNN constructor): Support `None' for func/perso args.
Mark Wooding [Mon, 14 Oct 2019 00:01:33 +0000 (01:01 +0100)]
algorithms.c (ShakeNN constructor): Support `None' for func/perso args.

4 years agobuffer.c: Implement `ec2osp' and `os2ecp' as operations on buffers.
Mark Wooding [Sat, 23 Nov 2019 14:26:34 +0000 (14:26 +0000)]
buffer.c: Implement `ec2osp' and `os2ecp' as operations on buffers.

4 years agobuffer.c: Refactor `WriteBuffer.putecptraw'.
Mark Wooding [Sat, 23 Nov 2019 14:22:30 +0000 (14:22 +0000)]
buffer.c: Refactor `WriteBuffer.putecptraw'.

This will better match a function to be added soon.  No functional
change.

4 years agoSplit 'pyke/' into commit 'c80de12d8d0827e0553fed2e4d392cb9bf3a378f'
Mark Wooding [Sat, 11 Apr 2020 11:46:03 +0000 (12:46 +0100)]
Split 'pyke/' into commit 'c80de12d8d0827e0553fed2e4d392cb9bf3a378f'

git-subtree-dir: pyke
git-subtree-mainline: f76230157bd427829e49628de37d53f9c8ae7842
git-subtree-split: c80de12d8d0827e0553fed2e4d392cb9bf3a378f

* commit 'c80de12d8d0827e0553fed2e4d392cb9bf3a378f':
  Port to Python 3.
  *.[ch]: Some preparatory reformatting for the Python 3 porting.
  mp.c, catacomb/__init__.py, pyke/: Fix mixed-mode arithmetic involving `float'.
  *.c: Separate string function calls according to text/binary usage.
  *.c: Introduce a new input conversion for binary strings.
  *.c: Use the new `Py_hash_t' type.
  *.c: Use `PyVarObject_HEAD_INIT' to initialize type object headers.
  *.c: Use the new `Py_TYPE' and `Py_SIZE' macros; define them if necessary.
  pyke/mapping.c, key.c: Make the mapping code more intrusive and complete.
  pyke/mapping.c: Introduce macro for unconstifying common keyword list.
  pyke/pyke.c (newtype): Explicitly clear `ht_slots'.
  *.c: Split the constant definitions into the various submodules.
  pyke/pyke.[ch]: Make type skeleton structures be read-only.
  pyke/pyke.h: Add a `MEMBER' variant with explicit member name.
  *.c: Use Python `METH_NOARGS' methods where applicable.
  *.c: Use Python's facilities for defining class and static methods.
  *.c: Make all of the type-definition tables read-only.
  pyke/pyke.c: Check conversions hidden inside `KWLIST' and `KWMETH'.
  pyke/pyke.h, key.c: Rename `INDEXERR' to `MAPERR'.

4 years agoPort to Python 3.
Mark Wooding [Tue, 22 Oct 2019 18:12:28 +0000 (19:12 +0100)]
Port to Python 3.

Most of this is `#ifdef ...' ... `#endif' noise, with a few tweaks
thrown in.

Some notes on specific parts of the port.

  * buffer.c: The Python 3 buffer protocol is completely different.
    Read buffers work differently, but aren't problematic; write buffers
    can now be held open for an extended period, so we need the locking
    machinery that was added recently.

  * catacomb.c: Module initialization has changed, but isn't a great
    deal more difficult.

  * catacomb/__init__.py, catacomb/pwsafe.py, pock, pwsafe: There are a
    number of places which need language-version switches, but none of
    them is especially interesting.  This diff is noisier than it should
    be because I couldn't adjust the indentation in advance.

  * mp.c: With the abolition of a separate fixnum type, `mp_frompylong'
    needed to express the fast path from a fixnum in a different way.

  * pwsafe: The hacking to alter the error-handling behaviour associated
    with the `stdout' stream is deeply unpleasant.  Sorry.

  * pyke/pyke.h: Most of the porting work happens here, with alternative
    definitions for the various macros introduced earlier.

  * .gitignore: Ignore Python 3 `__pycache__/' turds.  Python 3 leaves
    its pre-tokenized files in `__pycache__/' directories, which somehow
    manage to be much more objectionable than the loose Python 2 `*.pyc'
    files.  Ignore these.

  * debian/: Add the necessary things to build a Python 3 extension
    package.

4 years agoPort to Python 3.
Mark Wooding [Tue, 22 Oct 2019 18:12:28 +0000 (19:12 +0100)]
Port to Python 3.

Most of this is `#ifdef ...' ... `#endif' noise, with a few tweaks
thrown in.

Some notes on specific parts of the port.

  * buffer.c: The Python 3 buffer protocol is completely different.
    Read buffers work differently, but aren't problematic; write buffers
    can now be held open for an extended period, so we need the locking
    machinery that was added recently.

  * catacomb.c: Module initialization has changed, but isn't a great
    deal more difficult.

  * catacomb/__init__.py, catacomb/pwsafe.py, pock, pwsafe: There are a
    number of places which need language-version switches, but none of
    them is especially interesting.  This diff is noisier than it should
    be because I couldn't adjust the indentation in advance.

  * mp.c: With the abolition of a separate fixnum type, `mp_frompylong'
    needed to express the fast path from a fixnum in a different way.

  * pwsafe: The hacking to alter the error-handling behaviour associated
    with the `stdout' stream is deeply unpleasant.  Sorry.

  * pyke/pyke.h: Most of the porting work happens here, with alternative
    definitions for the various macros introduced earlier.

  * .gitignore: Ignore Python 3 `__pycache__/' turds.  Python 3 leaves
    its pre-tokenized files in `__pycache__/' directories, which somehow
    manage to be much more objectionable than the loose Python 2 `*.pyc'
    files.  Ignore these.

  * debian/: Add the necessary things to build a Python 3 extension
    package.

4 years ago*.[ch]: Some preparatory reformatting for the Python 3 porting.
Mark Wooding [Tue, 22 Oct 2019 17:50:19 +0000 (18:50 +0100)]
*.[ch]: Some preparatory reformatting for the Python 3 porting.

Nothing very complicated or profound.  These are intended to make the
upcoming diff easier to check for stupid mistakes.  A lot of this is
whitespace change that disappears in `git show -w'.

4 years ago*.[ch]: Some preparatory reformatting for the Python 3 porting.
Mark Wooding [Tue, 22 Oct 2019 17:50:19 +0000 (18:50 +0100)]
*.[ch]: Some preparatory reformatting for the Python 3 porting.

Nothing very complicated or profound.  These are intended to make the
upcoming diff easier to check for stupid mistakes.  A lot of this is
whitespace change that disappears in `git show -w'.

4 years agomp.c, catacomb/__init__.py, pyke/: Fix mixed-mode arithmetic involving `float'.
Mark Wooding [Tue, 22 Oct 2019 17:31:57 +0000 (18:31 +0100)]
mp.c, catacomb/__init__.py, pyke/: Fix mixed-mode arithmetic involving `float'.

This is a bit embarrassing.

>>> import catacomb as C
>>> x = C.MP(5)
>>> x == 5.1
True
>>> x < 5.1
False
>>> r = x/2
>>> r
5/2
>>> r == 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required
>>> r == 2.5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required
>>> r*1.0
5/2
>>> r*1.1
5/2
>>> r*2.0
5
>>> r*2.5
5

Fix this nonsense.

  * Change the `obvious' arithmetic operators so that they notice that
    one of the operands is a float.  Handle this by converting to a
    Python bignum and letting Python handle the arithmetic.  The result
    is a float, which seems sensible inexact contagion.

  * Introduce a rich-comparison method which also detects a float
    operand and hands off to Python.  Python seems to get this right,
    comparing the float to the bignum in its full precision, so that's a
    win.

  * Also, modify the `IntRat' code to apply inexact contagion in the
    same way.  Comparisons may be imperfect here, but that's
    surprisingly hard to get right.

The new results:

>>> import catacomb as C
>>> x = C.MP(5)
>>> x == 5.1
False
>>> x < 5.1
True
>>> r = x/2
>>> r
5/2
>>> r == 2
False
>>> r == 2.5
True
>>> r*1.0
2.5
>>> r*1.1
2.75
>>> r*2.0
5.0
>>> r*2.5
6.25

4 years agofield.c, mp.c: Implement the `nb_index' conversion.
Mark Wooding [Mon, 18 Nov 2019 14:52:54 +0000 (14:52 +0000)]
field.c, mp.c: Implement the `nb_index' conversion.

This is Yet Another Integer Conversion added in 2.5, for the purpose of
/implicit/ conversions (since strings and other strange things will
convert explicitly to integers).

It's significant because the `bin' builtin (conversion to base-2 string,
added in 2.6) works by applying the `index' conversion rather than
having a special-purpose method like `hex' and `oct'.  (Indeed, Python 3
abolishes these methods, and `hex' and `oct' convert via `index' too.)

So add conversions somewhat judiciously.

4 years ago*.c: Separate string function calls according to text/binary usage.
Mark Wooding [Mon, 21 Oct 2019 01:38:59 +0000 (02:38 +0100)]
*.c: Separate string function calls according to text/binary usage.

Don't use the `PyString_...' functions directly.  Instead, define two
collections of macros `TEXT_...' and `BIN_...', which can be used on
text and binary strings respectively.  The two sets are slightly
different, because they've been designed to adapt to Python 3's
different interfaces.

There are no `PyString...' calls remaining outside of these macros.

4 years agorand.c: Cope with large `range' arguments.
Mark Wooding [Fri, 15 Nov 2019 18:26:57 +0000 (18:26 +0000)]
rand.c: Cope with large `range' arguments.

Python 3 will report large numbers as being `int' values, and
`PyInt_AS_LONG' will return nonsense in this case.

4 years ago*.c: Introduce a new input conversion for binary strings.
Mark Wooding [Mon, 21 Oct 2019 00:43:54 +0000 (01:43 +0100)]
*.c: Introduce a new input conversion for binary strings.

One of the major differences in Python 3 is that it firmly distinguishes
between binary and text strings: the former consist of small integers,
while the latter consist of Unicode scalars.  The Python 3 `s#'
conversion's main purpose is to accept text strings, and though it will
also accept binary strings it's not really ideal for the purpose.
Python 3 introduces a new conversion `y#' specifically for binary
strings, though this isn't quite what we want because, for some reason,
it /doesn't/ work with bufferish objects which require explicit release.

The best answer seems to be to introduce our own custom conversion for
binary strings, so we do this here, replacing all of the binary-input
argument conversions.  While we're at it, replace all of the by-steam
argument conversions using `PyObject_AsReadBuffer' too.

4 years agorand.c: Some gratuitous reformatting.
Mark Wooding [Fri, 15 Nov 2019 18:28:35 +0000 (18:28 +0000)]
rand.c: Some gratuitous reformatting.

4 years agocatacomb/__init__.py, mp.c: Remove `L' suffix from `repr' output.
Mark Wooding [Thu, 21 Nov 2019 22:44:15 +0000 (22:44 +0000)]
catacomb/__init__.py, mp.c: Remove `L' suffix from `repr' output.

Python 3 doesn't produce an `L', and it doesn't really help anything.

4 years agomp.c, catacomb/__init__.py, pyke/: Fix mixed-mode arithmetic involving `float'.
Mark Wooding [Tue, 22 Oct 2019 17:31:57 +0000 (18:31 +0100)]
mp.c, catacomb/__init__.py, pyke/: Fix mixed-mode arithmetic involving `float'.

This is a bit embarrassing.

>>> import catacomb as C
>>> x = C.MP(5)
>>> x == 5.1
True
>>> x < 5.1
False
>>> r = x/2
>>> r
5/2
>>> r == 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required
>>> r == 2.5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required
>>> r*1.0
5/2
>>> r*1.1
5/2
>>> r*2.0
5
>>> r*2.5
5

Fix this nonsense.

  * Change the `obvious' arithmetic operators so that they notice that
    one of the operands is a float.  Handle this by converting to a
    Python bignum and letting Python handle the arithmetic.  The result
    is a float, which seems sensible inexact contagion.

  * Introduce a rich-comparison method which also detects a float
    operand and hands off to Python.  Python seems to get this right,
    comparing the float to the bignum in its full precision, so that's a
    win.

  * Also, modify the `IntRat' code to apply inexact contagion in the
    same way.  Comparisons may be imperfect here, but that's
    surprisingly hard to get right.

The new results:

>>> import catacomb as C
>>> x = C.MP(5)
>>> x == 5.1
False
>>> x < 5.1
True
>>> r = x/2
>>> r
5/2
>>> r == 2
False
>>> r == 2.5
True
>>> r*1.0
2.5
>>> r*1.1
2.75
>>> r*2.0
5.0
>>> r*2.5
6.25

4 years agoec.c, field.c: Remove redundant `FOO_pyrichcompare' methods.
Mark Wooding [Tue, 22 Oct 2019 16:53:44 +0000 (17:53 +0100)]
ec.c, field.c: Remove redundant `FOO_pyrichcompare' methods.

The abstract base classes already have the necessary comparison methods,
so there's nothing to be gained by repeating the same methods on
subclasses.  (Indeed, Python might pointlessly allocate more memory for
the corresponding Python-level descriptors, but I haven't checked.)  But
there is something to be lost: Python 3 explicitly clobbers the
`__hash__' methods of subclasses which declare new equality methods.  We
could add duplicates of the hash methods throughout the subclass graph,
but it seems better to delete the pointless equality methods which are
causing the problem.

4 years agocatacomb/__init__.py: Implement equality and hashing for `KeyData' objects.
Mark Wooding [Mon, 25 Nov 2019 12:07:16 +0000 (12:07 +0000)]
catacomb/__init__.py: Implement equality and hashing for `KeyData' objects.

Equality is determined by value, so don't use `KeyData' objects as
hashtable keys and then mutate them.

4 years agokey.c: Add hashing and comparison for `Key' objects.
Mark Wooding [Mon, 28 Oct 2019 10:49:55 +0000 (10:49 +0000)]
key.c: Add hashing and comparison for `Key' objects.

4 years agokey.c, catacomb/__init__.py: Split key file I/O into Python and C pieces.
Mark Wooding [Tue, 22 Oct 2019 11:35:40 +0000 (12:35 +0100)]
key.c, catacomb/__init__.py: Split key file I/O into Python and C pieces.

The new Catacomb `key_mergeline' and `key_extractline' functions were
specifically added to interface with language-specific I/O systems which
couldn't easily present as a `FILE *'.  Expose these new functions, and
implement the old file-oriented methods in terms of the new bindings.

4 years agokey.c: Make `None' be the default report-function designator.
Mark Wooding [Tue, 22 Oct 2019 11:34:48 +0000 (12:34 +0100)]
key.c: Make `None' be the default report-function designator.

Now we can pass it in from Python as an explicit argument.

4 years agobuffer.c: Publish the internal structure of buffer objects.
Mark Wooding [Mon, 21 Oct 2019 11:01:48 +0000 (12:01 +0100)]
buffer.c: Publish the internal structure of buffer objects.

4 years agobuffer.c: Add a lock count which pins a write-buffer's backing store.
Mark Wooding [Tue, 22 Oct 2019 16:14:46 +0000 (17:14 +0100)]
buffer.c: Add a lock count which pins a write-buffer's backing store.

Python 3's buffer protocol allows a long-term acquisition of a buffer's
backing-store address.  It would be Bad if one of our `WriteBuffer'
objects were to reallocate its backing store while something still
thought it had the old address, so keep track of how many times the
buffer has been referenced in this way.

Nothing actually makes use of this machinery yet, but its time will come.

4 years agobuffer.c: Funnel control through a common exit point.
Mark Wooding [Tue, 22 Oct 2019 10:29:09 +0000 (11:29 +0100)]
buffer.c: Funnel control through a common exit point.

I'm not going on a rampage to fix this throughout the code (yet?), but
while I'm here I might as well.

4 years agobuffer.c: Handle curve object as `PyObject *'.
Mark Wooding [Tue, 22 Oct 2019 10:27:49 +0000 (11:27 +0100)]
buffer.c: Handle curve object as `PyObject *'.

I've not checked, but I think this is the only place where a
pre- existing curve object was handled as `PyTypeObject *'.

4 years agobuffer.c: Delete pointless initialization.
Mark Wooding [Tue, 22 Oct 2019 10:27:12 +0000 (11:27 +0100)]
buffer.c: Delete pointless initialization.

The variable `cobj' will be set by `PyArg_ParseTuple' anyway.

4 years agopock: Use floor division on integers.
Mark Wooding [Tue, 22 Oct 2019 11:18:41 +0000 (12:18 +0100)]
pock: Use floor division on integers.

Python 3 wants to give me a float here, which is obviously wrong.

4 years agopock: Probe for the fixnum width by enquiring the largest fixnum value.
Mark Wooding [Tue, 22 Oct 2019 11:16:37 +0000 (12:16 +0100)]
pock: Probe for the fixnum width by enquiring the largest fixnum value.

Python 3 doesn't distinguish fixnums from bignums at the type level, so
this would run forever.  Instead, get (a guess at) the largest fixnum
value, and use that.

4 years agopock: Use `MPMul' so that we can avoid `reduce'.
Mark Wooding [Tue, 22 Oct 2019 11:14:10 +0000 (12:14 +0100)]
pock: Use `MPMul' so that we can avoid `reduce'.

For some reason, Python 3 hides `reduce' away in some library module.
Anyway, we have a very efficient mass-multiplier so we might as well use
it.

4 years agopwsafe: Trim an overly-long section boundary.
Mark Wooding [Mon, 21 Oct 2019 17:03:20 +0000 (18:03 +0100)]
pwsafe: Trim an overly-long section boundary.

4 years agocatacomb/pwsafe.py: Use `binascii' for Base64 conversion.
Mark Wooding [Mon, 21 Oct 2019 17:26:39 +0000 (18:26 +0100)]
catacomb/pwsafe.py: Use `binascii' for Base64 conversion.

The `.encode()' and `.decode()' string methods were apparently too
convenient, so Python 3 doesn't do Base64 conversion like this.
Instead, we have to use the steam-powered `binascii'.  And, to make
things even better, encoding produces `bytes' rather than `str' because,
err, it's not really text or something?  No idea.  Stoats.

4 years agocatacomb/pwsafe.py: Hack around the change in metaclass syntax.
Mark Wooding [Mon, 21 Oct 2019 17:17:21 +0000 (18:17 +0100)]
catacomb/pwsafe.py: Hack around the change in metaclass syntax.

Python 2 (all versions) want you to set `__metaclass__' within the class
body.  Python 3 ignores this, treating it like an ordinary class
attribute, and wants you to say `metaclass = ...' in the direct-
superclass list; but the latter is a syntax error to Python 2.

Instead, add a hack which constructs a anonymous class with the right
metaclass to use as a direct superclass.  It's awful, but it works.

4 years agocatacomb/pwsafe.py: Mark abstract storage-backend classes as lacking `NAME'.
Mark Wooding [Mon, 21 Oct 2019 17:20:42 +0000 (18:20 +0100)]
catacomb/pwsafe.py: Mark abstract storage-backend classes as lacking `NAME'.

Previously, they'd have a name of `None'.  This will make a later hack
work better.

4 years agocatacomb/pwsafe.py: Hack around the difference in octal literal syntax.
Mark Wooding [Mon, 21 Oct 2019 17:06:31 +0000 (18:06 +0100)]
catacomb/pwsafe.py: Hack around the difference in octal literal syntax.

Python 2.5 wants `0DIGITS', while Python 3 insists on `0oDIGITS'.  The
latter is prettier, but the difference is annoying.  We only need these
for Unix permissions, so just set up the ones we want in advance using
an explicit conversion from strings.

4 years agocatacomb/__init__.py: Implement `rich' comparisons on rationals.
Mark Wooding [Sun, 20 Oct 2019 01:29:36 +0000 (02:29 +0100)]
catacomb/__init__.py: Implement `rich' comparisons on rationals.

These are the only kind in Python 3, and they work everywhere.

4 years agocatacomb/__init__.py: Don't try to convert text strings to `ByteString'.
Mark Wooding [Mon, 21 Oct 2019 19:06:31 +0000 (20:06 +0100)]
catacomb/__init__.py: Don't try to convert text strings to `ByteString'.

This won't work in Python 3, so get used to the idea.

Fortunately, this only matters for the empty string, so use
`ByteString.zero(0)' instead.

4 years agocatacomb/__init__.py: Hack because Python 3 `hex' builtin works differently.
Mark Wooding [Mon, 21 Oct 2019 19:01:45 +0000 (20:01 +0100)]
catacomb/__init__.py: Hack because Python 3 `hex' builtin works differently.

In Python 3, the `hex' builtin function converts its operand to an
integer (using the new `__index__' special method) and then converts
that to hex, rather than calling a special method directly.  The new
`bytes' type offers a `hex' method for conversion to hex, so we do the
same here.

4 years agocatacomb/__init__.py: Hack because Python 3 won't destructure arguments.
Mark Wooding [Mon, 21 Oct 2019 17:25:28 +0000 (18:25 +0100)]
catacomb/__init__.py: Hack because Python 3 won't destructure arguments.

Why?!

4 years ago*.py: Hack around the mapping change from `itervalues' to just `values', etc.
Mark Wooding [Mon, 21 Oct 2019 17:13:57 +0000 (18:13 +0100)]
*.py: Hack around the mapping change from `itervalues' to just `values', etc.

This change is only about accessing existing mappings.  We'll have to
set up our own mappings the hard way.

4 years ago*.py: Add explicit conversions between binary and text strings.
Mark Wooding [Mon, 21 Oct 2019 17:04:58 +0000 (18:04 +0100)]
*.py: Add explicit conversions between binary and text strings.

This doesn't do anything right now, but it'll be an important hook for
porting to Python 3.

4 years ago*.py: Add parentheses around `print' arguments.
Mark Wooding [Mon, 21 Oct 2019 16:57:29 +0000 (17:57 +0100)]
*.py: Add parentheses around `print' arguments.

For `print >>FILE', just switch to `FILE.write(...)' instead, with an
explicit newline.

4 years ago*.py: Hack around Python exception-catching syntax mismatch.
Mark Wooding [Mon, 21 Oct 2019 16:53:43 +0000 (17:53 +0100)]
*.py: Hack around Python exception-catching syntax mismatch.

There's no common syntax to capture the current exception value between
Python 2.5 and 3: the former wants

try: ...
except EXCCLS, VAR: ...

while the latter wants

try: ...
except EXCCLS as VAR: ...

Give up and dig the exception value out of `sys.exc_info()' instead.

4 years ago*.py: Upgrade old-fashioned `raise' syntax.
Mark Wooding [Mon, 21 Oct 2019 16:47:30 +0000 (17:47 +0100)]
*.py: Upgrade old-fashioned `raise' syntax.

4 years agocatacomb/pwsafe.py: Add missing `.errno' projection.
Mark Wooding [Mon, 21 Oct 2019 16:52:35 +0000 (17:52 +0100)]
catacomb/pwsafe.py: Add missing `.errno' projection.

4 years agomp.c: Fix misformatting.
Mark Wooding [Mon, 21 Oct 2019 10:46:14 +0000 (11:46 +0100)]
mp.c: Fix misformatting.

4 years agoalgorithms.c: Delete a spurious blank line.
Mark Wooding [Tue, 19 Nov 2019 22:35:49 +0000 (22:35 +0000)]
algorithms.c: Delete a spurious blank line.

4 years agomp.c: Use newer names for the internal `long' representation parameters.
Mark Wooding [Mon, 21 Oct 2019 10:43:43 +0000 (11:43 +0100)]
mp.c: Use newer names for the internal `long' representation parameters.

In Python 2.6, the `SHIFT' and `MASK' constants grew `PyLong_' prefixes,
but the old names were retained for compatibility; in Python 3, the old
names are gone.  Use the new names, defining them as necessary.

4 years agomp.c: Assert about the Python limb size statically.
Mark Wooding [Mon, 21 Oct 2019 10:40:39 +0000 (11:40 +0100)]
mp.c: Assert about the Python limb size statically.

Python extensions build with assertions off, so this was much less
helpful than it looked.

4 years ago*.c: Separate string function calls according to text/binary usage.
Mark Wooding [Mon, 21 Oct 2019 01:38:59 +0000 (02:38 +0100)]
*.c: Separate string function calls according to text/binary usage.

Don't use the `PyString_...' functions directly.  Instead, define two
collections of macros `TEXT_...' and `BIN_...', which can be used on
text and binary strings respectively.  The two sets are slightly
different, because they've been designed to adapt to Python 3's
different interfaces.

There are no `PyString...' calls remaining outside of these macros.

4 years ago*.c: Introduce a new input conversion for binary strings.
Mark Wooding [Mon, 21 Oct 2019 00:43:54 +0000 (01:43 +0100)]
*.c: Introduce a new input conversion for binary strings.

One of the major differences in Python 3 is that it firmly distinguishes
between binary and text strings: the former consist of small integers,
while the latter consist of Unicode scalars.  The Python 3 `s#'
conversion's main purpose is to accept text strings, and though it will
also accept binary strings it's not really ideal for the purpose.
Python 3 introduces a new conversion `y#' specifically for binary
strings, though this isn't quite what we want because, for some reason,
it /doesn't/ work with bufferish objects which require explicit release.

The best answer seems to be to introduce our own custom conversion for
binary strings, so we do this here, replacing all of the binary-input
argument conversions.  While we're at it, replace all of the by-steam
argument conversions using `PyObject_AsReadBuffer' too.

4 years ago*.c: Use the new `Py_hash_t' type.
Mark Wooding [Sun, 20 Oct 2019 23:33:22 +0000 (00:33 +0100)]
*.c: Use the new `Py_hash_t' type.