ladybird/Libraries/LibCrypto/BigInt/TommathForward.h
devgianlu 4b3715ccba
Some checks are pending
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Build Dev Container Image / build (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (arm64, macos-15, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (x86_64, ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
LibCrypto: Replace {Unsigned,Signed}BigInteger impl with LibTomMath
Replace the implementation of maths in `UnsignedBigInteger`
and `SignedBigInteger` with LibTomMath. This gives benefits in terms of
less code to maintain, correctness and speed.

These changes also remove now-unsued methods and improve the error
propagation for functions allocating lots of memory. Additionally, the
new implementation is always trimmed and won't have dangling zeros when
exporting it.
2025-05-23 11:57:21 +02:00

36 lines
1.1 KiB
C

/*
* Copyright (c) 2025, Altomani Gianluca <altomanigianluca@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
extern "C" {
typedef uint64_t mp_digit;
typedef int mp_sign;
// This is a workaround for the fact that Tommath doesn't have a proper
// header file. It uses an anonymous struct to define the mp_int struct
// which makes it impossible to forward declare.
// Declare the mp_int struct with the same layout as the one in tommath.h
// and check that the layout is the same while avoiding a conflicting
// definition error (BN_H_ is defined in tommath.h).
// When importing this file it is important to always do it AFTER tommath.h.
typedef struct {
int used, alloc;
mp_sign sign;
mp_digit* dp;
} mp_int_;
#ifndef BN_H_
typedef mp_int_ mp_int;
#else
static_assert(sizeof(mp_int_) == sizeof(mp_int));
static_assert(offsetof(mp_int_, used) == offsetof(mp_int, used));
static_assert(offsetof(mp_int_, alloc) == offsetof(mp_int, alloc));
static_assert(offsetof(mp_int_, sign) == offsetof(mp_int, sign));
static_assert(offsetof(mp_int_, dp) == offsetof(mp_int, dp));
#endif
}