-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBigUtil.c
106 lines (96 loc) · 1.87 KB
/
BigUtil.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
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include <stdlib.h>
#include "miracl.h"
#include "BigUtil.h"
//大数异或
big Xor2(big x, big y) {
big result = mirvar(0);
unsigned char xString[1000];
unsigned char yString[1000];
//大数转换为二进制比特串
int lengthX = big_to_bytes(0, x, xString, FALSE);
int lengthY = big_to_bytes(0, y, yString, FALSE);
if (lengthX < lengthY) {
for (int i = 0; i < lengthX; i++) {
yString[lengthY - 1 - i] ^= xString[lengthX - 1 - i];
}
//二进制比特串转换为大数
bytes_to_big(lengthY, yString, result);
}
else {
for (int i = 0; i < lengthY; i++) {
xString[lengthX - 1 - i] ^= yString[lengthY - 1 - i];
}
//二进制比特串转换为大数
bytes_to_big(lengthX, xString, result);
}
return result;
}
//大数加法
big Add2(big x, big y) {
big result = mirvar(0);
add(x, y, result);
return result;
}
//大数减法
big Sub2(big x, big y) {
big result = mirvar(0);
subtract(x, y, result);
return result;
}
//大数乘法
big Multiply2(big x, big y) {
big result = mirvar(0);
multiply(x, y, result);
return result;
}
//大数除法
big Divide2(big x, big y) {
big x1 = mirvar(0);
big y1 = mirvar(0);
big z1 = mirvar(0);
copy(x, x1);
copy(y, y1);
divide(x1, y1, z1);
mirkill(x1);
mirkill(y1);
return z1;
}
//大数取余
big Mod2(big x, big y) {
big x1 = mirvar(0);
big y1 = mirvar(0);
big z1 = mirvar(0);
copy(x, x1);
copy(y, y1);
powmod(x1, mirvar(1), y1, z1);
mirkill(x1);
mirkill(y1);
return z1;
}
//大数乘方
big Pow2(big x, int y) {
big x1 = mirvar(0);
big y1 = mirvar(0);
copy(x, x1);
for (int i = 0; i < y; i++)
{
y1 = Multiply2(x1, x1);
}
mirkill(x1);
return y1;
}
//大数转换为十六进制串
char* BigToHexChars2(big x) {
mip->IOBASE = 16;
char* str = (char*)malloc(sizeof(char) * maxLen);
cotstr(x, str);
return str;
}
//十六进制数串转换为大数
big HexCharsToBig(char* str) {
mip->IOBASE = 16;
big result = mirvar(0);
cinstr(result, str);
return result;
}