Commit graph

25 commits

Author SHA1 Message Date
asynts
4709b700bd LibCrypto: Change [XXX]BigInteger::export_data() to use Span. 2020-07-27 19:58:09 +02:00
asynts
68cf22d580 LibCrypto: This method wrote to a const pointer. 2020-07-27 19:58:09 +02:00
Tom
3fdacef07f LibCrypto: Optimize UnsignedBigInteger import_data/export_data
No need to do complicated math to import or export numbers,
just convert the byte stream to words and vice versa.
2020-07-25 12:53:02 +02:00
AnotherTest
02c53fd1f9 LibCrypto: Add bitwise operations (and/or/xor) 2020-06-07 19:29:40 +02:00
Linus Groh
75b4cc13a0 LibCrypto: Fix to_base10() for zero-value BigIntegers
All the magic is happening in a "while != 0" loop, so we ended up with
an empty string for zero-value BigIntegers. Now we just check that
upfront and return early.
2020-06-07 19:29:40 +02:00
AnotherTest
d8208fd37c LibCrypto: Add a simple SignedBigInteger
This patchset adds a simple SignedBigInteger that is entirely defined in
terms of UnsignedBigInteger.

It also adds a NumberTheory::Power function, which is terribly
inefficient, but since the use of exponentiation is very much
discouraged for large inputs, no particular attempts were made
to make it more performant.
2020-06-05 13:29:44 +02:00
Emanuele Torre
937d0be762 Meta: Add a script check the presence of "#pragma once" in header files
.. and make travis run it.

I renamed check-license-headers.sh to check-style.sh and expanded it so
that it now also checks for the presence of "#pragma once" in .h files.

It also checks the presence of a (single) blank line above and below the
"#pragma once" line.

I also added "#pragma once" to all the files that need it: even the ones
we are not check.
I also added/removed blank lines in order to make the script not fail.

I also ran clang-format on the files I modified.
2020-05-29 07:59:45 +02:00
Andreas Kling
444b6c8407 LibCrypto: Cache the "trimmed length" of UnsignedBigIntegers
This avoids repeated traversals of the underlying words and gives a
30% speed-up on "test-crypto -t pk" :^)
2020-05-07 12:23:09 +02:00
Andreas Kling
e6922c0d40 LibCrypto: Manage vector sizing manually in performance-critical places
Use Vector::resize_and_keep_capacity() to resize BigInt vectors to just
the right size without risking deallocation. Then do direct indexed
accesses to the underlying words (or use memset/memcpy.)

This gives a ~40% speed-up on the RSA tests in "test-crypto -t pk" :^)
2020-05-03 19:14:50 +02:00
DexesTTP
8aeccf4f02 LibCrypto: Added BigInteger 'division by u16' operator 2020-05-03 14:31:26 +02:00
DexesTTP
28ea347e55 LibCrypto: Added static non-allocating UnsignedBigInteger operators
This changes the plus, minus, etc... operators from UnsignedBigInteger to use a
static helper method. The static methods do not allocate any variables, instead
all the required BigInteger output and temporary variables are required on call
as parameters.

This change already optimizes the number of allocations in complex operations
such as multiply or divide, by having a single allocation per call (instead of
one per loop).

This new API also provides a way to limit the number of allocations for complex
computations in other parts of the code. This is done by using these helpers in
any place that currently makes use of the standard operators.
2020-05-03 14:31:26 +02:00
DexesTTP
d008a38f93 LibCrypto: Small fixes in BigInteger & test-crypto 2020-05-03 14:31:26 +02:00
AnotherTest
c9321b4f00 LibCrypto: Make UnsignedBigInteger as fast as architecturally possible
This commit attempts to make UnsignedBigInteger as fast as possible
without changing the underlaying architecture.
This effort involves
- Preallocating space for vector operations
- Avoiding calls to computationally expensive functions
- Inlining or flattening functions (sensibly)
2020-05-02 12:24:10 +02:00
AnotherTest
adab43987d LibCrypto: Rename UnsignedBigInteger APIs to match their actions 2020-05-02 12:24:10 +02:00
AnotherTest
e366416d51 LibCrypto: Preallocate capacity and cache trimmed_length() in UnsignedBigInteger 2020-05-02 12:24:10 +02:00
AnotherTest
8d20a526e5 LibCrypto: Preallocate 128 words of space for UnsignedBigInteger
This shaves off 1 second of runtime
2020-05-02 12:24:10 +02:00
AnotherTest
6b742c69bd LibCrypto: Add ::import_data() and ::export_data() to UnsignedBigInteger
These functions allow conversion to-and-from big-endian buffers
This commit also adds a ""_bigint operator for easy bigint use
2020-05-02 12:24:10 +02:00
Itamar
c52d3e65b9 LibCrypto: Cleanup UnsignedBigInteger a bit
- Add missing 'explicit' to the constructor
- Remove unneeded 'AK::' in AK::Vector
- Avoid copying 'words' in constructor
2020-05-02 12:24:10 +02:00
Itamar
2125a4debb LibCrypto: Add base-10 string de/serialization methods for bigint 2020-05-02 12:24:10 +02:00
Itamar
709c691f38 LibCrypto: Fix bug in big int subtraction
A regression test was added to the suite.

This commit also generally simplifies the subtraction method.
2020-05-02 12:24:10 +02:00
Itamar
0d2777752e LibCrypto: Add UnsignedBigInteger division
The division operation returns both the quotient and the remainder.
2020-05-02 12:24:10 +02:00
Itamar
2959c4a5e9 LibCrypto: Add UnsignedBigInteger multiplication
Also added documentation for the runtime complexity of some operations.
2020-05-02 12:24:10 +02:00
Itamar
2843dce498 LibCrypto: Fix a bug in big int addition
There was a bug when dealing with a carry when the addition
result for the current word was UINT32_MAX.

This commit also adds a regression test for the bug.
2020-05-02 12:24:10 +02:00
Itamar
e0cf40518c LibCrypto: Add UnsignedBigInteger subtraction and comparison 2020-05-02 12:24:10 +02:00
Itamar
6201f741d4 LibCrypto: Add UnsignedBigInteger and implement addition
UnsignedBigInteger stores an unsigned ainteger of arbitrary length.

A big integer is represented  as a vector of word. Each
word is an unsigned int.
2020-05-02 12:24:10 +02:00