<commit_before>
#include <stdint.h>
#include <string.h>

#include "crypto_verify_16.h"
#include "utils.h"
#include "poly1305_sse2.h"
#include "../onetimeauth_poly1305.h"

#if defined(HAVE_TI_MODE) && defined(HAVE_EMMINTRIN_H)

#pragma GCC target("sse2")

#include <emmintrin.h>

typedef __m128i xmmi;

#if defined(__SIZEOF_INT128__)
typedef unsigned __int128 uint128_t;
#else
typedef unsigned uint128_t __attribute__((mode(TI)));
#endif

#if defined(_MSC_VER)
# define POLY1305_NOINLINE __declspec(noinline)
#elif defined(__GNUC__)
# define POLY1305_NOINLINE __attribute__((noinline))
#else
# define POLY1305_NOINLINE
#endif

#define poly1305_block_size 32

enum poly1305_state_flags_t {
    poly1305_started = 1,
    poly1305_final_shift8 = 4,
    poly1305_final_shift16 = 8,
    poly1305_final_r2_r = 16, /* use [r^2,r] for the final block */
    poly1305_final_r_1 = 32, /* use [r,1] for the final block */
};

typedef struct poly1305_state_internal_t {
    union {
        uint64_t h[3];
        uint32_t hh[10];
    };                       /*  40 bytes  */
    uint32_t R[5];           /*  20 bytes  */
    uint32_t R2[5];          /*  20 bytes  */
    uint32_t R4[5];          /*  20 bytes  */
    uint64_t pad[2];         /*  16 bytes  */
    uint64_t flags;          /*   8 bytes  */
    unsigned long long leftover; /* 8 bytes */
    unsigned char buffer[poly1305_block_size]; /* 32 bytes */
} poly1305_state_internal_t;   /* 164 bytes total */

/* copy 0-31 bytes */
static inline void
poly1305_block_copy31(unsigned char *dst, const unsigned char *src, unsigned long long bytes)
{
    unsigned long long offset = src - dst;

    if (bytes & 16) { _mm_store_si128((xmmi *)(void *)dst, _mm_loadu_si128((xmmi *)(void *)(dst + offset))); dst += 16; }
    if (bytes &  8) { *(uint64_t *)(void *)dst = *(uint64_t *)(void *)(dst + offset); dst += 8; }
    if (bytes &  4) { *(uint32_t *)dst = *(uint32_t *)(dst + offset); dst += 4; }
    if (bytes &  2) { *(uint16_t *)dst = *(uint16_t *)(dst + offset); dst += 2; }
    if (bytes &  1) { *( unsigned char *)dst = *( unsigned char *)(dst + offset);           }
}

static POLY1305_NOINLINE void
poly1305_init_ext(poly1305_state_internal_t *st,
                  const unsigned char key[32], unsigned long long bytes)
{
    uint32_t *R;
    uint128_t d[3],m0;
    uint64_t r0,r1,r2;
    uint32_t rp0,rp1,rp2,rp3,rp4;
    uint64_t rt0,rt1,rt2,st2,c;
    uint64_t t0,t1;
    unsigned long long i;

    if (!bytes) bytes = ~(unsigned long long)0;

    /* H = 0 */
    _mm_storeu_si128((xmmi *)(void *)&st->hh[0], _mm_setzero_si128());
    _mm_storeu_si128((xmmi *)(void *)&st->hh[4], _mm_setzero_si128());
    _mm_storeu_si128((xmmi *)(void *)&st->hh[8], _mm_setzero_si128());<commit_msg>Don't assume that substracting unrelated pointers is acceptable. Having to increment two pointers instead of one is the price to pay for portability, but it's not that big of a deal here.<commit_after>
#include <stdint.h>
#include <string.h>

#include "crypto_verify_16.h"
#include "utils.h"
#include "poly1305_sse2.h"
#include "../onetimeauth_poly1305.h"

#if defined(HAVE_TI_MODE) && defined(HAVE_EMMINTRIN_H)

#pragma GCC target("sse2")

#include <emmintrin.h>

typedef __m128i xmmi;

#if defined(__SIZEOF_INT128__)
typedef unsigned __int128 uint128_t;
#else
typedef unsigned uint128_t __attribute__((mode(TI)));
#endif

#if defined(_MSC_VER)
# define POLY1305_NOINLINE __declspec(noinline)
#elif defined(__GNUC__)
# define POLY1305_NOINLINE __attribute__((noinline))
#else
# define POLY1305_NOINLINE
#endif

#define poly1305_block_size 32

enum poly1305_state_flags_t {
    poly1305_started = 1,
    poly1305_final_shift8 = 4,
    poly1305_final_shift16 = 8,
    poly1305_final_r2_r = 16, /* use [r^2,r] for the final block */
    poly1305_final_r_1 = 32, /* use [r,1] for the final block */
};

typedef struct poly1305_state_internal_t {
    union {
        uint64_t h[3];
        uint32_t hh[10];
    };                       /*  40 bytes  */
    uint32_t R[5];           /*  20 bytes  */
    uint32_t R2[5];          /*  20 bytes  */
    uint32_t R4[5];          /*  20 bytes  */
    uint64_t pad[2];         /*  16 bytes  */
    uint64_t flags;          /*   8 bytes  */
    unsigned long long leftover; /* 8 bytes */
    unsigned char buffer[poly1305_block_size]; /* 32 bytes */
} poly1305_state_internal_t;   /* 164 bytes total */

/* copy 0-31 bytes */
static inline void
poly1305_block_copy31(unsigned char *dst, const unsigned char *src, unsigned long long bytes)
{
    if (bytes & 16) {
        _mm_store_si128((xmmi *) (void *) dst,
                        _mm_loadu_si128((const xmmi *) (const void *) src));
        src += 16; dst += 16;
    }
    if (bytes &  8) { memcpy(dst, src, 8); src += 8; dst += 8; }
    if (bytes &  4) { memcpy(dst, src, 4); src += 4; dst += 4; }
    if (bytes &  2) { memcpy(dst, src, 2); src += 2; dst += 2; }
    if (bytes &  1) { *dst = *src; }
}

static POLY1305_NOINLINE void
poly1305_init_ext(poly1305_state_internal_t *st,
                  const unsigned char key[32], unsigned long long bytes)
{
    uint32_t *R;
    uint128_t d[3],m0;
    uint64_t r0,r1,r2;
    uint32_t rp0,rp1,rp2,rp3,rp4;
    uint64_t rt0,rt1,rt2,st2,c;
    uint64_t t0,t1;
    unsigned long long i;

    if (!bytes) bytes = ~(unsigned long long)0;

    /* H = 0 */
    _mm_storeu_si128((xmmi *)(void *)&st->hh[0], _mm_setzero_si128());
    _mm_storeu_si128((xmmi *)(void *)&st->hh[4], _mm_setzero_si128());
    _mm_storeu_si128((xmmi *)(void *)&st->hh[8], _mm_setzero_si128());
{'fraction_non_alphanumeric': 0.13826714801444043, 'fraction_numerical': 0.0855595667870036, 'mean_word_length': 3.178733031674208, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2368718', 'n_tokens_mistral': 2413, 'n_tokens_neox': 2107, 'n_words': 516}