Skip to content

Commit 4a5569a

Browse files
committed
First run of 3C
1 parent e1e7d95 commit 4a5569a

File tree

9 files changed

+153
-120
lines changed

9 files changed

+153
-120
lines changed

bn.c

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ There may well be room for performance-optimizations and improvements.
2626

2727

2828
/* Functions for shifting number in-place. */
29-
static void _lshift_one_bit(struct bn* a);
30-
static void _rshift_one_bit(struct bn* a);
31-
static void _lshift_word(struct bn* a, int nwords);
32-
static void _rshift_word(struct bn* a, int nwords);
29+
static void _lshift_one_bit(_Ptr<struct bn> a);
30+
static void _rshift_one_bit(_Ptr<struct bn> a);
31+
static void _lshift_word(_Ptr<struct bn> a, int nwords);
32+
static void _rshift_word(_Ptr<struct bn> a, int nwords);
3333

3434

3535

3636
/* Public / Exported functions. */
37-
void bignum_init(struct bn* n)
37+
void bignum_init(_Ptr<struct bn> n)
3838
{
3939
require(n, "n is null");
4040

@@ -46,7 +46,7 @@ void bignum_init(struct bn* n)
4646
}
4747

4848

49-
void bignum_from_int(struct bn* n, DTYPE_TMP i)
49+
void bignum_from_int(_Ptr<struct bn> n, DTYPE_TMP i)
5050
{
5151
require(n, "n is null");
5252

@@ -72,7 +72,7 @@ void bignum_from_int(struct bn* n, DTYPE_TMP i)
7272
}
7373

7474

75-
int bignum_to_int(struct bn* n)
75+
int bignum_to_int(_Ptr<struct bn> n)
7676
{
7777
require(n, "n is null");
7878

@@ -95,7 +95,7 @@ int bignum_to_int(struct bn* n)
9595
}
9696

9797

98-
void bignum_from_string(struct bn* n, char* str, int nbytes)
98+
void bignum_from_string(_Ptr<struct bn> n, _Nt_array_ptr<char> str : count(nbytes), int nbytes)
9999
{
100100
require(n, "n is null");
101101
require(str, "str is null");
@@ -122,7 +122,7 @@ void bignum_from_string(struct bn* n, char* str, int nbytes)
122122
}
123123

124124

125-
void bignum_to_string(struct bn* n, char* str, int nbytes)
125+
void bignum_to_string(_Ptr<struct bn> n, char *str : itype(_Array_ptr<char>) count(8191), int nbytes)
126126
{
127127
require(n, "n is null");
128128
require(str, "str is null");
@@ -158,7 +158,7 @@ void bignum_to_string(struct bn* n, char* str, int nbytes)
158158
}
159159

160160

161-
void bignum_dec(struct bn* n)
161+
void bignum_dec(_Ptr<struct bn> n)
162162
{
163163
require(n, "n is null");
164164

@@ -180,7 +180,7 @@ void bignum_dec(struct bn* n)
180180
}
181181

182182

183-
void bignum_inc(struct bn* n)
183+
void bignum_inc(_Ptr<struct bn> n)
184184
{
185185
require(n, "n is null");
186186

@@ -202,7 +202,7 @@ void bignum_inc(struct bn* n)
202202
}
203203

204204

205-
void bignum_add(struct bn* a, struct bn* b, struct bn* c)
205+
void bignum_add(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c)
206206
{
207207
require(a, "a is null");
208208
require(b, "b is null");
@@ -220,7 +220,7 @@ void bignum_add(struct bn* a, struct bn* b, struct bn* c)
220220
}
221221

222222

223-
void bignum_sub(struct bn* a, struct bn* b, struct bn* c)
223+
void bignum_sub(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c)
224224
{
225225
require(a, "a is null");
226226
require(b, "b is null");
@@ -242,14 +242,14 @@ void bignum_sub(struct bn* a, struct bn* b, struct bn* c)
242242
}
243243

244244

245-
void bignum_mul(struct bn* a, struct bn* b, struct bn* c)
245+
void bignum_mul(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c)
246246
{
247247
require(a, "a is null");
248248
require(b, "b is null");
249249
require(c, "c is null");
250250

251-
struct bn row;
252-
struct bn tmp;
251+
struct bn row = {};
252+
struct bn tmp = {};
253253
int i, j;
254254

255255
bignum_init(c);
@@ -274,15 +274,15 @@ void bignum_mul(struct bn* a, struct bn* b, struct bn* c)
274274
}
275275

276276

277-
void bignum_div(struct bn* a, struct bn* b, struct bn* c)
277+
void bignum_div(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c)
278278
{
279279
require(a, "a is null");
280280
require(b, "b is null");
281281
require(c, "c is null");
282282

283-
struct bn current;
284-
struct bn denom;
285-
struct bn tmp;
283+
struct bn current = {};
284+
struct bn denom = {};
285+
struct bn tmp = {};
286286

287287
bignum_from_int(&current, 1); // int current = 1;
288288
bignum_assign(&denom, b); // denom = b
@@ -320,7 +320,7 @@ void bignum_div(struct bn* a, struct bn* b, struct bn* c)
320320
}
321321

322322

323-
void bignum_lshift(struct bn* a, struct bn* b, int nbits)
323+
void bignum_lshift(_Ptr<struct bn> a, _Ptr<struct bn> b, int nbits)
324324
{
325325
require(a, "a is null");
326326
require(b, "b is null");
@@ -348,7 +348,7 @@ void bignum_lshift(struct bn* a, struct bn* b, int nbits)
348348
}
349349

350350

351-
void bignum_rshift(struct bn* a, struct bn* b, int nbits)
351+
void bignum_rshift(_Ptr<struct bn> a, _Ptr<struct bn> b, int nbits)
352352
{
353353
require(a, "a is null");
354354
require(b, "b is null");
@@ -377,7 +377,7 @@ void bignum_rshift(struct bn* a, struct bn* b, int nbits)
377377
}
378378

379379

380-
void bignum_mod(struct bn* a, struct bn* b, struct bn* c)
380+
void bignum_mod(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c)
381381
{
382382
/*
383383
Take divmod and throw away div part
@@ -386,12 +386,12 @@ void bignum_mod(struct bn* a, struct bn* b, struct bn* c)
386386
require(b, "b is null");
387387
require(c, "c is null");
388388

389-
struct bn tmp;
389+
struct bn tmp = {};
390390

391391
bignum_divmod(a,b,&tmp,c);
392392
}
393393

394-
void bignum_divmod(struct bn* a, struct bn* b, struct bn* c, struct bn* d)
394+
void bignum_divmod(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c, _Ptr<struct bn> d)
395395
{
396396
/*
397397
Puts a%b in d
@@ -406,7 +406,7 @@ void bignum_divmod(struct bn* a, struct bn* b, struct bn* c, struct bn* d)
406406
require(b, "b is null");
407407
require(c, "c is null");
408408

409-
struct bn tmp;
409+
struct bn tmp = {};
410410

411411
/* c = (a / b) */
412412
bignum_div(a, b, c);
@@ -419,7 +419,7 @@ void bignum_divmod(struct bn* a, struct bn* b, struct bn* c, struct bn* d)
419419
}
420420

421421

422-
void bignum_and(struct bn* a, struct bn* b, struct bn* c)
422+
void bignum_and(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c)
423423
{
424424
require(a, "a is null");
425425
require(b, "b is null");
@@ -433,7 +433,7 @@ void bignum_and(struct bn* a, struct bn* b, struct bn* c)
433433
}
434434

435435

436-
void bignum_or(struct bn* a, struct bn* b, struct bn* c)
436+
void bignum_or(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c)
437437
{
438438
require(a, "a is null");
439439
require(b, "b is null");
@@ -447,7 +447,7 @@ void bignum_or(struct bn* a, struct bn* b, struct bn* c)
447447
}
448448

449449

450-
void bignum_xor(struct bn* a, struct bn* b, struct bn* c)
450+
void bignum_xor(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c)
451451
{
452452
require(a, "a is null");
453453
require(b, "b is null");
@@ -461,7 +461,7 @@ void bignum_xor(struct bn* a, struct bn* b, struct bn* c)
461461
}
462462

463463

464-
int bignum_cmp(struct bn* a, struct bn* b)
464+
int bignum_cmp(_Ptr<struct bn> a, _Ptr<struct bn> b)
465465
{
466466
require(a, "a is null");
467467
require(b, "b is null");
@@ -485,7 +485,7 @@ int bignum_cmp(struct bn* a, struct bn* b)
485485
}
486486

487487

488-
int bignum_is_zero(struct bn* n)
488+
int bignum_is_zero(_Ptr<struct bn> n)
489489
{
490490
require(n, "n is null");
491491

@@ -502,13 +502,13 @@ int bignum_is_zero(struct bn* n)
502502
}
503503

504504

505-
void bignum_pow(struct bn* a, struct bn* b, struct bn* c)
505+
void bignum_pow(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c)
506506
{
507507
require(a, "a is null");
508508
require(b, "b is null");
509509
require(c, "c is null");
510510

511-
struct bn tmp;
511+
struct bn tmp = {};
512512

513513
bignum_init(c);
514514

@@ -519,7 +519,7 @@ void bignum_pow(struct bn* a, struct bn* b, struct bn* c)
519519
}
520520
else
521521
{
522-
struct bn bcopy;
522+
struct bn bcopy = {};
523523
bignum_assign(&bcopy, b);
524524

525525
/* Copy a -> tmp */
@@ -544,12 +544,15 @@ void bignum_pow(struct bn* a, struct bn* b, struct bn* c)
544544
}
545545
}
546546

547-
void bignum_isqrt(struct bn *a, struct bn* b)
547+
void bignum_isqrt(_Ptr<struct bn> a, _Ptr<struct bn> b)
548548
{
549549
require(a, "a is null");
550550
require(b, "b is null");
551551

552-
struct bn low, high, mid, tmp;
552+
struct bn low = {};
553+
struct bn high = {};
554+
struct bn mid = {};
555+
struct bn tmp = {};
553556

554557
bignum_init(&low);
555558
bignum_assign(&high, a);
@@ -577,7 +580,7 @@ void bignum_isqrt(struct bn *a, struct bn* b)
577580
}
578581

579582

580-
void bignum_assign(struct bn* dst, struct bn* src)
583+
void bignum_assign(_Ptr<struct bn> dst, _Ptr<struct bn> src)
581584
{
582585
require(dst, "dst is null");
583586
require(src, "src is null");
@@ -591,7 +594,7 @@ void bignum_assign(struct bn* dst, struct bn* src)
591594

592595

593596
/* Private / Static functions. */
594-
static void _rshift_word(struct bn* a, int nwords)
597+
static void _rshift_word(_Ptr<struct bn> a, int nwords)
595598
{
596599
/* Naive method: */
597600
require(a, "a is null");
@@ -618,7 +621,7 @@ static void _rshift_word(struct bn* a, int nwords)
618621
}
619622

620623

621-
static void _lshift_word(struct bn* a, int nwords)
624+
static void _lshift_word(_Ptr<struct bn> a, int nwords)
622625
{
623626
require(a, "a is null");
624627
require(nwords >= 0, "no negative shifts");
@@ -637,7 +640,7 @@ static void _lshift_word(struct bn* a, int nwords)
637640
}
638641

639642

640-
static void _lshift_one_bit(struct bn* a)
643+
static void _lshift_one_bit(_Ptr<struct bn> a)
641644
{
642645
require(a, "a is null");
643646

@@ -650,7 +653,7 @@ static void _lshift_one_bit(struct bn* a)
650653
}
651654

652655

653-
static void _rshift_one_bit(struct bn* a)
656+
static void _rshift_one_bit(_Ptr<struct bn> a)
654657
{
655658
require(a, "a is null");
656659

bn.h

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ There may well be room for performance-optimizations and improvements.
7676
/* Data-holding structure: array of DTYPEs */
7777
struct bn
7878
{
79-
DTYPE array[BN_ARRAY_SIZE];
79+
uint32_t array _Checked[BN_ARRAY_SIZE];
8080
};
8181

8282

@@ -87,35 +87,35 @@ enum { SMALLER = -1, EQUAL = 0, LARGER = 1 };
8787

8888

8989
/* Initialization functions: */
90-
void bignum_init(struct bn* n);
91-
void bignum_from_int(struct bn* n, DTYPE_TMP i);
92-
int bignum_to_int(struct bn* n);
93-
void bignum_from_string(struct bn* n, char* str, int nbytes);
94-
void bignum_to_string(struct bn* n, char* str, int maxsize);
90+
void bignum_init(_Ptr<struct bn> n);
91+
void bignum_from_int(_Ptr<struct bn> n, DTYPE_TMP i);
92+
int bignum_to_int(_Ptr<struct bn> n);
93+
void bignum_from_string(_Ptr<struct bn> n, _Nt_array_ptr<char> str : count(nbytes), int nbytes);
94+
void bignum_to_string(_Ptr<struct bn> n, char *str : itype(_Array_ptr<char>) count(8191), int maxsize);
9595

9696
/* Basic arithmetic operations: */
97-
void bignum_add(struct bn* a, struct bn* b, struct bn* c); /* c = a + b */
98-
void bignum_sub(struct bn* a, struct bn* b, struct bn* c); /* c = a - b */
99-
void bignum_mul(struct bn* a, struct bn* b, struct bn* c); /* c = a * b */
100-
void bignum_div(struct bn* a, struct bn* b, struct bn* c); /* c = a / b */
101-
void bignum_mod(struct bn* a, struct bn* b, struct bn* c); /* c = a % b */
102-
void bignum_divmod(struct bn* a, struct bn* b, struct bn* c, struct bn* d); /* c = a/b, d = a%b */
97+
void bignum_add(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c); /* c = a + b */
98+
void bignum_sub(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c); /* c = a - b */
99+
void bignum_mul(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c); /* c = a * b */
100+
void bignum_div(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c); /* c = a / b */
101+
void bignum_mod(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c); /* c = a % b */
102+
void bignum_divmod(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c, _Ptr<struct bn> d); /* c = a/b, d = a%b */
103103

104104
/* Bitwise operations: */
105-
void bignum_and(struct bn* a, struct bn* b, struct bn* c); /* c = a & b */
106-
void bignum_or(struct bn* a, struct bn* b, struct bn* c); /* c = a | b */
107-
void bignum_xor(struct bn* a, struct bn* b, struct bn* c); /* c = a ^ b */
108-
void bignum_lshift(struct bn* a, struct bn* b, int nbits); /* b = a << nbits */
109-
void bignum_rshift(struct bn* a, struct bn* b, int nbits); /* b = a >> nbits */
105+
void bignum_and(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c); /* c = a & b */
106+
void bignum_or(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c); /* c = a | b */
107+
void bignum_xor(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c); /* c = a ^ b */
108+
void bignum_lshift(_Ptr<struct bn> a, _Ptr<struct bn> b, int nbits); /* b = a << nbits */
109+
void bignum_rshift(_Ptr<struct bn> a, _Ptr<struct bn> b, int nbits); /* b = a >> nbits */
110110

111111
/* Special operators and comparison */
112-
int bignum_cmp(struct bn* a, struct bn* b); /* Compare: returns LARGER, EQUAL or SMALLER */
113-
int bignum_is_zero(struct bn* n); /* For comparison with zero */
114-
void bignum_inc(struct bn* n); /* Increment: add one to n */
115-
void bignum_dec(struct bn* n); /* Decrement: subtract one from n */
116-
void bignum_pow(struct bn* a, struct bn* b, struct bn* c); /* Calculate a^b -- e.g. 2^10 => 1024 */
117-
void bignum_isqrt(struct bn* a, struct bn* b); /* Integer square root -- e.g. isqrt(5) => 2*/
118-
void bignum_assign(struct bn* dst, struct bn* src); /* Copy src into dst -- dst := src */
112+
int bignum_cmp(_Ptr<struct bn> a, _Ptr<struct bn> b); /* Compare: returns LARGER, EQUAL or SMALLER */
113+
int bignum_is_zero(_Ptr<struct bn> n); /* For comparison with zero */
114+
void bignum_inc(_Ptr<struct bn> n); /* Increment: add one to n */
115+
void bignum_dec(_Ptr<struct bn> n); /* Decrement: subtract one from n */
116+
void bignum_pow(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c); /* Calculate a^b -- e.g. 2^10 => 1024 */
117+
void bignum_isqrt(_Ptr<struct bn> a, _Ptr<struct bn> b); /* Integer square root -- e.g. isqrt(5) => 2*/
118+
void bignum_assign(_Ptr<struct bn> dst, _Ptr<struct bn> src); /* Copy src into dst -- dst := src */
119119

120120

121121
#endif /* #ifndef __BIGNUM_H__ */

0 commit comments

Comments
 (0)