cosmopolitan/third_party/mbedtls/bigshift.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

121 lines
3.8 KiB
C
Raw Normal View History

/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:4;tab-width:4;coding:utf-8 -*-│
vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright The Mbed TLS Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 │
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "libc/macros.h"
#include "libc/str/str.h"
#include "third_party/mbedtls/bignum.h"
#include "third_party/mbedtls/bignum_internal.h"
#include "third_party/mbedtls/platform.h"
typedef long long xmm_t __attribute__((__vector_size__(16), __aligned__(1)));
static inline void shrd(mbedtls_mpi_uint *p, size_t n, size_t j, size_t m,
char k)
{
mbedtls_mpi_uint x, y, *e, *f;
f = p + m;
if (n)
{
y = 0;
x = p[j];
e = p + n;
for (; ++p < e; x = y)
{
y = p[j];
p[-1] = x >> k | y << (biL - k);
}
p[-1] = x >> k;
}
while (p < f)
*p++ = 0;
}
static inline void shld(mbedtls_mpi_uint *p, size_t n, size_t m, char k)
{
size_t i;
mbedtls_mpi_uint x, y;
MBEDTLS_ASSERT(n > m);
i = n - 1;
y = p[i - m];
for (; i - m > 0; --i, y = x)
{
x = p[i - m - 1];
p[i] = y << k | x >> (64 - k);
}
p[i] = y << k;
while (i)
{
p[--i] = 0;
}
}
/**
* Performs left shift on big number: X <<= k
*/
int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t k)
{
int r;
size_t b, n, m, l;
MPI_VALIDATE_RET(X);
l = mbedtls_mpi_bitlen(X);
b = l + k;
n = BITS_TO_LIMBS(b);
m = k / biL;
k = k % biL;
if (n > X->n && (r = mbedtls_mpi_grow(X, n)))
return r;
if (k)
{
shld(X->p, X->n, m, k);
}
else if (m)
{
Make numerous improvements - Python static hello world now 1.8mb - Python static fully loaded now 10mb - Python HTTPS client now uses MbedTLS - Python REPL now completes import stmts - Increase stack size for Python for now - Begin synthesizing posixpath and ntpath - Restore Python \N{UNICODE NAME} support - Restore Python NFKD symbol normalization - Add optimized code path for Intel SHA-NI - Get more Python unit tests passing faster - Get Python help() pagination working on NT - Python hashlib now supports MbedTLS PBKDF2 - Make memcpy/memmove/memcmp/bcmp/etc. faster - Add Mersenne Twister and Vigna to LIBC_RAND - Provide privileged __printf() for error code - Fix zipos opendir() so that it reports ENOTDIR - Add basic chmod() implementation for Windows NT - Add Cosmo's best functions to Python cosmo module - Pin function trace indent depth to that of caller - Show memory diagram on invalid access in MODE=dbg - Differentiate stack overflow on crash in MODE=dbg - Add stb_truetype and tools for analyzing font files - Upgrade to UNICODE 13 and reduce its binary footprint - COMPILE.COM now logs resource usage of build commands - Start implementing basic poll() support on bare metal - Set getauxval(AT_EXECFN) to GetModuleFileName() on NT - Add descriptions to strerror() in non-TINY build modes - Add COUNTBRANCH() macro to help with micro-optimizations - Make error / backtrace / asan / memory code more unbreakable - Add fast perfect C implementation of μ-Law and a-Law audio codecs - Make strtol() functions consistent with other libc implementations - Improve Linenoise implementation (see also github.com/jart/bestline) - COMPILE.COM now suppresses stdout/stderr of successful build commands
2021-09-28 05:58:51 +00:00
memmove(X->p + m, X->p, (X->n - m) * ciL);
explicit_bzero(X->p, m * ciL);
}
return 0;
}
void ShiftRightPure(mbedtls_mpi_uint *p, size_t n, unsigned char k) {
shrd(p, n, 0, n, k);
}
/**
* Performs right arithmetic shift on big number: X >>= k
*/
int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t k)
{
size_t n;
MPI_VALIDATE_RET(X);
k = MIN(k, X->n * biL);
n = k / biL;
k = k % biL;
if (k)
{
if (!n)
ShiftRight(X->p, X->n, k);
else
shrd(X->p, X->n - n, n, X->n, k);
}
else if (n)
{
Make numerous improvements - Python static hello world now 1.8mb - Python static fully loaded now 10mb - Python HTTPS client now uses MbedTLS - Python REPL now completes import stmts - Increase stack size for Python for now - Begin synthesizing posixpath and ntpath - Restore Python \N{UNICODE NAME} support - Restore Python NFKD symbol normalization - Add optimized code path for Intel SHA-NI - Get more Python unit tests passing faster - Get Python help() pagination working on NT - Python hashlib now supports MbedTLS PBKDF2 - Make memcpy/memmove/memcmp/bcmp/etc. faster - Add Mersenne Twister and Vigna to LIBC_RAND - Provide privileged __printf() for error code - Fix zipos opendir() so that it reports ENOTDIR - Add basic chmod() implementation for Windows NT - Add Cosmo's best functions to Python cosmo module - Pin function trace indent depth to that of caller - Show memory diagram on invalid access in MODE=dbg - Differentiate stack overflow on crash in MODE=dbg - Add stb_truetype and tools for analyzing font files - Upgrade to UNICODE 13 and reduce its binary footprint - COMPILE.COM now logs resource usage of build commands - Start implementing basic poll() support on bare metal - Set getauxval(AT_EXECFN) to GetModuleFileName() on NT - Add descriptions to strerror() in non-TINY build modes - Add COUNTBRANCH() macro to help with micro-optimizations - Make error / backtrace / asan / memory code more unbreakable - Add fast perfect C implementation of μ-Law and a-Law audio codecs - Make strtol() functions consistent with other libc implementations - Improve Linenoise implementation (see also github.com/jart/bestline) - COMPILE.COM now suppresses stdout/stderr of successful build commands
2021-09-28 05:58:51 +00:00
memmove(X->p, X->p + n, (X->n - n) * ciL);
explicit_bzero(X->p + X->n - n, n * ciL);
}
return 0;
}