Charm-Crypto: Installation Documentation
-------------------------------------------------------------------------------------------

Charm has automated the installation process such that the end-user
does not have to directly handle dependencies, linking, compiler flag setting, and the
like.  However, there is always the corner case in which an end-user is using a currently
unsupported platform, and thus may need to build and install using a very manual process.

We would like to support you in this case, and have written this documentation to get 
you started on your path using Charm.  This installation file will contain some
installation blocks highlighting each respective implementation.  

Before we begin, please note the current dependencies:
    
- Python 3.8 or later (Python 2 is no longer supported)

- Pyparsing http://pyparsing.wikispaces.com/

- GMP 5.x http://gmplib.org/ 

- PBC 0.5.14 http://crypto.stanford.edu/pbc/news.html

- OPENSSL http://www.openssl.org/

See ./configure.sh --help for other options.
	
	
BUILDING IN LINUX
------------------------

Note that the entire compilation process is supported by the Charm configure/make scripts.
The steps for building in linux this way are:
	1. In a terminal, run configure.sh
    2. sudo make
    3. sudo make install
	
[Ubuntu 20.04/22.04/24.04 LTS]

1. Before installing Charm, there are a few prerequisites that need to be installed on your system:
	1. Git
		sudo apt-get install git
	2. Python 3 and header files
		sudo apt-get install python3 python3-dev python3-pip
	3. Build tools
		sudo apt-get install build-essential m4 flex bison
	4. Required libraries
		sudo apt-get install libgmp-dev libssl-dev

2. Now we need to obtain a copy of Charm:
	git clone https://github.com/JHUISI/charm.git

3. Next, we will install Charm. Navigate to the Charm directory.
	1. We must first run the configuration script:
		./configure.sh
	2. Now we will build and install Charm:
		make
		sudo make install
	3. And finally we must rebuild the searchpath for libraries
		sudo ldconfig

[Fedora] 

1. Before installing Charm, there are a few prerequisites that need to be installed on your system. These are:
	1. m4
		su -c "yum install m4"
    2. Python 3
        su -c "yum install python3"
	3. Header files/static library
		su -c "yum install python3-devel"
	4. openssl-devel
		su -c "yum install openssl-devel"

2. Red Hat/Fedora has decided not to support ECC in OpenSSL due to patent concerns, so we now need to remove their restriction, manually import the required files, and build new shared libraries.
	1. Remove the ECC restriction
		1. Navigate to /usr/include/openssl
			cd /usr/include/openssl
		2. Open the OpenSSL configuration file for editing using your editor of choice
			su -c "vi opensslconf-i386.h"
		3. Remove the flags that restrict the use of ECC
			Delete (at the beginning of file)
				#ifndef OPENSSL_NO_EC
				# define OPENSSL_NO_EC
				#endif
				#ifndef OPENSSL_NO_ECDH
				# define OPENSSL_NO_ECDH
				#endif
				#ifndef OPENSSL_NO_ECDSA
				# define OPENSSL_NO_ECDSA
				#endif
			Delete (later on the file)
				# if defined(OPENSSL_NO_EC) && !defined(NO_EC)
				# define NO_EC
				# endif
				# if defined(OPENSSL_NO_ECDH) && !defined(NO_ECDH)
				# define NO_ECDH
				# endif
				# if defined(OPENSSL_NO_ECDSA) && !defined(NO_ECDSA)
				# define NO_ECDSA
				# endif
		4. Save the file and close it
	2. Add the ECC files
		1. Navigate to http://www.openssl.org/source/ and download the latest version of openssl source
		2. Untar it
		3. Navigate to /path/to/openssl-[version]/include/openssl (ie inside the untarred file)
			cd /path/to/openssl-[version]/include/openssl
		4. Add the new files to the current OpenSSL installation
			su -c "yes n | cp * /usr/include/openssl"
	3. Build the new shared libraries
		1. Navigate to your downloaded version of OpenSSL
			cd /path/to/openssl-[version]/
		2. Configure OpenSSL to make the shared libraries
			./config shared
		3. Make the new OpenSSL library
			make
	4. Move the new shared libraries with their links
		su -c "cp --no-dereference libcrypto.so /usr/local/lib/libcrypto.so.10"
		su -c "cp libcrypto.so.1.0.0 /usr/local/lib"
		su -c "cp --no-dereference libssl.so /usr/local/lib/libssl.so.10"
		su -c "cp libssl.so.1.0.0 /usr/local/lib"
		su -c "cp libcrypto.a /usr/local/lib"
		su -c "cp libssl.a /usr/local/lib"
	5. And now we need to set the library search path to look for the libraries we just added first.
		cd /etc/ld.so.conf.d/
		su -c "vi charm.conf"
		put "/usr/local/lib" (without quotes) into the file, save, and close
		su -c ldconfig
	6. Set the library search path to look for the new libraries first (either type this into your terminal or put it in your .bashrc file and source it)
		export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH

3. Now we need to obtain a copy of Charm:
	git clone -b master https://github.com/JHUISI/charm.git

4. Next, we will install Charm. Navigate to the Charm directory.
	1. We must first run the configuration script:
		su -c ./configure.sh

		If the output of the configure script says that libgmp is already installed (and this is your first time installing Charm), chances are you have an outdated version of GMP.  We need to check which version of libgmp you have installed.
			su -c updatedb
			locate libgmp
		If you don't see libgmp.so.10 OR libgmp.so.10.0.2 in the resulting list, then you will need to build the GMP libraries from source.
			1. Download the latest version of the GMP source from ftp://ftp.gnu.org/gnu/gmp/gmp-5.0.2.tar.gz
			2. Untar it
			3. Navigate to /path/to/gmpsource (ie inside the untarred file)
				cd /path/to/gmpsource
			4. Build and install the libraries
				./configure
				make
				su -c "cp gmp.h /usr/local/include"
				su -c "make install"
            5. You now have two options (remove or keep the old libraries):
				1. Remove the old libraries
					cd /usr/lib
					su -c "rm libgmp.*"
				2. Keep the old libraries, rename the new one
					1. Determine the version of the old library
						su -c updatedb
						locate libgmp
					2. Look for /usr/lib/libgmp.so.X in the resulting list where X is a number (NOT /usr/lib/libgmp.X.Y.Z)
					3. Rename the new link
						cd /usr/local/lib
						su -c "mv libgmp.so.10 libgmp.so.X"
			6. Rebuild the searchpath for libraries
				su -c ldconfig
	2. Now we will build and install Charm:
		su -c "make"
		su -c "make install"
	3. And finally we must rebuild the searchpath for libraries
		su -c ldconfig

[Fedora x86_64]

[Mint x86_64]
1. Before installing Charm, there are a few prerequisites that need to be installed on your system. These are:
        1. Git
                sudo apt-get install git
        2. m4
                sudo apt-get install m4
        3. Python 3
                sudo apt-get install python3
        4. Header files/static library
                sudo apt-get install python3-dev
        5. libssl-dev (only necessary if you did not install Python 3)
                sudo apt-get install libssl-dev
        6. This distro doesn't seem to come with binutils or gcc, install those.

2. Now we need to obtain a copy of Charm:
	git clone git://github.com/JHUISI/charm.git

3. Next, we will install Charm. Navigate to the Charm directory.
        1. We must first run the configuration script:
                sudo bash ./configure.sh                * Bash to avoid unexpected operator error.
        2. Now we will build and install Charm:
                sudo make
                sudo make install
        3. And finally we must rebuild the searchpath for libraries
                sudo ldconfig

[Creating a .deb]
If you want to create a .deb binary from the charm source, there are a
couple more steps. The following was tested in Ubuntu 16.04 (x86_64).
1. Before installing Charm, there are a few prerequisites that need to be installed on your system. These are:
        1. Git
                sudo apt-get install git
        2. m4
                sudo apt-get install m4
        3. Python 3
                sudo apt-get install python3
        4. Header files/static library
                sudo apt-get install python3-dev
        5. libssl-dev (only necessary if you did not install Python 3)
                sudo apt-get install libssl-dev
        6. Additional packages related to packaging for .deb
                sudo apt-get install python3-all-dev debhelper python3-pip
        7. The stdeb python package, installed via pip
                pip3 install stdeb
        8. Verify that *all* of the dependencies at the top of the
            document are satisfied. Not doing this may result in strange
            bugs while compiling the .deb from source.

2. Now we need to obtain a copy of Charm:
	git clone git://github.com/JHUISI/charm.git

3. Next, we will install Charm. Navigate to the Charm directory.
        1. We must first run the configuration script:
                sudo bash ./configure.sh                * Bash to avoid unexpected operator error.
        2. Now we will build and install Charm:
                sudo make
                sudo make install
        3. And finally we must rebuild the searchpath for libraries
                sudo ldconfig

4. Finally, we will build the installer. Navigate to the installers/deb.installer directory, and at a shell prompt run
    sudo python3 create_deb.py      * use python instead of python3 to make a 2.7 binary

5. Install the created .deb file.


    
BUILDING IN WINDOWS
------------------------

Note that the entire compilation process is now supported by the Charm configure/make scripts.
The steps for building in mingw32 this way are:
    1. Download the latest source version of openssl.
	2. Run MinGW Shell.
    3. Extract openssl, configure and install as shown below.	
	4. Extract Charm, and navigate to the top directory.
	5. Run configure.sh as shown below.
	6. The process will fail out at wget, and open Internet Explorer to the wget download 
	   page.
	7. Install wget, and set it's bin directory on your PATH.  To do this, right-click My 
	   Computer, Select Properties, Select Advanced System Settings, Select Advanced, Select
       Environment Variables, and than PATH.  Scroll to the end, and enter a ; followed by 
       the absolute path to the bin directory (e.g., C:\Program Files\etc).
    8. With wget installed, run the configure.sh script again, and it should set up your
       Make dependencies for you.  
    9. Make build.
   10. Make install.
    *. Another way to install dependencies is to follow the Windows blocks below.	

[MinGW32 and Cygwin]

Let's first build our dependencies with the following scripts:
	
	#GMP
	./configure --prefix=/mingw --disable-static --enable-shared
	make
	make install
	
	#OPENSSL
	./config --openssldir=/mingw --shared # This gets us around installing perl.
	make
	make install

        # ** NOTE ** openssl-1.0.0e ./test compilation problems.
        # You will run into a compilation error that looks similar to:
        #    mdtest.c:1:10 error: expected ...
        #
        # To mitigate, do the following:
        #    grep "./test/dummytest.c" *
        #    edit each file from "dummytest.c" to "#include "dummytest.c"

	#PBC
	./configure --prefix=/mingw --disable-static --enable-shared
	make
	make install
	
	#Building Charm
	./configure --prefix=/mingw --python=/c/Python32/python.exe 

        # ** NOTE ** The latest mingw installer comes coupled with gcc-4.6.x
        # which no longer supports the flag -mno-cygwin.  This, unfortunately,
        # is called during python setup.py build as it is a part of a class
        # in distutils for python.  A fix is coming for python to evaluate
        # the gcc compiler version, and remove the flag -mno-cygwin if it
        # 4.6+, but that has yet to be implemented.  

[Building Executable]

	If you are building to make an executable with NSIS, than be sure to
	compile the dependencies first and pass the appropriate header and
    library files to --python-build-ext (calls build_ext option for setup).	
	
	    ./configure.sh --python=/c/Python32/python.exe --python-build-ext="-L/path/to/lib -I/path/to/header"
	

     Need help building the dependencies? Follow the below:
		#GMP
		./configure --prefix=/c/charm-crypto --disable-static --enable-shared
		make
		make install

		#PBC
		./configure --prefix=/c/charm-crypto --disable-static --enable-shared LDFLAGS="-L/c/charm-crypto/lib" CPPFLAGS="-I/c/charm-crypto/include"
		make
		make install

		#OPENSSL
		./config --openssldir=/c/charm-crypto --shared # This gets us around installing perl.
		make
		make install


BUILDING IN OS X
------------------------

Note that the entire compilation process is supported by the Charm configure/make scripts.

[macOS (Monterey, Ventura, Sonoma)]

1. Install Homebrew if not already installed:
	/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

2. Install dependencies via Homebrew:
	brew install gmp openssl@3 flex bison wget

3. Build and install PBC library:
	wget https://crypto.stanford.edu/pbc/files/pbc-1.0.0.tar.gz
	tar -xzf pbc-1.0.0.tar.gz
	cd pbc-1.0.0
	./configure LDFLAGS="-L$(brew --prefix gmp)/lib" CPPFLAGS="-I$(brew --prefix gmp)/include"
	make
	sudo make install

4. Configure and build Charm:
	./configure.sh --enable-darwin
	make
	sudo make install

