diff options
author | Dirk Engling <erdgeist@erdgeist.org> | 2013-07-15 14:41:24 +0200 |
---|---|---|
committer | Dirk Engling <erdgeist@erdgeist.org> | 2013-07-15 14:41:24 +0200 |
commit | ad3496a66d2810c2368bc86bd2904255d48e214e (patch) | |
tree | 967026a80f6ba7692f44dbfdd6466c13f37f1e84 | |
parent | ce15c0e9a6a3801774a7f76a3036cdf6abd18466 (diff) |
Stable version
-rw-r--r-- | sqr.c | 96 |
1 files changed, 96 insertions, 0 deletions
@@ -0,0 +1,96 @@ | |||
1 | #include <stdint.h> | ||
2 | #include <string.h> | ||
3 | #include <stdio.h> | ||
4 | |||
5 | typedef uint8_t leg_t; | ||
6 | typedef uint16_t dleg_t; | ||
7 | |||
8 | #if 0 | ||
9 | static void mp_sqr( leg_t *result, leg_t const * a, int legs ) | ||
10 | { | ||
11 | while( legs-- ) { | ||
12 | leg_t *offs = result+2*legs+1; | ||
13 | leg_t fac = a[legs]; | ||
14 | int leg = legs; | ||
15 | dleg_t acc = (dleg_t)*offs + (dleg_t)fac * fac; | ||
16 | |||
17 | *(offs--) = (leg_t)acc; | ||
18 | acc >>= 8*sizeof(leg_t); | ||
19 | |||
20 | while( leg-- ) | ||
21 | { | ||
22 | dleg_t subresult = fac * a[leg]; | ||
23 | int carry = !!(subresult & (1<<(8*sizeof(dleg_t)-1))); | ||
24 | |||
25 | subresult <<= 1; | ||
26 | acc += subresult + *offs; | ||
27 | |||
28 | *(offs--) = (leg_t)acc; | ||
29 | acc >>= 8*sizeof(leg_t); | ||
30 | acc += carry; | ||
31 | } | ||
32 | |||
33 | while( acc ) | ||
34 | { | ||
35 | acc += *offs; | ||
36 | *(offs--) = (leg_t)acc; | ||
37 | acc >>= 8*sizeof(leg_t); | ||
38 | } | ||
39 | } | ||
40 | } | ||
41 | #else | ||
42 | static void mp_sqr( leg_t *result, leg_t const * a, int legs ) | ||
43 | { | ||
44 | int i; | ||
45 | while( legs-- ) { | ||
46 | leg_t *offs = result+2*legs+1; | ||
47 | leg_t fac = a[legs]; | ||
48 | int leg = legs,i; | ||
49 | dleg_t acc = (dleg_t)*offs + (dleg_t)fac * (dleg_t)fac; | ||
50 | |||
51 | *(offs--) = (leg_t)acc; | ||
52 | acc >>= 8*sizeof(leg_t); | ||
53 | |||
54 | while( leg-- ) | ||
55 | { | ||
56 | dleg_t subresult = (dleg_t)fac * (dleg_t)a[leg]; | ||
57 | int carry = 1 & ( subresult >> (16*sizeof(leg_t)-1)); | ||
58 | |||
59 | subresult ^= carry << (16*sizeof(leg_t)-1); | ||
60 | |||
61 | acc += 2 * subresult + (dleg_t)*offs; | ||
62 | *(offs--) = (leg_t)acc; | ||
63 | |||
64 | acc >>= 8*sizeof(leg_t); | ||
65 | acc += (dleg_t)carry << 8*sizeof(leg_t); | ||
66 | |||
67 | printf( "%04X %04X %d\n", acc, subresult, carry ); | ||
68 | |||
69 | } | ||
70 | for(i=0;i<6;++i) | ||
71 | printf( "%02X", result[i] ); | ||
72 | |||
73 | putchar(10); | ||
74 | while( acc ) | ||
75 | { | ||
76 | acc += (dleg_t)*offs; | ||
77 | *(offs--) = (leg_t)acc; | ||
78 | acc >>= 8*sizeof(leg_t); | ||
79 | } | ||
80 | } | ||
81 | for(i=0;i<6;++i) | ||
82 | printf( "%02X", result[i] ); | ||
83 | putchar(10); | ||
84 | } | ||
85 | #endif | ||
86 | |||
87 | int main() { | ||
88 | leg_t a[3] = { 0xff, 0xff, 0xa1 }; | ||
89 | leg_t r[6] = {0,0,0,0,0,0}; | ||
90 | int i; | ||
91 | |||
92 | mp_sqr( r, a, 3 ); | ||
93 | for(i=0;i<6;++i) | ||
94 | printf( "%02X", r[i] ); | ||
95 | putchar(10); | ||
96 | } | ||