include limits include iostream include ctime include cstring include

  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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <limits>
#include <iostream>
#include <ctime>
#include <cstring>
#include <assert.h>
extern "C"{
#include <miracl.h>
}
struct big_size
{
private:
static big e;
public:
static const int value =
sizeof(*e->w) * std::numeric_limits<unsigned char>::digits;
};
int mr_bit(mr_utype e, int bit)
{
return (e >> bit) & 0x1;
}
int big_len(big e)
{
mr_utype last = e->w[ e->len-1 ];
int i = 0;
for(i = MR_LBITS-1; i >= 0; i--)
if(mr_bit(last, i))
break;
return (((e->len-1) * big_size::value) + (i+1));
}
int big_bit(big e, int bit)
{
int block = bit / big_size::value;
int off = bit % big_size::value;
return (e->w[block] >> off) & 0x1;
}
void init_curve()
{
const int m = 163;
const int a = 7;
const int b = 6;
const int c = 3;
big A = mirvar(1);
big B = mirvar(0);
const char *strB = "000000020A601907B8C953CA1481EB10512F78744A3205FD";
cinstr(B, const_cast<char*>(strB));
ecurve2_init(m, a, b, c, A, B, FALSE, MR_AFFINE);
}
// pt = k x I
void point_mult_binary(big k, epoint *P, epoint *R)
{
//epoint *R = epoint_init();
epoint2_copy(P, R);
int t = big_len(k);
//for(int i = 0; i < t; i++)
for(int i = t - 2; i >= 0; i--)
{
//ecurve2_double(R);
ecurve2_add(R, R);
if(big_bit(k, i))
ecurve2_add(P, R);
}
//pt = R;
}
int main()
{
miracl *mip = mirsys(100, 0);
mip->IOBASE = 16;
init_curve();
big Gx = mirvar(0);
big Gy = mirvar(0);
cinstr(Gx, (char*)"00000003F0EBA16286A2D57EA0991168D4994637E8343E36");
cinstr(Gy, (char*)"D51FBC6C71A0094FA2CDD545B11C5C0C797324F1");
epoint* G = epoint_init();
assert(epoint2_set(Gx, Gy, 0, G) == TRUE);
//big x = mirvar(0);
//big y = mirvar(0);
big N = mirvar(0);
cinstr(N, (char*)"000000040000000000000000000292FE77E70C12A4234C33");
big two = mirvar(3);
subtract(N, two, N);
// epoint2_get(G, x, y);
// std::cout << "G" << std::endl;
// cotnum(x, stdout);
// cotnum(y, stdout);
//epoint2_copy(G, test);
csprng rng;
char raw[256] = "dlblnbkgnbnbkgjbnkgbnlkrg";
long seed;
time(&seed);
strong_init(&rng, strlen(raw), raw, seed);
for(int i = 1; i < 64; i++)
{
big n = mirvar(0);
strong_bigrand(&rng, Gx, n);
add(n, mirvar(2), n);
cotnum(n, stdout);
epoint *test = epoint_init();
point_mult_binary(n, G, test);
epoint *control = epoint_init();
//epoint2_copy(G, control);
ecurve2_mult(n, G, control);
//epoint2_get(test, x, y);
//std::cout << "Test" << std::endl;
//cotnum(x, stdout);
//cotnum(y, stdout);
//epoint2_get(control, x, y);
//std::cout << "Control" << std::endl;
//cotnum(x, stdout);
//cotnum(y, stdout);
assert(epoint2_comp(test, control) == TRUE);
}
mirexit();
return 0;
}