-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto_math.c
95 lines (73 loc) · 1.68 KB
/
crypto_math.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* =====================================================================================
*
* Filename: crypto_math.c
*
* Description: This file implements the general maths routines used un Cryptography
*
* Version: 1.0
* Created: 10/24/2021 01:01:00 AM
* Revision: none
* Compiler: gcc
*
* Author: ABHISHEK SAGAR (), sachinites@gmail.com
* Organization: Cisco Systems ( March 2021 - Present, Previous : Juniper Networks
*
* =====================================================================================
*/
#include <stdio.h>
#include "crypto_math.h"
uint64_t
gcd(int64_t a, int64_t b) {
/* because
* gcd (a,b) = gcd(-a, b) = gcd(a, -b) = gcd (-a, -b)*/
if (a < 0) a = a * -1;
if (b < 0) b = b * -1;
if (a < b) {
int64_t temp;
temp = a;
a = b;
b = temp;
}
int64_t rem = (a % b);
if (!rem) return b;
return gcd(b, rem);
}
void print_bits32 (uint32_t n) {
int i = 0;
for (i = 0; i < 32; i++) {
if (n & (1 << 31)) {
printf ("1");
}
else {
printf("0");
}
n = n << 1;
}
}
void print_bits64 (uint64_t n) {
int i = 0;
for (i = 0; i < 64; i++) {
if (n & (1 << 63)) {
printf ("1");
}
else {
printf("0");
}
n = n << 1;
}
}
void print_hex(uint32_t n) {
printf ("%x", n);
}
#if 0
int
main(int argc, char **argv) {
int64_t a = 1160718174;
int64_t b = 316258250;
uint64_t gcd_r = gcd(a, b);
/*Expected answer is 1078*/
printf("gcd(%ld, %ld) = %lu\n", a, b, gcd_r);
return 0;
}
#endif