/*
 * elgamal-enc.c
 *
 * Copyright (C) 2012 Jerônimo C. Pellegrini
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to:
 *   The Free Software Foundation, Inc.,
 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdlib.h>
#include <stdio.h>
#include <gmp.h>
#include <math.h>
#include <fcntl.h>
#include <unistd.h>

#include "elgamal.h"

int main() {
	FILE *enc;
	elg_pk pk;
	/* m is the message */
	mpz_t a, b, m;
	unsigned long msg;
	
	printf("Using gmp %s\n", gmp_version);

	read_pk(&pk,"sk");
	printf("OK, the key is:\n");
	gmp_printf("n= %Zd\ng= %Zd\nh= %Zd\n",
		   pk.n,pk.g,pk.h);
	
	printf("Enter a SHORT number ");
	
	scanf("%lu", &msg);

	mpz_init_set_ui(m,msg);

	elg_enc(a,b,m,&pk);
	
	gmp_printf("c1= %Zd\nc2= %Zd\n",a,b);

	enc = fopen("enc","w");
	mpz_out_raw(enc,a);
	mpz_out_raw(enc,b);
	fclose(enc);

	/* Release memory alocated by libmgp */
	mpz_clears(a, b, m, NULL);
	clear_elg_pk(pk);
	
	return EXIT_SUCCESS;
}
