Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

picoke

picoke provides practical Python utilities. Its six Cython crypto extensions cover Base64, Keccak-256, Ed25519, recoverable secp256k1, EIP-712, and Bitcoin signed messages. The extensions use the CPython C API where needed and delegate underlying operations to OpenSSL libcrypto, pybase64, the standard library, PyCryptodome, btclib-secp256k1, and eth-account. picop is a runtime dependency. No independent security audit is implied.

Installation

With CPython 3.15 (or 3.14), install from a checkout:

python3.15 -m pip install .

Source builds require a C compiler, the target interpreter's development headers, and OpenSSL development headers (openssl/evp.h) and the libcrypto library discoverable by the compiler and linker. The isolated build installs Cython and picop's Cython declarations; it does not install these system development dependencies. Base64 encoding and Keccak-256 link libcrypto; dynamically linked builds need a compatible libcrypto shared library. Large Base64 decodes use the pybase64 runtime dependency on CPython 3.14 and 3.15; its libbase64 backend selects available SIMD implementations.

Wheels are specific to a CPython ABI and platform and include type stubs. Cross-platform wheel compatibility and bundled OpenSSL libraries are not claimed. There is no pure-Python fallback for the crypto extensions.

Cryptography

from picoke.crypto import (
    b64decode, b64encode, ed25519_public_key, ed25519_sign,
    ed25519_verify, keccak256,
)

assert b64encode(b"foo") == b"Zm9v"
assert b64decode("Zm9v") == b"foo"
digest = keccak256(b"hello")  # Keccak-256, not SHA3-256
seed = bytes.fromhex("9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60")
public_key = ed25519_public_key(seed)
signature = ed25519_sign(b"message", seed)
assert ed25519_verify(b"message", signature, public_key)

Generate production seeds with a cryptographically secure random source; the fixed seed above is only an example. Python bytes cannot be guaranteed erased from memory. Signing and key APIs accept bytes; seeds and public keys are 32 bytes, and signatures are 64 bytes. Verification returns False for invalid signatures, malformed or noncanonical point encodings, noncanonical scalars and small-order public keys; TypeError is raised for non-bytes arguments. Invalid seed lengths raise ValueError. Remaining verification follows PyCryptodome's cofactored RFC 8032 behavior; applications must authenticate public keys independently.

b64encode_ascii returns a str; b64encode returns bytes. Both decoders, b64decode and b64decode_ascii, accept ASCII str and return bytes. Decoding follows base64.b64decode(..., validate=True) semantics, raising ValueError or binascii.Error for non-ASCII text, invalid alphabet or rejected padding. Excess-padding behavior follows the running Python version. Base64 is an encoding, not encryption.

Short Base64 decodes combine validation and decoding in one native pass. Keccak-256 uses the OpenSSL KECCAK-256 provider for exact bytes when available, and PyCryptodome otherwise, including for bytes subclasses.

secp256k1 and Bitcoin messages

from picoke.crypto import (
    bip137_sign_message, bip137_verify_message, privkey_to_address,
    privkey_to_pubkey, recover_pubkey, sign_recoverable,
)

private_key = (1).to_bytes(32, "big")  # Example only; never use as a real key.
public_key = privkey_to_pubkey(private_key)  # 65-byte uncompressed SEC key
address = privkey_to_address(private_key)  # lowercase Ethereum address
message = b"example"
signature = bip137_sign_message(private_key, message)
assert bip137_verify_message(message, signature, public_key)

sign_recoverable(private_key, digest) accepts a 32-byte digest and returns (r, s, v) with v = 27 + recovery_id; recover_pubkey(digest, r, s, recovery_id) accepts IDs 0–3. Bitcoin signing accepts the address_type keyword: p2pkh-uncompressed (default), p2pkh, p2sh-p2wpkh, or p2wpkh. Verification checks the recovered public key and header/key compression; it does not validate a Bitcoin address or prove that the header matches an address.

Bitcoin messages use the standard framed, double-SHA-256 digest. This deliberately differs from the legacy function of the same name, which hashes raw input once. Legacy Ethereum addresses also differ: legacy hashed the 0x04 SEC prefix; picoke hashes the 64-byte coordinates as Ethereum requires.

EIP-712

eip712_hash_full_message(full_message) hashes an EIP-712 envelope with types, primaryType, domain and message; eip712_hash_agent_message(domain, source, connection_id) uses the legacy Agent schema but requires exactly 32 bytes for connection_id. Array values may be lists or tuples. EIP712Domain fields must be unique and in canonical order (name, version, chainId, verifyingContract, salt, omitting absent fields). Canonical Agent inputs use a fixed-schema Cython encoder; other Agent inputs and generic envelopes use eth-account. Mutually recursive type graphs that eth-account cannot resolve remain unsupported. Check the typed data and authenticate the domain before signing: a digest alone does not establish trust.

eth-account loads on the first call that needs the generic encoder, after picoke validates the input. Importing picoke.crypto or hashing a canonical Agent message does not load it.

eth-account currently constrains PyCryptodome to the 3.x series via eth-keyfile; picoke therefore requires PyCryptodome 3.24.1 or newer, below 4. This increases the dependency footprint and differs from the earlier 4.5 baseline; see the benchmark notes.

Rate limiting

from picoke.ratelimit import TokenBucket

# Allow a burst of 20 tokens, then refill continuously at 10 tokens per second.
bucket = TokenBucket(20, refill_amount=10)

async def handle_batch() -> None:
    await bucket.acquire(3)

TokenBucket(capacity, *, refill_amount=1, refill_period_ns=1_000_000_000) starts full and uses a monotonic clock. Constructor settings are exact int values in 1..2**63 - 1; acquisition costs are exact int values in 1..capacity. Requests are served FIFO, so a large head request can delay smaller ones. Cancellation while waiting does not consume tokens; use asyncio.timeout() to bound a wait. Construct the bucket anywhere, but use it from one asyncio event loop only. Importing picoke.ratelimit does not load the crypto providers.

The Cython implementation uses picop's native nanosecond clock and fixed-width arithmetic when scaled capacity fits in 64 bits. Larger settings retain exact integer arithmetic. It only allocates a lock when an acquisition has to wait. See the rate-limit benchmarks for workloads and measured tradeoffs.

Queues

from picoke.queues import ByteRing

ring = ByteRing(capacity=1024, max_item_size=4096)
assert ring.try_push(b"message")
assert ring.try_pop() == b"message"
assert ring.try_pop() is None

ByteRing is an in-process FIFO for one producer and one consumer, which may run on separate threads on GIL-enabled CPython. Neither method waits for space or data. try_push returns False when full, without evicting anything. try_pop returns None when empty; b"" is a valid item.

Capacity must be positive and maximum item size nonnegative. Both constructor arguments must be exact int values that fit Py_ssize_t, and items must be exact bytes. Oversized items raise ValueError even when the ring is full. The ring allocates its reference slots upfront and retains immutable bytes without copying them. Popped bytes remain valid after the slot is reused. See the queue benchmarks for tradeoffs.

File I/O

from contextlib import closing
from picoke.io.files import read_jsonl, write_jsonl

write_jsonl("events.jsonl", [{"event": "started"}, {"event": "finished"}])
with closing(read_jsonl("events.jsonl")) as records:
    print(next(records))

read_json and write_json handle one UTF-8 JSON document. read_jsonl streams one UTF-8 JSON value per line and raises JSONLDecodeError with the physical line number for invalid UTF-8, invalid JSON, or blank lines. write_jsonl overwrites the file with compact, LF-terminated records.

Use iter_lines(path, strip=True, skip_empty=True, comment_prefix="#") for filtered text, or wrap it in list() for a snapshot. Close line and JSONL iterators explicitly when stopping early, as above.

read_toml is available without extras; write_toml needs picoke[toml]. read_yaml and write_yaml use PyYAML's safe document APIs and need picoke[yaml]. Install both extras with python3.15 -m pip install 'picoke[files]'.

Use picoke.io.files.zip.read_zip for bounded reads of selected archive members. It returns names and bytes without extracting files.

Writes are not atomic. See the file I/O contracts for error handling, ZIP limits, and performance results.

Structured logging

picoke.logging.JSONFormatter works with standard-library logging handlers.

import logging

from picoke.logging import JSONFormatter

handler = logging.StreamHandler()
handler.setFormatter(JSONFormatter())
logger = logging.getLogger("app")
logger.addHandler(handler)
logger.setLevel(logging.INFO)
logger.info("processed %d records", 3, extra={"fields": {"worker": "ingest"}})

Each record becomes one compact JSON object with a UTC millisecond timestamp, level, logger, and message. Exception and stack text are included when available. Put structured data in extra={"fields": ...}: mappings with string keys, lists, strings, booleans, integers, finite floats, and None values. Unsupported values and circular containers raise during formatting; handlers retain their usual error policy. The handler owns the destination, newline, and flushing. See the logging benchmarks for the cost of formatting and handler delivery.

For bounded background delivery to a binary stream:

import logging
from picoke.logging import BackgroundJSONHandler, BackgroundWriter

with open("events.jsonl", "ab") as stream:
    with BackgroundWriter(stream, capacity=256, max_item_size=65536) as writer:
        handler = BackgroundJSONHandler(writer)
        logger = logging.getLogger("background-example")
        logger.addHandler(handler)
        try:
            logger.setLevel(logging.INFO)
            logger.info("started", extra={"fields": {"city": "雪"}})
            handler.flush()
        finally:
            logger.removeHandler(handler)
            handler.close()

BackgroundWriter defaults to 1,024 queued items and a maximum item size of 1,048,576 bytes. Capacity must be a positive exact int; the maximum size must be a nonnegative exact int. Both must fit Py_ssize_t. One additional item can be in flight. write(data, block=True) accepts exact bytes and waits for capacity; block=False returns False when full. Oversized items raise ValueError, even when the queue is full. Accepted items are delivered in FIFO admission order. Queue slots retain the original bytes without copying; formatting, encoding, short writes, stream buffering and the operating system may copy. On a short write the stream must accept a memoryview for the remainder.

flush() waits for earlier admissions and flushes the stream. close() stops admission, drains and flushes but never closes the caller-owned stream; the handler also leaves its writer open when closed. A blocked stream can make these operations wait indefinitely; flushing does not guarantee durability. A sink failure terminates delivery and is chained as the cause of RuntimeError on subsequent writer operations or handler flush, potentially after the logging call. Construct a new writer after a fork. GIL-enabled CPython is required; free-threaded operation has not been validated.

Append-only binary logs

Install the optional codec with python3.15 -m pip install 'picoke[binary-logging]'. BackgroundBinaryHandler writes versioned, length-prefixed MessagePack records with CRC-32 checksums through the same background writer:

import logging
from picoke.binary_logging import BackgroundBinaryHandler, iter_binary_records
from picoke.logging import BackgroundWriter

logger = logging.getLogger("binary-example")
logger.setLevel(logging.INFO)
with open("events.pklog", "ab") as stream:
    with BackgroundWriter(stream, max_item_size=1048588) as writer:
        handler = BackgroundBinaryHandler(writer)
        logger.addHandler(handler)
        try:
            logger.info("started", extra={"fields": {"city": "雪"}})
            handler.flush()
        finally:
            logger.removeHandler(handler)
            handler.close()

with open("events.pklog", "rb") as stream:
    for event in iter_binary_records(stream):
        print(event)

The framing adds 12 bytes to each MessagePack payload; the writer's maximum item size must include that overhead. The reader bounds payloads at 1 MiB by default. By default, incomplete frames raise BinaryLogError; recover_truncated=True ignores only an incomplete final frame. Checksum, version, size and decoding failures still raise. Repair a truncated tail before appending more frames. The handler and reader do not close their caller-owned writer or stream.

MessagePack integers are limited to its signed-64/unsigned-64 range. CRC-32 detects accidental corruption; it does not authenticate records. Use one writer per file; append mode does not coordinate concurrent processes or make multiple writes atomic. A flush is not fsync and does not guarantee crash durability. Encoding, framing and downstream I/O may copy data.

Python support

Target CPython 3.14 and newer, with 3.15 as the default for development and primary validation. Python versions below 3.14 are out of scope. The initial compatibility matrix includes 3.14 and 3.15; newer versions need their own validation. Free-threaded support is a goal, not a claim until tested.

Documentation

Metadata

Release files for picoke 0.1.0rc3

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

Source distribution (sdist)

Source distribution for picoke 0.1.0rc3
File Size Uploaded Provenance Actions
picoke-0.1.0rc3.tar.gz 71.0 kB With provenance Details for picoke-0.1.0rc3.tar.gz

Release files / picoke-0.1.0rc3.tar.gz

Download URL picoke-0.1.0rc3.tar.gz
Size 71.0 kB
Tags Source
SHA-256 checksum
How to use checksums
7deff385e6672e61034ca69bfaf21b300a2d7a1cf2d4af80bbe4ae9b3836d155
BLAKE2b-256 checksum
How to use checksums
0f31068bd82b7244ece4ac06551aa199af5e1e8f566c34e9da5bf678b589ba2e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Security for picoke 0.1.0rc3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

1 file in this release has been uploaded with provenance metadata.

Attestation for picoke-0.1.0rc3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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