xref: /titanic_50/usr/src/common/bignum/bignum.h (revision da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _BIGNUM_H
28 #define	_BIGNUM_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef	__cplusplus
33 extern "C" {
34 #endif
35 
36 #include <sys/types.h>
37 
38 typedef int BIG_ERR_CODE;
39 
40 
41 /*
42  * leading 0's are permitted
43  * 0 should be represented by size>=1, size>=len>=1, sign=1,
44  * value[i]=0 for 0<i<len
45  */
46 typedef struct {
47 	int size; /* the size of memory allocated for value (in words) */
48 	int len;  /* the number of words that hold valid data in value */
49 	int sign; /* 1 for nonnegative, -1 for negative   */
50 	int malloced; /* 1 if value was malloced 0 if not */
51 	uint32_t *value;
52 } BIGNUM;
53 
54 #define	BIGTMPSIZE 65
55 
56 #define	BIG_TRUE 1
57 #define	BIG_FALSE 0
58 
59 /* error codes */
60 #define	BIG_OK 0
61 #define	BIG_NO_MEM -1
62 #define	BIG_INVALID_ARGS -2
63 #define	BIG_DIV_BY_0 -3
64 #define	BIG_NO_RANDOM -4
65 #define	BIG_GENERAL_ERR	-5
66 
67 #define	arraysize(x) (sizeof (x) / sizeof (x[0]))
68 
69 #ifdef USE_FLOATING_POINT
70 void conv_d16_to_i32(uint32_t *i32, double *d16, int64_t *tmp, int ilen);
71 void conv_i32_to_d32(double *d32, uint32_t *i32, int len);
72 void conv_i32_to_d16(double *d16, uint32_t *i32, int len);
73 void conv_i32_to_d32_and_d16(double *d32, double *d16,
74     uint32_t *i32, int len);
75 void mont_mulf_noconv(uint32_t *result, double *dm1, double *dm2, double *dt,
76     double *dn, uint32_t *nint, int nlen, double dn0);
77 #endif /* USE_FLOATING_POINT */
78 
79 void printbignum(char *aname, BIGNUM *a);
80 
81 BIG_ERR_CODE big_init(BIGNUM *number, int size);
82 BIG_ERR_CODE big_extend(BIGNUM *number, int size);
83 void big_finish(BIGNUM *number);
84 void bytestring2bignum(BIGNUM *bn, uchar_t *kn, size_t len);
85 void bignum2bytestring(uchar_t *kn, BIGNUM *bn, size_t len);
86 BIG_ERR_CODE big_mont_rr(BIGNUM *result, BIGNUM *n);
87 BIG_ERR_CODE big_modexp(BIGNUM *result, BIGNUM *a, BIGNUM *e,
88     BIGNUM *n, BIGNUM *n_rr);
89 BIG_ERR_CODE big_modexp_crt(BIGNUM *result, BIGNUM *a, BIGNUM *dmodpminus1,
90     BIGNUM *dmodqminus1, BIGNUM *p, BIGNUM *q, BIGNUM *pinvmodq,
91     BIGNUM *p_rr, BIGNUM *q_rr);
92 int big_cmp_abs(BIGNUM *a, BIGNUM *b);
93 BIG_ERR_CODE randombignum(BIGNUM *r, int length);
94 BIG_ERR_CODE big_div_pos(BIGNUM *result, BIGNUM *remainder,
95     BIGNUM *aa, BIGNUM *bb);
96 BIG_ERR_CODE big_ext_gcd_pos(BIGNUM *gcd, BIGNUM *cm, BIGNUM *ce,
97     BIGNUM *m, BIGNUM *e);
98 BIG_ERR_CODE big_add(BIGNUM *result, BIGNUM *aa, BIGNUM *bb);
99 BIG_ERR_CODE big_mul(BIGNUM *result, BIGNUM *aa, BIGNUM *bb);
100 BIG_ERR_CODE big_nextprime_pos(BIGNUM *result, BIGNUM *n);
101 BIG_ERR_CODE big_sub_pos(BIGNUM *result, BIGNUM *aa, BIGNUM *bb);
102 BIG_ERR_CODE big_copy(BIGNUM *dest, BIGNUM *src);
103 BIG_ERR_CODE big_sub(BIGNUM *result, BIGNUM *aa, BIGNUM *bb);
104 int big_bitlength(BIGNUM *n);
105 BIG_ERR_CODE big_init1(BIGNUM *number, int size, uint32_t *buf, int bufsize);
106 
107 #if defined(HWCAP)
108 
109 #define	BIG_MUL_SET_VEC(r, a, len, digit) \
110 	(*big_mul_set_vec_impl)(r, a, len, digit)
111 #define	BIG_MUL_ADD_VEC(r, a, len, digit) \
112 	(*big_mul_add_vec_impl)(r, a, len, digit)
113 #define	BIG_MUL_VEC(r, a, alen, b, blen) \
114 	(*big_mul_vec_impl)(r, a, alen, b, blen)
115 #define	BIG_SQR_VEC(r, a, len) \
116 	(*big_sqr_vec_impl)(r, a, len)
117 
118 extern uint32_t (*big_mul_set_vec_impl)
119 	(uint32_t *r, uint32_t *a, int len, uint32_t digit);
120 extern uint32_t (*big_mul_add_vec_impl)
121 	(uint32_t *r, uint32_t *a, int len, uint32_t digit);
122 extern void (*big_mul_vec_impl)
123 	(uint32_t *r, uint32_t *a, int alen, uint32_t *b, int blen);
124 extern void (*big_sqr_vec_impl)
125 	(uint32_t *r, uint32_t *a, int len);
126 
127 #else /* ! HWCAP */
128 
129 #define	BIG_MUL_SET_VEC(r, a, len, digit) big_mul_set_vec(r, a, len, digit)
130 #define	BIG_MUL_ADD_VEC(r, a, len, digit) big_mul_add_vec(r, a, len, digit)
131 #define	BIG_MUL_VEC(r, a, alen, b, blen) big_mul_vec(r, a, alen, b, blen)
132 #define	BIG_SQR_VEC(r, a, len) big_sqr_vec(r, a, len)
133 
134 extern uint32_t big_mul_set_vec(uint32_t *r, uint32_t *a, int len, uint32_t d);
135 extern uint32_t big_mul_add_vec(uint32_t *r, uint32_t *a, int len, uint32_t d);
136 extern void big_mul_vec(uint32_t *r, uint32_t *a, int alen,
137     uint32_t *b, int blen);
138 extern void big_sqr_vec(uint32_t *r, uint32_t *a, int len);
139 
140 #endif /* HWCAP */
141 
142 #ifdef	__cplusplus
143 }
144 #endif
145 
146 #endif	/* _BIGNUM_H */
147