1
0
mirror of https://github.com/biergaizi/codecrypt synced 2024-06-27 09:18:16 +00:00

math stuff, matrix ops

This commit is contained in:
Mirek Kratochvil 2012-03-26 12:40:05 +02:00
parent ce93665905
commit d8c4d054d3
8 changed files with 159 additions and 17 deletions

@ -2,12 +2,15 @@
#ifndef _CODECRYPT_H_
#define _CODECRYPT_H_
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/* codecrypt matrix/vector/whatever type */
typedef char* ccr_mtx;
typedef uint8_t* ccr_mtx;
/* permutation as a list of transpositions */
typedef int* ccr_perm;
@ -67,12 +70,15 @@ extern "C" {
/* actual functions */
int ccr_mce_gen (struct ccr_mce_pubkey*, struct ccr_mce_privkey*);
int ccr_mce_encrypt (struct ccr_mce_pubkey*, const char*, char*);
int ccr_mce_decrypt (struct ccr_mce_privkey*, const char*, char*);
int ccr_mce_encrypt (struct ccr_mce_pubkey*, const uint8_t*, uint8_t*);
int ccr_mce_decrypt (struct ccr_mce_privkey*, const uint8_t*, uint8_t*);
int ccr_nd_gen (struct ccr_nd_pubkey*, struct ccr_nd_privkey*);
int ccr_nd_encrypt (struct ccr_nd_privkey*, const char*, char*);
int ccr_nd_decrypt (struct ccr_nd_pubkey*, const char*, char*);
int ccr_nd_encrypt (struct ccr_nd_privkey*, const uint8_t*, uint8_t*);
int ccr_nd_decrypt (struct ccr_nd_pubkey*, const uint8_t*, uint8_t*);
void ccr_set_log_func (void (*) (const char*) );
void ccr_set_internal_allocator (void* (*) (size_t), void (*) (void*) );
#ifdef __cplusplus
}

@ -2,15 +2,15 @@
#include "codecrypt.h"
#include "log.h"
typedef void(*)(const char*) logfunc;
static void (*global_log) (const char*) = NULL;
static logfunc global_log=NULL;
//TODO export
void ccr_set_log_func(logfunc x) {
global_log=x;
void ccr_set_log_func (void (*x) (const char*) )
{
global_log = x;
}
void ccr_log(const char*, ...) {
void ccr_log (const char* fmt, ...)
{
if (!global_log) return;
//TODO
}

@ -2,7 +2,7 @@
#ifndef _CCR_LOG_H_
#define _CCR_LOG_H_
void ccr_log(const char*, ...);
void ccr_log (const char*, ...);
#endif

@ -0,0 +1,99 @@
#include "math.h"
#include <stdint.h>
void ccr_vec_xor (int bits, ccr_mtx a, ccr_mtx b, ccr_mtx r)
{
/* possible speedup for wideword architectures
while(bits>=32) {
*(uint32_t*)r = *(uint32_t*)a ^ *(uint32_t*)b;
a+=4;b+=4;r+=4;bits-=32;
} */
while (bits > 0) {
* (uint8_t*) r = * (uint8_t*) a ^ * (uint8_t*) b;
a += 1;
b += 1;
r += 1;
bits -= 8;
}
/* we can safely ignore padding bytes at the end of the vector */
}
void ccr_vec_and (int bits, ccr_mtx a, ccr_mtx b, ccr_mtx r)
{
while (bits > 0) {
* (uint8_t*) r = * (uint8_t*) a & * (uint8_t*) b;
a += 1;
b += 1;
r += 1;
bits -= 8;
}
}
int ccr_vec_parity (int bits, ccr_mtx a)
{
/* first, xor everything to one byte */
uint8_t b = 0;
while (bits >= 8) {
b ^= * (uint8_t*) a;
a += 1;
bits -= 8;
}
if (bits > 0) /* overflow padding bits away */
b ^= * (uint8_t*) a << (8 - bits);
/* squash the result in a single bit */
b ^= b >> 4;
b ^= b >> 2;
b ^= b >> 1;
return b & 1;
}
void ccr_vec_bit_set (ccr_mtx a, int offset, int bit)
{
if (bit)
( (uint8_t*) a) [offset/8] |= (uint8_t) (1 << (offset % 8) );
else
( (uint8_t*) a) [offset/8] &= ~ (uint8_t) (1 << (offset % 8) );
}
uint8_t ccr_vec_bit_get (ccr_mtx a, int offset)
{
return 1 & ( ( (uint8_t*) a) [offset/8] >> (offset % 8) );
}
void ccr_mtx_add (int cols, int rows,
ccr_mtx a, ccr_mtx b, ccr_mtx r)
{
int i, t;
for (i = 0; i < cols; ++i) {
t = ccr_mtx_vec_offset (rows, i);
ccr_vec_xor (rows, a + t, b + t, r + t);
}
}
int ccr_mtx_dotproduct (ccr_mtx a, ccr_mtx b,
int aoff, int aheight, int boff, int len)
{
uint8_t r = 0;
int i;
for (i = 0; i < len; ++i)
r ^= ccr_vec_bit_get (a + ccr_mtx_vec_offset (aheight, i), aoff)
& ccr_vec_bit_get (b + ccr_mtx_vec_offset (len, boff), i);
return r;
}
void ccr_mtx_multiply (int rows, int veclen, int cols,
ccr_mtx a, ccr_mtx b, ccr_mtx r)
{
/* TODO use faster algorithm */
int i, j;
for (i = 0; i < cols; ++i)
for (j = 0; j < rows; ++j)
ccr_vec_bit_set (r + ccr_mtx_vec_offset (rows, i), j,
ccr_mtx_dotproduct (a, b,
j, rows, i,
veclen) );
}

@ -4,8 +4,8 @@
#include "codecrypt.h"
void ccr_mtx_add(int, int, const ccr_mtx, const ccr_mtx, ccr_mtx);
void ccr_mtx_multiply(int, int, int, const ccr_mtx, const ccr_mtx, ccr_mtx);
void ccr_mtx_add (int, int, ccr_mtx, ccr_mtx, ccr_mtx);
void ccr_mtx_multiply (int, int, int, ccr_mtx, ccr_mtx, ccr_mtx);
#endif

@ -2,8 +2,10 @@
#ifndef _CCR_PRNG_H_
#define _CCR_PRNG_H_
#include "codecrypt.h"
int ccr_prng_seed (const char*);
int ccr_prng_bit();
int ccr_prng_int(int);
int ccr_prng_int (int);
#endif

@ -0,0 +1,25 @@
#include "tools.h"
#include <stdlib.h>
static void* (*malloc_func) (size_t) = NULL;
static void (*free_func) (void*) = NULL;
void* ccr_malloc (size_t s)
{
if (malloc_func) return malloc_func (s);
else return malloc (s);
}
void ccr_free (void*p)
{
if (free_func) return free_func (p);
else return free (p);
}
void ccr_set_internal_allocator (void* (*new_malloc) (size_t), void (*new_free) (void*) )
{
malloc_func = new_malloc;
free_func = new_free;
}

@ -0,0 +1,10 @@
#ifndef _CCR_TOOLS_H_
#define _CCR_TOOLS_H_
#include "codecrypt.h"
void* ccr_malloc (size_t);
void ccr_free (void*);
#endif