Skip to content

feat: add BinPoly arithmetic layer, expand ASN.1/X.509/Ed25519 test coverage, and miscellaneous cleanups#108

Merged
Xor-el merged 23 commits into
masterfrom
enhancement/general-refactorings
Jun 12, 2026
Merged

feat: add BinPoly arithmetic layer, expand ASN.1/X.509/Ed25519 test coverage, and miscellaneous cleanups#108
Xor-el merged 23 commits into
masterfrom
enhancement/general-refactorings

Conversation

@Xor-el

@Xor-el Xor-el commented Jun 12, 2026

Copy link
Copy Markdown
Owner

Summary

This PR introduces a new binary-polynomial (GF(2^n)) arithmetic subsystem,
expands test coverage across ASN.1, X.509, Ed25519, and utility layers,
adds an ONVIF raw-keyBag PKCS#12 fixture, and applies several small
correctness and structural cleanups throughout the library.

New: Binary Polynomial (BinPoly) Arithmetic

A self-contained GF(2^n) arithmetic layer has been added under
CryptoLib/src/Math/BinPoly/ and CryptoLib/src/Interfaces/Math/BinPoly/.

Interfaces

  • ClpIBinPolyMulIBinPolyMul: multiply, square, squareN, size
  • ClpIBinPolyInvIBinPolyInv: field inversion

Scalar (portable) backend

  • ClpBinPolyScalarKernels — core carryless-multiply kernels
  • ClpBinPolyScalarMedium / ClpBinPolyScalarLarge — size-dispatch wrappers
  • ClpBinPolyScalarBackend — backend wiring

x86 SSE2/PCLMULQDQ backend

  • ClpBinPolyX86V128Kernels / ClpBinPolyX86V128Sizes
  • ClpBinPolyX86V128Medium / ClpBinPolyX86V128Large
  • ClpBinPolyX86V128Backend

Reduction strategies

  • ClpBinPolyMulBaseBinomialReduce — x^r + 1 reduction (BIKE-style)
  • ClpBinPolyMulBaseTrinomialReduce — x^n + x^k + 1
  • ClpBinPolyMulBasePentanomialReduce — x^n + x^k3 + x^k2 + x^k1 + 1

Supporting units

  • ClpBinPolyMulBase — abstract base for all mul implementations
  • ClpItohTsujiiInv — Itoh–Tsujii inversion algorithm
  • ClpBinPolys — public facade: Create, Add, AddTo, Zero,
    EqualTo, TBinPolysMul.Binomial/Trinomial/Pentanomial
  • ClpIF2mFieldData / ClpF2mFieldData — F(2^m) field parameter data,
    replacing the previous ClpLongArray reference in all .dpr/.lpr
    project files

New Tests

BinPolyTests.pas — comprehensive property-based tests for the new
arithmetic layer covering:

  • Add and AddTo against XOR reference for BIKE-R1
  • Multiply, Square, SquareN against reference implementations for
    binomial, trinomial, and pentanomial reductions across many sizes
    (including small edge cases, even-degree binomials, and BIKE-R1)
  • Offset/guard-zone tests (RunAllOpsAtOffsets) verifying no out-of-bounds
    writes to input or surrounding buffer regions
  • Inversion correctness: Invert(0)=0, Invert(1)=1, a*inv(a)=1,
    inv(inv(a))=a, and in-place inversion

Asn1GeneratorTests.pas — new test suite for streaming ASN.1 generators:

  • Untagged BER/DER sequence and BER octet-string generators
  • Tagged BER/DER sequence generators (explicit and implicit) across a range
    of tag numbers
  • Tagged BER octet-string generators
  • Round-trip parsing of all generated encodings

Asn1TimeFormatTests.pas — accept/reject format tests for
TAsn1UtcTime and TAsn1GeneralizedTime:

  • Valid and invalid UTCTime formats and field ranges
  • Valid and invalid GeneralizedTime formats and field ranges
  • Rejection of fuzzing-corpus inputs that were previously accepted

IdpRelativeNameTests.pas — tests for TIssuingDistributionPoint
relative distinguished name encoding and round-trip parsing.

BinaryPrimitivesTests.pas — tests for TBinaryPrimitives:

  • ReadUInt16/32/64 LE/BE array vs pointer overloads
  • Misaligned pointer reads for UInt32
  • CopyUInt32/64 LE/BE bulk copy correctness

ByteUtilitiesTests.pas — tests for TByteUtilities number-utility
helpers.

New Test Data

  • CryptoLib.Tests/Data/Pkcs/Pkcs12Store/Stores/RawKeyBagStore.der
    binary PKCS#12 store containing an ONVIF raw keyBag (no
    shroudedKeyBag), for testing stores where the private key is carried
    unencrypted.
  • CryptoLib.Tests/Data/Pkcs/Pkcs12Store/Manifest.csv — entry added for
    the new store (RawKeyBagStore, no password).

Ed25519 Additions

  • ImplTestEd25519ConsistencyExpandedKey — consistency tests between
    seed-path signing and TExpandedKey-path signing for both the standard
    SHA-512 digest and the Blake2b variant, including sign/verify
    round-trips and single-bit corruption rejection.
  • TestEd25519ExpandedKeyBlake2bPinnedVector — pinned known-answer test
    for the Blake2b ExpandedKey path against a fixed sk/xk/pk/sig tuple.
  • TestEd25519ConsistencyExpandedKey / TestEd25519ConsistencyExpandedKeyBlake2b
    — published test methods wiring the above.

Ed448 Fix

  • Removed redundant explicit TEd448.Precompute() call from
    TTestEd448.SetUp; precomputation is handled internally and the
    explicit call was unnecessary.

Structural Cleanups

Boot method elimination — the two-step class constructor → Boot()
indirection has been collapsed into a direct class constructor body
across multiple classes:

  • TX509AttrCertParser, TX509CertificateParser, TX509CrlParser,
    TX509Utilities, TOSRandomProvider

In each case the Boot method is removed and its body is inlined directly
into the class constructor. For TX509Utilities and TOSRandomProvider
the class destructor declaration order is also corrected to follow the
class constructor in the implementation section.

Asn1SequenceParserTests tidy-up — all
TDerInteger.Create(TBigInteger.Zero/One/ValueOf(n)) call sites replaced
with TDerInteger.ValueOf(n), which is the preferred allocation-friendly
factory.

Project file updates — all Delphi .dpr files
(CryptoLib.Tests.dpr, CryptoLib.Tests.Mobile.dpr,
CryptoLib.BenchmarkConsole.dpr, CryptoLib.Examples.dpr) and the
Lazarus .lpi/.lpr files updated to:

  • Replace ClpLongArray with the new BinPoly unit set
  • Add all new test units
  • Add src\Math\BinPoly to the FPC OtherUnitFiles search path

@Xor-el Xor-el merged commit 33e63f5 into master Jun 12, 2026
22 checks passed
@Xor-el Xor-el deleted the enhancement/general-refactorings branch June 12, 2026 00:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant