📄 t_bpadd.c
字号:
/* t_bpAdd.c */
/******************* SHORT COPYRIGHT NOTICE*************************
This source code is part of the BigDigits multiple-precision
arithmetic library Version 1.0 originally written by David Ireland,
copyright (c) 2001 D.I. Management Services Pty Limited, all rights
reserved. It is provided "as is" with no warranties. You may use
this software under the terms of the full copyright notice
"bigdigitsCopyright.txt" that should have been included with
this library. To obtain a copy send an email to
<code@di-mgt.com.au> or visit <www.di-mgt.com.au/crypto.html>.
This notice must be retained in any copy.
****************** END OF COPYRIGHT NOTICE*************************/
#include <stdio.h>
#include <assert.h>
#include "bigdigits.h"
#define TEST_LEN 6
void ShowBPAdd(BIGD_T *w, BIGD_T *u, BIGD_T *v)
{
printf("bpAdd: ");
bpPrint(u);
printf("+ ");
bpPrint(v);
printf("= ");
bpPrint(w);
printf("\n");
}
void ShowBPSubtract(BIGD_T *w, BIGD_T *u, BIGD_T *v)
{
printf("bpSubtract: ");
bpPrint(u);
printf("- ");
bpPrint(v);
printf("= ");
bpPrint(w);
printf("\n");
}
unsigned int MakeBigdRandom(BIGD_T *a, unsigned int ndigits)
{
/* Make a random number of up to ndigits digits */
unsigned int i, n, bits;
DIGIT_T mask;
/* Pick a random size */
n = (unsigned int)spPseudoRand(1, ndigits);
/* Check allocated memory */
if (a->maxdigits < n)
bpResize(a, n);
/* Now fill with random digits */
for (i = 0; i < n; i++)
a->digits[i] = spPseudoRand(0, MAX_DIGIT);
a->ndigits = n;
/* Zero out a random number of bits in leading digit
about half the time */
bits = (unsigned int)spPseudoRand(0, 2*BITS_PER_DIGIT);
if (bits != 0 && bits < BITS_PER_DIGIT)
{
mask = HIBITMASK;
for (i = 1; i < bits; i++)
{
mask |= (mask >> 1);
}
mask = ~mask;
a->digits[n-1] &= mask;
}
return n;
}
main()
{
/* Declare structures */
BIGD_T w, u, v, r;
/* Initialise structures */
bpInit(&u, 2);
bpInit(&v, 2);
bpInit(&w, 0); /* Let's let bp take care of size */
/* Set simple test values and new sizes */
u.digits[0] = MAX_DIGIT;
v.digits[0] = 1;
u.ndigits= v.ndigits = 1;
/* Do the business w = u + v */
bpAdd(&w, &u, &v);
ShowBPAdd(&w, &u, &v);
/* Now do a larger, random addition */
MakeBigdRandom(&u, TEST_LEN);
MakeBigdRandom(&v, TEST_LEN);
/* Do the business w = u + v */
bpAdd(&w, &u, &v);
ShowBPAdd(&w, &u, &v);
/* Let's check r = w - v == u */
bpInit(&r, 0);
bpSubtract(&r, &w, &v);
ShowBPSubtract(&r, &w, &v);
assert(bpEqual(&r, &u));
/* Clear up */
bpFinal(&u);
bpFinal(&v);
bpFinal(&w);
/* Force linker to include copyright notice in
executable object image
*/
copyright_notice();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -