#ifndef COSMOPOLITAN_THIRD_PARTY_ARGON2_CORE_H_
#define COSMOPOLITAN_THIRD_PARTY_ARGON2_CORE_H_
#include "third_party/argon2/argon2.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_

enum argon2_core_constants {
  /* Memory block size in bytes */
  ARGON2_BLOCK_SIZE = 1024,
  ARGON2_QWORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 8,
  ARGON2_OWORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 16,
  ARGON2_HWORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 32,
  ARGON2_512BIT_WORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 64,
  /* Number of pseudo-random values generated by one call to Blake in
     Argon2i to generate reference block positions */
  ARGON2_ADDRESSES_IN_BLOCK = 128,
  /* Pre-hashing digest length and its extension*/
  ARGON2_PREHASH_DIGEST_LENGTH = 64,
  ARGON2_PREHASH_SEED_LENGTH = 72
};

/*
 * Structure for the (1KB) memory block implemented as 128 64-bit words.
 * Memory blocks can be copied, XORed. Internal words can be accessed by [] (no
 * bounds checking).
 */
typedef struct block_ {
  uint64_t v[ARGON2_QWORDS_IN_BLOCK];
} block;

void init_block_value(block *, uint8_t);
void copy_block(block *, const block *);
void xor_block(block *, const block *);

/*
 * Argon2 instance: memory pointer, number of passes, amount of memory,
 * type, and derived values. Used to evaluate the number and location of
 * blocks to construct in each thread.
 */
typedef struct Argon2_instance_t {
  block *memory; /* Memory pointer */
  uint32_t version;
  uint32_t passes;        /* Number of passes */
  uint32_t memory_blocks; /* Number of blocks in memory */
  uint32_t segment_length;
  uint32_t lane_length;
  uint32_t lanes;
  uint32_t threads;
  argon2_type type;
  int print_internals;         /* whether to print the memory blocks */
  argon2_context *context_ptr; /* points back to original context */
} argon2_instance_t;

/*
 * Argon2 position: where we construct the block right now. Used to
 * distribute work between threads.
 */
typedef struct Argon2_position_t {
  uint32_t pass;
  uint32_t lane;
  uint8_t slice;
  uint32_t index;
} argon2_position_t;

/*Struct that holds the inputs for thread handling FillSegment*/
typedef struct Argon2_thread_data {
  argon2_instance_t *instance_ptr;
  argon2_position_t pos;
} argon2_thread_data;

/* argon2 core functions */
int allocate_memory(const argon2_context *, uint8_t **, size_t, size_t);
void free_memory(const argon2_context *, uint8_t *, size_t, size_t);
void clear_internal_memory(void *, size_t);
uint32_t index_alpha(const argon2_instance_t *, const argon2_position_t *,
                     uint32_t, int);
int validate_inputs(const argon2_context *);
void initial_hash(uint8_t *, argon2_context *, argon2_type);
void fill_first_blocks(uint8_t *, const argon2_instance_t *);
int initialize(argon2_instance_t *, argon2_context *);
void finalize(const argon2_context *, argon2_instance_t *);
void fill_segment(const argon2_instance_t *, argon2_position_t);
int fill_memory_blocks(argon2_instance_t *);

COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_THIRD_PARTY_ARGON2_CORE_H_ */
