Started from buttom go to the sky

This commit is contained in:
Raphaël 2024-04-28 19:59:01 +02:00
parent 96215449bd
commit f811e55dea
4781 changed files with 10121 additions and 1743 deletions

View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hash_signed.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 17:26:06 by maiboyer #+# #+# */
/* Updated: 2023/12/11 17:27:12 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/hash/hasher.h"
#include "me/types.h"
void hasher_write_i8(t_hasher *hasher, t_i8 n)
{
hasher_write_bytes(hasher, (t_u8 *)&n, 1);
}
void hasher_write_i16(t_hasher *hasher, t_i16 n)
{
hasher_write_bytes(hasher, (t_u8 *)&n, 2);
}
void hasher_write_i32(t_hasher *hasher, t_i32 n)
{
hasher_write_bytes(hasher, (t_u8 *)&n, 1);
}
void hasher_write_i64(t_hasher *hasher, t_i64 n)
{
hasher_write_bytes(hasher, (t_u8 *)&n, 1);
}
void hasher_write_isize(t_hasher *hasher, t_isize n)
{
hasher_write_bytes(hasher, (t_u8 *)&n, sizeof(t_isize));
}

View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hash_unsigned.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 17:25:23 by maiboyer #+# #+# */
/* Updated: 2023/12/27 16:37:58 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/hash/hasher.h"
#include "me/types.h"
void hasher_write_u8(t_hasher *hasher, t_u8 byte)
{
hasher->hash_bytes(hasher->hasher, &byte, 1);
}
void hasher_write_u16(t_hasher *hasher, t_u16 byte)
{
hasher->hash_bytes(hasher->hasher, (t_u8 *)&byte, 2);
}
void hasher_write_u32(t_hasher *hasher, t_u32 byte)
{
hasher->hash_bytes(hasher->hasher, (t_u8 *)&byte, 4);
}
void hasher_write_u64(t_hasher *hasher, t_u64 byte)
{
hasher->hash_bytes(hasher->hasher, (t_u8 *)&byte, 8);
}

29
stdme/src/hash/hasher.c Normal file
View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hasher.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 15:58:12 by maiboyer #+# #+# */
/* Updated: 2023/12/27 16:44:25 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/hash/hasher.h"
#include "me/types.h"
t_u64 hasher_finish(t_hasher *hasher)
{
return (hasher->finish(hasher->hasher));
}
t_u64 hasher_reset_and_finish(t_hasher *hasher)
{
return (hasher->reset_and_finish(hasher->hasher));
}
void hasher_write_bytes(t_hasher *hasher, t_u8 *bytes, t_usize count)
{
hasher->hash_bytes(hasher->hasher, bytes, count);
}

View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sip13.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 19:32:28 by maiboyer #+# #+# */
/* Updated: 2023/12/27 16:48:13 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/hash/sip.h"
#include "me/hash/sip/sip_utils.h"
#include "me/mem/mem_alloc.h"
t_hasher hasher_sip13_new(void)
{
t_hasher out;
t_sip13 *inner;
inner = mem_alloc(sizeof(t_sip13));
inner->state = create_state_with_key(0, 0);
inner->k0 = 0;
inner->k1 = 0;
out.hasher = inner;
out.hash_bytes = (t_hash_bytes)sip13_write_bytes;
out.finish = (t_hasher_finish)sip13_finish;
out.reset_and_finish = (t_hasher_reset_and_finish)sip13_reset_and_finish;
return (out);
}

View file

@ -0,0 +1,106 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sip_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maix <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 20:02:12 by maix #+# #+# */
/* Updated: 2023/12/11 19:09:32 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/hash/sip.h"
#include "me/hash/sip/sip_utils.h"
#include "me/num/u64.h"
#include "me/num/usize.h"
#include <stdio.h>
#include <stdlib.h>
static t_usize me_min(t_usize lhs, t_usize rhs)
{
if (lhs < rhs)
return (lhs);
return (rhs);
}
static t_usize handle_remaining(t_sip13 *self, t_u8 *msg, t_usize count,
bool *exit_early)
{
t_usize needed;
needed = 0;
*exit_early = false;
if (self->ntail != 0)
{
needed = 8 - self->ntail;
self->tail |= u64_from_bytes(msg, me_min(count, needed)) << (8 \
* self->ntail);
if (count < needed)
{
self->ntail += count;
*exit_early = true;
return (needed);
}
else
{
self->state.v3 ^= self->tail;
compress(&self->state);
self->state.v0 ^= self->tail;
self->ntail = 0;
}
}
return (needed);
}
t_usize read_u8_to_u64(t_u8 p[])
{
return (((t_u64)((p)[0])) | ((t_u64)((p)[1]) << 8) | \
((t_u64)((p)[2]) << 16) | ((t_u64)((p)[3]) << 24) | \
((t_u64)((p)[4]) << 32) | ((t_u64)((p)[5]) << 40) | \
((t_u64)((p)[6]) << 48) | ((t_u64)((p)[7]) << 56));
}
void sip13_write_bytes(t_sip13 *self, t_u8 *msg, t_usize count)
{
bool exit_early;
t_usize needed;
t_usize left;
t_usize i;
t_u64 mi;
self->length += count;
needed = handle_remaining(self, msg, count, &exit_early);
if (exit_early)
return ;
count = count - needed;
left = count & 0x7;
i = needed;
while (i < count - left)
{
mi = read_u8_to_u64(msg + i);
self->state.v3 ^= mi;
compress(&self->state);
self->state.v0 ^= mi;
i += 8;
}
self->tail = u64_from_7bytes(msg, i, left);
self->ntail = left;
}
t_u64 sip13_finish(t_sip13 *self)
{
t_sip_state state;
t_u64 b;
state = self->state;
b = (((t_u64)self->length & 0xff) << 56) | self->tail;
state.v3 ^= b;
compress(&state);
state.v0 ^= b;
state.v2 ^= 0xff;
compress(&state);
compress(&state);
free(self);
return (state.v0 ^ state.v1 ^ state.v2 ^ state.v3);
}

View file

@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sip_utils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 14:29:03 by maiboyer #+# #+# */
/* Updated: 2023/12/27 16:52:17 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/hash/sip/sip_utils.h"
#include "me/num/usize.h"
void compress(t_sip_state *state)
{
state->v0 = state->v0 + state->v1;
state->v1 = usize_rotate_left(state->v1, 13);
state->v1 ^= state->v0;
state->v0 = usize_rotate_left(state->v0, 32);
state->v2 = state->v2 + state->v3;
state->v3 = usize_rotate_left(state->v3, 16);
state->v3 ^= state->v2;
state->v0 = state->v0 + state->v3;
state->v3 = usize_rotate_left(state->v3, 21);
state->v3 ^= state->v0;
state->v2 = state->v2 + state->v1;
state->v1 = usize_rotate_left(state->v1, 17);
state->v1 ^= state->v2;
state->v2 = usize_rotate_left(state->v2, 32);
}
t_sip_state create_state_with_key(t_u64 k0, t_u64 k1)
{
t_sip_state state;
state = (t_sip_state){.v0 = 0, .v1 = 0, .v2 = 0, .v3 = 0};
state.v0 = k0 ^ 0x736f6d6570736575;
state.v1 = k1 ^ 0x646f72616e646f6d;
state.v2 = k0 ^ 0x6c7967656e657261;
state.v3 = k1 ^ 0x7465646279746573;
return (state);
}
t_u64 sip13_reset_and_finish(t_sip13 *self)
{
t_sip_state state;
t_u64 b;
t_u64 ret;
state = self->state;
b = (((t_u64)self->length & 0xff) << 56) | self->tail;
state.v3 ^= b;
compress(&state);
state.v0 ^= b;
state.v2 ^= 0xff;
compress(&state);
compress(&state);
ret = (state.v0 ^ state.v1 ^ state.v2 ^ state.v3);
self->length = 0;
self->state.v0 = self->k0 ^ 0x736f6d6570736575;
self->state.v1 = self->k1 ^ 0x646f72616e646f6d;
self->state.v2 = self->k0 ^ 0x6c7967656e657261;
self->state.v3 = self->k1 ^ 0x7465646279746573;
self->ntail = 0;
return (ret);
}