Skip to content
Snippets Groups Projects
Commit c2568d8c authored by Mohamed Akaddar's avatar Mohamed Akaddar
Browse files

Fin D'implémentation d'Algorithme rho Pollard et floyd

Merge remote-tracking branch 'prof/main' into main
parents 9509036c 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 <stddef.h>
#include <stdlib.h>
struct cell_t {
void* val;
unsigned long int id;
......@@ -15,63 +13,55 @@ list_t list_empty(){
return NULL;
}
int list_is_empty(list_t l)
{
return l==list_empty();
}
list_t list_push(list_t l, void* x)
{
list_t head = malloc(sizeof(struct cell_t));
head->next = l;
head->val = x;
head->id = list_is_empty(l) ? 1 : l->id+1;
return head;
int list_is_empty(list_t l){
return l == NULL;
}
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;
}
list_t list_tail(list_t l)
{
list_t list_tail(list_t l){
return list_is_empty(l) ? NULL : l->next;
}
void* list_pop(list_t* l)
{
void* list_pop(list_t* l){
if(list_is_empty(*l))
return NULL;
void* res = (*l)->val;
list_t new_head = (*l)->next;
void* res = (*l)->val;
free(*l);
*l = new_head;
return res;
>>>>>>> prof/main
}
void* list_top(list_t l){
return list_is_empty(l) ? NULL : l->val;
}
void list_destroy(list_t l, void (*free)(void*))
{
void list_destroy(list_t l, void (*free)(void*)){
>>>>>>> prof/main
while(!list_is_empty(l))
free(list_pop(&l));
}
// 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(l->val,x))
if(eq(x, l->val))
return l->val;
else
l = l->next;
return NULL;
}
}
unsigned long int list_len(list_t l){
return list_is_empty(l) ? 0 : l->id;
......
#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;
}
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("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))));
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment