/*
 *  elliptic_curves.cpp
 *  EC_find_no_MPI
 *
 *  Created by Christopher Evans on 6/14/11.
 *  Copyright 2011 University of Arkansas. All rights reserved.
 *  
 *  This is the header file for the class elliptic_curves
 *  This implements the structure of an elliptic curve in weierstrass normal form over fields of characteristic not equal to 2 or 3
 *  The curve is given by the equation y^2= x^3 +ax +b
 *  The variable p denotes the field of definition for the elliptic curve (that is, the elliptic curve is considered over Z/pZ
 *  This class also implements a basepoint for the curve, to be utilized in the find_curves class
 *  This class contains no real mathematics, it simply defines a convenient structure for find_curves to utilize
 */

#include "elliptic_curves.h"


Elliptic_Curve::Elliptic_Curve(){
	//default constructor, initializes all variables to 0; which won't do you much good
	a=0;
	b=0;
	p=0;
	b1=0;
	b2=0;
}
Elliptic_Curve::Elliptic_Curve(long _a, long _b, long _p, long _b1, long _b2){
	// constructor, initializes all variables to the given input
	a=_a;
	b=_b;
	p=_p;
	b1=_b1;
	b2=_b2;
}

int Elliptic_Curve::string_to_int(string object){ //casts a string into an integer for parsing purposes
	int result;
	istringstream ss(object);
	ss >> result;
	return result;
}

string Elliptic_Curve::int_to_string(int object){ //casts an integer into a string for parsing purposes
	ostringstream result;
	result << object;
	return result.str();
}

Elliptic_Curve::Elliptic_Curve(string input_data){
	//constructor, initializes all variables from a serialized string, presumably given in the correct format
	char * cstr, *c;
	cstr= new char[input_data.size()+1];
	strcpy (cstr, input_data.c_str());
	c=strtok( cstr, ", ");
	int array[10];
	int index=0;
	
	while( c != NULL){
		array[index]=string_to_int(c);
		c=strtok(NULL, ", ");
	}
	delete[] cstr;
	a=array[0];
	b=array[1];
	p=array[2];
	b1=array[3];
	b2=array[4];
}
string Elliptic_Curve::to_serial(){
	// serializes an instance of Elliptic_Curve into a string in CSV format.
	return int_to_string(a) + ", " + int_to_string(b) + ", " + int_to_string(p) + ", " + int_to_string(b1) + ", " + int_to_string(b2);

}