/*****************************************************************
Copyright (c) 2005 Michele Citterio
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions 
are met:
* Redistributions of source code must retain the above copyright 
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright 
notice, this list of conditions and the following disclaimer in the 
documentation and/or other materials provided with the distribution.
* Neither the name of the <ORGANIZATION> nor the names of its 
contributors may be used to endorse or promote products derived from 
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
PARTICULAR PURPOSE AND NONINFRINGEMENT ARE DISCLAIMED. IN NO EVENT 
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.

owaccudemo is a win32 command line demo tool to perform membership
accumulation and testing using the owaccu class in a step-by-step
manner useful to understand the algorithms implemented for
cryptographic one-way accumulators as described in the paper: 
"Benaloh J. & de Mare M. (199?) - One-Way Accumulators: a 
Decentralized Alternative to Digital Signatures". Defining 
VERBOSE_OUTPUT increase the explanatory output
Please see the accompanying file owaccu.txt for further details.

Contacts: michele.citterio@unimi.it - www.citterio.net

TODO:
*****************************************************************/
//#define VERBOSE_OUTPUT
#include "owaccu.h"

using namespace std;
Miracl precision(500,10); 


int main(int argc, char *argv[])
{ 
	int rigidprimedigits = 100;
	int rigidprimebase = 10;
	int participants = 10;
	member_data *dumb_memberstruct_array;
	dumb_memberstruct_array = new member_data [participants];
	owaccu accumulator(rigidprimedigits, rigidprimebase, participants); //creates and initializes an instance of class owaccu
	
	//dumbly populating the members structure for sake of DEMO implementation
	strcpy (dumb_memberstruct_array[0].name, "Michele");
	strcpy (dumb_memberstruct_array[0].passwd, "iche");
	strcpy (dumb_memberstruct_array[1].name, "Chiara");
	strcpy (dumb_memberstruct_array[1].passwd, "hiar");
	strcpy (dumb_memberstruct_array[9].name, "Elena");
	strcpy (dumb_memberstruct_array[9].passwd, "lena");
	strcpy (dumb_memberstruct_array[2].name, "Willy");
	strcpy (dumb_memberstruct_array[2].passwd, "illy");
	strcpy (dumb_memberstruct_array[3].name, "Tere");
	strcpy (dumb_memberstruct_array[3].passwd, "tere");
	strcpy (dumb_memberstruct_array[4].name, "Carlo");
	strcpy (dumb_memberstruct_array[4].passwd, "arlo");
	strcpy (dumb_memberstruct_array[5].name, "Alice");
	strcpy (dumb_memberstruct_array[5].passwd, "lice");
	strcpy (dumb_memberstruct_array[6].name, "Andrea");
	strcpy (dumb_memberstruct_array[6].passwd, "ndre");
	strcpy (dumb_memberstruct_array[7].name, "Claudia");
	strcpy (dumb_memberstruct_array[7].passwd, "");
	strcpy (dumb_memberstruct_array[8].name, "Simona");
	strcpy (dumb_memberstruct_array[8].passwd, "imon");

	// now adding all of the above members...
	for(int it = 0; it < participants; it++)
	{
		accumulator.add_member(&dumb_memberstruct_array[it]);
#ifdef VERBOSE_OUTPUT
			cout << "\nAt this stage of members accumulation, the z_k credential for n. " << it << " " << accumulator.get_member_data(it).name << " is:\n" << accumulator.get_member_data(it).z_k <<"\n";
			cout << "Resulting accumulated hash is:\n" << accumulator.get_accumulated_hash()<< "\n\n";
#endif
	}
	
	// now checking for membership of a given name...
	member_data user;
	do
	{
		char entered_name[40];
		cout << "\nPlease enter the name you want to check for membership, without spaces\n(or CTRL-C to quit):\n";
		cin >> entered_name;
		strcpy(user.name, entered_name);
		cout << "\nIf required, enter the password (without spaces) for " << user.name << ":\n";
		cin >> user.passwd; 
		cout << "\nYou should now supply " << user.name << "'s z_k credential. For your convenience, in\nthis DEMO it is being retrieved internally:\n";
		for (int it1 = 0; it1 <= participants +1 ; it1++)// it1 = participants means no user found in memory with the entered name
			{
				if (it1 == participants) cout << "\nSorry, no member is known whose name is " <<  user.name << "! You may have mispelled\nthe name, please try again\n\n";
				else
					if (!strcmp(accumulator.get_member_data(it1).name, user.name))
					{
						user.z_k = accumulator.get_member_data(it1).z_k;
						cout << user.z_k << "\n";						
						if (accumulator.check_membership(&user)) cout << "\n\n YES, "<< user.name << " is a member!" ;
						else cout << "\n\nNO, "<< user.name << " is not a member, or the supplied password (if any) is wrong!" ;
						cout << "\n-------------------------\n";
						break;
					}
			}	
	} while (1);
    return 0;
}

