Skip to content

Add BigInt utility functions for number theory operations#2205

Open
p-leriche wants to merge 4 commits intogchq:masterfrom
p-leriche:bigint-utils
Open

Add BigInt utility functions for number theory operations#2205
p-leriche wants to merge 4 commits intogchq:masterfrom
p-leriche:bigint-utils

Conversation

@p-leriche
Copy link

@p-leriche p-leriche commented Feb 25, 2026

Description

This PR adds utility functions for working with BigInt values in number theory operations. These utilities provide a foundation for upcoming cryptographic and mathematical operations.

Changes

  • Add src/core/lib/BigIntUtils.mjs with:
    • parseBigInt() - Parse decimal or hexadecimal (0x...) strings to BigInt
    • egcd() - Extended Euclidean Algorithm (iterative implementation, no recursion limits)

Features

  • Supports decimal and hexadecimal input (0x prefix)
  • Iterative algorithm handles arbitrarily large integers without stack overflow
  • Returns GCD and Bézout coefficients for use in modular arithmetic

Testing

  • Tested with decimal inputs
  • Tested with hexadecimal inputs
  • Tested with crypto-grade large integers (2048+ bit)
  • No recursion depth issues with iterative implementation
  • Built successfully with npm run build

Related

This is a dependency for PRs:

Related PRs:

This collection of 5 new operations together with this libray of supporting functions has been created as a teaching resource for Diffie-Hellman and RSA, hopefully as an extension activity for CyberFirst Girls' competitors. (Unfortunately, my last years groups have all dispersed - but there's always another year.) After a frantic burst of activity, no more related PRs are currently envisaged.

A build containing all 5 new operations is online at https://blueskylark.org/CyberFirst/CyberChef

@GCHQDeveloper581 GCHQDeveloper581 self-requested a review March 2, 2026 17:08
Copy link
Contributor

@GCHQDeveloper581 GCHQDeveloper581 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added some very basic tests (since this represented a new "type" of testing and I wanted to establish the file structure).

Please could you add to these to more completely test the desired functionality.

@GCHQDeveloper581
Copy link
Contributor

GCHQDeveloper581 commented Mar 2, 2026

I appreciate that there are tests for the Operations you've added in separate PRs and these will to some extent overlap. However I think it best practice to have unit tests to test these library functions, and then test the higher level functionality of the Operation(s) in their tests.

(this may mean moving some of the detailed testing from the Operations into the unit tests).

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.

2 participants