/*****************************************************************
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.

The owaccu class contains an implementation of cryptographic
one-way accumulators as described in the paper:
"Benaloh J. & de Mare M. (199?) - One-Way Accumulators: a
Decentralized Alternative to Digital Signatures". Please see
the accompanying file owaccu.txt for further details.

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

TODO: implement exceptions handling, write actual file parser
*****************************************************************/

#pragma once
#include "big.h"

struct member_data
{
	char name[40];
	char passwd[40];
	Big z_k;//the final accumulated hash value missing of the k-th y
	member_data(){z_k = 0;};
};

class owaccu
{
	Big x; //agreed upon base, rigidprimedigits digits long, have to be published
	Big r; //strong random bignum used in generating p and q (sensible data: to be wiped ASAP)
	Big p; //first safe prime (sensible data: to be wiped ASAP)
	Big q; //second safe prime (sensible data: to be wiped ASAP)
	Big n; //rigid prime n = p * q
	Big z; //the accumulated hash value
	//sha sh;
	csprng rng;
	Big gen_rigid_prime(int rigidprimedigits, int rigidprimebase);
	Big one_way_accumulator(Big x, Big y, Big n);
	Big hash_member_data(member_data *member);
	int participants;
	int already_added;
	member_data *members;//see add_memmber() for the need to have the whole list in members[], hence it is safer to have it inside the class
public:
	owaccu(int rigidprimedigits, int rigidprimebase, int participants);//perchè pubblico??
	owaccu(Big modulus, Big agreedbase, Big accumulatedhash);
	~owaccu();
	Big add_member(member_data *member);
	member_data get_member_data(int member_number);
	int check_membership(member_data *member);
	Big get_accumulated_hash() {return z;}
	Big get_agreed_base() {return x;}
	Big get_rigid_prime() {return n;}
};


owaccu::owaccu(int rigidprimedigits, int rigidprimebase, int participants);

owaccu::owaccu(Big modulus, Big agreedbase, Big accumulatedhash);
owaccu::~owaccu();
Big owaccu::add_member(member_data *member);
member_data owaccu::get_member_data(int member_number);
int owaccu::check_membership(member_data *member);
Big owaccu::gen_rigid_prime(int rigidprimedigits, int rigidprimebase);
Big owaccu::one_way_accumulator(Big x, Big y, Big n);
Big owaccu::hash_member_data(member_data *member);

