xref: /linux-6.15/include/linux/mpi.h (revision 8e3a67f2)
1465ae836SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
25ce3e312SDmitry Kasatkin /* mpi.h  -  Multi Precision Integers
35ce3e312SDmitry Kasatkin  *	Copyright (C) 1994, 1996, 1998, 1999,
45ce3e312SDmitry Kasatkin  *                    2000, 2001 Free Software Foundation, Inc.
55ce3e312SDmitry Kasatkin  *
65ce3e312SDmitry Kasatkin  * This file is part of GNUPG.
75ce3e312SDmitry Kasatkin  *
85ce3e312SDmitry Kasatkin  * Note: This code is heavily based on the GNU MP Library.
95ce3e312SDmitry Kasatkin  *	 Actually it's the same code with only minor changes in the
105ce3e312SDmitry Kasatkin  *	 way the data is stored; this is to support the abstraction
115ce3e312SDmitry Kasatkin  *	 of an optional secure memory allocation which may be used
125ce3e312SDmitry Kasatkin  *	 to avoid revealing of sensitive data due to paging etc.
135ce3e312SDmitry Kasatkin  *	 The GNU MP Library itself is published under the LGPL;
145ce3e312SDmitry Kasatkin  *	 however I decided to publish this code under the plain GPL.
155ce3e312SDmitry Kasatkin  */
165ce3e312SDmitry Kasatkin 
175ce3e312SDmitry Kasatkin #ifndef G10_MPI_H
185ce3e312SDmitry Kasatkin #define G10_MPI_H
195ce3e312SDmitry Kasatkin 
205ce3e312SDmitry Kasatkin #include <linux/types.h>
212d4d1eeaSTadeusz Struk #include <linux/scatterlist.h>
225ce3e312SDmitry Kasatkin 
235ce3e312SDmitry Kasatkin #define BYTES_PER_MPI_LIMB	(BITS_PER_LONG / 8)
245ce3e312SDmitry Kasatkin #define BITS_PER_MPI_LIMB	BITS_PER_LONG
255ce3e312SDmitry Kasatkin 
265ce3e312SDmitry Kasatkin typedef unsigned long int mpi_limb_t;
275ce3e312SDmitry Kasatkin typedef signed long int mpi_limb_signed_t;
285ce3e312SDmitry Kasatkin 
295ce3e312SDmitry Kasatkin struct gcry_mpi {
305ce3e312SDmitry Kasatkin 	int alloced;		/* array size (# of allocated limbs) */
315ce3e312SDmitry Kasatkin 	int nlimbs;		/* number of valid limbs */
325ce3e312SDmitry Kasatkin 	int nbits;		/* the real number of valid bits (info only) */
335ce3e312SDmitry Kasatkin 	int sign;		/* indicates a negative number */
345ce3e312SDmitry Kasatkin 	unsigned flags;		/* bit 0: array must be allocated in secure memory space */
355ce3e312SDmitry Kasatkin 	/* bit 1: not used */
365ce3e312SDmitry Kasatkin 	/* bit 2: the limb is a pointer to some m_alloced data */
375ce3e312SDmitry Kasatkin 	mpi_limb_t *d;		/* array with the limbs */
385ce3e312SDmitry Kasatkin };
395ce3e312SDmitry Kasatkin 
405ce3e312SDmitry Kasatkin typedef struct gcry_mpi *MPI;
415ce3e312SDmitry Kasatkin 
425ce3e312SDmitry Kasatkin #define mpi_get_nlimbs(a)     ((a)->nlimbs)
435ce3e312SDmitry Kasatkin 
445ce3e312SDmitry Kasatkin /*-- mpiutil.c --*/
455ce3e312SDmitry Kasatkin MPI mpi_alloc(unsigned nlimbs);
465ce3e312SDmitry Kasatkin void mpi_free(MPI a);
475ce3e312SDmitry Kasatkin int mpi_resize(MPI a, unsigned nlimbs);
485ce3e312SDmitry Kasatkin 
49a8ea8bddSTianjia Zhang MPI mpi_copy(MPI a);
50a8ea8bddSTianjia Zhang 
515ce3e312SDmitry Kasatkin /*-- mpicoder.c --*/
52e1045992SDavid Howells MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes);
535ce3e312SDmitry Kasatkin MPI mpi_read_from_buffer(const void *buffer, unsigned *ret_nread);
542d4d1eeaSTadeusz Struk MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int len);
555ce3e312SDmitry Kasatkin void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign);
56d37e2969STadeusz Struk int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
57d37e2969STadeusz Struk 		    int *sign);
589b45b7bbSHerbert Xu int mpi_write_to_sgl(MPI a, struct scatterlist *sg, unsigned nbytes,
592d4d1eeaSTadeusz Struk 		     int *sign);
60a8ea8bddSTianjia Zhang 
61a8ea8bddSTianjia Zhang /*-- mpi-mod.c --*/
62*8e3a67f2SHerbert Xu int mpi_mod(MPI rem, MPI dividend, MPI divisor);
63a8ea8bddSTianjia Zhang 
645ce3e312SDmitry Kasatkin /*-- mpi-pow.c --*/
655ce3e312SDmitry Kasatkin int mpi_powm(MPI res, MPI base, MPI exp, MPI mod);
665ce3e312SDmitry Kasatkin 
675ce3e312SDmitry Kasatkin /*-- mpi-cmp.c --*/
685ce3e312SDmitry Kasatkin int mpi_cmp_ui(MPI u, ulong v);
695ce3e312SDmitry Kasatkin int mpi_cmp(MPI u, MPI v);
705ce3e312SDmitry Kasatkin 
714278e9d9SMarcelo Henrique Cerri /*-- mpi-sub-ui.c --*/
724278e9d9SMarcelo Henrique Cerri int mpi_sub_ui(MPI w, MPI u, unsigned long vval);
734278e9d9SMarcelo Henrique Cerri 
745ce3e312SDmitry Kasatkin /*-- mpi-bit.c --*/
755ce3e312SDmitry Kasatkin void mpi_normalize(MPI a);
765ce3e312SDmitry Kasatkin unsigned mpi_get_nbits(MPI a);
77a8ea8bddSTianjia Zhang int mpi_test_bit(MPI a, unsigned int n);
78*8e3a67f2SHerbert Xu int mpi_set_bit(MPI a, unsigned int n);
79*8e3a67f2SHerbert Xu int mpi_rshift(MPI x, MPI a, unsigned int n);
80a8ea8bddSTianjia Zhang 
81a8ea8bddSTianjia Zhang /*-- mpi-add.c --*/
82*8e3a67f2SHerbert Xu int mpi_add(MPI w, MPI u, MPI v);
83*8e3a67f2SHerbert Xu int mpi_sub(MPI w, MPI u, MPI v);
84*8e3a67f2SHerbert Xu int mpi_addm(MPI w, MPI u, MPI v, MPI m);
85*8e3a67f2SHerbert Xu int mpi_subm(MPI w, MPI u, MPI v, MPI m);
86a8ea8bddSTianjia Zhang 
87a8ea8bddSTianjia Zhang /*-- mpi-mul.c --*/
88*8e3a67f2SHerbert Xu int mpi_mul(MPI w, MPI u, MPI v);
89*8e3a67f2SHerbert Xu int mpi_mulm(MPI w, MPI u, MPI v, MPI m);
90a8ea8bddSTianjia Zhang 
91a8ea8bddSTianjia Zhang /*-- mpi-div.c --*/
92*8e3a67f2SHerbert Xu int mpi_tdiv_r(MPI rem, MPI num, MPI den);
93*8e3a67f2SHerbert Xu int mpi_fdiv_r(MPI rem, MPI dividend, MPI divisor);
945ce3e312SDmitry Kasatkin 
95d37e2969STadeusz Struk /* inline functions */
96d37e2969STadeusz Struk 
97d37e2969STadeusz Struk /**
98d37e2969STadeusz Struk  * mpi_get_size() - returns max size required to store the number
99d37e2969STadeusz Struk  *
100478485f6SZhen Lei  * @a:	A multi precision integer for which we want to allocate a buffer
101d37e2969STadeusz Struk  *
102d37e2969STadeusz Struk  * Return: size required to store the number
103d37e2969STadeusz Struk  */
mpi_get_size(MPI a)104d37e2969STadeusz Struk static inline unsigned int mpi_get_size(MPI a)
105d37e2969STadeusz Struk {
106d37e2969STadeusz Struk 	return a->nlimbs * BYTES_PER_MPI_LIMB;
107d37e2969STadeusz Struk }
1085ce3e312SDmitry Kasatkin #endif /*G10_MPI_H */
109