Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
shesl-meow committed Mar 18, 2020
0 parents commit d9891c6
Show file tree
Hide file tree
Showing 14 changed files with 539 additions and 0 deletions.
147 changes: 147 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
### C++ template
# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea/workspace.xml
.idea/tasks.xml
.idea/dictionaries
.idea/vcs.xml
.idea/jsLibraryMappings.xml

# Sensitive or high-churn files:
.idea/dataSources.ids
.idea/dataSources.xml
.idea/dataSources.local.xml
.idea/sqlDataSources.xml
.idea/dynamic.xml
.idea/uiDesigner.xml

# Gradle:
.idea/gradle.xml
.idea/libraries

# Mongo Explorer plugin:
.idea/mongoSettings.xml

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
__MACOSX/
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/KeyMiningAlgorithm.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
cmake_minimum_required(VERSION 3.15)
project(KeyMiningAlgorithm)

set(CMAKE_CXX_STANDARD 11)
include_directories(/usr/local/include)
link_directories(/usr/local/lib)

# TODO: Prepared for boost testing
enable_testing()
set(Boost_ADDITIONAL_VERSIONS "1.XX" "1.XX.0")

# TODO: Effiently gcd algorithm and it's test cases
add_library(EfficientlyGCD include/EffientlyGCD.hpp ./src/ProductTree.cpp ./src/RemainderTree.cpp)
target_include_directories(EfficientlyGCD
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/
)
set_target_properties(EfficientlyGCD
PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib
)
target_link_libraries(EfficientlyGCD gmp boost_unit_test_framework)

add_executable(EfficientlyGCDTestCases ./test/EfficientlyGCDTestCases.cpp)
target_link_libraries(EfficientlyGCDTestCases boost_unit_test_framework EfficientlyGCD)
add_test(NAME EfficientlyGCDTest COMMAND EfficientlyGCDTestCases)
49 changes: 49 additions & 0 deletions include/EffientlyGCD.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Created by 佘崧林 on 2020/1/22.
//
#ifndef KEYMININGALGORITHM_EFFIENTLYGCD_HPP
#define KEYMININGALGORITHM_EFFIENTLYGCD_HPP

#include <gmpxx.h>
#include <list>
#if BOOST_TEST_MODULE == EfficientlyGCDTestCases
#include <boost/test/included/unit_test.hpp>
#endif

#include "../src/ProductTree.h"
#include "../src/RemainderTree.h"


std::list<mpz_class> EffientlyGCD(const std::list<mpz_class>& bigNumbers) {
std::list<mpz_class> gcdList;
// TODO: STEP1 - build product tree from RSA moduli
auto *productTree = new ProductTree(bigNumbers);
#if BOOST_TEST_MODULE == EfficientlyGCDTestCases
auto root = productTree->getNodeData();
BOOST_TEST_MESSAGE("\nProduct tree has been built success. Product tree's root bits length: " << root.get_str(2).size());
#endif
// TODO: STEP2 - compute remainder tree with `P mod N^2`
auto *remainderTree = new RemainderTree((const ProductTree *&)productTree);
std::list<mpz_class> remainderLeaf = remainderTree->preOrderGetLeafList();
std::list<mpz_class> productLeaf = productTree->preOrderGetLeafList();
#if BOOST_TEST_MODULE == EfficientlyGCDTestCases
BOOST_TEST_MESSAGE("Remainder tree has built success. Remainder tree's leaf list:");
for (const auto& remainder: remainderLeaf) {
BOOST_TEST_MESSAGE("\t\t" << remainder.get_str(16).substr(0, 16) << "...");
}
BOOST_REQUIRE_MESSAGE(productLeaf.size() == bigNumbers.size(), "Product tree leaf number");
BOOST_REQUIRE_MESSAGE(productLeaf == bigNumbers, "Product tree leaf order");
#endif
// TODO: STEP3 - compute `gcd(N, z/N)`
mpz_t quotientNumber, gcdNumber;
mpz_inits(quotientNumber, gcdNumber, NULL);
for (auto N = productLeaf.begin(), z = remainderLeaf.begin(); N != productLeaf.end() && z != remainderLeaf.end(); N++, z++) {
mpz_div(quotientNumber, z->get_mpz_t(), N->get_mpz_t());
mpz_gcd(gcdNumber, quotientNumber, N->get_mpz_t());
gcdList.emplace_back(gcdNumber);
}
mpz_clears(quotientNumber, gcdNumber, NULL);
return gcdList;
}

#endif
38 changes: 38 additions & 0 deletions src/AbstractTree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Created by 佘崧林 on 2020/1/22.
//

#ifndef KEYMININGALGORITHM_ABSTRACTTREE_H
#define KEYMININGALGORITHM_ABSTRACTTREE_H

#include <list>
#include <ostream>

template <typename DataType>
class AbstractTree {
protected:
DataType nodeData;
AbstractTree *parentTree = nullptr;
AbstractTree *leftChild = nullptr;
AbstractTree *rightChild = nullptr;

public:
~AbstractTree();
AbstractTree(const AbstractTree &copy);
explicit AbstractTree(const DataType &data): nodeData(data) {}
AbstractTree(const DataType &data, AbstractTree *lt, AbstractTree *rt);

const DataType &getNodeData() const { return this->nodeData; }
AbstractTree *getParent() const { return this->parentTree; }
AbstractTree *getLeftChild() const { return this->leftChild; }
AbstractTree *getRightChild() const { return this->rightChild; }

void setNodeData(const DataType &data) { this->nodeData = data; }
void setParent(AbstractTree *data) { this->parentTree = data; }
void preOrderForEach(const std::function<void(AbstractTree<mpz_class> *)> &exec);
std::list<DataType> preOrderGetLeafList();
};

#include "AbstractTree.tpp"

#endif //KEYMININGALGORITHM_ABSTRACTTREE_H
53 changes: 53 additions & 0 deletions src/AbstractTree.tpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// Created by 佘崧林 on 2020/1/22.
//

template<typename DataType>
AbstractTree<DataType>::~AbstractTree() {
if (this->leftChild != nullptr) {
delete this->leftChild;
this->leftChild = nullptr;
}
if (this->rightChild != nullptr) {
delete this->rightChild;
this->rightChild = nullptr;
}
}

template<typename DataType>
AbstractTree<DataType>::AbstractTree(const AbstractTree &copy) {
this->nodeData = copy.getNodeData();
if (copy.getLeftChild() != nullptr) {
this->leftChild = new AbstractTree(*copy.getLeftChild());
this->leftChild->setParent(this);
}
if (copy.getRightChild() != nullptr) {
this->rightChild = new AbstractTree(*copy.getRightChild());
this->rightChild->setParent(this);
}
}

template<typename DataType>
AbstractTree<DataType>::AbstractTree(const DataType &data, AbstractTree *lt, AbstractTree *rt)
: nodeData(data), leftChild(lt), rightChild(rt) {
assert(lt->parentTree == nullptr && rt->parentTree == nullptr);
lt->parentTree = this;
rt->parentTree = this;
}

template<typename DataType>
void AbstractTree<DataType>::preOrderForEach(const std::function<void(AbstractTree<mpz_class> *)> &exec) {
exec(this);
if (this->leftChild != nullptr) this->leftChild->preOrderForEach(exec);
if (this->rightChild != nullptr) this->rightChild->preOrderForEach(exec);
}

template<typename DataType>
std::list<DataType> AbstractTree<DataType>::preOrderGetLeafList(){
std::list<DataType> leafList;
this->preOrderForEach([&leafList](AbstractTree *root) {
if (root->leftChild == nullptr && root->rightChild == nullptr) leafList.push_back(root->getNodeData());
});
return leafList;
}

Loading

0 comments on commit d9891c6

Please sign in to comment.