Skip to content
Snippets Groups Projects
Commit cddca9ff authored by Typhaine Corroller's avatar Typhaine Corroller
Browse files

fin cours

parents 97edf8aa 84c3d114
No related branches found
No related tags found
No related merge requests found
#include <stdlib.h>
#include <stdio.h>
#include <gmp.h>
typedef mpz_t* bignum_t;
......@@ -15,6 +16,13 @@ bignum_t bignum_from_int(int n){
return x;
}
bignum_t bignum_from_str(char* s, int base){
bignum_t x = (bignum_t) malloc(sizeof(mpz_t));
mpz_set_str(*x, s, base);
return x;
}
bignum_t bignum_copy(bignum_t x){
bignum_t y = bignum_zero();
mpz_set(*y, *x);
......@@ -91,6 +99,24 @@ bignum_t bignum_pow(bignum_t x, bignum_t exp){
return z;
}
bignum_t bignum_gcd(bignum_t a, bignum_t b){
bignum_t g = bignum_zero();
mpz_gcd(*g, *a, *b);
return g;
}
bignum_t bignum_from_MD5(unsigned char* md5sum, size_t len){
char s[100];
size_t r = 0;
bignum_t res = bignum_zero();
for(size_t i = 0; i < len; i++)
r = r + sprintf(s+r, "%x", md5sum[i]);
mpz_set_str(*res, s, 16);
return res;
}
char* bignum_to_str(bignum_t x){
return mpz_get_str(NULL, 10, *x);
}
......@@ -7,6 +7,7 @@ typedef mpz_t* bignum_t;
bignum_t bignum_zero();
bignum_t bignum_from_int(int);
bignum_t bignum_from_str(char*, int);
bignum_t bignum_copy(bignum_t);
void bignum_destroy(bignum_t);
......@@ -24,5 +25,9 @@ bignum_t bignum_mod(bignum_t, bignum_t);
bignum_t bignum_powm(bignum_t, bignum_t, bignum_t);
bignum_t bignum_pow(bignum_t, bignum_t);
bignum_t bignum_gcd(bignum_t, bignum_t);
bignum_t bignum_from_MD5(unsigned char* md5sum, size_t len);
char* bignum_to_str(bignum_t);
#endif
struct node_t {
unsigned long int key;
void* val;
struct node_t* left;
struct node_t* right;
};
typedef struct bst_t {
// Use this to compute a key from a val
// It is not really a hash function, the key must be unique
unsigned long int (*h)(void*);
unsigned long int size;
struct node_t* tree;
} bst_t;
bst_t bst_empty(void){
}
int bst_is_empty(bst_t t){
}
bst_t bst_add(bst_t t, void* x){
}
void bst_destroy(bst_t t, void (*free_void)(void*)){
}
int bst_in(bst_t t, void* x){
}
unsigned long int bst_size(bst_t t){
}
#ifndef __BST__
#define __BST__
struct node_t {
unsigned long int key;
void* val;
struct node_t* left;
struct node_t* right;
};
typedef struct bst_t {
unsigned long int size;
struct node_t* tree;
} bst_t;
unsigned long int hash(void*);
bst_t bst_empty(void);
int bst_is_empty(bst_t);
bst_t bst_add(bst_t, void*);
void bst_destroy(bst_t, void (*free_void)(void*));
int bst_in(bst_t, void*);
unsigned long int bst_size(bst_t);
#endif
#include <stddef.h>
#include <stdlib.h>
#include "list.h"
#define ARRAY_CAPACITY 1 << 16
......
#ifndef __HASH_TBL__
#define __HASH_TBL__
#include "linked_list.h"
#include "list.h"
struct hash_tbl {
size_t (*h)(void*);
......@@ -19,7 +19,7 @@ hash_tbl htbl_empty(size_t (*h)(void*),
void (*free)(void*));
int htbl_add(hash_tbl, void*);
int htbl_in(hash_tbl, void*);
void htbl_free(hash_tbl);
void htbl_destroy(hash_tbl);
#endif
#include <stdio.h>
#include <stdlib.h>
......@@ -24,20 +25,8 @@ list_t list_tail(list_t l){
return NULL ;
}
/*
list_t list_push(list_t l, void* x){
list_t head = malloc(sizeof(struct cell_t));
head = l ;
while (!list_is_empty(l)) {
l = list_tail(l) ;
}
l -> val = x ;
l -> next = NULL ;
return head ;
}
*/
/*
list_t list_push(list_t l, void* x){
list_t head = malloc(sizeof(struct cell_t));
head -> val = x ;
......@@ -50,16 +39,33 @@ list_t list_push(list_t l, void* x){
}
//list_is_empty(l) ? 1 : l -> id + 1 ;
return head ;
}
}*/
unsigned long int list_len(list_t l){
while (list_tail(l) != NULL) {
/*
list_t list_push(list_t l, void* x){
list_t head = malloc(sizeof(struct cell_t));
head = l ;
while (!list_is_empty(l)) {
l = list_tail(l) ;
}
return l -> id ;
l -> val = x ;
l -> next = NULL ;
return head ;
}*/
list_t list_push(list_t l, void* x){
list_t res = malloc(sizeof(struct cell_t));
res->next = l;
res->val = x;
res->id = list_is_empty(l) ? 1 : 1 + l->id;
return res;
}
// return the found element or NULL
/*
void* list_in(list_t l, void* x, int (*eq)(void*, void*)){
while (!list_is_empty(l)) {
if (eq(x, l -> val)) {
......@@ -68,22 +74,46 @@ void* list_in(list_t l, void* x, int (*eq)(void*, void*)){
l = list_tail(l) ;
}
return NULL ;
}*/
void* list_in(list_t l, void* x, int (*eq)(void*, void*)){
while(!list_is_empty(l))
if(eq(x, l->val))
return l->val;
else
l = l->next;
return NULL;
}
void* list_top(list_t l){
//si liste vide, return NULL, sinon l -> val
return list_is_empty(l) ? NULL : l -> val ;
}
/*
unsigned long int list_len(list_t l){
while (list_tail(l) != NULL) {
l = list_tail(l) ;
}
return l -> id ;
}*/
unsigned long int list_len(list_t l){
return list_is_empty(l) ? 0 : l->id;
}
void* list_pop(list_t* l){
if (list_is_empty(*l)) {
if(list_is_empty(*l))
return NULL;
}
void * tmp = (*l) -> val ;
list_t head = (*l) -> next ;
list_t new_head = (*l) -> next;
void* res = (*l) -> val;
free(*l);
*l = head ;
return tmp ;
*l = new_head;
return res;
}
void list_destroy(list_t l, void (*free)(void*)){
......
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <openssl/md5.h>
#include "bignum.h"
#define BG(n) bignum_from_int(n)
......@@ -35,11 +36,26 @@ void test_str(){
printf("test_str OK\n");
}
void test_from_md5(){
bignum_t t, tt;
unsigned char digest[MD5_DIGEST_LENGTH];
char x[] = "coucou";
char xx[] = "719d8179d0cea7774b518d58fd52fc4";
MD5((const unsigned char*)x, 7, digest);
t = bignum_from_MD5(digest, MD5_DIGEST_LENGTH);
tt = bignum_from_str(xx, 16);
assert(bignum_eq(t, tt));
printf("test_from_md5 OK\n");
}
int main(){
printf("=== BIGNUM tests ===\n");
test_arithmetic();
test_cmp();
test_str();
test_from_md5();
return 0;
}
......
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "hash_tbl.h"
// char* val
size_t hash(void* val){
return (*((char*) val)) & ((1<<5) - 1);
}
int eq(void* a, void* b){
char* aa = (char*) a;
char* bb = (char*) b;
for(int i = 0; i <16; i++)
if(aa[i] != bb[i])
return 0;
return 1;
}
void test_empty() {
hash_tbl htbl = htbl_empty(&hash, &eq, &free);
assert(htbl->size == 0);
printf("test_empty OK\n");
}
void test_add() {
char a[] = "Coucou 1";
char b[] = "Coucou 2";
hash_tbl htbl = htbl_empty(&hash, &eq, &free);
htbl_add(htbl, a);
assert(htbl->size == 1);
assert(list_in(htbl->array[hash(a)], a, &eq));
htbl_add(htbl, a);
assert(htbl->size == 1);
assert(list_in(htbl->array[hash(a)], a, &eq));
htbl_add(htbl, b);
assert(htbl->size == 2);
assert(list_in(htbl->array[hash(a)], a, &eq));
assert(list_in(htbl->array[hash(b)], b, &eq));
printf("test_add OK\n");
}
void test_in() {
char a[] = "Coucou 1";
char b[] = "Coucou 2";
hash_tbl htbl = htbl_empty(&hash, &eq, &free);
htbl_add(htbl, a);
assert(htbl_in(htbl, a));
assert(!htbl_in(htbl, b));
htbl_add(htbl, b);
assert(htbl_in(htbl, a));
assert(htbl_in(htbl, b));
printf("test_in OK\n");
}
void test_destroy() {
char a[] = "Coucou 1";
char b[] = "Coucou 2";
hash_tbl htbl = htbl_empty(&hash, &eq, &free);
htbl_add(htbl, a);
htbl_add(htbl, b);
htbl_destroy(htbl);
printf("test_destroy OK\n");
}
int main() {
printf("=== HTBL tests ===\n");
test_empty();
test_add();
test_in();
test_destroy();
return 0;
}
#include <stdio.h>
#include "bignum.h"
unsigned long int gcd(unsigned long int a, unsigned long int b){
unsigned long int old_r, r = 1;
if(a < b)
return gcd(b,a);
while(r != 0){
old_r = r;
r = a % b;
a = b;
b = r;
}
return old_r;
}
unsigned long int f(unsigned long int x){
return (x*x) + 1 ;
}
unsigned long int facto(unsigned long int a){
unsigned long int i = 1l;
while (gcd(a, i) == 1){
i ++ ;
//i = f(i);
//printf("%lu\n", i);
}
return gcd(a, i);
}
int main() {
// En utilisant l'algorithme rho de Pollard, factorisez les entiers suivants
unsigned long int n1 = 17 * 113;
unsigned long int n2 = 239 * 431;
unsigned long int n3 = 3469 * 4363;
unsigned long int n4 = 15241 * 18119;
unsigned long int n5 = 366127l * 416797l;
unsigned long int n6 = 15651941l * 15485863l;
bignum_t n7, n8;
n7 = bignum_mul(bignum_sub(bignum_pow(bignum_from_int(2), bignum_from_int(127)),
bignum_from_int(1)),
bignum_sub(bignum_pow(bignum_from_int(2), bignum_from_int(61)),
bignum_from_int(1)));
n8 = bignum_mul(bignum_sub(bignum_pow(bignum_from_int(2), bignum_from_int(607)),
bignum_from_int(1)),
bignum_sub(bignum_pow(bignum_from_int(2), bignum_from_int(2203)),
bignum_from_int(1)));
printf("\n-------------------------------------\n");
printf("n1 : %lu\n", facto(n1));
printf("n2 : %lu\n", facto(n2));
printf("n3 : %lu\n", facto(n3));
printf("n4 : %lu\n", facto(n4));
printf("n5 : %lu\n", facto(n5));
printf("n6 : %lu\n", facto(n6));
//printf("n7 : %lu\n", facto(n7));
//printf("n8 : %lu\n", facto(n8));
printf("PGCD(42,24) = %lu\n", gcd(42,24));
printf("PGCD(42,24) = %s\n", bignum_to_str(bignum_gcd(bignum_from_int(42),bignum_from_int(24))));
printf("\n-------------------------------------\n");
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment