Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update tutorial ml folder #52

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
./src/*.o
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see some .o files in the commit, something like *.o might work.

10 changes: 6 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ RUN apt-get update \
make \
g++ \
libssl-dev \
nano \
tmux \
&& rm -r /var/lib/apt/lists/*

RUN git clone https://github.com/snwagh/falcon-public.git Falcon

RUN cd Falcon \
&& make all -j$(nproc)
RUN git clone https://github.com/trhieung/falcon-public Falcon
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're pushing these changes upstream, can you edit the repo address to link to https://github.com/snwagh/falcon-public.git.


WORKDIR Falcon

RUN chmod +x /Falcon/entrypoint.sh
ENTRYPOINT ["/Falcon/entrypoint.sh"]
107 changes: 56 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
# Falcon: Honest-Majority Maliciously Secure Framework for Private Deep Learning
# Falcon: Honest-Majority Maliciously Secure Framework for Private Deep Learning and Fixed Key Random Oracle

A maliciously secure framework for efficient 3-party protocols tailored for neural networks. This work builds off [SecureNN](https://github.com/snwagh/securenn-public), [ABY3](https://github.com/ladnir/aby3) and other prior works. This work is published in [Privacy Enhancing Technologies Symposium (PETS) 2021](https://petsymposium.org). Paper available [here](https://snwagh.github.io). If you're looking to run Neural Network training, strongly consider using this GPU-based codebase [Piranha](https://github.com/ucbrise/piranha).
This project is a fork of [Falcon framework](https://github.com/snwagh/falcon-public.git) implementation, integrating the setting of [the random oracle](https://github.com/anbaccar/RSS_ring_ppml/blob/master/include/randBit.h) as demonstrated in [RingPPML](https://github.com/anbaccar/RSS_ring_ppml.git).


### Table of Contents

- [Warning](#warning)
- [Requirements](#requirements)
- [Source Code](#source-code)
- [Repository Structure](#repository-structure)
- [Building the code](#building-the-code)
- [Running the code](#running-the-code)
- [Additional Resources](#additional-resources)
- [Comparison with RingPPML](#comparison-with-ringppml)
- [Citation](#citation)

<!-- ### Table of Contents

- [Warning](#warning)
- [Requirements](#requirements)
- [Source Code](#source-code)
Expand All @@ -16,13 +28,13 @@ A maliciously secure framework for efficient 3-party protocols tailored for neur
- [Comparison with SecureNN](#comparison-with-securenn)
- [Errors and Issues](#errors-and-issues)
- [Todos](#todos)
- [Citation](#citation)
- [Citation](#citation) -->


### Warning
---
This codebase is released solely as a reference for other developers, as a proof-of-concept, and for benchmarking purposes. In particular, it has not had any security review, has a number of implementational TODOs, has a number of known bugs (especially in the malicious implementation), and thus, should be used at your own risk. You can contribute to this project by creating pull requests and submitting fixes and implementations. The code has not run end-to-end training and we expect this to require some parameter tuning, hence training and inference won't work out of the box (however, inference from pre-trained networks can be repreduced easily).

With a base warning displayed in the original repository of the [Falcon framework](https://github.com/snwagh/falcon-public.git), we note that this is the final project intended to support studying.

### Requirements
---
Expand All @@ -35,78 +47,63 @@ This codebase is released solely as a reference for other developers, as a proof

Install these packages with your favorite package manager, e.g, `sudo apt-get install <package-name>`.

* For more details and up-to-date information, please refer to the original [falcon-framework](https://github.com/snwagh/falcon-public.git)
### Docker
---

To install and run Falcon using docker, first build the container:
To install and run Falcon using docker by downloading the [Dockerfile](https://github.com/trhieung/falcon-public/blob/master/Dockerfile) in this project,
- first build the container:
` docker build -t falcon .`
then run
`docker run -it falcon '/bin/bash'`.
- Then run the container
`docker run -it falcon '/bin/bash'`
- Use tmux and the following command combination `Ctrl+b %` to create 3 windows for demonstrating with 3 parties.
- Each window represents the ID of the respective parties. Run the following commands in each window to get the results

From the prompt, you can execute any of the commands specified in [Running the code](#running).
#### Party 0
```
./Falcon.out 0 files/IP_localhost files/keyA files/keyAB files/keyAC LeNet MNIST Semi-honest
```
#### Party 1
```
./Falcon.out 1 files/IP_localhost files/keyB files/keyBC files/keyAB LeNet MNIST Semi-honest
```
#### Party 2
```
./Falcon.out 2 files/IP_localhost files/keyC files/keyAC files/keyBC LeNet MNIST Semi-honest
```

### Source Code
---

#### Repository Structure

* `files/` - Shared keys, IP addresses and data files.
* `files/preload` - Contains data for pretrained network from SecureML. The other networks can be generated using `scripts` and functions in `secondary.cpp`
* `lib_eigen/` - [Eigen library](http://eigen.tuxfamily.org/) for faster matrix multiplication.
* `src/` - Source code.
* `util/` - Dependencies for AES randomness.
* `scripts/` - Contains python code to generate trained models for accuracy testing over a batch.
* The `god` script makes remote runs simpler (as well as the `makefile`)
* `src/Ranbit.h` and `src/Ranbit.cpp` - The additions we contributed

#### Building the code

To build Falcon, run the following commands:

```
git clone https://github.com/snwagh/falcon-public.git Falcon
git clone https://github.com/trhieung/falcon-public Falcon
cd Falcon
make all -j$(nproc)
```

#### Running the code

To run the code, simply choose one of the following options:

* `make`: Prints all the possible makefile options.
* `make terminal`: Runs the 3PC code on localhost with output from $P_0$ printed to standard output.
* `make file`: : Runs the 3PC code on localhost with output from $P_0$ printed to a file (in `output/3PC.txt`)
* `make valg`: Useful for debugging the code for set faults. Note that the -03 optimization flag needs to be suppressed (toggle lines 42, 43 in `makefile`)
* `make command`: Enables running a specific network, dataset, adversarial model, and run type (localhost/LAN/WAN) specified through the `makefile`. This takes precedence over choices in the `src/main.cpp` file.
* To run the code over tmux over multiple terminals, `make zero`, `make one`, and `make two` come in handy.
* Finally, the `makefile` (line 4-15) contains the descriptions of the arguments accepted by the executable.

To run the code, please refer to the original [falcon-framework](https://github.com/snwagh/falcon-public.git) or follow the steps with [docker](#docker) for testing purposes

### Additional Resources
---
#### Run combinations
Note that given the size of the larger networks (AlexNet, VGG16) and the need to explicitly define network parameters, these networks can only be run for the CIFAR10 and Tiny ImageNet dataset. On the contrary, the smaller datasets (SecureML, Sarda, MiniONN, and LeNet) can only be run for the MNIST dataset. Running them otherwise should result in assertion errors. The following configuration was sufficient to produce the results for the larger networks: 2.9 GHz Intel Xeon E5-2666 v3 Processor, 36 cores, 60 GB RAM (in particular, a similar processor with 16 GB RAM was insifficient).

#### Comparison with [SecureNN](https://github.com/snwagh/securenn-public)
While a bulk of the Falcon code builds on SecureNN, it differs in two important characterastics (1) Building on replicated secret sharing (RSS) (2) Modularity of the design. The latter enables each layer to self contained in forward and backward pass (in contrast to SecureNN where layers are merged for the networks to be tested). The functions are reasonably tested (including ReLU) however they are more tested for 32-bit datatype so the 64-bit might have minor bugs.

#### Errors and Issues
If there are compile/installation/runtime errors, please create git issues. Some of the common errors and their resolutions are listed below:
* `cannot find` something error: This would be a linker error, make sure your code has access to the right paths in the `$PATH` environment variable.
* `Binding error`: There is probably an execution running in the background. Kill the process with `pkill Falcon.out` (this happens since the socket ports are hardcoded in the program and a number of the makefile commands run code in the background and `Ctrl+c` only kills the Party 0 code)
* `Bad alloc`: This is probably a memory issue. If you're running one of the larger networks (AlexNet, VGG16) on a laptop, try running them on a server/cluster/machine with more memory.


#### Todos/Bug todos

* Remove size argument from all functions (generate it inside functions)
* Clean-up tools and functionalities file -- move reconstruction functions to tools
* Pointers to layer configurations are never deleted --> needs to be fixed
* Precompute implementation
* Incorrect communication numbers for parallel implememntations
* ...
#### Comparison with [RingPPML](https://github.com/anbaccar/RSS_ring_ppml.git)
| Element | Falcon | RingPPML |
| :---: | :---: | :---: |
| Data Transfer | Not supported: encrypt before sending or decrypt when receiving data <br> (as this is just for benchmarking) | Supported: AES encryption and decryption with block size, [implementation](https://github.com/anbaccar/RSS_ring_ppml/blob/7b9483e203b8f9547532cef6cd446d40e9947a7b/src/connection/NodeNetwork.cpp#L399) |
| Computation Base | some protocol base on field modulo, [implentation](https://github.com/snwagh/falcon-public/blob/master/src/tools.h#L193) | All computation on Ring $Z_{2^k}$, speeding up the computation cost of modulo |

#### Citation
You can cite the paper using the following bibtex entry (the paper links to this repo):
You can cite the paper using the following bibtex entry (the paper links to this origin repo):
```
@inproceedings{wagh2021falcon,
title={{FALCON: Honest-Majority Maliciously Secure Framework for Private Deep Learning}},
Expand All @@ -115,7 +112,15 @@ You can cite the paper using the following bibtex entry (the paper links to this
year={2021}
}
```

---
For questions, please create git issues; for eventual replies, you can also reach out to [[email protected]]([email protected])

or cite RingPPML paper with by
```
@article{baccarini2023rss,
title={Multi-Party Replicated Secret Sharing over a Ring with Applications to Privacy-Preserving Machine Learning},
author={Baccarini, Alessandro and Blanton, Marina and Yuan, Chen},
journal={Proceedings on Privacy Enhancing Technologies (PoPETs)},
volume = 2023,
number = 1,
pages={608-626},
year={2023}
}
```
7 changes: 7 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
# RandBit
cd /Falcon
make all -j$(nproc)

# Execute any additional command you want after the build, for example, running a bash shell
exec "$@"
Binary file added src/AESObject.o
Binary file not shown.
33 changes: 33 additions & 0 deletions src/Functionalities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1631,6 +1631,39 @@ void funcMaxpool(RSSVectorMyType &a, RSSVectorMyType &max, RSSVectorSmallType &m
computation. So it might be the case that the approximations
introduced by negative and positive numbers in effect cancel out to
preserve overall NN accuracy. */


void debugRandBit(){
struct timeval start;
struct timeval end;
unsigned long timer;

size_t size;
string network = "Debug randbit";

for (int j = 0; j < 4; j++){
RSSVectorMyType b;
vector<myType> b_reconst;

size = (1 << 10) * pow(10, j);
for (int i = 0; i < size; i++){
b.push_back(std::make_pair(0, 0));
b_reconst.push_back(0);
}

gettimeofday(&start, NULL); // start timer here
Rss_RandBit(b, size);
gettimeofday(&end, NULL); // stop timer here
timer = 1e6 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;
printf("randbit: \t%.6lf ms\n", (double)(timer * 0.001));

#if (!LOG_DEBUG)
funcReconstruct(b, b_reconst, size, "b", true);
#endif
}

}

void debugMatMul()
{
// size_t rows = 1000;
Expand Down
2 changes: 2 additions & 0 deletions src/Functionalities.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ void debugSSBits();
void debugSS();
void debugMaxpool();

// custom debug
void debugRandBit();

//Test
void testMatMul(size_t rows, size_t common_dim, size_t columns, size_t iter);
Expand Down
Binary file added src/basicSockets.o
Binary file not shown.
Binary file added src/connect.o
Binary file not shown.
6 changes: 5 additions & 1 deletion src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


/********************* AES and other globals *********************/
#define LOG_DEBUG false
#define LOG_DEBUG true
#define LOG_DEBUG_NETWORK false
#define FUNCTION_TIME false
#define RANDOM_COMPUTE 256 //Size of buffer for random elements
Expand Down Expand Up @@ -64,4 +64,8 @@ const myType LARGEST_NEG = ((myType)1 << (BIT_SIZE - 1));
const myType MINUS_ONE = (myType)-1;
const smallType BOUNDARY = (256/PRIME_NUMBER) * PRIME_NUMBER;

/********************* Typedefs for rings *********************/
typedef unsigned long Lint;
typedef long long int sLint;

#endif
79 changes: 39 additions & 40 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,30 @@
#include "NeuralNetwork.h"
#include "unitTests.h"


int partyNum;
AESObject* aes_indep;
AESObject* aes_next;
AESObject* aes_prev;
AESObject *aes_indep;
AESObject *aes_next;
AESObject *aes_prev;
Precompute PrecomputeObject;


int main(int argc, char** argv)
int main(int argc, char **argv)
{
/****************************** PREPROCESSING ******************************/
/****************************** PREPROCESSING ******************************/
parseInputs(argc, argv);
NeuralNetConfig* config = new NeuralNetConfig(NUM_ITERATIONS);
NeuralNetConfig *config = new NeuralNetConfig(NUM_ITERATIONS);
string network, dataset, security;
bool PRELOADING = false;

/****************************** SELECT NETWORK ******************************/
//Network {SecureML, Sarda, MiniONN, LeNet, AlexNet, and VGG16}
//Dataset {MNIST, CIFAR10, and ImageNet}
//Security {Semi-honest or Malicious}
/****************************** SELECT NETWORK ******************************/
// Network {SecureML, Sarda, MiniONN, LeNet, AlexNet, and VGG16}
// Dataset {MNIST, CIFAR10, and ImageNet}
// Security {Semi-honest or Malicious}
if (argc == 9)
{network = argv[6]; dataset = argv[7]; security = argv[8];}
{
network = argv[6];
dataset = argv[7];
security = argv[8];
}
else
{
network = "SecureML";
Expand All @@ -38,28 +40,29 @@ int main(int argc, char** argv)
}
selectNetwork(network, dataset, security, config);
config->checkNetwork();
NeuralNetwork* net = new NeuralNetwork(config);
NeuralNetwork *net = new NeuralNetwork(config);

/****************************** AES SETUP and SYNC ******************************/
/****************************** AES SETUP and SYNC ******************************/
aes_indep = new AESObject(argv[3]);
aes_next = new AESObject(argv[4]);
aes_prev = new AESObject(argv[5]);

initializeCommunication(argv[2], partyNum);
synchronize(2000000);

/****************************** RUN NETWORK/UNIT TESTS ******************************/
//Run these if you want a preloaded network to be tested
//assert(NUM_ITERATION == 1 and "check if readMiniBatch is false in test(net)")
//First argument {SecureML, Sarda, MiniONN, or LeNet}
// network += " preloaded"; PRELOADING = true;
// preload_network(PRELOADING, network, net);
/****************************** RUN NETWORK/UNIT TESTS ******************************/
// Run these if you want a preloaded network to be tested
// assert(NUM_ITERATION == 1 and "check if readMiniBatch is false in test(net)")
// First argument {SecureML, Sarda, MiniONN, or LeNet}
// network += " preloaded"; PRELOADING = true;
// preload_network(PRELOADING, network, net);

start_m();
//Run unit tests in two modes:
// start_m();
// Run unit tests in two modes:
// 1. Debug {Mat-Mul, DotProd, PC, Wrap, ReLUPrime, ReLU, Division, BN, SSBits, SS, and Maxpool}
// 2. Test {Mat-Mul1, Mat-Mul2, Mat-Mul3 (and similarly) Conv*, ReLU*, ReLUPrime*, and Maxpool*} where * = {1,2,3}
// runTest("Debug", "BN", network);
// runTest("Debug", "RandBit", network);
// runTest("Debug", "MultPub", network);
// runTest("Test", "ReLUPrime1", network);

// Run forward/backward for single layers
Expand All @@ -69,24 +72,24 @@ int main(int argc, char** argv)
// string what = "F";
// runOnly(net, l, what, network);

//Run training
// Run training
network += " train";
train(net);

//Run inference (possibly with preloading a network)
// network += " test";
// test(PRELOADING, network, net);
// Run inference (possibly with preloading a network)
// network += " test";
// test(PRELOADING, network, net);

end_m(network);
cout << "----------------------------------------------" << endl;
cout << "Run details: " << NUM_OF_PARTIES << "PC (P" << partyNum
<< "), " << NUM_ITERATIONS << " iterations, batch size " << MINI_BATCH_SIZE << endl
<< "Running " << security << " " << network << " on " << dataset << " dataset" << endl;
cout << "----------------------------------------------" << endl << endl;
// end_m(network);
// cout << "----------------------------------------------" << endl;
// cout << "Run details: " << NUM_OF_PARTIES << "PC (P" << partyNum
// << "), " << NUM_ITERATIONS << " iterations, batch size " << MINI_BATCH_SIZE << endl
// << "Running " << security << " " << network << " on " << dataset << " dataset" << endl;
// cout << "----------------------------------------------" << endl << endl;

printNetwork(net);
// printNetwork(net);

/****************************** CLEAN-UP ******************************/
/****************************** CLEAN-UP ******************************/
delete aes_indep;
delete aes_next;
delete aes_prev;
Expand All @@ -96,7 +99,3 @@ int main(int argc, char** argv)

return 0;
}




Binary file added src/main.o
Binary file not shown.
Loading