4 * @brief Class with helper functions for gmp operations
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 #include "gmp_helper.h"
25 #include "allocator.h"
26 #include "randomizer.h"
29 * Private data of an gmp_helper_t object.
32 typedef struct private_gmp_helper_s private_gmp_helper_t
;
34 struct private_gmp_helper_s
{
36 * public gmp_helper_t interface
44 * Implements private_gmp_helper_t's chunk_to_mpz function.
45 * See #private_gmp_helper_t.chunk_to_mpz for description.
47 static void chunk_to_mpz(private_gmp_helper_t
*this, mpz_t
*mpz_value
, chunk_t data
)
51 mpz_init_set_ui(*(mpz_value
), 0);
53 for (i
= 0; i
< data
.len
; i
++)
55 mpz_mul_ui(*(mpz_value
),*(mpz_value
), 1 << 8);
56 mpz_add_ui(*(mpz_value
),*(mpz_value
), data
.ptr
[i
]);
61 * Implements private_gmp_helper_t's mpz_to_chunk function.
62 * See #private_gmp_helper_t.mpz_to_chunk for description.
64 static status_t
mpz_to_chunk (private_gmp_helper_t
*this,mpz_t
*mpz_value
, chunk_t
*data
,size_t bytes
)
67 status_t status
= SUCCESS
;
71 data
->ptr
= allocator_alloc(data
->len
);
73 if (data
->ptr
== NULL
)
79 memset(data
->ptr
,0,data
->len
);
84 mpz_set(temp1
, *mpz_value
);
86 for (i
= data
->len
-1; i
>= 0; i
--)
88 data
->ptr
[i
] = mpz_mdivmod_ui(temp2
, NULL
, temp1
, 1 << 8);
89 mpz_set(temp1
, temp2
);
93 if (mpz_sgn(temp1
) != 0)
103 * Implements gmp_helper_t's init_prime function.
104 * See #gmp_helper_t.init_prime for description.
106 static status_t
init_prime (private_gmp_helper_t
*this, mpz_t
*prime
, int bytes
)
108 randomizer_t
*randomizer
;
109 chunk_t random_bytes
;
111 randomizer
= randomizer_create();
113 if (randomizer
== NULL
)
118 status
= randomizer
->allocate_random_bytes(randomizer
,bytes
, &random_bytes
);
119 /* not needed anymore */
120 randomizer
->destroy(randomizer
);
121 if (status
!= SUCCESS
)
126 /* convert chunk to mpz value */
127 this->public.chunk_to_mpz(&(this->public),prime
, random_bytes
);
129 /* chunk is not used anymore */
130 allocator_free(random_bytes
.ptr
);
131 random_bytes
.ptr
= NULL
;
133 /* composites are possible but should never occur */
134 mpz_nextprime (*(prime
),*(prime
));
142 * Implements gmp_helper_t's destroy function.
143 * See #gmp_helper_t.destroy for description.
145 static status_t
destroy(private_gmp_helper_t
*this)
147 allocator_free(this);
152 * Described in header
154 gmp_helper_t
*gmp_helper_create()
156 private_gmp_helper_t
*this = allocator_alloc_thing(private_gmp_helper_t
);
162 /* public functions */
163 this->public.destroy
= (status_t (*)(gmp_helper_t
*)) destroy
;
164 this->public.init_prime
= (status_t (*) (gmp_helper_t
*, mpz_t
*, int)) init_prime
;
166 /* private functions */
167 this->public.chunk_to_mpz
= (void (*) (gmp_helper_t
*,mpz_t
*, chunk_t
)) chunk_to_mpz
;
168 this->public.mpz_to_chunk
= (status_t (*) (gmp_helper_t
*,mpz_t
*, chunk_t
*,size_t )) mpz_to_chunk
;
170 return &(this->public);