2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 #ifndef _BIGNUM_H
27 #define _BIGNUM_H
28
29 #pragma ident "%Z%%M% %I% %E% SMI"
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 #include <sys/types.h>
36
37 #ifndef __sparcv9
38 #define BIGNUM_CHUNK_32
39 #else
40 #ifndef UMUL64
41 #define UMUL64
42 #endif
43 #endif
44
45
46 #define BITSINBYTE 8
47
48 #ifdef BIGNUM_CHUNK_32
49 #define BIG_CHUNK_SIZE 32
50 #define BIG_CHUNK_TYPE uint32_t
51 #define BIG_CHUNK_TYPE_SIGNED int32_t
52 #define BIG_CHUNK_HIGHBIT 0x80000000
53 #define BIG_CHUNK_ALLBITS 0xffffffff
54 #define BIG_CHUNK_LOWHALFBITS 0xffff
55 #define BIG_CHUNK_HALF_HIGHBIT 0x8000
56 #else
57 #define BIG_CHUNK_SIZE 64
58 #define BIG_CHUNK_TYPE uint64_t
59 #define BIG_CHUNK_TYPE_SIGNED int64_t
60 #define BIG_CHUNK_HIGHBIT 0x8000000000000000ULL
61 #define BIG_CHUNK_ALLBITS 0xffffffffffffffffULL
62 #define BIG_CHUNK_LOWHALFBITS 0xffffffffULL
63 #define BIG_CHUNK_HALF_HIGHBIT 0x80000000ULL
64 #endif
65
66 #define BITLEN2BIGNUMLEN(x) (((x) + BIG_CHUNK_SIZE - 1) / BIG_CHUNK_SIZE)
67 #define CHARLEN2BIGNUMLEN(x) (((x) + sizeof (BIG_CHUNK_TYPE) - 1) / \
68 sizeof (BIG_CHUNK_TYPE))
69
70 #define BIGNUM_WORDSIZE (BIG_CHUNK_SIZE / BITSINBYTE) /* word size in bytes */
71 #define BIG_CHUNKS_FOR_160BITS ((160 + BIG_CHUNK_SIZE - 1) / BIG_CHUNK_SIZE)
72
73
74 /*
75 * leading 0's are permitted
76 * 0 should be represented by size>=1, size>=len>=1, sign=1,
77 * value[i]=0 for 0<i<len
78 */
79 typedef struct {
80 /* size and len in units of BIG_CHUNK_TYPE words */
81 int size; /* size of memory allocated for value */
82 int len; /* number of words that hold valid data in value */
83 int sign; /* 1 for nonnegative, -1 for negative */
84 int malloced; /* 1 if value was malloced 0 if not */
85 BIG_CHUNK_TYPE *value;
86 } BIGNUM;
87
88 #define BIGTMPSIZE 65
89
90 #define BIG_TRUE 1
91 #define BIG_FALSE 0
92
93 typedef int BIG_ERR_CODE;
94
95 /* error codes */
96 #define BIG_OK 0
97 #define BIG_NO_MEM -1
98 #define BIG_INVALID_ARGS -2
99 #define BIG_DIV_BY_0 -3
100 #define BIG_NO_RANDOM -4
101 #define BIG_GENERAL_ERR -5
102 #define BIG_TEST_FAILED -6
103 #define BIG_BUFFER_TOO_SMALL -7
104
|
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 #ifndef _BIGNUM_H
27 #define _BIGNUM_H
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 #include <sys/types.h>
34
35 #if defined(__sparcv9) || defined(__amd64) /* 64-bit chunk size */
36 #ifndef UMUL64
37 #define UMUL64 /* 64-bit multiplication results are supported */
38 #endif
39 #else
40 #define BIGNUM_CHUNK_32
41 #endif
42
43
44 #define BITSINBYTE 8
45
46 /* Bignum "digits" (aka "chunks" or "words") are either 32- or 64-bits */
47 #ifdef BIGNUM_CHUNK_32
48 #define BIG_CHUNK_SIZE 32
49 #define BIG_CHUNK_TYPE uint32_t
50 #define BIG_CHUNK_TYPE_SIGNED int32_t
51 #define BIG_CHUNK_HIGHBIT 0x80000000
52 #define BIG_CHUNK_ALLBITS 0xffffffff
53 #define BIG_CHUNK_LOWHALFBITS 0xffff
54 #define BIG_CHUNK_HALF_HIGHBIT 0x8000
55
56 #else
57 #define BIG_CHUNK_SIZE 64
58 #define BIG_CHUNK_TYPE uint64_t
59 #define BIG_CHUNK_TYPE_SIGNED int64_t
60 #define BIG_CHUNK_HIGHBIT 0x8000000000000000ULL
61 #define BIG_CHUNK_ALLBITS 0xffffffffffffffffULL
62 #define BIG_CHUNK_LOWHALFBITS 0xffffffffULL
63 #define BIG_CHUNK_HALF_HIGHBIT 0x80000000ULL
64 #endif
65
66 #define BITLEN2BIGNUMLEN(x) (((x) + BIG_CHUNK_SIZE - 1) / BIG_CHUNK_SIZE)
67 #define CHARLEN2BIGNUMLEN(x) (((x) + sizeof (BIG_CHUNK_TYPE) - 1) / \
68 sizeof (BIG_CHUNK_TYPE))
69
70 #define BIGNUM_WORDSIZE (BIG_CHUNK_SIZE / BITSINBYTE) /* word size in bytes */
71 #define BIG_CHUNKS_FOR_160BITS ((160 + BIG_CHUNK_SIZE - 1) / BIG_CHUNK_SIZE)
72
73
74 /*
75 * leading 0's are permitted
76 * 0 should be represented by size>=1, size>=len>=1, sign=1,
77 * value[i]=0 for 0<i<len
78 */
79 typedef struct {
80 /* size and len in units of BIG_CHUNK_TYPE words */
81 uint32_t size; /* size of memory allocated for value */
82 uint32_t len; /* number of valid data words in value */
83 int sign; /* 1 for nonnegative, -1 for negative */
84 int malloced; /* 1 if value was malloced, 0 if not */
85 BIG_CHUNK_TYPE *value;
86 } BIGNUM;
87
88 #define BIGTMPSIZE 65
89
90 #define BIG_TRUE 1
91 #define BIG_FALSE 0
92
93 typedef int BIG_ERR_CODE;
94
95 /* error codes */
96 #define BIG_OK 0
97 #define BIG_NO_MEM -1
98 #define BIG_INVALID_ARGS -2
99 #define BIG_DIV_BY_0 -3
100 #define BIG_NO_RANDOM -4
101 #define BIG_GENERAL_ERR -5
102 #define BIG_TEST_FAILED -6
103 #define BIG_BUFFER_TOO_SMALL -7
104
|