├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── CMakePresets.json ├── LICENSE ├── README.md ├── build.py ├── cmake ├── Config.cmake.in ├── buildOptions.cmake ├── findDependancies.cmake ├── install.cmake ├── libPSIConfig.cmake └── preamble.cmake ├── frontend ├── CMakeLists.txt ├── DrrnPSIMain.cpp ├── OtBinMain.cpp ├── OtBinMain.h ├── bloomFilterMain.cpp ├── bloomFilterMain.h ├── cuckoo │ ├── SimpleCuckoo.cpp │ ├── SimpleCuckoo.h │ ├── bounds.xlsx │ ├── bounds2.xlsx │ ├── cuckooTests.cpp │ ├── cuckooTests.h │ └── stashSize_2h.txt ├── dcwMain.cpp ├── dcwMain.h ├── dktMain.cpp ├── dktMain.h ├── ecdhMain.cpp ├── ecdhMain.h ├── frontend.vcxproj ├── main.cpp ├── util.cpp └── util.h ├── libPSI ├── CMakeLists.txt ├── MPSI │ ├── DKT │ │ ├── DktMPsiReceiver.cpp │ │ ├── DktMPsiReceiver.h │ │ ├── DktMPsiSender.cpp │ │ └── DktMPsiSender.h │ ├── Grr18 │ │ ├── Grr18Common.cpp │ │ ├── Grr18Common.h │ │ ├── Grr18MPsiReceiver.cpp │ │ ├── Grr18MPsiReceiver.h │ │ ├── Grr18MPsiSender.cpp │ │ ├── Grr18MPsiSender.h │ │ ├── bayesian.gp │ │ ├── bayesian.ps1 │ │ ├── bayesian2.gp │ │ ├── bayesian3.gp │ │ ├── gp_history.txt │ │ └── output.txt │ ├── Rr16 │ │ ├── AknBfMPsiReceiver.cpp │ │ ├── AknBfMPsiReceiver.h │ │ ├── AknBfMPsiSender.cpp │ │ └── AknBfMPsiSender.h │ └── Rr17 │ │ ├── Rr17MPsiDefines.h │ │ ├── Rr17a │ │ ├── Rr17aMPsiReceiver.cpp │ │ ├── Rr17aMPsiReceiver.h │ │ ├── Rr17aMPsiSender.cpp │ │ └── Rr17aMPsiSender.h │ │ └── Rr17b │ │ ├── Rr17bMPsiReceiver.cpp │ │ ├── Rr17bMPsiReceiver.h │ │ ├── Rr17bMPsiSender.cpp │ │ └── Rr17bMPsiSender.h ├── PIR │ ├── BgiPirClient.cpp │ ├── BgiPirClient.h │ ├── BgiPirServer.cpp │ └── BgiPirServer.h ├── PSI │ ├── Dcw │ │ ├── DcwRBfPsiReceiver.cpp │ │ ├── DcwRBfPsiReceiver.h │ │ ├── DcwRBfPsiSender.cpp │ │ └── DcwRBfPsiSender.h │ ├── Drrn │ │ ├── DrrnPsiClient.cpp │ │ ├── DrrnPsiClient.h │ │ ├── DrrnPsiServer.cpp │ │ └── DrrnPsiServer.h │ ├── ECDH │ │ ├── EcdhPsiReceiver.cpp │ │ ├── EcdhPsiReceiver.h │ │ ├── EcdhPsiSender.cpp │ │ └── EcdhPsiSender.h │ ├── Kkrt │ │ ├── KkrtPsiReceiver.cpp │ │ ├── KkrtPsiReceiver.h │ │ ├── KkrtPsiSender.cpp │ │ └── KkrtPsiSender.h │ └── Prty │ │ ├── Poly │ │ ├── polyFFT.cpp │ │ ├── polyFFT.h │ │ ├── polyFFT2.cpp │ │ ├── polyFFT2.h │ │ ├── polyNTL.cpp │ │ └── polyNTL.h │ │ ├── PrtyDefines.h │ │ ├── PrtyReceiver.cpp │ │ ├── PrtyReceiver.h │ │ ├── PrtySender.cpp │ │ └── PrtySender.h ├── Tools │ ├── BalancedIndex.cpp │ ├── BalancedIndex.h │ ├── CuckooHasher.cpp │ ├── CuckooHasher.h │ ├── CuckooIndex2.cpp │ ├── CuckooIndex2.h │ ├── RandomShuffle.cpp │ ├── RandomShuffle.h │ ├── SimpleHasher.cpp │ ├── SimpleHasher.h │ ├── SimpleIndex.cpp │ ├── SimpleIndex.h │ ├── fileBased.cpp │ └── fileBased.h ├── Version.cpp ├── Version.h └── config.h.in ├── libPSI_Tests ├── AknBfPsi_Tests.cpp ├── AknBfPsi_Tests.h ├── BgiPirTests.cpp ├── BgiPirTests.h ├── BinOtPsi_Tests.cpp ├── BinOtPsi_Tests.h ├── CMakeLists.txt ├── Common.cpp ├── Common.h ├── DcwBfPsi_Tests.cpp ├── DcwBfPsi_Tests.h ├── DktMPsi_Tests.cpp ├── DktMPsi_Tests.h ├── DrrnPsi_Tests.cpp ├── DrrnPsi_Tests.h ├── EcdhPsi_Tests.cpp ├── EcdhPsi_Tests.h ├── FileBase_Tests.cpp ├── FileBase_Tests.h ├── Grr18MPSI_Tests.cpp ├── Grr18MPSI_Tests.h ├── ShamirSSScheme_Tests.cpp ├── ShamirSSScheme_Tests.h ├── UnitTests.cpp └── UnitTests.h └── thirdparty ├── fetch.cmake ├── getLibOTe.cmake └── getSparsehash.cmake /.gitignore: -------------------------------------------------------------------------------- 1 | .vs/ 2 | out/ 3 | CMakeSettings.json 4 | thirdparty/ -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/osu-crypto/libPSI/1159df1fecad06570ec384d64ab0724d05b6905b/.gitmodules -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required (VERSION 3.18) 2 | 3 | project("libPSI" VERSION 1.0.0) 4 | 5 | # sets some variables 6 | include(cmake/preamble.cmake) 7 | 8 | # sets build option 9 | include(cmake/buildOptions.cmake) 10 | 11 | # find all the dependancies. 12 | include(cmake/findDependancies.cmake) 13 | 14 | add_definitions(-DSOLUTION_DIR=\"${CMAKE_SOURCE_DIR}\") 15 | 16 | ############################################# 17 | # Build libPSI # 18 | ############################################# 19 | 20 | add_subdirectory(libPSI) 21 | add_subdirectory(libPSI_Tests) 22 | add_subdirectory(frontend) 23 | 24 | 25 | 26 | # setup the install 27 | include(cmake/install.cmake) -------------------------------------------------------------------------------- /CMakePresets.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 2, 3 | "configurePresets": [ 4 | { 5 | "name": "linux", 6 | "displayName": "Linux", 7 | "description": "Target the Windows Subsystem for Linux (WSL) or a remote Linux system.", 8 | "generator": "Ninja", 9 | "binaryDir": "${sourceDir}/out/build/${presetName}", 10 | "cacheVariables": { 11 | "CMAKE_BUILD_TYPE": "Release", 12 | "ENABLE_ALL_PSI": true, 13 | "FETCH_AUTO": true, 14 | "CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}" 15 | }, 16 | "vendor": { 17 | "microsoft.com/VisualStudioSettings/CMake/1.0": { "hostOS": [ "Linux" ] }, 18 | "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": { "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}" } 19 | } 20 | }, 21 | { 22 | "name": "x64-Debug", 23 | "displayName": "Windows x64 Debug", 24 | "description": "Target Windows with the Visual Studio development environment.", 25 | "generator": "Ninja", 26 | "binaryDir": "${sourceDir}/out/build/${presetName}", 27 | "architecture": { 28 | "value": "x64", 29 | "strategy": "external" 30 | }, 31 | "cacheVariables": { 32 | "CMAKE_BUILD_TYPE": "Debug", 33 | "FETCH_AUTO": true, 34 | "ENABLE_ALL_PSI": true, 35 | "CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}" 36 | }, 37 | "vendor": { "microsoft.com/VisualStudioSettings/CMake/1.0": { "hostOS": [ "Windows" ] } } 38 | }, 39 | { 40 | "name": "x64-Release", 41 | "displayName": "Windows x64 Release", 42 | "description": "Target Windows with the Visual Studio development environment.", 43 | "generator": "Ninja", 44 | "binaryDir": "${sourceDir}/out/build/${presetName}", 45 | "architecture": { 46 | "value": "x64", 47 | "strategy": "external" 48 | }, 49 | "cacheVariables": { 50 | "CMAKE_BUILD_TYPE": "RelWithDebInfo", 51 | "FETCH_AUTO": true, 52 | "ENABLE_ALL_PSI": true, 53 | "CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}" 54 | }, 55 | "vendor": { "microsoft.com/VisualStudioSettings/CMake/1.0": { "hostOS": [ "Windows" ] } } 56 | }, 57 | { 58 | "name": "osx", 59 | "displayName": "Custom configure preset", 60 | "description": "Sets Ninja generator, build and install directory", 61 | "generator": "Unix Makefiles", 62 | "binaryDir": "${sourceDir}/out/build/${presetName}", 63 | "cacheVariables": { 64 | "FETCH_AUTO": true, 65 | "ENABLE_ALL_PSI": true, 66 | "CMAKE_BUILD_TYPE": "Release", 67 | "CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}" 68 | }, 69 | "vendor": { "microsoft.com/VisualStudioSettings/CMake/1.0": { "hostOS": [ "macOS" ] } } 70 | } 71 | ] 72 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Dual-licensed under Unlicense or MIT. 2 | 3 | 4 | ----------------------- Unlicense --------------------------- 5 | 6 | This is free and unencumbered software released into the public domain. 7 | 8 | Anyone is free to copy, modify, publish, use, compile, sell, or 9 | distribute this software, either in source code form or as a compiled 10 | binary, for any purpose, commercial or non-commercial, and by any 11 | means. 12 | 13 | In jurisdictions that recognize copyright laws, the author or authors 14 | of this software dedicate any and all copyright interest in the 15 | software to the public domain. We make this dedication for the benefit 16 | of the public at large and to the detriment of our heirs and 17 | successors. We intend this dedication to be an overt act of 18 | relinquishment in perpetuity of all present and future rights to this 19 | software under copyright law. 20 | 21 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 24 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 25 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 26 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 | OTHER DEALINGS IN THE SOFTWARE. 28 | 29 | For more information, please refer to 30 | 31 | 32 | ----------------------- MIT --------------------------- 33 | Copyright 2021 Peter Rindal 34 | 35 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 36 | software and associated documentation files (the "Software"), to deal in the Software 37 | without restriction, including without limitation the rights to use, copy, modify, 38 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 39 | permit persons to whom the Software is furnished to do so, subject to the following 40 | conditions: 41 | 42 | The above copyright notice and this permission notice shall be included in all copies 43 | or substantial portions of the Software. 44 | 45 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 46 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 47 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 48 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 49 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 50 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 51 | 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # libPSI 2 | A repository for private set intersection. Most protocols were written just for benchmarking them while (RR17,KKRT,Mea86=ECDH) can be run from the command line and take a file as input. Run the program for details. 3 | 4 | ## VolePSI 5 | Also consider using https://github.com/Visa-Research/volepsi/ for improved performance. 6 | 7 | ## Introduction 8 | Protocols: 9 | 10 | * Malicious Secure [RR17](https://eprint.iacr.org/2017/769) based on simple hashing and OTs (fastest) 11 | * Malicious Secure [RR16](https://eprint.iacr.org/2016/746) based on Bloom filters and OTs 12 | * Malicious Secure [DKT10](https://eprint.iacr.org/2010/469) based on public key crypto (ECC) 13 | * Semi-Honest Secure [KKRT16](https://eprint.iacr.org/2016/799) based on cuckoo hashing and OTs (fastest) 14 | * Semi-Honest Secure [Mea86](http://ieeexplore.ieee.org/document/6234849/) base on public key crypto (ECC) 15 | * Semi-Honest Secure [DRRT18](https://eprint.iacr.org/2018/579.pdf) based on cuckoo hashing, PIR and OTs (fastest unbalanced) 16 | 17 | ## Install 18 | 19 | Our library is cross platform and has been tested on both Windows and Linux. The library should work on Mac but it has not been tested. There are several library dependencies including [libOTe](https://github.com/osu-crypto/libOte). 20 | 21 | 22 | ``` 23 | git clone https://github.com/osu-crypto/libPSI.git 24 | cd libPSI 25 | python build.py 26 | ``` 27 | 28 | Unit tests can be run by executing the program. 29 | 30 | ``` 31 | /out/build//frontend/frontend.exe -u 32 | ``` 33 | Other options can be seen by executing with no arguments. 34 | ## Help 35 | 36 | Contact Peter Rindal `peterrindal@gmail.com` for any assistance on building or running the library. 37 | -------------------------------------------------------------------------------- /build.py: -------------------------------------------------------------------------------- 1 | import os 2 | import platform 3 | import sys 4 | import multiprocessing 5 | import subprocess 6 | import glob 7 | 8 | # find the ninja generator on windows. 9 | def getGenerator(args): 10 | #osStr = (platform.system()) 11 | # 12 | #if osStr == "Windows": 13 | # 14 | # for x in args: 15 | # if x.startswith("-G"): 16 | # break 17 | # 18 | # vswhereArgs = ['C:/Program Files (x86)/Microsoft Visual Studio/Installer/vswhere.exe', "-prerelease", "-latest", "-property", "installationPath"] 19 | # rootpath = subprocess.check_output(vswhereArgs).decode("utf-8").strip() 20 | # 21 | # ninja = rootpath + "/COMMON7/IDE/COMMONEXTENSIONS/MICROSOFT/CMAKE/Ninja/ninja.exe" 22 | # cl = rootpath + "/VC/Tools/MSVC/*/bin/Hostx64/x64/cl.exe" 23 | # cls = glob.glob(cl) 24 | # if len(cls) > 0: 25 | # cl = cls[-1]; 26 | # 27 | # # use ninja 28 | # if os.path.exists(ninja) and os.path.exists(cl): 29 | # return "-G \"Ninja\" -DCMAKE_MAKE_PROGRAM=\"{0}\" -DCMAKE_C_COMPILER:FILEPATH=\"{1}\" -DCMAKE_CXX_COMPILER:FILEPATH=\"{1}\" ".format(ninja, cl) 30 | # else: 31 | # print("failed to find ninja at: {0}\n or cl".format(ninja)) 32 | # 33 | # use the default 34 | return "" 35 | 36 | 37 | def parseInstallArgs(args): 38 | prefix = "" 39 | doInstall = False 40 | for x in args: 41 | if x.startswith("--install="): 42 | prefix = x.split("=",1)[1] 43 | prefix = os.path.abspath(os.path.expanduser(prefix)) 44 | idx = args.index(x) 45 | args[idx] = "-DCMAKE_INSTALL_PREFIX=" + prefix 46 | doInstall = True 47 | if x == "--install": 48 | idx = args.index(x) 49 | osStr = (platform.system()) 50 | if osStr == "Windows": 51 | args[idx] = "-DCMAKE_INSTALL_PREFIX=c:/lib" 52 | else: 53 | args[idx] = "-DCMAKE_INSTALL_PREFIX=/usr/local" 54 | doInstall = True 55 | 56 | return (args, doInstall) 57 | 58 | def getParallel(args): 59 | par = multiprocessing.cpu_count() 60 | for x in args: 61 | if x.startswith("--par="): 62 | val = x.split("=",1)[1] 63 | par = int(val) 64 | if par < 1: 65 | par = 1 66 | idx = args.index(x) 67 | args[idx] = "" 68 | return (args,par) 69 | 70 | 71 | def replace(list, find, replace): 72 | if find in list: 73 | idx = list.index(find) 74 | list[idx] = replace; 75 | return list 76 | 77 | def Build(projectName, argv): 78 | 79 | osStr = (platform.system()) 80 | buildDir = "" 81 | config = "" 82 | buildType = "" 83 | 84 | # use sudo when installing? 85 | sudo = "--sudo" in argv; 86 | argv = replace(argv, "--sudo", "-DSUDO_FETCH=ON") 87 | if not sudo: 88 | argv.append("-DSUDO_FETCH=OFF") 89 | 90 | argv.append("-DENABLE_ALL_PSI=ON") 91 | generator = getGenerator(argv) 92 | 93 | # do not automaticly download dependancies 94 | if "--noauto" in argv: 95 | argv = replace(argv, "--noauto", "") 96 | argv.append("-DFETCH_AUTO=OFF") 97 | else: 98 | argv.append("-DFETCH_AUTO=ON") 99 | 100 | # get install options 101 | argv, install = parseInstallArgs(argv) 102 | 103 | # get parallel build options 104 | argv, par = getParallel(argv) 105 | argv.append("-DPARALLEL_FETCH="+str(par)) 106 | 107 | # do not run cmake config 108 | noConfig = "--nc" in argv 109 | argv = replace(argv, "--nc", "") 110 | 111 | # only run cmake config. 112 | setup = "--setup" in argv; 113 | argv = replace(argv, "--setup", "") 114 | 115 | # build type. 116 | if "--debug" in argv: 117 | buildType = "Debug" 118 | else: 119 | buildType = "Release" 120 | argv.append("-DCMAKE_BUILD_TYPE={0}".format(buildType)) 121 | argv = replace(argv, "--debug", "") 122 | 123 | # build dir 124 | if osStr == "Windows": 125 | buildDir = "out/build/x64-{0}".format(buildType) 126 | config = "--config {0}".format(buildType) 127 | elif osStr == "Darwin": 128 | buildDir = "out/build/osx" 129 | else: 130 | buildDir = "out/build/linux" 131 | 132 | # convert args to a string. 133 | argStr = "" 134 | for a in argv: 135 | argStr = argStr + " " + a 136 | 137 | # parallel build 138 | parallel = "" 139 | if par != 1: 140 | parallel = " --parallel " + str(par) 141 | 142 | 143 | # build commands 144 | mkDirCmd = "mkdir -p {0}".format(buildDir); 145 | CMakeCmd = "cmake {0} -S . -B {1} {2} ".format(generator, buildDir, argStr) 146 | BuildCmd = "cmake --build {0} {1} {2} ".format(buildDir, config, parallel) 147 | InstallCmd = "" 148 | if sudo: 149 | sudo = "sudo " 150 | else: 151 | sudo = "" 152 | if install: 153 | InstallCmd = sudo 154 | InstallCmd += "cmake --install {0} {1} ".format(buildDir, config) 155 | 156 | # print and execute commands. 157 | print("\n\n====== build.py ("+projectName+") ========") 158 | if not noConfig: 159 | print(mkDirCmd) 160 | print(CMakeCmd) 161 | 162 | if not setup: 163 | print(BuildCmd) 164 | if len(InstallCmd): 165 | print(InstallCmd) 166 | print("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv\n\n") 167 | 168 | if not noConfig: 169 | os.system(mkDirCmd) 170 | os.system(CMakeCmd) 171 | 172 | if not setup: 173 | os.system(BuildCmd) 174 | 175 | if len(sudo) > 0: 176 | print("installing "+projectName+": {0}\n".format(InstallCmd)) 177 | 178 | os.system(InstallCmd) 179 | 180 | 181 | 182 | def help(): 183 | 184 | print(" --install \n\tInstructs the script to install whatever is currently being built to the default location.") 185 | print(" --install=prefix \n\tinstall to the provided predix.") 186 | print(" --sudo \n\twhen installing, use sudo. May require password.") 187 | print(" --par=n \n\twhen building do use parallel builds with n threads. default = num cores.") 188 | print(" --noauto \n\twhen building do not automaticly fetch dependancies.") 189 | print(" --par=n \n\twhen building do use parallel builds with n threads. default = num cores.") 190 | print(" --debug \n\tdebug build.") 191 | print("any additioanl arguments are forwared to cmake.\n") 192 | 193 | print("-build the library") 194 | print(" python build.py") 195 | print("-build the library with cmake configurations") 196 | print(" python build.py --debug -DLIBPSI_ENABLE_X=ON") 197 | print("-build the library and install with sudo") 198 | print(" python build.py --install --sudo") 199 | print("-build the library and install to prefix") 200 | print(" python build.py --install=~/my/install/dir ") 201 | 202 | 203 | 204 | def main(projectName, argv): 205 | 206 | if "--help" in argv: 207 | help() 208 | return 209 | 210 | # build the project. 211 | Build(projectName, argv) 212 | 213 | if __name__ == "__main__": 214 | 215 | main("LIBPSI", sys.argv[1:]) 216 | -------------------------------------------------------------------------------- /cmake/Config.cmake.in: -------------------------------------------------------------------------------- 1 | @PACKAGE_INIT@ 2 | 3 | include("${CMAKE_CURRENT_LIST_DIR}/libPSITargets.cmake") 4 | 5 | # when someone finds our project, we must then find our depandancies. 6 | include("${CMAKE_CURRENT_LIST_DIR}/findDependancies.cmake") 7 | 8 | # define any variables you want consumers of your library ro be able to see. 9 | set(LIBPSI_ENABLE_X @LIBPSI_ENABLE_X@) 10 | 11 | # set the library location and header location info from the target. 12 | get_target_property(libPSI_INCLUDE_DIRS oc::libPSI INTERFACE_INCLUDE_DIRECTORIES) 13 | get_target_property(libPSI_LIBRARIES oc::libPSI LOCATION) 14 | 15 | # print helper info on where the project was found. 16 | message("libPSI_INCLUDE_DIRS=${libPSI_INCLUDE_DIRS}") 17 | message("libPSI_LIBRARIES=${libPSI_LIBRARIES}") 18 | -------------------------------------------------------------------------------- /cmake/buildOptions.cmake: -------------------------------------------------------------------------------- 1 | #helper macro to assign a boolean variable 2 | macro(SET_BOOL var) 3 | if(${ARGN}) 4 | set(${var} ON) 5 | else() 6 | set(${var} OFF) 7 | endif() 8 | endmacro() 9 | 10 | option(FETCH_AUTO "automaticly download and build dependancies" OFF) 11 | 12 | # here we have to do some special logic to determine if we should 13 | # automaticly download sparsehash. This is done if we used 14 | # 15 | # does not define FETCH_SPARSEHASH and define FETCH_AUTO 16 | # or 17 | # define FETCH_SPARSEHASH as True/ON 18 | SET_BOOL(FETCH_SPARSEHASH_AUTO 19 | (DEFINED FETCH_SPARSEHASH AND FETCH_SPARSEHASH) OR 20 | ((NOT DEFINED FETCH_SPARSEHASH) AND (FETCH_AUTO))) 21 | 22 | # here we have to do some special logic to determine if we should 23 | # automaticly download sparsehash. This is done if we used 24 | # 25 | # does not define FETCH_LIBOTE and define FETCH_AUTO 26 | # or 27 | # define FETCH_LIBOTE as True/ON 28 | SET_BOOL(FETCH_LIBOTE_AUTO 29 | (DEFINED FETCH_LIBOTE AND FETCH_LIBOTE) OR 30 | ((NOT DEFINED FETCH_LIBOTE) AND (FETCH_AUTO))) 31 | 32 | 33 | message(STATUS "fetch options\n=======================================================") 34 | 35 | message(STATUS "Option: FETCH_AUTO = ${FETCH_AUTO}") 36 | message(STATUS "Option: FETCH_SPARSEHASH = ${FETCH_SPARSEHASH}") 37 | message(STATUS "Option: FETCH_LIBOTE = ${FETCH_LIBOTE}\n") 38 | 39 | 40 | ############################################# 41 | # CONFIGURE # 42 | ############################################# 43 | 44 | if(DEFINED ENABLE_ALL_PSI) 45 | set(ENABLE_DCW_PSI ${ENABLE_ALL_PSI} CACHE BOOL "" FORCE) 46 | set(ENABLE_DKT_PSI ${ENABLE_ALL_PSI} CACHE BOOL "" FORCE) 47 | set(ENABLE_GRR_PSI ${ENABLE_ALL_PSI} CACHE BOOL "" FORCE) 48 | set(ENABLE_RR16_PSI ${ENABLE_ALL_PSI} CACHE BOOL "" FORCE) 49 | set(ENABLE_RR17_PSI ${ENABLE_ALL_PSI} CACHE BOOL "" FORCE) 50 | set(ENABLE_RR17B_PSI ${ENABLE_ALL_PSI} CACHE BOOL "" FORCE) 51 | set(ENABLE_KKRT_PSI ${ENABLE_ALL_PSI} CACHE BOOL "" FORCE) 52 | set(ENABLE_ECDH_PSI ${ENABLE_ALL_PSI} CACHE BOOL "" FORCE) 53 | set(ENABLE_DRRN_PSI ${ENABLE_ALL_PSI} CACHE BOOL "" FORCE) 54 | unset(ENABLE_ALL_PSI CACHE) 55 | endif() 56 | 57 | 58 | option(ENABLE_DCW_PSI "Build the DCW PSI protocol" OFF) 59 | option(ENABLE_DKT_PSI "Build the DKT PSI protocol" OFF) 60 | option(ENABLE_GRR_PSI "Build the GRR PSI protocol" OFF) 61 | option(ENABLE_RR16_PSI "Build the RR16 PSI protocol" OFF) 62 | option(ENABLE_RR17_PSI "Build the RR17 PSI protocol" OFF) 63 | option(ENABLE_RR17B_PSI "Build the RR17B PSI protocol" OFF) 64 | option(ENABLE_KKRT_PSI "Build the KKRT PSI protocol" OFF) 65 | option(ENABLE_ECDH_PSI "Build the EC DH PSI protocol" OFF) 66 | option(ENABLE_DRRN_PSI "Build the DRRN PSI protocol" OFF) 67 | option(ENABLE_PRTY_PSI "Build the PRTY PSI protocol" OFF) 68 | option(ENABLE_RELIC "Build with relic" ON) 69 | option(ENABLE_SODIUM "Build with sodium" OFF) 70 | 71 | if(NOT DEFINED LIBPSI_STD_VER) 72 | set(LIBPSI_STD_VER 20) 73 | endif() 74 | 75 | message(STATUS "General Options\n=======================================================") 76 | message(STATUS "Option: CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}\n\tRelease\n\tDebug\n\tRELWITHDEBINFO") 77 | message(STATUS "Option: ENABLE_ALL_PSI = ON/OFF\n\n") 78 | 79 | message(STATUS "PSI protocols\n=======================================================") 80 | message(STATUS "Option: ENABLE_DCW_PSI = ${ENABLE_DCW_PSI}") 81 | message(STATUS "Option: ENABLE_DKT_PSI = ${ENABLE_DKT_PSI}") 82 | message(STATUS "Option: ENABLE_GRR_PSI = ${ENABLE_GRR_PSI}") 83 | message(STATUS "Option: ENABLE_RR16_PSI = ${ENABLE_RR16_PSI}") 84 | message(STATUS "Option: ENABLE_RR17_PSI = ${ENABLE_RR17_PSI}") 85 | message(STATUS "Option: ENABLE_RR17B_PSI = ${ENABLE_RR17B_PSI}") 86 | message(STATUS "Option: ENABLE_KKRT_PSI = ${ENABLE_KKRT_PSI}") 87 | message(STATUS "Option: ENABLE_ECDH_PSI = ${ENABLE_ECDH_PSI}") 88 | message(STATUS "Option: ENABLE_DRRN_PSI = ${ENABLE_DRRN_PSI}\n") 89 | 90 | message(STATUS "Option: ENABLE_RELIC = ${ENABLE_RELIC}") 91 | message(STATUS "Option: ENABLE_SODIUM = ${ENABLE_SODIUM}\n") 92 | 93 | 94 | configure_file(libPSI/config.h.in libPSI/config.h) 95 | 96 | 97 | -------------------------------------------------------------------------------- /cmake/findDependancies.cmake: -------------------------------------------------------------------------------- 1 | include(${CMAKE_CURRENT_LIST_DIR}/preamble.cmake) 2 | 3 | 4 | # here we find out depandancies. This happens when we build our project and 5 | # when someone includes our project via find_project(LIBPSI). As such, 6 | # we also have to make sure this also works when we are installed. 7 | 8 | 9 | message(STATUS "LIBPSI_THIRDPARTY_DIR=${LIBPSI_THIRDPARTY_DIR}") 10 | 11 | # cmake will look for out depandancies at the paths in CMAKE_PREFIX_PATH 12 | # if LIBPSI_THIRDPARTY_DIR is defined, we want this to be the first place 13 | # that is looked at. To make sure only our libraries are looked for here, 14 | # we will resort CMAKE_PREFIX_PATH to its old value at the end if the file. 15 | set(PUSHED_CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH}) 16 | set(CMAKE_PREFIX_PATH "${LIBPSI_THIRDPARTY_DIR};${CMAKE_PREFIX_PATH}") 17 | 18 | 19 | ####################################### 20 | # sparsehash 21 | 22 | 23 | # here we will look for sparsehash and download it if requested. 24 | # sparsehash doesnt supports find_package so we look for it manually. 25 | 26 | # first we will define a macro because we might look for it more than once. 27 | macro(FIND_SPARSEHASH) 28 | # assign any macro arguemnts to the ARGS variable. 29 | set(ARGS ${ARGN}) 30 | 31 | # If the user explicitly asked to fetch libOTe, then we dont want to 32 | # look for libOTe at any location other than LIBPSI_THIRDPARTY_DIR. 33 | # this is done with including NO_DEFAULT_PATH as an argument and 34 | # specifying where we want to look using PATHS 35 | if(FETCH_SPARSEHASH) 36 | list(APPEND ARGS NO_DEFAULT_PATH PATHS ${LIBPSI_THIRDPARTY_DIR}) 37 | endif() 38 | 39 | # next we need to look for the sparsehash headers. One such header is dense_hash_map. 40 | # we expect this header to be at /include/sparsehash/dense_hash_map. We 41 | # will have cmake look for this file. cmake will look at system locations and paths 42 | # specified in the CMAKE_PREFIX_PATH variable. 43 | find_path(SPARSEHASH_INCLUDE_DIRS "sparsehash/dense_hash_map" PATH_SUFFIXES "include" ${ARGS} 44 | DOC "Use -DFETCH_AUTO=ON to automaticly download dependancies") 45 | 46 | # if you are linking a library you will also need to find that via find_library(...) 47 | 48 | # check if we found sparse hash. 49 | if(EXISTS ${SPARSEHASH_INCLUDE_DIRS}) 50 | set(SPARSEHASH_FOUND ON) 51 | else() 52 | set(SPARSEHASH_FOUND OFF) 53 | endif() 54 | endmacro() 55 | 56 | 57 | # FETCH_SPARSEHASH_AUTO is set if we should download sparsehash. If we should, 58 | # then we first check if we already have it via the FIND_SPARSEHASH(QUIET) 59 | # call. Then we call the getSparsehash.cmake script to download sparsehash if 60 | # we dont already have it. 61 | if(FETCH_SPARSEHASH_AUTO) 62 | FIND_SPARSEHASH(QUIET) 63 | include(${CMAKE_CURRENT_LIST_DIR}/../thirdparty/getSparsehash.cmake) 64 | endif() 65 | 66 | FIND_SPARSEHASH(REQUIRED) 67 | message("SPARSEHASH_INCLUDE_DIRS=${SPARSEHASH_INCLUDE_DIRS}") 68 | 69 | # If the sparse ahsh target has not been previously defined, lets define it. 70 | if(NOT TARGET sparsehash) 71 | 72 | # since we didnt build sparse, we declare it as an IMPORTED target. 73 | # moreover, sparsehash is header only so we declare it as INTERFACE. 74 | add_library(sparsehash INTERFACE IMPORTED) 75 | 76 | #if sparsehash had an associated static library which we previously found, then 77 | # we could declare it as: 78 | # 79 | # add_library(sparsehash STATIC IMPORTED) 80 | # set_property(TARGET sparsehash PROPERTY IMPORTED_LOCATION ${SPARSEHASH_LIB}) 81 | 82 | # in either case, we set the header directory as 83 | target_include_directories(sparsehash INTERFACE 84 | $ 85 | $) 86 | endif() 87 | 88 | 89 | 90 | ####################################### 91 | # libOTe 92 | 93 | 94 | # here we will look for libOTe and download it if requested. 95 | # libOTe supports find_package so its pretty easy. 96 | 97 | # first we will define a macro because we might look for it more than once. 98 | macro(FIND_LIBOTE) 99 | 100 | # assign any macro arguemnts to the ARGS variable. 101 | set(ARGS ${ARGN}) 102 | 103 | # If the user explicitly asked to fetch libOTe, then we dont want to 104 | # look for libOTe at any location other than LIBPSI_THIRDPARTY_DIR. 105 | # this is done with including NO_DEFAULT_PATH as an argument and 106 | # specifying where we want to look using PATHS 107 | if(FETCH_LIBOTE) 108 | list(APPEND ARGS NO_DEFAULT_PATH PATHS ${LIBPSI_THIRDPARTY_DIR}) 109 | endif() 110 | 111 | # look for libOTe. cmake will look at system locations and paths 112 | # specified in the CMAKE_PREFIX_PATH variable. 113 | # 114 | # libOTeConfig.cmake 115 | # 116 | # CMAKE_PREFIX_PATH/lib/cmake/libOTe/libOTeConfig.cmake 117 | # CMAKE_PREFIX_PATH/libOTe/cmake/libOTeConfig.cmake 118 | find_package(libOTe ${ARGS}) 119 | 120 | # check if we found it. 121 | if(TARGET oc::libOTe) 122 | set(libOTe_FOUND ON) 123 | else() 124 | set(libOTe_FOUND OFF) 125 | endif() 126 | endmacro() 127 | 128 | # FETCH_LIBOTE_AUTO is set if we should download libOTe. If we should, 129 | # then we first check if we already have it via the FIND_LIBOTE(QUIET) 130 | # call. Then we call the getLibOTe.cmake script to download libOTe if 131 | # we dont already have it. 132 | if(FETCH_LIBOTE_AUTO) 133 | FIND_LIBOTE(QUIET) 134 | include(${CMAKE_CURRENT_LIST_DIR}/../thirdparty/getLibOTe.cmake) 135 | endif() 136 | 137 | # finally, we make sure we have found libOTe. 138 | FIND_LIBOTE(REQUIRED) 139 | 140 | 141 | # resort the previous prefix path 142 | set(CMAKE_PREFIX_PATH ${PUSHED_CMAKE_PREFIX_PATH}) 143 | -------------------------------------------------------------------------------- /cmake/install.cmake: -------------------------------------------------------------------------------- 1 | 2 | 3 | ############################################# 4 | # Install # 5 | ############################################# 6 | 7 | # we have to install these scripts since the we required 8 | # find our depandancies wheneven someone want to include 9 | # this project. 10 | configure_file("${CMAKE_CURRENT_LIST_DIR}/findDependancies.cmake" "findDependancies.cmake" COPYONLY) 11 | configure_file("${CMAKE_CURRENT_LIST_DIR}/preamble.cmake" "preamble.cmake" COPYONLY) 12 | 13 | # make cache variables for install destinations 14 | include(GNUInstallDirs) 15 | include(CMakePackageConfigHelpers) 16 | 17 | # generate the config file that is includes the exports 18 | configure_package_config_file( 19 | "${CMAKE_CURRENT_LIST_DIR}/Config.cmake.in" 20 | "${CMAKE_CURRENT_BINARY_DIR}/libPSIConfig.cmake" 21 | INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libPSI 22 | NO_SET_AND_CHECK_MACRO 23 | NO_CHECK_REQUIRED_COMPONENTS_MACRO 24 | ) 25 | 26 | if(NOT DEFINED libPSI_VERSION_MAJOR) 27 | message("\n\n\n\n warning, libPSI_VERSION_MAJOR not defined ${libPSI_VERSION_MAJOR}") 28 | endif() 29 | 30 | set_property(TARGET libPSI PROPERTY VERSION ${libPSI_VERSION}) 31 | 32 | # generate the version file for the config file 33 | write_basic_package_version_file( 34 | "${CMAKE_CURRENT_BINARY_DIR}/libPSIConfigVersion.cmake" 35 | VERSION "${libPSI_VERSION_MAJOR}.${libPSI_VERSION_MINOR}.${libPSI_VERSION_PATCH}" 36 | COMPATIBILITY AnyNewerVersion 37 | ) 38 | 39 | # install the configuration file 40 | install(FILES 41 | "${CMAKE_CURRENT_BINARY_DIR}/libPSIConfig.cmake" 42 | "${CMAKE_CURRENT_BINARY_DIR}/libPSIConfigVersion.cmake" 43 | "${CMAKE_CURRENT_BINARY_DIR}/findDependancies.cmake" 44 | "${CMAKE_CURRENT_BINARY_DIR}/preamble.cmake" 45 | DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libPSI 46 | ) 47 | 48 | # install library 49 | install( 50 | TARGETS libPSI 51 | DESTINATION ${CMAKE_INSTALL_LIBDIR} 52 | EXPORT libPSITargets) 53 | 54 | # install headers 55 | install( 56 | DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/../libPSI" 57 | DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/" 58 | FILES_MATCHING PATTERN "*.h") 59 | 60 | # install config and use the "namespace" of oc:: 61 | install(EXPORT libPSITargets 62 | FILE libPSITargets.cmake 63 | DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libPSI 64 | NAMESPACE oc:: 65 | ) 66 | export(EXPORT libPSITargets 67 | FILE "${CMAKE_CURRENT_BINARY_DIR}/libPSITargets.cmake" 68 | NAMESPACE oc:: 69 | ) -------------------------------------------------------------------------------- /cmake/libPSIConfig.cmake: -------------------------------------------------------------------------------- 1 | # these are just pass through config file for the ones that are placed in the build directory. 2 | 3 | 4 | include("${CMAKE_CURRENT_LIST_DIR}/preamble.cmake") 5 | 6 | if(NOT EXISTS "${VOLEPSI_BUILD_DIR}") 7 | message(FATAL_ERROR "failed to find the volePSI build directory. Looked at VOLEPSI_BUILD_DIR: ${VOLEPSI_BUILD_DIR}\n Please set it manually.") 8 | endif() 9 | 10 | include("${VOLEPSI_BUILD_DIR}/volePSIConfig.cmake") -------------------------------------------------------------------------------- /cmake/preamble.cmake: -------------------------------------------------------------------------------- 1 | 2 | 3 | if("${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}") 4 | 5 | ############################################ 6 | # If top level cmake # 7 | ############################################ 8 | if(MSVC) 9 | else() 10 | set(COMMON_FLAGS "-Wall -Wfatal-errors") 11 | 12 | if(NOT DEFINED NO_ARCH_NATIVE) 13 | set(COMMON_FLAGS "${COMMON_FLAGS} -march=native") 14 | endif() 15 | SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") 16 | SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO " -O2 -g -ggdb") 17 | SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -ggdb") 18 | endif() 19 | 20 | 21 | 22 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMMON_FLAGS}") 23 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMMON_FLAGS}") 24 | 25 | 26 | ############################################ 27 | # Build mode checks # 28 | ############################################ 29 | 30 | # Set a default build type for single-configuration 31 | # CMake generators if no build type is set. 32 | if(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE) 33 | SET(CMAKE_BUILD_TYPE Release) 34 | endif() 35 | 36 | if( NOT "${CMAKE_BUILD_TYPE}" STREQUAL "Release" 37 | AND NOT "${CMAKE_BUILD_TYPE}" STREQUAL "Debug" 38 | AND NOT "${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo" ) 39 | 40 | message(WARNING ": Unknown build type - \${CMAKE_BUILD_TYPE}=${CMAKE_BUILD_TYPE}. Please use one of Debug, Release, or RelWithDebInfo. e.g. call\n\tcmake . -DCMAKE_BUILD_TYPE=Release\n" ) 41 | endif() 42 | endif() 43 | 44 | if(MSVC) 45 | set(LIBPSI_CONFIG_NAME "${CMAKE_BUILD_TYPE}") 46 | if("${LIBPSI_CONFIG_NAME}" STREQUAL "RelWithDebInfo" OR "${LIBPSI_CONFIG_NAME}" STREQUAL "") 47 | set(LIBPSI_CONFIG_NAME "Release") 48 | endif() 49 | set(LIBPSI_CONFIG "x64-${LIBPSI_CONFIG_NAME}") 50 | elseif(APPLE) 51 | set(LIBPSI_CONFIG "osx") 52 | else() 53 | set(LIBPSI_CONFIG "linux") 54 | endif() 55 | 56 | if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/install.cmake) 57 | set(LIBPSI_IN_BUILD_TREE ON) 58 | else() 59 | set(LIBPSI_IN_BUILD_TREE OFF) 60 | endif() 61 | 62 | if(LIBPSI_IN_BUILD_TREE) 63 | 64 | # we currenty are in the vole psi source tree, vole-psi/cmake 65 | if(NOT DEFINED LIBPSI_BUILD_DIR) 66 | set(LIBPSI_BUILD_DIR "${CMAKE_CURRENT_LIST_DIR}/../out/build/${LIBPSI_CONFIG}") 67 | get_filename_component(LIBPSI_BUILD_DIR ${LIBPSI_BUILD_DIR} ABSOLUTE) 68 | endif() 69 | 70 | if(NOT (${CMAKE_BINARY_DIR} STREQUAL ${LIBPSI_BUILD_DIR})) 71 | message(WARNING "incorrect build directory. \n\tCMAKE_BINARY_DIR=${CMAKE_BINARY_DIR}\nbut expect\n\tLIBPSI_BUILD_DIR=${LIBPSI_BUILD_DIR}") 72 | endif() 73 | 74 | if(NOT DEFINED LIBPSI_THIRDPARTY_DIR) 75 | set(LIBPSI_THIRDPARTY_DIR "${CMAKE_CURRENT_LIST_DIR}/../out/install/${LIBPSI_CONFIG}") 76 | get_filename_component(LIBPSI_THIRDPARTY_DIR ${LIBPSI_THIRDPARTY_DIR} ABSOLUTE) 77 | endif() 78 | else() 79 | # we currenty are in install tree, /lib/cmake/vole-psi 80 | if(NOT DEFINED LIBPSI_THIRDPARTY_DIR) 81 | set(LIBPSI_THIRDPARTY_DIR "${CMAKE_CURRENT_LIST_DIR}/../../..") 82 | get_filename_component(LIBPSI_THIRDPARTY_DIR ${LIBPSI_THIRDPARTY_DIR} ABSOLUTE) 83 | endif() 84 | endif() 85 | 86 | -------------------------------------------------------------------------------- /frontend/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | #project(fronend) 3 | 4 | 5 | 6 | 7 | ############################################# 8 | # Build fronend.exe # 9 | ############################################# 10 | 11 | file(GLOB_RECURSE SRC_FRONTEND ${CMAKE_SOURCE_DIR}/frontend/*.cpp) 12 | include_directories(${CMAKE_SOURCE_DIR}/frontend/) 13 | 14 | add_executable(frontend.exe ${SRC_FRONTEND}) 15 | 16 | 17 | ############################################# 18 | # Link our libraries # 19 | ############################################# 20 | 21 | 22 | 23 | ############################################# 24 | # Link libOTe # 25 | ############################################# 26 | 27 | target_link_libraries(frontend.exe PUBLIC libPSI libPSI_Tests oc::libOTe_Tests oc::tests_cryptoTools) 28 | 29 | 30 | if(MSVC) 31 | target_compile_options(frontend.exe PRIVATE $<$:/std:c++${LIBPSI_STD_VER}>) 32 | #target_compile_options(libOTe PRIVATE -openmp:experimental) 33 | else() 34 | target_compile_options(frontend.exe PRIVATE $<$:-std=c++${LIBPSI_STD_VER}>) 35 | 36 | endif() -------------------------------------------------------------------------------- /frontend/DrrnPSIMain.cpp: -------------------------------------------------------------------------------- 1 | #include "bloomFilterMain.h" 2 | #include "cryptoTools/Network/Endpoint.h" 3 | 4 | #include "libPSI/PSI/Drrn/DrrnPsiClient.h" 5 | #include "libPSI/PSI/Drrn/DrrnPsiServer.h" 6 | 7 | #include 8 | using namespace osuCrypto; 9 | #include "util.h" 10 | 11 | #include "cryptoTools/Common/Defines.h" 12 | 13 | #include "cryptoTools/Common/Log.h" 14 | #include "cryptoTools/Common/Timer.h" 15 | #include "cryptoTools/Crypto/PRNG.h" 16 | #include 17 | #include "cuckoo/SimpleCuckoo.h" 18 | 19 | void Drrn17Send( 20 | LaunchParams& params) 21 | { 22 | #ifdef ENABLE_DRRN_PSI 23 | setThreadName("CP_Test_Thread"); 24 | 25 | if (params.mIdx < 1 || params.mIdx > 2) throw std::runtime_error("server index must be 1 or 2"); 26 | 27 | u64 me = params.mIdx - 1 + 1010; 28 | u64 them; 29 | 30 | PRNG prng(_mm_set_epi32(4253465, 434565, 234435, 23987045)); 31 | 32 | 33 | 34 | for (auto serverSetSize : params.mNumItems2) 35 | { 36 | std::unique_ptr setPtr(new block[serverSetSize]); 37 | span set(setPtr.get(), serverSetSize); 38 | prng.get(set.data(), set.size()); 39 | 40 | 41 | for (auto clientSetSize : params.mNumItems) 42 | { 43 | for (auto numThreads : params.mNumThreads) 44 | { 45 | std::vector clientChls = params.getChannels(1); 46 | std::vector serverChls = params.getChannels2(1); 47 | 48 | for (auto ss : params.mBinScaler) 49 | { 50 | for (u64 jj = 0; jj < params.mTrials; jj++) 51 | { 52 | try { 53 | DrrnPsiServer srv; 54 | srv.mUseSingleDataPass = params.mCmd->isSet("multiDP") == false; 55 | srv.mNiave = params.mCmd->isSet("niave"); 56 | //{ 57 | // auto param = CuckooIndex<>::selectParams(set.size(), 20, true, 2); 58 | // //SimpleCuckoo cc; 59 | // //cc.mParams = param; 60 | // //cc.init(); 61 | // //cc.insert(set, ZeroBlock); 62 | 63 | // CuckooIndex mm; 64 | // mm.init(param); 65 | // mm.insert(set, ZeroBlock); 66 | //} 67 | Timer tt; 68 | auto s = tt.setTimePoint("s"); 69 | srv.setInputs(set, params.mNumHash, 10); 70 | auto e = tt.setTimePoint("e"); 71 | 72 | std::this_thread::sleep_for(std::chrono::seconds(1)); 73 | 74 | if (params.mCmd->isSet("cuckooTime") && params.mIdx == 1) 75 | std::cout << "ch:" << std::chrono::duration_cast(e - s).count() << "ms " << std::flush; 76 | 77 | clientChls[0].send(me); 78 | clientChls[0].recv(them); 79 | if (them != 10021) 80 | throw RTE_LOC; 81 | 82 | srv.init(u8(params.mIdx - 1), clientChls[0], serverChls[0], serverSetSize, clientSetSize, ZeroBlock, ss, params.mCmd->get("bigBlock")); 83 | srv.send(clientChls[0], serverChls[0], numThreads); 84 | 85 | } 86 | catch (std::exception& e) 87 | { 88 | std::cout << e.what() << std::endl; 89 | } 90 | } 91 | } 92 | } 93 | } 94 | } 95 | #else 96 | std::cout << Color::Red << "DRRN is not enabled " << std::endl << Color::Default; 97 | #endif 98 | } 99 | 100 | void Drrn17Recv( 101 | LaunchParams& params) 102 | { 103 | #ifdef ENABLE_DRRN_PSI 104 | setThreadName("CP_Test_Thread"); 105 | //u64 dummy[1]; 106 | u64 me = 0 + 10021; 107 | u64 them; 108 | 109 | 110 | PRNG prng(_mm_set_epi32(4253465, 3434565, 234435, 23987045)); 111 | 112 | 113 | if (params.mVerbose) std::cout << "\n"; 114 | 115 | 116 | for (auto serverSetSize : params.mNumItems2) 117 | { 118 | for (auto clientSetSize : params.mNumItems) 119 | { 120 | for (auto numThreads : params.mNumThreads) 121 | { 122 | auto s0 = params.getChannels(1); 123 | auto s1 = params.getChannels2(1); 124 | 125 | for (auto ss : params.mBinScaler) 126 | { 127 | for (u64 jj = 0; jj < params.mTrials; jj++) 128 | { 129 | try { 130 | std::string tag("Drrn "); 131 | 132 | std::vector recvSet(clientSetSize); 133 | prng.get(recvSet.data(), recvSet.size()); 134 | s0[0].send(me); 135 | s1[0].send(me); 136 | s0[0].recv(them); 137 | 138 | if (them != 0 + 1010) 139 | throw RTE_LOC; 140 | 141 | s1[0].recv(them); 142 | if (them != 1 + 1010) 143 | throw RTE_LOC; 144 | 145 | 146 | gTimer.reset(); 147 | Timer timer; 148 | auto start = timer.setTimePoint("start"); 149 | DrrnPsiClient client; 150 | client.init(s0[0], s1[0], serverSetSize, clientSetSize, ZeroBlock, params.mNumHash, ss, 10, params.mCmd->get("bigBlock")); 151 | 152 | auto mid = timer.setTimePoint("online"); 153 | 154 | client.recv(s0[0], s1[0], recvSet); 155 | auto end = timer.setTimePoint("done"); 156 | 157 | auto offlineTime = std::chrono::duration_cast(mid - start).count(); 158 | auto onlineTime = std::chrono::duration_cast(end - mid).count(); 159 | 160 | //auto byteSent = chls[0]->getTotalDataSent() *chls.size(); 161 | 162 | printTimings(tag, s0, offlineTime, onlineTime, params, clientSetSize, numThreads, ss, &s1, serverSetSize); 163 | 164 | } 165 | catch (std::exception& e) 166 | { 167 | std::cout << e.what() << std::endl; 168 | } 169 | } 170 | } 171 | } 172 | } 173 | } 174 | #else 175 | std::cout << Color::Red << "DRRN is not enabled " << std::endl << Color::Default; 176 | #endif 177 | } 178 | 179 | -------------------------------------------------------------------------------- /frontend/OtBinMain.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | 4 | 5 | 6 | #include 7 | #include "cryptoTools/Common/Defines.h" 8 | 9 | 10 | //void otBin(); 11 | void rr17aRecv( 12 | LaunchParams& params); 13 | 14 | void rr17aSend( 15 | LaunchParams& params); 16 | 17 | 18 | //void otBin(); 19 | void rr17aRecv_StandardModel( 20 | LaunchParams& params); 21 | 22 | void rr17aSend_StandardModel( 23 | LaunchParams& params); 24 | 25 | void rr17bRecv( 26 | LaunchParams& params); 27 | 28 | void rr17bSend( 29 | LaunchParams& params); 30 | 31 | 32 | void kkrtRecv( 33 | LaunchParams& params); 34 | 35 | void kkrtSend( 36 | LaunchParams& params); 37 | 38 | void grr18Recv( 39 | LaunchParams& params); 40 | 41 | void grr18Send( 42 | LaunchParams& params); 43 | -------------------------------------------------------------------------------- /frontend/bloomFilterMain.cpp: -------------------------------------------------------------------------------- 1 | #include "bloomFilterMain.h" 2 | #include "cryptoTools/Network/Endpoint.h" 3 | 4 | 5 | #include 6 | using namespace osuCrypto; 7 | #include "util.h" 8 | 9 | #include "cryptoTools/Common/Defines.h" 10 | #include "libOTe/TwoChooseOne/KosOtExtReceiver.h" 11 | #include "libOTe/TwoChooseOne/KosOtExtSender.h" 12 | 13 | //#include "libOTe/TwoChooseOne/LzKosOtExtReceiver.h" 14 | //#include "libOTe/TwoChooseOne/LzKosOtExtSender.h" 15 | #include "cryptoTools/Common/Log.h" 16 | #include "cryptoTools/Common/Timer.h" 17 | #include "cryptoTools/Crypto/PRNG.h" 18 | #include 19 | 20 | extern u8 dummy[]; 21 | //#define LAZY_OT 22 | 23 | void bfSend(LaunchParams& params) 24 | { 25 | #ifdef ENABLE_RR16_PSI 26 | PRNG prng(_mm_set_epi32(4253465, 3434565, 234435, 23987045)); 27 | 28 | for (auto setSize : params.mNumItems) 29 | { 30 | for (auto cc : params.mNumThreads) 31 | { 32 | auto chls = params.getChannels(cc); 33 | 34 | for (u64 jj = 0; jj < params.mTrials; jj++) 35 | { 36 | std::vector set(setSize); 37 | for (u64 i = 0; i < setSize; ++i) 38 | set[i] = prng.get(); 39 | 40 | #ifdef LAZY_OT 41 | LzKosOtExtReceiver otRecv; 42 | LzKosOtExtSender otSend; 43 | #else 44 | KosOtExtReceiver otRecv; 45 | KosOtExtSender otSend; 46 | #endif // LAZY_OT 47 | 48 | 49 | AknBfMPsiSender sendPSIs; 50 | 51 | sendPSIs.init(setSize, params.mStatSecParam, otSend, chls, prng.get()); 52 | 53 | 54 | chls[0].asyncSend(dummy, 1); 55 | chls[0].recv(dummy, 1); 56 | 57 | sendPSIs.sendInput(set, chls); 58 | } 59 | } 60 | } 61 | 62 | #else 63 | std::cout << Color::Red << "RR16 PSI is not enabled" << std::endl << Color::Default; 64 | #endif 65 | } 66 | 67 | 68 | void bfRecv(LaunchParams& params) 69 | { 70 | #ifdef ENABLE_RR16_PSI 71 | for (u64 g = 0; g < params.mChls.size(); ++g) 72 | params.mChls[g].resetStats(); 73 | 74 | 75 | PRNG prng(_mm_set_epi32(4253465, 3434565, 234435, 23987045)); 76 | 77 | for (auto setSize : params.mNumItems) 78 | { 79 | for (auto numThreads : params.mNumThreads) 80 | { 81 | auto chls = params.getChannels(numThreads); 82 | 83 | for (u64 jj = 0; jj < params.mTrials; jj++) 84 | { 85 | std::vector set(setSize); 86 | prng.get(set.data(), set.size()); 87 | 88 | #ifdef LAZY_OT 89 | LzKosOtExtReceiver otRecv; 90 | LzKosOtExtSender otSend; 91 | #else 92 | KosOtExtReceiver otRecv; 93 | KosOtExtSender otSend; 94 | #endif // LAZY_OT 95 | 96 | 97 | AknBfMPsiReceiver recvPSIs; 98 | 99 | 100 | Timer timer; 101 | auto start = timer.setTimePoint("start"); 102 | recvPSIs.init(setSize, params.mStatSecParam, otRecv, chls, ZeroBlock); 103 | 104 | 105 | 106 | chls[0].asyncSend(dummy, 1); 107 | chls[0].recv(dummy, 1); 108 | auto mid = timer.setTimePoint("init"); 109 | 110 | 111 | recvPSIs.sendInput(set, chls); 112 | auto end = timer.setTimePoint("done"); 113 | 114 | auto offlineTime = std::chrono::duration_cast(mid - start).count(); 115 | auto onlineTime = std::chrono::duration_cast(end - mid).count(); 116 | 117 | std::string tag("RR16"); 118 | printTimings(tag, chls, offlineTime, onlineTime, params, setSize, numThreads); 119 | 120 | } 121 | } 122 | } 123 | 124 | #else 125 | std::cout << Color::Red << "RR16 PSI is not enabled" << std::endl << Color::Default; 126 | #endif 127 | } 128 | 129 | 130 | -------------------------------------------------------------------------------- /frontend/bloomFilterMain.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | 4 | #include "util.h" 5 | 6 | void bfSend(LaunchParams& params); 7 | void bfRecv(LaunchParams& params); 8 | -------------------------------------------------------------------------------- /frontend/cuckoo/SimpleCuckoo.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cryptoTools/Common/Defines.h" 3 | #include "cryptoTools/Common/Log.h" 4 | #include "cryptoTools/Common/BitVector.h" 5 | 6 | #include "cryptoTools/Common/Matrix.h" 7 | //#include 8 | #include 9 | //#define THREAD_SAFE_CUCKOO 10 | #include "cryptoTools/Common/CuckooIndex.h" 11 | 12 | namespace osuCrypto 13 | { 14 | 15 | class SimpleCuckoo 16 | { 17 | public: 18 | SimpleCuckoo(); 19 | ~SimpleCuckoo(); 20 | 21 | struct Bin 22 | { 23 | Bin() :mVal(-1) {} 24 | Bin(u64 idx, u64 hashIdx) : mVal(idx | (hashIdx << 56)) {} 25 | 26 | bool isEmpty() const; 27 | u64 idx() const; 28 | u64 hashIdx() const; 29 | 30 | void swap(u64& idx, u64& hashIdx); 31 | #ifdef THREAD_SAFE_CUCKOO 32 | Bin(const Bin& b) : mVal(b.mVal.load(std::memory_order_relaxed)) {} 33 | Bin(Bin&& b) : mVal(b.mVal.load(std::memory_order_relaxed)) {} 34 | std::atomic mVal; 35 | #else 36 | Bin(const Bin& b) : mVal(b.mVal) {} 37 | Bin(Bin&& b) : mVal(b.mVal) {} 38 | u64 mVal; 39 | #endif 40 | }; 41 | struct Workspace 42 | { 43 | Workspace(u64 n, u64 h) 44 | : curAddrs(n) 45 | , curHashIdxs(n) 46 | , oldVals(n) 47 | , findVal(n, h) 48 | {} 49 | 50 | std::vector 51 | curAddrs, 52 | curHashIdxs, 53 | oldVals; 54 | 55 | Matrix findVal; 56 | }; 57 | 58 | 59 | 60 | u64 mTotalTries; 61 | 62 | bool operator==(const SimpleCuckoo& cmp)const; 63 | bool operator!=(const SimpleCuckoo& cmp)const; 64 | 65 | //std::mutex mStashx; 66 | 67 | CuckooParam mParams; 68 | 69 | void print() const; 70 | void init(); 71 | 72 | void insert(span items, block hashingSeed) 73 | { 74 | std::vector hashs(items.size()); 75 | std::vector idxs(items.size()); 76 | AES hasher(hashingSeed); 77 | 78 | for (u64 i = 0; i < u64(items.size()); i += u64(hashs.size())) 79 | { 80 | auto min = std::min(items.size() - i, hashs.size()); 81 | 82 | hasher.ecbEncBlocks(items.data() + i, min, hashs.data()); 83 | 84 | for (u64 j = 0, jj = i; j < min; ++j, ++jj) 85 | { 86 | idxs[j] = jj; 87 | hashs[j] = hashs[j] ^ items[jj]; 88 | 89 | //if(jj < 1) std::cout<< IoStream::lock << "item[" << jj << "] = " < " << hashs[j] << std::endl << IoStream::unlock; 90 | } 91 | 92 | insert(idxs, hashs); 93 | } 94 | } 95 | 96 | void insert(span itemIdxs, span hashs) 97 | { 98 | Workspace ws(itemIdxs.size(), mParams.mNumHashes); 99 | std::vector bb(mParams.mNumHashes); 100 | Matrix hh(hashs.size(), mParams.mNumHashes); 101 | 102 | for (i64 i = 0; i < i64(hashs.size()); ++i) 103 | { 104 | AES aes(hashs[i]); 105 | aes.ecbEncCounterMode(0, bb.size(), bb.data()); 106 | for (u64 j = 0; j < mParams.mNumHashes; ++j) 107 | { 108 | hh(i, j) = *(u64*)&bb[j]; 109 | //hh(i,j) = CuckooIndex<>::getHash(hashs[i], j, mParams.numBins()); 110 | } 111 | } 112 | 113 | insertBatch(itemIdxs, hh, ws); 114 | } 115 | void insertBatch(span itemIdxs, MatrixView hashs, Workspace& workspace); 116 | 117 | u64 findBatch(MatrixView hashes, 118 | span idxs, 119 | Workspace& wordkspace); 120 | 121 | 122 | u64 stashUtilization(); 123 | 124 | std::vector mHashes; 125 | MatrixView mHashesView; 126 | 127 | std::vector mBins; 128 | std::vector mStash; 129 | 130 | //std::vector mBins; 131 | //std::vector mStash; 132 | 133 | 134 | //void insertItems(std::array,4>& hashs); 135 | }; 136 | 137 | } 138 | -------------------------------------------------------------------------------- /frontend/cuckoo/bounds.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/osu-crypto/libPSI/1159df1fecad06570ec384d64ab0724d05b6905b/frontend/cuckoo/bounds.xlsx -------------------------------------------------------------------------------- /frontend/cuckoo/bounds2.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/osu-crypto/libPSI/1159df1fecad06570ec384d64ab0724d05b6905b/frontend/cuckoo/bounds2.xlsx -------------------------------------------------------------------------------- /frontend/cuckoo/cuckooTests.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | 4 | 5 | 6 | void simpleTest(int argc, char** argv); 7 | 8 | void sweepE( 9 | const osuCrypto::u64 &setSize, 10 | const osuCrypto::u64 &h, 11 | double &e, 12 | const osuCrypto::u64 &t, 13 | const osuCrypto::u64 &numThrds, 14 | bool varyCuckooSize, 15 | const osuCrypto::u64 &stashSize, 16 | std::fstream &out); 17 | -------------------------------------------------------------------------------- /frontend/dcwMain.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include "dcwMain.h" 4 | 5 | #include "cryptoTools/Network/Endpoint.h" 6 | 7 | 8 | #include "libPSI/PSI/Dcw/DcwRBfPsiReceiver.h" 9 | #include "libPSI/PSI/Dcw/DcwRBfPsiSender.h" 10 | 11 | 12 | 13 | #include "cryptoTools/Common/Defines.h" 14 | #include "libOTe/TwoChooseOne/IknpOtExtReceiver.h" 15 | #include "libOTe/TwoChooseOne/IknpOtExtSender.h" 16 | #include "libOTe/TwoChooseOne/SilentOtExtReceiver.h" 17 | #include "libOTe/TwoChooseOne/SilentOtExtSender.h" 18 | 19 | #include "cryptoTools/Common/Log.h" 20 | #include "cryptoTools/Common/Timer.h" 21 | #include "cryptoTools/Crypto/PRNG.h" 22 | #include 23 | #include 24 | #include "boost/format.hpp" 25 | extern u8 dummy[]; 26 | 27 | using namespace osuCrypto; 28 | 29 | 30 | void DcwRSend( 31 | LaunchParams& params) 32 | { 33 | #ifdef ENABLE_DCW_PSI 34 | PRNG prng(_mm_set_epi32(4253465, 3434565, 234435, 23987045)); 35 | 36 | for (auto setSize : params.mNumItems) 37 | { 38 | for (auto tt : params.mNumThreads) 39 | { 40 | if (tt != 1) 41 | { 42 | continue; 43 | } 44 | 45 | auto chls = params.getChannels(tt); 46 | 47 | for (u64 jj = 0; jj < params.mTrials; jj++) 48 | { 49 | std::vector set(setSize); 50 | 51 | for (u64 i = 0; i < setSize; ++i) 52 | set[i] = prng.get(); 53 | 54 | SilentOtExtReceiver sRecv; 55 | SilentOtExtSender sSend; 56 | IknpOtExtReceiver iRecv; 57 | IknpOtExtSender iSend; 58 | bool silent = params.mCmd->isSet("silent"); 59 | //OtExtReceiver& otRecv = silent ? (OtExtReceiver&)sRecv : iRecv; 60 | OtExtSender& otSend = silent ? (OtExtSender&)sSend : iSend; 61 | DcwRBfPsiSender sendPSIs; 62 | 63 | gTimer.reset(); 64 | sendPSIs.init(setSize, params.mStatSecParam, otSend, chls, prng.get()); 65 | chls[0].asyncSend(dummy, 1); 66 | sendPSIs.sendInput(set, chls); 67 | } 68 | } 69 | } 70 | 71 | #else 72 | std::cout << Color::Red << "DCW PSI is not enabled" << std::endl << Color::Default; 73 | #endif 74 | } 75 | 76 | void DcwRRecv( 77 | LaunchParams& params) 78 | { 79 | #ifdef ENABLE_DCW_PSI 80 | PRNG prng(_mm_set_epi32(4253465, 3434565, 234435, 23987045)); 81 | 82 | for (auto setSize : params.mNumItems) 83 | { 84 | for (u64 numThreads : params.mNumThreads) 85 | { 86 | if (numThreads != 1) 87 | { 88 | std::cout << "dcwr n = " << setSize << " t = " << numThreads << " skipped, t > 1 (multi-thread) not implemented." << std::endl; 89 | continue; 90 | } 91 | 92 | auto chls = params.getChannels(numThreads); 93 | 94 | for (u64 jj = 0; jj < params.mTrials; jj++) 95 | { 96 | std::vector set(setSize); 97 | for (u64 i = 0; i < setSize; ++i) 98 | set[i] =prng.get(); 99 | 100 | SilentOtExtReceiver sRecv; 101 | SilentOtExtSender sSend; 102 | IknpOtExtReceiver iRecv; 103 | IknpOtExtSender iSend; 104 | bool silent = params.mCmd->isSet("silent"); 105 | OtExtReceiver& otRecv = silent ? (OtExtReceiver&)sRecv : iRecv; 106 | //OtExtSender& otSend = silent ? (OtExtSender&)sSend : iSend; 107 | DcwRBfPsiReceiver recvPSIs; 108 | 109 | 110 | 111 | gTimer.reset(); 112 | Timer timer; 113 | auto start = timer.setTimePoint("start"); 114 | 115 | recvPSIs.init(setSize, params.mStatSecParam, otRecv, chls, sysRandomSeed()); 116 | 117 | chls[0].recv(dummy, 1); 118 | auto mid = timer.setTimePoint("init"); 119 | 120 | 121 | 122 | recvPSIs.sendInput(set, chls); 123 | auto end = timer.setTimePoint("done"); 124 | 125 | auto offlineTime = std::chrono::duration_cast(mid - start).count(); 126 | auto onlineTime = std::chrono::duration_cast(end - mid).count(); 127 | 128 | 129 | //std::cout << setSize << " " << offlineTime << " " << online << std::endl; 130 | 131 | std::string tag("DCWR"); 132 | 133 | printTimings(tag, chls, offlineTime, onlineTime, params, setSize, numThreads); 134 | 135 | } 136 | } 137 | } 138 | #else 139 | std::cout << Color::Red << "DCW PSI is not enabled" << std::endl << Color::Default; 140 | #endif 141 | } 142 | -------------------------------------------------------------------------------- /frontend/dcwMain.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | 4 | 5 | 6 | 7 | #include "util.h" 8 | 9 | 10 | #include 11 | #include "cryptoTools/Common/Defines.h" 12 | 13 | 14 | 15 | void DcwRSend(LaunchParams&); 16 | void DcwRRecv(LaunchParams&); 17 | 18 | -------------------------------------------------------------------------------- /frontend/dktMain.cpp: -------------------------------------------------------------------------------- 1 | #include "dcwMain.h" 2 | 3 | #include "cryptoTools/Network/Endpoint.h" 4 | 5 | #include "libPSI/MPSI/DKT/DktMPsiReceiver.h" 6 | #include "libPSI/MPSI/DKT/DktMPsiSender.h" 7 | 8 | 9 | 10 | #include "cryptoTools/Common/Defines.h" 11 | #include "cryptoTools/Common/Log.h" 12 | #include "cryptoTools/Common/Timer.h" 13 | #include "cryptoTools/Crypto/PRNG.h" 14 | #include 15 | #include "dktMain.h" 16 | 17 | using namespace osuCrypto; 18 | 19 | extern u8 dummy[]; 20 | 21 | void DktSend(LaunchParams& params) 22 | { 23 | #ifdef ENABLE_DKT_PSI 24 | 25 | PRNG prng(_mm_set_epi32(4253465, 3434565, 234435, 23987045)); 26 | 27 | for (auto setSize : params.mNumItems) 28 | { 29 | for (auto numThreads : params.mNumThreads) 30 | { 31 | auto sendChls = params.getChannels(numThreads); 32 | 33 | for (u64 jj = 0; jj < params.mTrials; jj++) 34 | { 35 | std::vector set(setSize); 36 | prng.get(set.data(), set.size()); 37 | 38 | 39 | DktMPsiSender sendPSIs; 40 | 41 | Timer timer; 42 | 43 | sendPSIs.init(setSize, params.mStatSecParam, prng.get()); 44 | sendChls[0].asyncSend(dummy, 1); 45 | 46 | sendPSIs.sendInput(set, sendChls); 47 | } 48 | } 49 | } 50 | #else 51 | std::cout < set(setSize); 72 | prng.get(set.data(), set.size()); 73 | 74 | DktMPsiReceiver recvPSIs; 75 | 76 | Timer timer; 77 | auto start = timer.setTimePoint("start"); 78 | recvPSIs.init(setSize, params.mStatSecParam, ZeroBlock); 79 | 80 | chls[0].recv(dummy, 1); 81 | auto mid = timer.setTimePoint("init"); 82 | 83 | recvPSIs.sendInput(set, chls); 84 | auto end = timer.setTimePoint("done"); 85 | 86 | auto offlineTime = std::chrono::duration_cast(mid - start).count(); 87 | auto onlineTime = std::chrono::duration_cast(end - mid).count(); 88 | 89 | 90 | std::string tag("DKT11"); 91 | printTimings(tag, chls, offlineTime, onlineTime, params, setSize, numThreads); 92 | 93 | } 94 | } 95 | } 96 | #else 97 | std::cout << Color::Red << "DKT is not enabled " << std::endl << Color::Default; 98 | #endif 99 | } 100 | 101 | 102 | 103 | -------------------------------------------------------------------------------- /frontend/dktMain.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | 4 | 5 | 6 | #include 7 | #include "cryptoTools/Common/Defines.h" 8 | #include "util.h" 9 | 10 | 11 | void DktSend(LaunchParams& params); 12 | void DktRecv(LaunchParams& params); 13 | 14 | -------------------------------------------------------------------------------- /frontend/ecdhMain.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "cryptoTools/Network/Endpoint.h" 3 | 4 | #include "libPSI/PSI/ECDH/EcdhPsiReceiver.h" 5 | #include "libPSI/PSI/ECDH/EcdhPsiSender.h" 6 | 7 | 8 | 9 | #include "cryptoTools/Common/Defines.h" 10 | #include "cryptoTools/Common/Log.h" 11 | #include "cryptoTools/Common/Timer.h" 12 | #include "cryptoTools/Crypto/PRNG.h" 13 | #include 14 | #include "ecdhMain.h" 15 | 16 | using namespace osuCrypto; 17 | 18 | extern u8 dummy[]; 19 | 20 | void EcdhSend(LaunchParams& params) 21 | { 22 | #ifdef ENABLE_ECDH_PSI 23 | 24 | PRNG prng(_mm_set_epi32(4253465, 3434565, 234435, 23987045)); 25 | 26 | for (auto setSize : params.mNumItems) 27 | { 28 | for (auto numThreads : params.mNumThreads) 29 | { 30 | auto sendChls = params.getChannels(numThreads); 31 | 32 | for (u64 jj = 0; jj < params.mTrials; jj++) 33 | { 34 | std::vector set(setSize); 35 | prng.get(set.data(), set.size()); 36 | 37 | 38 | EcdhPsiSender sendPSIs; 39 | 40 | gTimer.reset(); 41 | Timer timer; 42 | 43 | sendPSIs.init(setSize, params.mStatSecParam, prng.get()); 44 | sendChls[0].asyncSend(dummy, 1); 45 | 46 | sendPSIs.sendInput(set, sendChls); 47 | } 48 | } 49 | } 50 | #else 51 | std::cout << Color::Red << "ECDH PSI is not enabled" << std::endl << Color::Default; 52 | #endif 53 | } 54 | 55 | 56 | void EcdhRecv(LaunchParams& params) 57 | { 58 | #ifdef ENABLE_ECDH_PSI 59 | 60 | PRNG prng(_mm_set_epi32(4253465, 3434565, 234435, 23987045)); 61 | 62 | for (auto setSize : params.mNumItems) 63 | { 64 | for (auto numThreads : params.mNumThreads) 65 | { 66 | auto chls = params.getChannels(numThreads); 67 | 68 | for (u64 jj = 0; jj < params.mTrials; jj++) 69 | { 70 | 71 | std::vector set(setSize); 72 | prng.get(set.data(), set.size()); 73 | 74 | EcdhPsiReceiver recvPSIs; 75 | 76 | gTimer.reset(); 77 | 78 | Timer timer; 79 | auto start = timer.setTimePoint("start"); 80 | recvPSIs.init(setSize, params.mStatSecParam, ZeroBlock); 81 | 82 | chls[0].recv(dummy, 1); 83 | auto mid = timer.setTimePoint("init"); 84 | 85 | recvPSIs.sendInput(set, chls); 86 | auto end = timer.setTimePoint("done"); 87 | 88 | auto offlineTime = std::chrono::duration_cast(mid - start).count(); 89 | auto onlineTime = std::chrono::duration_cast(end - mid).count(); 90 | 91 | 92 | std::string tag("Ecdh_Curve25519"); 93 | printTimings(tag, chls, offlineTime, onlineTime, params, setSize, numThreads); 94 | 95 | } 96 | } 97 | } 98 | #else 99 | std::cout << Color::Red << "ECDH PSI is not enabled" << std::endl << Color::Default; 100 | #endif 101 | } 102 | 103 | 104 | -------------------------------------------------------------------------------- /frontend/ecdhMain.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | 4 | 5 | 6 | #include 7 | #include "cryptoTools/Common/Defines.h" 8 | #include "util.h" 9 | 10 | 11 | void EcdhSend(LaunchParams& params); 12 | void EcdhRecv(LaunchParams& params); 13 | 14 | -------------------------------------------------------------------------------- /frontend/frontend.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | x64 7 | 8 | 9 | Release 10 | x64 11 | 12 | 13 | 14 | {C81DC04B-A0F0-4B77-8DCE-C8190E629467} 15 | frontend 16 | 10.0 17 | 18 | 19 | 20 | Application 21 | true 22 | v142 23 | MultiByte 24 | 25 | 26 | Application 27 | false 28 | v142 29 | true 30 | MultiByte 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | false 48 | 49 | 50 | 51 | Level3 52 | Disabled 53 | true 54 | $(ProjectDir)/..;$(ProjectDir);$(libOTeDir);$(libOTeDir)/cryptoTools;$(ProjectDir)../libPSI_Tests;$(ProjectDir)../libPSI;$(ProjectDir)../cryptoTools\thirdparty\win\boost\;$(ProjectDir)../cryptoTools/thirdparty\win\;$(ProjectDir)../cryptoTools/thirdparty/win/NTL/include;$(SolutionDir)thirdparty/win/miracl;C:/libs/boost;C:/libs/;C:/libs/NTL/include;C:/libs/miracl;C:\libs\include; 55 | MultiThreadedDebug 56 | SOLUTION_DIR=R"**($(SolutionDir))**";_WIN32_WINNT=0x0501;_MBCS;%(PreprocessorDefinitions) 57 | true 58 | true 59 | false 60 | 61 | 62 | %(AdditionalLibraryDirectories);$(SolutionDir)\thirdparty\win\boost\stage\lib;C:/libs/boost\stage\lib;$(OutDir);$(SolutionDir)thirdparty\win\;C:/libs/;$(libOTeDir)\x64\$(Configuration);C:/libs/lib 63 | libOTe_Tests.lib;tests_cryptoTools.lib;libOTe.lib;libPSI.lib;libPSI_Tests.lib;cryptoTools.lib;Miracl\x64\$(Configuration)\miracl.lib;%(AdditionalDependencies) 64 | 65 | 66 | 67 | 68 | Level3 69 | MaxSpeed 70 | true 71 | true 72 | true 73 | $(ProjectDir)/..;$(ProjectDir);$(libOTeDir);$(libOTeDir)/cryptoTools;$(ProjectDir)../libPSI_Tests;$(ProjectDir)../libPSI;$(ProjectDir)../cryptoTools\thirdparty\win\boost\;$(ProjectDir)../cryptoTools/thirdparty\win\;$(ProjectDir)../cryptoTools/thirdparty/win/NTL/include;$(SolutionDir)thirdparty/win/miracl;C:/libs/boost;C:/libs/;C:/libs/NTL/include;C:/libs/miracl;C:\libs\include; 74 | MultiThreaded 75 | SOLUTION_DIR=R"**($(SolutionDir))**";_WIN32_WINNT=0x0501;DEBUG;_MBCS;%(PreprocessorDefinitions); 76 | true 77 | true 78 | 79 | 80 | true 81 | true 82 | %(AdditionalLibraryDirectories);$(SolutionDir)\thirdparty\win\boost\stage\lib;C:/libs/boost\stage\lib;$(OutDir);$(SolutionDir)thirdparty\win\;C:/libs/;$(libOTeDir)\x64\$(Configuration);C:/libs/lib 83 | libOTe_Tests.lib;tests_cryptoTools.lib;libOTe.lib;libPSI.lib;libPSI_Tests.lib;cryptoTools.lib;Miracl\x64\$(Configuration)\miracl.lib;%(AdditionalDependencies) 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /frontend/util.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cryptoTools/Network/Channel.h" 3 | #include "cryptoTools/Common/CLP.h" 4 | #include "cryptoTools/Network/Channel.h" 5 | 6 | using namespace osuCrypto; 7 | 8 | template 9 | std::string string_format(const std::string& format, Args ... args) 10 | { 11 | size_t size = std::snprintf(nullptr, 0, format.c_str(), args ...) + 1; // Extra space for '\0' 12 | std::unique_ptr buf(new char[size]); 13 | std::snprintf(buf.get(), size, format.c_str(), args ...); 14 | return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside 15 | } 16 | 17 | 18 | 19 | struct LaunchParams 20 | { 21 | LaunchParams() 22 | :mVerbose(0), 23 | mTrials(1), 24 | mStatSecParam(40) 25 | { 26 | } 27 | 28 | std::vector getChannels(u64 n) { 29 | return std::vector( mChls.begin(), mChls.begin() + n); 30 | } 31 | 32 | std::vector getChannels2(u64 n) { 33 | return std::vector(mChls2.begin(), mChls2.begin() + n); 34 | } 35 | 36 | std::string mHostName; 37 | std::vector mChls, mChls2; 38 | std::vector mNumItems, mNumItems2; 39 | std::vector mNumThreads; 40 | std::vector mBinScaler; 41 | 42 | u64 mBitSize; 43 | u64 mVerbose; 44 | u64 mTrials; 45 | u64 mStatSecParam; 46 | u64 mIdx; 47 | u64 mNumHash; 48 | 49 | std::string mIP; 50 | CLP* mCmd; 51 | }; 52 | 53 | 54 | #include "cryptoTools/Network/Channel.h" 55 | void senderGetLatency(osuCrypto::Channel& chl); 56 | 57 | void recverGetLatency(osuCrypto::Channel& chl); 58 | 59 | 60 | 61 | 62 | void printTimings( 63 | std::string tag, 64 | std::vector chls, 65 | long long offlineTime, long long onlineTime, 66 | LaunchParams & params, 67 | const osuCrypto::u64 &setSize, 68 | const osuCrypto::u64 &numThreads, 69 | double s = 1, 70 | std::vector* chls2 = nullptr, 71 | u64 n2 = -1); 72 | 73 | void printHeader(); 74 | -------------------------------------------------------------------------------- /libPSI/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | ############################################# 4 | # Build libPSI # 5 | ############################################# 6 | 7 | file(GLOB_RECURSE SRCS *.cpp) 8 | 9 | 10 | add_library(libPSI ${SRCS}) 11 | 12 | ############################################# 13 | # Link libOTe # 14 | ############################################# 15 | 16 | target_link_libraries(libPSI oc::libOTe sparsehash) 17 | 18 | target_include_directories(libPSI PUBLIC 19 | $ 20 | $) 21 | target_include_directories(libPSI PUBLIC 22 | $ 23 | $) 24 | 25 | 26 | if(MSVC) 27 | target_compile_options(libPSI PRIVATE $<$:/std:c++${LIBPSI_STD_VER}>) 28 | #target_compile_options(libOTe PRIVATE -openmp:experimental) 29 | else() 30 | target_compile_options(libPSI PRIVATE $<$:-std=c++${LIBPSI_STD_VER}>) 31 | 32 | endif() -------------------------------------------------------------------------------- /libPSI/MPSI/DKT/DktMPsiReceiver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "libPSI/config.h" 3 | #ifdef ENABLE_DKT_PSI 4 | 5 | #include "cryptoTools/Common/Defines.h" 6 | #include "cryptoTools/Network/Channel.h" 7 | #include "cryptoTools/Crypto/PRNG.h" 8 | 9 | namespace osuCrypto 10 | { 11 | 12 | class DktMPsiReceiver 13 | { 14 | public: 15 | DktMPsiReceiver(); 16 | ~DktMPsiReceiver(); 17 | 18 | 19 | u64 mN, mSecParam; 20 | PRNG mPrng; 21 | 22 | std::vector mIntersection; 23 | 24 | void init(u64 n, u64 secParam, block seed); 25 | 26 | 27 | void sendInput(span inputs, span chl0); 28 | 29 | }; 30 | } 31 | #endif -------------------------------------------------------------------------------- /libPSI/MPSI/DKT/DktMPsiSender.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "libPSI/config.h" 3 | #ifdef ENABLE_DKT_PSI 4 | 5 | #include "cryptoTools/Common/Defines.h" 6 | 7 | #include "cryptoTools/Network/Channel.h" 8 | #include "cryptoTools/Crypto/PRNG.h" 9 | 10 | namespace osuCrypto 11 | { 12 | 13 | 14 | class DktMPsiSender 15 | { 16 | public: 17 | DktMPsiSender(); 18 | ~DktMPsiSender(); 19 | 20 | 21 | u64 mN, mSecParam; 22 | PRNG mPrng; 23 | 24 | void init(u64 n, u64 secParam, block seed); 25 | //void init(u64 n, u64 statSecParam); 26 | 27 | 28 | void sendInput(std::vector& inputs, span chl); 29 | //void sendInput(std::vector& inputs, std::vector& chl); 30 | }; 31 | 32 | 33 | } 34 | #endif -------------------------------------------------------------------------------- /libPSI/MPSI/Grr18/Grr18Common.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "libPSI/config.h" 3 | #ifdef ENABLE_GRR_PSI 4 | 5 | #include 6 | #include 7 | #include 8 | #include "libPSI/Tools/SimpleHasher.h" 9 | 10 | namespace osuCrypto 11 | { 12 | 13 | extern bool mGrr18PrintWarning; 14 | 15 | u64 computeLoads( 16 | std::vector& loads, 17 | PRNG & prng, 18 | u64 binStart, 19 | bool oneSided, 20 | bool lapPlusBuffer, 21 | u64 n, 22 | SimpleHasher& bins, 23 | double eps, 24 | i64 cwThreshold = -1, 25 | bool print = false); 26 | 27 | } 28 | #endif -------------------------------------------------------------------------------- /libPSI/MPSI/Grr18/Grr18MPsiReceiver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "libPSI/config.h" 3 | #ifdef ENABLE_GRR_PSI 4 | #ifndef ENABLE_OOS 5 | static_assert(0, "ENABLE_OOS must be defined in libOTe"); 6 | #endif 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include "libOTe/NChooseOne/Oos/OosNcoOtReceiver.h" 13 | #include "libOTe/NChooseOne/Oos/OosNcoOtSender.h" 14 | #include 15 | 16 | namespace osuCrypto 17 | { 18 | 19 | class Grr18MPsiReceiver : public TimerAdapter 20 | { 21 | public: 22 | Grr18MPsiReceiver(); 23 | ~Grr18MPsiReceiver(); 24 | 25 | //static const u64 CodeWordSize = 7; 26 | //static const u64 hasherStepSize; 27 | 28 | bool mHashToSmallerDomain, mOneSided = false, mLapPlusBuff = false; 29 | double mEpsBins = 0.9, mEpsMasks= 0.1; 30 | u64 mN, mStatSecParam;// , mNumOTsUpperBound;// , mOtMsgBlkSize; 31 | block mHashingSeed; 32 | std::vector mIntersection; 33 | std::unique_ptr> mTotalLoad; 34 | 35 | std::vector mOtSends; 36 | std::vector mOtRecvs; 37 | 38 | SimpleHasher mBins; 39 | PRNG mPrng; 40 | 41 | void init(u64 n, u64 statSecParam, Channel& chl0, OosNcoOtReceiver& otRecv, OosNcoOtSender& otSend, block seed, 42 | double binScaler = 4.0, u64 inputBitSize = -1); 43 | void init(u64 n, u64 statSecParam, span chls, OosNcoOtReceiver& ots, OosNcoOtSender& otSend, block seed, 44 | double binScaler = 4.0, u64 inputBitSize = -1); 45 | 46 | void sendInput(std::vector& inputs, Channel& chl); 47 | void sendInput(std::vector& inputs, span chls); 48 | 49 | }; 50 | 51 | 52 | 53 | 54 | } 55 | #endif -------------------------------------------------------------------------------- /libPSI/MPSI/Grr18/Grr18MPsiSender.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "libPSI/config.h" 3 | #ifdef ENABLE_GRR_PSI 4 | #ifndef ENABLE_OOS 5 | static_assert(0, "ENABLE_OOS must be defined in libOTe"); 6 | #endif 7 | 8 | 9 | #include "cryptoTools/Common/Defines.h" 10 | #include "cryptoTools/Common/Timer.h" 11 | #include "cryptoTools/Network/Channel.h" 12 | #include "libOTe/NChooseOne/Oos/OosNcoOtSender.h" 13 | #include "libOTe/NChooseOne/Oos/OosNcoOtReceiver.h" 14 | #include "libPSI/Tools/SimpleHasher.h" 15 | 16 | namespace osuCrypto 17 | { 18 | 19 | 20 | class Grr18MPsiSender : public TimerAdapter 21 | { 22 | public: 23 | 24 | 25 | //static const u64 CodeWordSize = 7; 26 | //static const u64 hasherStepSize; 27 | 28 | Grr18MPsiSender(); 29 | ~Grr18MPsiSender(); 30 | 31 | bool mHashToSmallerDomain, mOneSided = false, mLapPlusBuff = false; 32 | double mEpsBins = 0.9, mEpsMasks = 0.1; 33 | u64 mN, mStatSecParam, mOtMsgBlkSize, mCWThreshold = -1;//, mNumOTsUpperBound; 34 | block mHashingSeed; 35 | SimpleHasher mBins; 36 | PRNG mPrng; 37 | 38 | std::vector mOtSends; 39 | std::vector mOtRecvs; 40 | 41 | void init(u64 n, u64 statSecParam, 42 | span chls, 43 | OosNcoOtSender& ots, 44 | OosNcoOtReceiver& otRecv, 45 | block seed, 46 | double binScaler = 4.0, 47 | u64 inputBitSize = -1); 48 | 49 | void init(u64 n, u64 statSecParam, 50 | Channel & chl0, 51 | OosNcoOtSender& ots, 52 | OosNcoOtReceiver& otRecv, 53 | block seed, 54 | double binScaler = 4.0, 55 | u64 inputBitSize = -1); 56 | 57 | void sendInput(std::vector& inputs, Channel& chl); 58 | void sendInput(std::vector& inputs, span chls); 59 | 60 | 61 | u64 mReporting_totalMaskCount, mReporting_totalRealMaskCount; 62 | }; 63 | 64 | } 65 | #endif -------------------------------------------------------------------------------- /libPSI/MPSI/Grr18/bayesian.gp: -------------------------------------------------------------------------------- 1 | default(realprecision, 100); 2 | default(parisizemax, 800000000); 3 | 4 | bdist(n,k,p) = 0.0+binomial(n,k)*(p^k)*(1-p)^(n-k); 5 | bdistleq(n,k,p) = sum(i=0,k, 0.0 + bdist(n,i,p)); 6 | log2(x) = log(x)/log(2); 7 | overflowprob(n,m,B) = m * (1- bdistleq(n,B,1/m)); 8 | lappdf(x, eps) = (eps/2)*exp(-abs(x)*eps); 9 | \\n = 2^16; \\ number of items 10 | \\m = n/4; \\ number of bins 11 | 12 | err = 1/(2^40); \\ acceptable overall error probability 13 | binerror = err/m ; \\implied acceptable error probability per bin 14 | stop = 120; \\ignore bin sizes above this 15 | prior = listcreate(stop); 16 | for (z=0, stop+1, listput(prior, bdist(n, z, 1/m))) 17 | prior; 18 | remainingprobprint(dist) = 19 | { 20 | a = 1; 21 | for(i=1, stop+1, a = a - dist[i]; print(i-1 " " a)) 22 | for(i=1, stop+1, a = a - dist[i]; print(i-1 " " a)) 23 | for(i=1, stop+1, a = a - dist[i]; print(i-1 " " a)) 24 | }; 25 | remainingprob(dist) = 26 | { 27 | a = 1; 28 | out = listcreate(stop); 29 | for(i=1, stop+1, a = a - dist[i]; listput(out, a)); 30 | return(out) 31 | }; 32 | \\ remainingprob(prior) 33 | postat(prior, evid, eps, x) = 34 | \\returns the posterior likelihood of a given real bin size 35 | { 36 | total = 0; \\total = total probability of seeing estimate evid 37 | for(i=0, stop+1, total = total + prior[i+1]*lappdf(abs(evid-i),eps)); 38 | new = prior[x+1] * lappdf(abs(evid-x),eps) / total; 39 | \\print("total " total); 40 | \\print("pdf " lappdf(abs(evid-x),eps)); 41 | return(new) 42 | }; 43 | p = listcreate(stop); 44 | for (z=0, stop+1, listput(p, postat(prior, estimate, 1, z)) ); 45 | posterior(prior, estimate, eps) = 46 | \\ calculates a posterior distribution when 'estimate' is the private bin size estimate 47 | { 48 | post = listcreate(stop); 49 | for (z=0, stop+1, listput(post, postat(prior, estimate, eps, z)) ); 50 | return(post) 51 | }; 52 | binsneeded(dist) = 53 | { 54 | flag = 1; 55 | a = 1; 56 | bin = 0; 57 | while(a>binerror, 58 | a = a-dist[bin+1]; 59 | bin++ 60 | ); 61 | return(bin-1); 62 | }; 63 | \\binsneeded(prior) \\testing 64 | createtable(mina, maxa, inc, prior, eps) = \\exclusive of 'max' value 65 | { 66 | est = mina; 67 | print("n = " n); 68 | print("m = " m); 69 | print("epsilon = " eps); 70 | print("error probability = " err); 71 | print("If estimated bin size is ____ then pad with dummies up to _____"); 72 | 73 | write1("C:/Users/Peter/repo/libPSI/libPSI/MPSI/Grr18/output.txt","Lookup{" n ", " floor(m) ", " eps ", " mina ", {"); 74 | while(est < maxa, 75 | post = posterior(prior, est, eps); 76 | needed = binsneeded(post); 77 | print(est ", " needed); 78 | write1("C:/Users/Peter/repo/libPSI/libPSI/MPSI/Grr18/output.txt", needed); 79 | est = est + inc; 80 | if(est < maxa, 81 | write1("C:/Users/Peter/repo/libPSI/libPSI/MPSI/Grr18/output.txt", ", "); 82 | ); 83 | ); 84 | 85 | write("C:/Users/Peter/repo/libPSI/libPSI/MPSI/Grr18//output.txt", "}},"); 86 | }; 87 | createtable(-10, 50, 1, prior, eps); 88 | 89 | 90 | \\quit -------------------------------------------------------------------------------- /libPSI/MPSI/Grr18/bayesian.ps1: -------------------------------------------------------------------------------- 1 | 2 | $psi = New-Object System.Diagnostics.ProcessStartInfo; 3 | $psi.FileName = "C:\Program Files (x86)\Pari64-2-9-4\gp.exe"; #process file 4 | $psi.WorkingDirectory = $pwd; 5 | $psi.UseShellExecute = $false; #start the process from it's own executable file 6 | $psi.RedirectStandardInput = $true; #enable the process to read from standard input 7 | 8 | $p = [System.Diagnostics.Process]::Start($psi); 9 | 10 | Start-Sleep -s 2 #wait 2 seconds so that the process can be up and running 11 | 12 | 13 | 14 | 15 | For($nn = 16; $nn -lt 21; $nn= $nn + 4) 16 | { 17 | for($mm = 12; $mm -lt 13; $mm = $mm + 2) 18 | { 19 | $n= [math]::pow(2, $nn) 20 | $m = $n/$mm 21 | 22 | 23 | $p.StandardInput.WriteLine("n=$n;"); 24 | $p.StandardInput.WriteLine("m=$m;"); 25 | $p.StandardInput.WriteLine("\r C:/Users/Peter/repo/libPSI/libPSI/MPSI/Grr18/bayesian2.gp;"); 26 | } 27 | } 28 | 29 | 30 | $p.StandardInput.WriteLine("quit"); -------------------------------------------------------------------------------- /libPSI/MPSI/Grr18/bayesian2.gp: -------------------------------------------------------------------------------- 1 | \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 2 | log2(x) = log(x)/log(2); 3 | \\ Pr[ Binom = k ] 4 | bdist_(n,k,p) = 0.0+binomial(n,k)*(p^k)*(1-p)^(n-k); 5 | build_bdist_table(n,m) = { 6 | bdist_tbl = vector(201); 7 | p = 1/m; 8 | for(k=0,200, 9 | bdist_tbl[k+1] = bdist_(n,k,p) 10 | ); 11 | } 12 | \\ Pr[ Binom = i ] 13 | bdist(k) = bdist_tbl[k+1] 14 | \\ Pr[ Binom = i ] 15 | bdistleq(k) = sum(i=0,k, bdist(i)) 16 | \\ Pr[ Lap <= d ] 17 | eps = 1 18 | lapleq(d) = if(d<0, exp(eps*d)/2, 1 - exp(-eps*d)/2) 19 | \\ Pr[ Binom <= k AND Binom + Lap <= d ] 20 | \\ Pr[ Binom <= k AND Lap <= d-Binom ] 21 | bothevents(k,d) = sum(B=0,k, bdist(B) * lapleq(d-B)) 22 | \\ Pr[ B <= k | B + Lap <= d ] 23 | \\ = Pr[ B <= k AND B + Lap <= d ] / Pr[ B + Lap <= d ] 24 | \\ computed as B<=200 AND ... 25 | conditional(k,d) = bothevents(k,d) / bothevents(200,d) 26 | posterior(d,lambda) = { 27 | k = 1; 28 | while( log2(1 - conditional(k,d)) > -lambda, 29 | k = k+1 30 | ); 31 | return(k-1); 32 | } 33 | nicetable(lambda) = { 34 | print("bayesian corrections for eps = ", eps); 35 | 36 | mina = -10; 37 | maxa = 50; 38 | 39 | write1("C:/Users/Peter/repo/libPSI/libPSI/MPSI/Grr18/output.txt", 40 | "Lookup{" n ", " floor(m) ", " eps ", " mina ", {"); 41 | 42 | 43 | for(i=mina, maxa, 44 | needed = posterior(i,lambda); 45 | print(i, " => ", needed); 46 | 47 | write1("C:/Users/Peter/repo/libPSI/libPSI/MPSI/Grr18//output.txt", needed ); 48 | 49 | if(i < maxa, 50 | write1("C:/Users/Peter/repo/libPSI/libPSI/MPSI/Grr18/output.txt", ", "); 51 | ); 52 | ); 53 | 54 | 55 | 56 | write("C:/Users/Peter/repo/libPSI/libPSI/MPSI/Grr18//output.txt", "}},") 57 | } 58 | \\\\\\\\\\\\\\\\\\\\ 59 | \\\\\\\\\\\\\\\\\\\\ 60 | \\\\\\\\\\\\\\\\\\\\ 61 | allocatemem(); 62 | \\n = 2^20; 63 | \\m = n/4; 64 | 65 | lambda = 40; 66 | print("all calculations for n = ", n, "; m = ", m, "; lambda = ", lambda); 67 | build_bdist_table(n,m); \\ takes a long time 68 | 69 | \\eps=0.2; nicetable(lambda + log2(m)) 70 | eps=0.01; nicetable(lambda + log2(m)) 71 | \\eps=0.05; nicetable(lambda + log2(m)) 72 | \\eps=0.005; nicetable(lambda + log2(m)) 73 | \\eps=4; nicetable(lambda + log2(m)) 74 | -------------------------------------------------------------------------------- /libPSI/MPSI/Grr18/bayesian3.gp: -------------------------------------------------------------------------------- 1 | \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 2 | log2(x) = log(x)/log(2); 3 | \\ Pr[ Binom = k ] 4 | bdist_(n,k,p) = 0.0+binomial(n,k)*(p^k)*(1-p)^(n-k); 5 | build_bdist_table(n,m) = { 6 | bdist_tbl = vector(201); 7 | p = 1/m; 8 | for(k=0,200, 9 | bdist_tbl[k+1] = bdist_(n,k,p) 10 | ); 11 | } 12 | \\ Pr[ Binom = i ] 13 | bdist(k) = bdist_tbl[k+1] 14 | \\ Pr[ Binom = i ] 15 | bdistleq(k) = sum(i=0,k, bdist(i)) 16 | \\ Pr[ Lap <= d ] 17 | eps = 1 18 | lapleq(d) = if(d<0, exp(eps*d)/2, 1 - exp(-eps*d)/2) 19 | \\ Pr[ Binom <= k AND Binom + Lap <= d ] 20 | \\ Pr[ Binom <= k AND Lap <= d-Binom ] 21 | bothevents(k,d) = sum(B=0,k, bdist(B) * lapleq(d-B)) 22 | \\ Pr[ B <= k | B + Lap <= d ] 23 | \\ = Pr[ B <= k AND B + Lap <= d ] / Pr[ B + Lap <= d ] 24 | \\ computed as B<=200 AND ... 25 | conditional(k,d) = bothevents(k,d) / bothevents(200,d) 26 | posterior(d,lambda) = { 27 | k = 1; 28 | while( log2(1 - conditional(k,d)) > -lambda, 29 | k = k+1 30 | ); 31 | return(k-1); 32 | } 33 | nicetable(lambda) = { 34 | print("bayesian corrections for eps = ", eps); 35 | for(i=-10, 50, 36 | print(i, " => ", posterior(i,lambda)) 37 | ); 38 | } 39 | \\\\\\\\\\\\\\\\\\\\ 40 | \\\\\\\\\\\\\\\\\\\\ 41 | \\\\\\\\\\\\\\\\\\\\ 42 | allocatemem(); 43 | n = 2^20; 44 | m = n/4; 45 | lambda = 40; 46 | print("all calculations for n = ", n, "; m = ", m, "; lambda = ", lambda); 47 | build_bdist_table(n,m); \\ takes a long time 48 | eps=0.5; nicetable(lambda + log2(m)) 49 | eps=1; nicetable(lambda + log2(m)) 50 | eps=2; nicetable(lambda + log2(m)) 51 | eps=4; nicetable(lambda + log2(m)) 52 | -------------------------------------------------------------------------------- /libPSI/MPSI/Grr18/gp_history.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/osu-crypto/libPSI/1159df1fecad06570ec384d64ab0724d05b6905b/libPSI/MPSI/Grr18/gp_history.txt -------------------------------------------------------------------------------- /libPSI/MPSI/Grr18/output.txt: -------------------------------------------------------------------------------- 1 | Lookup{65536, 5461, 0.0050000000000000000000000000000000000000, -10, {49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49}}, 2 | Lookup{1048576, 87381, 0.0050000000000000000000000000000000000000, -10, {51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51}}, 3 | Lookup{1048576, 131072, 0.0050000000000000000000000000000000000000, -10, {41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41}}, 4 | Lookup{1048576, 104857, 0.0050000000000000000000000000000000000000, -10, {46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46}}, 5 | Lookup{1048576, 87381, 0.0050000000000000000000000000000000000000, -10, {51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51}}, 6 | Lookup{1048576, 74898, 0.0050000000000000000000000000000000000000, -10, {55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55}}, 7 | Lookup{65536, 5461, 0.010000000000000000000000000000000000000, -10, {48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49}}, 8 | Lookup{1048576, 87381, 0.010000000000000000000000000000000000000, -10, {50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50}}, 9 | -------------------------------------------------------------------------------- /libPSI/MPSI/Rr16/AknBfMPsiReceiver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "libPSI/config.h" 3 | #ifdef ENABLE_RR16_PSI 4 | 5 | #ifndef ENABLE_AKN 6 | #pragma error("ENABLE_AKN must be defined in libOTe") 7 | #endif 8 | 9 | #include "cryptoTools/Common/Defines.h" 10 | #include "cryptoTools/Common/Timer.h" 11 | #include "cryptoTools/Network/Channel.h" 12 | #include "libOTe/NChooseK/AknOtReceiver.h" 13 | 14 | 15 | namespace osuCrypto 16 | { 17 | 18 | void computeAknBfParams(u64 n, u64 statSecParam, u64& totalOtCount, u64& totalOnesCount, u64& cncOnesThreshold, double& cncProb, u64& numHashFunctions, u64& bfBitCount); 19 | 20 | 21 | class AknBfMPsiReceiver : public TimerAdapter 22 | { 23 | public: 24 | typedef u32 LogOtCount_t; 25 | 26 | 27 | AknBfMPsiReceiver(); 28 | ~AknBfMPsiReceiver(); 29 | 30 | AknOtReceiver mAknOt; 31 | //SHA1 mHash; 32 | u64 mMyInputSize, mTheirInputSize, mBfBitCount, mStatSecParam, mTotalOtCount, mNumHashFunctions; 33 | block mHashingSeed, mSeed; 34 | std::vector mIntersection; 35 | 36 | void init(u64 n, u64 statSecParam, OtExtReceiver& otExt, Channel& chl0, block seed); 37 | void init(u64 n, u64 statSecParam, OtExtReceiver& otExt, span chl0, block seed); 38 | void sendInput(std::vector& inputs, Channel& chl); 39 | void sendInput(std::vector& inputs, span chl0); 40 | }; 41 | 42 | } 43 | 44 | #endif -------------------------------------------------------------------------------- /libPSI/MPSI/Rr16/AknBfMPsiSender.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "libPSI/config.h" 3 | #ifdef ENABLE_RR16_PSI 4 | 5 | #ifndef ENABLE_AKN 6 | #pragma error("ENABLE_AKN must be defined in libOTe") 7 | #endif 8 | 9 | #include "cryptoTools/Common/Defines.h" 10 | #include "cryptoTools/Common/Timer.h" 11 | #include "cryptoTools/Network/Channel.h" 12 | #include "libOTe/NChooseK/AknOtSender.h" 13 | 14 | namespace osuCrypto { 15 | 16 | extern void computeAknBfParams(u64 n, u64 statSecParam, u64& totalOtCount, u64& totalOnesCount, u64& cncOnesThreshold, double& cncProb, u64& numHashFunctions, u64& bfBitCount); 17 | 18 | 19 | class AknBfMPsiSender : public TimerAdapter 20 | { 21 | public: 22 | typedef u32 LogOtCount_t; 23 | 24 | 25 | AknBfMPsiSender(); 26 | ~AknBfMPsiSender(); 27 | 28 | //void computeParameters(u64 n, u64 statSecParam, u64& totalOtCount, u64& cncOnesThreshold, double& cncProb, u64& numHashFunctions, u64& bfBitCount); 29 | 30 | u64 mN, mStatSecParam, mBfBitCount, mNumHashFunctions; 31 | AknOtSender mAknOt; 32 | block mHashingSeed, mSeed; 33 | 34 | void init(u64 n, u64 statSecParam, OtExtSender& otExt, Channel& chl, block seed); 35 | void init(u64 n, u64 statSecParam, OtExtSender& otExt, spanchl, block seed); 36 | 37 | 38 | void sendInput(std::vector& inputs, Channel& chl); 39 | void sendInput(std::vector& inputs, span chl); 40 | }; 41 | 42 | } 43 | #endif -------------------------------------------------------------------------------- /libPSI/MPSI/Rr17/Rr17MPsiDefines.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cryptoTools/Common/Defines.h" 3 | 4 | namespace osuCrypto 5 | { 6 | 7 | 8 | static const u64 stepSize(512); 9 | } -------------------------------------------------------------------------------- /libPSI/MPSI/Rr17/Rr17a/Rr17aMPsiReceiver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "libPSI/config.h" 3 | #ifdef ENABLE_RR17_PSI 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | namespace osuCrypto 12 | { 13 | 14 | class Rr17aMPsiReceiver : public TimerAdapter 15 | { 16 | public: 17 | Rr17aMPsiReceiver(); 18 | ~Rr17aMPsiReceiver(); 19 | 20 | //static const u64 CodeWordSize = 7; 21 | //static const u64 hasherStepSize; 22 | 23 | bool mHashToSmallerDomain; 24 | u64 mN, mStatSecParam;// , mOtMsgBlkSize; 25 | block mHashingSeed; 26 | std::vector mIntersection; 27 | 28 | 29 | std::vector> mOtSends; 30 | std::vector> mOtRecvs; 31 | 32 | SimpleHasher mBins; 33 | PRNG mPrng; 34 | 35 | void init(u64 n, u64 statSecParam, Channel& chl0, NcoOtExtReceiver& otRecv, NcoOtExtSender& otSend, block seed, 36 | double binScaler = 1.0, u64 inputBitSize = -1); 37 | void init(u64 n, u64 statSecParam, span chls, NcoOtExtReceiver& ots, NcoOtExtSender& otSend, block seed, 38 | double binScaler = 1.0, u64 inputBitSize = -1); 39 | 40 | void sendInput(std::vector& inputs, Channel& chl); 41 | void sendInput(std::vector& inputs, span chls); 42 | 43 | }; 44 | 45 | 46 | 47 | 48 | } 49 | #endif -------------------------------------------------------------------------------- /libPSI/MPSI/Rr17/Rr17a/Rr17aMPsiSender.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "libPSI/config.h" 3 | #ifdef ENABLE_RR17_PSI 4 | 5 | 6 | #include "cryptoTools/Common/Defines.h" 7 | #include "cryptoTools/Common/Timer.h" 8 | #include "cryptoTools/Network/Channel.h" 9 | #include "libOTe/NChooseOne/NcoOtExt.h" 10 | #include "libPSI/Tools/SimpleHasher.h" 11 | 12 | namespace osuCrypto 13 | { 14 | 15 | 16 | class Rr17aMPsiSender : public TimerAdapter 17 | { 18 | public: 19 | 20 | 21 | //static const u64 CodeWordSize = 7; 22 | //static const u64 hasherStepSize; 23 | 24 | Rr17aMPsiSender(); 25 | ~Rr17aMPsiSender(); 26 | 27 | bool mHashToSmallerDomain; 28 | u64 mN, mStatSecParam, mOtMsgBlkSize; 29 | block mHashingSeed; 30 | SimpleHasher mBins; 31 | PRNG mPrng; 32 | 33 | std::vector> mOtSends; 34 | std::vector> mOtRecvs; 35 | 36 | void init(u64 n, u64 statSecParam, 37 | span chls, 38 | NcoOtExtSender& ots, 39 | NcoOtExtReceiver& otRecv, 40 | block seed, 41 | double binScaler = 1.0, 42 | u64 inputBitSize = -1); 43 | 44 | void init(u64 n, u64 statSecParam, 45 | Channel & chl0, 46 | NcoOtExtSender& ots, 47 | NcoOtExtReceiver& otRecv, 48 | block seed, 49 | double binScaler = 1.0, 50 | u64 inputBitSize = -1); 51 | 52 | void sendInput(std::vector& inputs, Channel& chl); 53 | void sendInput(std::vector& inputs, span chls); 54 | 55 | }; 56 | 57 | } 58 | #endif -------------------------------------------------------------------------------- /libPSI/MPSI/Rr17/Rr17b/Rr17bMPsiReceiver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "libPSI/config.h" 3 | #ifdef ENABLE_RR17B_PSI 4 | 5 | #include "cryptoTools/Common/Defines.h" 6 | #include "cryptoTools/Common/Timer.h" 7 | #include "cryptoTools/Network/Channel.h" 8 | #include "libOTe/NChooseOne/NcoOtExt.h" 9 | #include "libPSI/Tools/SimpleHasher.h" 10 | 11 | namespace osuCrypto 12 | { 13 | 14 | class Rr17bMPsiReceiver : public TimerAdapter 15 | { 16 | public: 17 | Rr17bMPsiReceiver(); 18 | ~Rr17bMPsiReceiver(); 19 | 20 | bool mHashToSmallerDomain; 21 | u64 mN, mStatSecParam; 22 | block mHashingSeed; 23 | std::vector mIntersection; 24 | 25 | 26 | std::vector> mOtRecvs; 27 | 28 | SimpleHasher mBins; 29 | PRNG mPrng; 30 | 31 | void init(u64 n, u64 statSecParam, Channel& chl0, NcoOtExtReceiver& otRecv, block seed, 32 | double binScaler = 1.0, u64 inputBitSize = -1); 33 | void init(u64 n, u64 statSecParam, span chls, NcoOtExtReceiver& ots, block seed, 34 | double binScaler = 1.0, u64 inputBitSize = -1); 35 | 36 | void sendInput(std::vector& inputs, Channel& chl); 37 | void sendInput(std::vector& inputs, span chls); 38 | 39 | }; 40 | 41 | 42 | 43 | 44 | } 45 | #endif -------------------------------------------------------------------------------- /libPSI/MPSI/Rr17/Rr17b/Rr17bMPsiSender.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "libPSI/config.h" 3 | #ifdef ENABLE_RR17B_PSI 4 | 5 | #include "cryptoTools/Common/Defines.h" 6 | #include "cryptoTools/Common/Timer.h" 7 | #include "cryptoTools/Network/Channel.h" 8 | #include "libOTe/NChooseOne/NcoOtExt.h" 9 | #include "libPSI/Tools/SimpleHasher.h" 10 | 11 | namespace osuCrypto 12 | { 13 | 14 | 15 | class Rr17bMPsiSender : public TimerAdapter 16 | { 17 | public: 18 | 19 | 20 | //static const u64 CodeWordSize = 7; 21 | //static const u64 hasherStepSize; 22 | 23 | Rr17bMPsiSender(); 24 | ~Rr17bMPsiSender(); 25 | 26 | bool mHashToSmallerDomain; 27 | u64 mN, mStatSecParam; 28 | block mHashingSeed; 29 | SimpleHasher mBins; 30 | PRNG mPrng; 31 | 32 | std::vector> mOtSends; 33 | 34 | void init(u64 n, u64 statSecParam, 35 | span chls, 36 | NcoOtExtSender& ots, 37 | block seed, 38 | double binScaler = 1.0, 39 | u64 inputBitSize = -1); 40 | 41 | void init(u64 n, u64 statSecParam, 42 | Channel & chl0, 43 | NcoOtExtSender& ots, 44 | block seed, 45 | double binScaler = 1.0, 46 | u64 inputBitSize = -1); 47 | 48 | void sendInput(std::vector& inputs, Channel& chl); 49 | void sendInput(std::vector& inputs, span chls); 50 | 51 | }; 52 | 53 | } 54 | #endif -------------------------------------------------------------------------------- /libPSI/PIR/BgiPirClient.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "libPSI/config.h" 3 | #ifdef ENABLE_DRRN_PSI 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | namespace osuCrypto 10 | { 11 | class BgiPirClient 12 | { 13 | public: 14 | typedef boost::multiprecision::uint128_t uint128_t; 15 | 16 | static uint128_t bytesToUint128_t(const span& data); 17 | 18 | u64 mDatasetSize; 19 | u64 mKDepth, mGroupBlkSize; 20 | 21 | void init(u64 dataSetSize, u64 groupByteSize); 22 | block query(span idx, Channel srv0, Channel Srv1, block seed); 23 | block query(uint128_t idx, Channel srv0, Channel Srv1, block seed); 24 | 25 | static void keyGen(span idx, block seed, span k0, span g0, span k1, span g1); 26 | static void keyGen(uint128_t idx, block seed, span k0, span g0, span k1, span g1); 27 | }; 28 | 29 | } 30 | #endif -------------------------------------------------------------------------------- /libPSI/PIR/BgiPirServer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "libPSI/config.h" 4 | #ifdef ENABLE_DRRN_PSI 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | namespace osuCrypto 12 | { 13 | 14 | class BgiPirServer 15 | { 16 | public: 17 | typedef boost::multiprecision::uint128_t uint128_t; 18 | 19 | u64 mKDepth, mGroupBlkSize; 20 | 21 | void init(u64 depth, u64 groupByteSize); 22 | 23 | void serve(Channel chan, span data); 24 | 25 | static u8 evalOne(span idx, span k, span g, block* = nullptr, block* = nullptr, u8* tt = nullptr); 26 | static u8 evalOne(uint128_t idx, span k, span g, block* = nullptr, block* = nullptr, u8* tt = nullptr); 27 | static block traversePath(u64 depth, uint128_t idx, span k); 28 | static block traverseOne(const block &s, const block&k, const osuCrypto::u8 &keep, bool print = false); 29 | static block fullDomainNaive(span data, span k, span g); 30 | static block fullDomain(span data, span k, span g); 31 | //static BitVector BgiPirServer_bv; 32 | 33 | 34 | 35 | struct FullDomainGenerator 36 | { 37 | span k, g; 38 | u64 kDepth; 39 | std::vector prev, next; 40 | bool mHasMore; 41 | 42 | 43 | struct State 44 | { 45 | State() 46 | : kIdx(0) 47 | , d(0) 48 | {} 49 | 50 | u64 kIdx, d, dEnd; 51 | std::vector expandedS;// (8 * g.size()); 52 | std::vector>> mByteView; 53 | 54 | std::vector> ss; 55 | 56 | std::array aes; 57 | 58 | std::vector> temp, enc; 59 | std::vector> t_cw; 60 | 61 | }; 62 | 63 | State state; 64 | 65 | void init(span kk, span gg); 66 | 67 | 68 | 69 | u64 size(); 70 | bool hasMore() { return mHasMore; } 71 | span>> yeild(); 72 | }; 73 | 74 | struct MultiKey 75 | { 76 | void init(span> kk, span> gg); 77 | void init(u64 numKeys, u64 kSize, u64 gSize); 78 | void setKey(u64 i, spank, span g); 79 | void setKey(u64 i, spankg); 80 | void setKeys(MatrixViewkg); 81 | 82 | span yeild(); 83 | 84 | std::array aes; 85 | 86 | 87 | 88 | 89 | u64 mD, mLeafIdx, mGIdx, mBIdx, mNumKeysRound8, mDEnd; 90 | Matrix mBuff; 91 | Matrix mS, mK, mG; 92 | Matrix> mTcw; 93 | 94 | 95 | 96 | std::vector getK(u64 i); 97 | }; 98 | }; 99 | 100 | } 101 | #endif -------------------------------------------------------------------------------- /libPSI/PSI/Dcw/DcwRBfPsiReceiver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "libPSI/config.h" 3 | #ifdef ENABLE_DCW_PSI 4 | #include "cryptoTools/Common/Defines.h" 5 | #include "cryptoTools/Network/Channel.h" 6 | 7 | #include "cryptoTools/Common/BitVector.h" 8 | #include "libOTe/TwoChooseOne/OTExtInterface.h" 9 | namespace osuCrypto 10 | { 11 | 12 | 13 | 14 | class DcwRBfPsiReceiver 15 | { 16 | public: 17 | 18 | u64 mMyInputSize, mTheirInputSize, mBfBitCount, mNumHashFunctions; 19 | block mHashingSeed; 20 | std::vector mIntersection; 21 | std::vector mMessages; 22 | BitVector mRandChoices; 23 | block mSeed; 24 | 25 | void init(u64 n, u64 statSecParam, OtExtReceiver& otExt, Channel& chl0, block seed); 26 | void init(u64 n, u64 statSecParam, OtExtReceiver& otExt, span chl0, block seed); 27 | void sendInput(std::vector& inputs, Channel& chl); 28 | void sendInput(std::vector& inputs, span chl0); 29 | }; 30 | 31 | } 32 | 33 | 34 | #endif -------------------------------------------------------------------------------- /libPSI/PSI/Dcw/DcwRBfPsiSender.cpp: -------------------------------------------------------------------------------- 1 | #include "DcwRBfPsiSender.h" 2 | #ifdef ENABLE_DCW_PSI 3 | #include "libOTe/TwoChooseOne/KosOtExtSender.h" 4 | #include "cryptoTools/Crypto/PRNG.h" 5 | #include "cryptoTools/Crypto/Commit.h" 6 | #include "cryptoTools/Common/Log.h" 7 | //#include "cryptoTools/Crypto/ShamirSSScheme2.h" 8 | #include "libOTe/Base/BaseOT.h" 9 | #include "libOTe/TwoChooseOne/SilentOtExtSender.h" 10 | 11 | 12 | namespace osuCrypto { 13 | 14 | void DcwRBfPsiSender::init(u64 n, u64 statSecParam, OtExtSender& otExt, Channel& chl, block seed) 15 | { 16 | init(n, statSecParam, otExt, { &chl, 1 }, seed); 17 | } 18 | 19 | void DcwRBfPsiSender::init(u64 n, u64 statSecParam, OtExtSender& otExt, span chls, block seed) 20 | { 21 | 22 | gTimer.setTimePoint("init.start"); 23 | 24 | mN = n; 25 | PRNG prng(seed); 26 | mSeed = prng.get(); 27 | auto myHashSeed = prng.get(); 28 | auto& chl0 = chls[0]; 29 | 30 | mNumHashFunctions = 128; 31 | mBfBitCount = (u64)(n * mNumHashFunctions * 1.5); 32 | 33 | mSendOtMessages.resize(mBfBitCount); 34 | 35 | chl0.asyncSendCopy(myHashSeed); 36 | block theirHashingSeed; 37 | chl0.recv(theirHashingSeed); 38 | gTimer.setTimePoint("init.commitDone"); 39 | 40 | mHashSeed = myHashSeed ^ theirHashingSeed; 41 | 42 | //if (dynamic_cast(&otExt)) 43 | //{ 44 | // auto rBefore = chls[0].getTotalDataRecv(); 45 | // auto sBefore = chls[0].getTotalDataSent(); 46 | 47 | // //std::cout << "silent" << std::endl; 48 | // auto& ot = dynamic_cast(otExt); 49 | // ot.silentSend(mSendOtMessages, prng, chls); 50 | 51 | // char c; 52 | // chls[0].send(c); 53 | // chls[0].recv(c); 54 | 55 | // auto rAfter = chls[0].getTotalDataRecv(); 56 | // auto sAfter = chls[0].getTotalDataSent(); 57 | 58 | // std::cout << "1 before sent " << sBefore << std::endl; 59 | // std::cout << "1 before recv " << rBefore << std::endl; 60 | 61 | // std::cout << "1 after sent " << sAfter << std::endl; 62 | // std::cout << "1 after recv " << rAfter << std::endl; 63 | //} 64 | //else 65 | { 66 | 67 | if (otExt.hasBaseOts() == false) 68 | { 69 | otExt.genBaseOts(prng, chl0); 70 | } 71 | 72 | // this is a lambda function that does part of the OT extension where i am the sender. Again 73 | // malicious PSI does OTs in both directions. 74 | auto sendOtRountine = [this](u64 i, u64 total, OtExtSender& ots, block seed, Channel& chl) 75 | { 76 | // compute the region of the OTs im going to do 77 | u64 start = std::min(roundUpTo(i * mSendOtMessages.size() / total, 128), mSendOtMessages.size()); 78 | u64 end = std::min(roundUpTo((i + 1) * mSendOtMessages.size() / total, 128), mSendOtMessages.size()); 79 | 80 | //std::cout << IoStream::lock << "send Chl " << chl.getName() <<" "<< i << "/"<< total << " get " << start << " - " << end << std::endl << IoStream::unlock; 81 | 82 | if (end - start) 83 | { 84 | 85 | // get a view of where the messages should be stored. 86 | span> range( 87 | mSendOtMessages.begin() + start, 88 | mSendOtMessages.begin() + end); 89 | PRNG prng(seed); 90 | 91 | // do the extension. 92 | ots.send(range, prng, chl); 93 | } 94 | 95 | }; 96 | 97 | 98 | // compute how many threads we want to do for each direction. 99 | // the current thread will do one of the OT receives so -1 for that. 100 | u64 numSendThreads = chls.size() - 1; 101 | 102 | std::vector> sendOts(numSendThreads); 103 | 104 | // where we will store the threads that are doing the extension 105 | std::vector thrds(numSendThreads); 106 | 107 | // some iters to help giving out resources. 108 | auto thrdIter = thrds.begin(); 109 | auto chlIter = chls.begin() + 1; 110 | 111 | 112 | // do the same thing but for the send OT extensions 113 | for (u64 i = 0; i < numSendThreads; ++i) 114 | { 115 | auto seed = prng.get(); 116 | sendOts[i] = std::move(otExt.split()); 117 | 118 | *thrdIter++ = std::thread([&, i, chlIter]() 119 | { 120 | //std::cout << IoStream::lock << "r sendOt " << i << " " << (**chlIter).getName() << std::endl << IoStream::unlock; 121 | sendOtRountine(i + 1, numSendThreads + 1, *sendOts[i].get(), seed, *chlIter); 122 | }); 123 | 124 | ++chlIter; 125 | } 126 | 127 | seed = prng.get(); 128 | sendOtRountine(0, numSendThreads + 1, otExt, seed, chl0); 129 | 130 | 131 | gTimer.setTimePoint("init.OtExtDone"); 132 | 133 | for (auto& thrd : thrds) 134 | thrd.join(); 135 | } 136 | 137 | gTimer.setTimePoint("init.Done"); 138 | 139 | } 140 | 141 | 142 | void DcwRBfPsiSender::sendInput(std::vector& inputs, Channel & chl) 143 | { 144 | std::vector cc{ chl }; 145 | 146 | sendInput(inputs, cc); 147 | } 148 | 149 | void DcwRBfPsiSender::sendInput(std::vector& inputs, span chls) 150 | { 151 | 152 | if (inputs.size() != mN) 153 | throw std::runtime_error(LOCATION); 154 | 155 | gTimer.setTimePoint("online.start"); 156 | PRNG prng(mSeed); 157 | auto & chl = chls[0]; 158 | 159 | BitVector otCorrection(mBfBitCount); 160 | chl.recv(otCorrection); 161 | 162 | gTimer.setTimePoint("online.otCorrectionRecv"); 163 | 164 | auto start = 0; 165 | auto end = inputs.size(); 166 | 167 | PRNG hashSeedGen(mHashSeed); 168 | std::vector mHashs(mNumHashFunctions / 2); 169 | 170 | for (u64 i = 0; i < mHashs.size(); ++i) 171 | { 172 | mHashs[i].setKey(hashSeedGen.get()); 173 | } 174 | 175 | std::vector myMasks(inputs.size()); 176 | 177 | for (u64 i = start, k = 0; i < end; ++i, ++k) 178 | { 179 | myMasks[i] = ZeroBlock; 180 | 181 | for (u64 j = 0; j < mHashs.size(); ++j) 182 | { 183 | auto hashOut = mHashs[j].ecbEncBlock(inputs[i]) ^ inputs[i]; 184 | auto idx = hashOut.get(); 185 | idx[0] %= mBfBitCount; 186 | idx[1] %= mBfBitCount; 187 | 188 | myMasks[i] = myMasks[i] ^ mSendOtMessages[idx[0]][otCorrection[idx[0]] ^ 1]; 189 | myMasks[i] = myMasks[i] ^ mSendOtMessages[idx[1]][otCorrection[idx[1]] ^ 1]; 190 | } 191 | } 192 | 193 | chl.asyncSend(std::move(myMasks)); 194 | gTimer.setTimePoint("online.masksSent"); 195 | } 196 | } 197 | #endif 198 | -------------------------------------------------------------------------------- /libPSI/PSI/Dcw/DcwRBfPsiSender.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "libPSI/config.h" 3 | #ifdef ENABLE_DCW_PSI 4 | 5 | #include "cryptoTools/Common/Defines.h" 6 | #include "cryptoTools/Network/Channel.h" 7 | #include "cryptoTools/Crypto/RandomOracle.h" 8 | #include "libOTe/TwoChooseOne/OTExtInterface.h" 9 | 10 | namespace osuCrypto { 11 | 12 | 13 | 14 | class DcwRBfPsiSender 15 | { 16 | public: 17 | 18 | u64 mN, mBfBitCount, mNumHashFunctions; 19 | 20 | std::vector> mSendOtMessages; 21 | block mSeed, mHashSeed; 22 | 23 | void init(u64 n, u64 statSecParam, OtExtSender& otExt, Channel& chl, block seed); 24 | void init(u64 n, u64 statSecParam, OtExtSender& otExt, span chl, block seed); 25 | 26 | void sendInput(std::vector& inputs, Channel& chl); 27 | void sendInput(std::vector& inputs, span chl); 28 | }; 29 | 30 | } 31 | 32 | 33 | #endif -------------------------------------------------------------------------------- /libPSI/PSI/Drrn/DrrnPsiClient.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "libPSI/config.h" 4 | #ifdef ENABLE_DRRN_PSI 5 | 6 | #ifndef ENABLE_KKRT_PSI 7 | #pragma error("ENABLE_KKRT_PSI must be defined."); 8 | #endif 9 | 10 | #ifndef ENABLE_KKRT 11 | #pragma error("ENABLE_KKRT must be defined in libOTe."); 12 | #endif 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include "libPSI/Tools/CuckooIndex2.h" 22 | 23 | 24 | namespace osuCrypto 25 | { 26 | 27 | class DrrnPsiClient 28 | { 29 | public: 30 | 31 | void init(Channel s0, Channel s1, u64 serverSetSize, u64 clientSetSize, block seed, 32 | u64 numHash = 2, 33 | double binScaler = 1, 34 | u64 cuckooSsp = 20, 35 | u64 bigBlockSize = 8); 36 | 37 | void recv(Channel s0, Channel s1, span inputs); 38 | 39 | PRNG mPrng; 40 | old::CuckooParam mCuckooParams; 41 | 42 | //Matr 43 | //SimpleIndex mSimpleIndex; 44 | 45 | KkrtPsiReceiver mPsi; 46 | 47 | u64 mClientSetSize, mServerSetSize, mNumSimpleBins, mBinSize, mBigBlockSize; 48 | std::unordered_set mIntersection; 49 | KkrtNcoOtReceiver otRecv; 50 | block mHashingSeed; 51 | }; 52 | 53 | } 54 | #endif -------------------------------------------------------------------------------- /libPSI/PSI/Drrn/DrrnPsiServer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "libPSI/config.h" 4 | #ifdef ENABLE_DRRN_PSI 5 | 6 | #ifndef ENABLE_KKRT_PSI 7 | static_assert(0,"ENABLE_KKRT_PSI must be defined."); 8 | #endif 9 | 10 | #ifndef ENABLE_KKRT 11 | static_assert(0,"ENABLE_KKRT must be defined in libOTe."); 12 | #endif 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | namespace osuCrypto 22 | { 23 | 24 | class DrrnPsiServer 25 | { 26 | public: 27 | DrrnPsiServer() 28 | : mUseSingleDataPass(true) 29 | , mNiave(false) 30 | {} 31 | 32 | void init(u8 serverId, Channel chan, Channel srvChl, u64 databaseSize, u64 clientSetSize, block seed, double binScaler = 1, u64 bigBlockSize = 8); 33 | 34 | //void setCuckooParam(osuCrypto::u64 &serverSetSize, int ssp); 35 | 36 | void setInputs(span inputs, u64 numThreads = 2, u64 ssp = 20); 37 | 38 | void send(Channel clientChl, Channel srvChl, u64 numThreads = 1); 39 | 40 | std::vector mPiS1, mPi1SigmaRS; 41 | 42 | 43 | struct Item 44 | { 45 | block mVal; 46 | u32 mCuckooIdx; 47 | std::array _padding; 48 | }; 49 | static_assert(sizeof(Item) == 32, ""); 50 | 51 | span mCuckooData; 52 | std::unique_ptr mCuckooDataPtr; 53 | 54 | u64 find(u64 cuckooIdx); 55 | 56 | //CuckooParam mCuckooParams; 57 | CuckooIndex mIndex; 58 | 59 | bool mUseSingleDataPass, mNiave; 60 | KkrtNcoOtSender otSend; 61 | PRNG mPrng; 62 | KkrtPsiSender mPsi; 63 | 64 | u64 mClientSetSize, mServerSetSize; 65 | 66 | // The number of regions that the server's cuckoo table is divided into. 67 | u64 mNumSimpleBins; 68 | 69 | // The number of queries that are made to any given bin (cuckoo table resions). 70 | u64 mBinSize; 71 | 72 | // The number of cuckoo table items that any given DPF point corresponds to. 73 | u64 mBigBlockSize; 74 | 75 | 76 | u8 mServerId; 77 | block mHashingSeed; 78 | }; 79 | 80 | } 81 | #endif -------------------------------------------------------------------------------- /libPSI/PSI/ECDH/EcdhPsiReceiver.cpp: -------------------------------------------------------------------------------- 1 | #include "EcdhPsiReceiver.h" 2 | #ifdef ENABLE_ECDH_PSI 3 | 4 | #include "cryptoTools/Crypto/RCurve.h" 5 | #ifndef ENABLE_RELIC 6 | static_assert(0, "ENABLE_RELIC must be defined in libOTe"); 7 | #endif 8 | 9 | #include "cryptoTools/Crypto/RCurve.h" 10 | #include "cryptoTools/Common/Log.h" 11 | #include 12 | #include 13 | 14 | namespace osuCrypto 15 | { 16 | 17 | EcdhPsiReceiver::EcdhPsiReceiver() 18 | { 19 | } 20 | 21 | 22 | EcdhPsiReceiver::~EcdhPsiReceiver() 23 | { 24 | } 25 | void EcdhPsiReceiver::init(u64 n, u64 secParam, block seed) 26 | { 27 | mN = n; 28 | mSecParam = secParam; 29 | mPrng.SetSeed(seed); 30 | mIntersection.clear(); 31 | } 32 | 33 | 34 | void EcdhPsiReceiver::sendInput( 35 | span inputs, 36 | span chls) 37 | { 38 | //std::vector thrdPrng(chls.size()); 39 | //for (u64 i = 0; i < thrdPrng.size(); i++) 40 | // thrdPrng[i].SetSeed(mPrng.get()); 41 | 42 | std::mutex mtx; 43 | 44 | std::vector thrdPrngBlock(chls.size()); 45 | std::vector> localIntersections(chls.size() - 1); 46 | 47 | u64 maskSizeByte = u64(40 + 2*log2(inputs.size()) + 7) / 8; 48 | 49 | auto RcSeed = mPrng.get(); 50 | 51 | std::unordered_map mapXab; 52 | mapXab.reserve(inputs.size()); 53 | 54 | const bool isMultiThreaded = chls.size() > 1; 55 | 56 | auto routine = [&](u64 t) 57 | { 58 | u64 inputStartIdx = inputs.size() * t / chls.size(); 59 | u64 inputEndIdx = inputs.size() * (t + 1) / chls.size(); 60 | u64 subsetInputSize = inputEndIdx - inputStartIdx; 61 | 62 | 63 | auto& chl = chls[t]; 64 | //auto& prng = thrdPrng[t]; 65 | u8 hashOut[RandomOracle::HashSize]; 66 | using Curve = REllipticCurve; 67 | using Point = REccPoint; 68 | //using Brick = REccPoint; 69 | using Number = REccNumber; 70 | Curve curve; 71 | 72 | 73 | RandomOracle inputHasher; 74 | Number b(curve); 75 | Point yb(curve), yba(curve), point(curve), xa(curve), xab(curve); 76 | b.randomize(RcSeed); 77 | 78 | std::vector sendBuff(yb.sizeBytes() * subsetInputSize); 79 | auto sendIter = sendBuff.data(); 80 | 81 | std::vector recvBuff(xa.sizeBytes() * subsetInputSize); 82 | std::vector recvBuff2(xab.sizeBytes() * subsetInputSize); 83 | 84 | std::vectortemp(xab.sizeBytes()); 85 | 86 | // std::cout << "send H(y)^b" << std::endl; 87 | 88 | //send H(y)^b 89 | for (u64 i = inputStartIdx; i < inputEndIdx; ++i) 90 | { 91 | 92 | inputHasher.Reset(); 93 | inputHasher.Update(inputs[i]); 94 | inputHasher.Final(hashOut); 95 | 96 | point.randomize(toBlock(hashOut)); 97 | //std::cout << "sp " << point << " " << toBlock(hashOut) << std::endl; 98 | 99 | yb = (point * b); 100 | 101 | #ifdef PRINT 102 | if (i == 0) 103 | std::cout << "yb[" << i << "] " << yb << std::endl; 104 | #endif 105 | yb.toBytes(sendIter); 106 | sendIter += yb.sizeBytes(); 107 | } 108 | chl.asyncSend(std::move(sendBuff)); 109 | 110 | 111 | //recv H(x)^a 112 | //std::cout << "recv H(x)^a" << std::endl; 113 | 114 | chl.recv(recvBuff); 115 | if (recvBuff.size() != subsetInputSize * xa.sizeBytes()) 116 | { 117 | std::cout << "error @ " << (LOCATION) << std::endl; 118 | throw std::runtime_error(LOCATION); 119 | } 120 | auto recvIter = recvBuff.data(); 121 | 122 | //compute H(x)^a^b as map 123 | //std::cout << "compute H(x)^a^b " << std::endl; 124 | 125 | for (u64 i = inputStartIdx; i < inputEndIdx;i++) 126 | { 127 | xa.fromBytes(recvIter); recvIter += xa.sizeBytes(); 128 | xab = xa*b; 129 | 130 | xab.toBytes(temp.data()); 131 | 132 | RandomOracle ro(sizeof(block)); 133 | ro.Update(temp.data(), temp.size()); 134 | block blk; 135 | ro.Final(blk); 136 | auto idx = blk.get()[0]; 137 | 138 | #ifdef PRINT 139 | if (i == 0) 140 | { 141 | std::cout << "xab[" << i << "] " << xab << std::endl; 142 | std::cout << "idx[" << i << "] " << toBlock(idx) << std::endl; 143 | } 144 | #endif // PRINT 145 | 146 | 147 | if (isMultiThreaded) 148 | { 149 | std::lock_guard lock(mtx); 150 | mapXab.insert({ idx, blk }); 151 | } 152 | else 153 | { 154 | mapXab.insert({ idx, blk }); 155 | } 156 | } 157 | }; 158 | 159 | 160 | std::vector thrds(chls.size()); 161 | for (u64 i = 0; i < u64(chls.size()); ++i) 162 | { 163 | thrds[i] = std::thread([=] { 164 | routine(i); 165 | }); 166 | } 167 | 168 | 169 | for (auto& thrd : thrds) 170 | thrd.join(); 171 | 172 | #if 1 173 | auto routine2 = [&](u64 t) 174 | { 175 | u64 inputStartIdx = inputs.size() * t / chls.size(); 176 | u64 inputEndIdx = inputs.size() * (t + 1) / chls.size(); 177 | u64 subsetInputSize = inputEndIdx - inputStartIdx; 178 | 179 | 180 | auto& chl = chls[t]; 181 | 182 | 183 | std::vector recvBuff2(maskSizeByte * subsetInputSize); 184 | 185 | //recv H(y)^b^a 186 | chl.recv(recvBuff2); 187 | if (recvBuff2.size() != subsetInputSize * maskSizeByte) 188 | { 189 | std::cout << "error @ " << (LOCATION) << std::endl; 190 | throw std::runtime_error(LOCATION); 191 | } 192 | auto recvIter2 = recvBuff2.data(); 193 | 194 | for (u64 i = inputStartIdx; i < inputEndIdx; i++) 195 | { 196 | 197 | auto& idx_yba = *(u32*)(recvIter2); 198 | 199 | #ifdef PRINT 200 | if (i == 0) 201 | std::cout << "idx_yba[" << i << "] " << toBlock(idx_yba) << std::endl; 202 | #endif // PRINT 203 | 204 | auto id = mapXab.find(idx_yba); 205 | if (id != mapXab.end()) { 206 | 207 | //std::cout << "id->first[" << i << "] " << toBlock(id->first) << std::endl; 208 | 209 | if (memcmp(recvIter2, &id->second, maskSizeByte) == 0) 210 | { 211 | //std::cout << "intersection item----------" << i << std::endl; 212 | if (t == 0) 213 | mIntersection.emplace_back(i); 214 | else 215 | localIntersections[t - 1].emplace_back(i); 216 | } 217 | } 218 | recvIter2 += maskSizeByte; 219 | 220 | } 221 | //std::cout << "done" << std::endl; 222 | 223 | }; 224 | 225 | 226 | for (u64 i = 0; i < u64(chls.size()); ++i) 227 | { 228 | thrds[i] = std::thread([=] { 229 | routine2(i); 230 | }); 231 | } 232 | 233 | for (auto& thrd : thrds) 234 | thrd.join(); 235 | 236 | u64 extraSize = 0; 237 | 238 | for (u64 i = 0; i < thrds.size()-1; ++i) 239 | extraSize += localIntersections[i].size(); 240 | 241 | mIntersection.reserve(mIntersection.size() + extraSize); 242 | for (u64 i = 0; i < thrds.size()-1; ++i) 243 | { 244 | mIntersection.insert(mIntersection.end(), localIntersections[i].begin(), localIntersections[i].end()); 245 | } 246 | #endif 247 | 248 | 249 | } 250 | 251 | } 252 | #endif -------------------------------------------------------------------------------- /libPSI/PSI/ECDH/EcdhPsiReceiver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "libPSI/config.h" 4 | #ifdef ENABLE_ECDH_PSI 5 | 6 | #include "cryptoTools/Common/Defines.h" 7 | #include "cryptoTools/Network/Channel.h" 8 | #include "cryptoTools/Crypto/PRNG.h" 9 | 10 | namespace osuCrypto 11 | { 12 | 13 | class EcdhPsiReceiver 14 | { 15 | public: 16 | EcdhPsiReceiver(); 17 | ~EcdhPsiReceiver(); 18 | 19 | u64 mN, mSecParam; 20 | PRNG mPrng; 21 | 22 | std::vector mIntersection; 23 | 24 | void init(u64 n, u64 secParam, block seed); 25 | 26 | void sendInput(span inputs, span chl0); 27 | 28 | }; 29 | 30 | } 31 | 32 | #endif -------------------------------------------------------------------------------- /libPSI/PSI/ECDH/EcdhPsiSender.cpp: -------------------------------------------------------------------------------- 1 | #include "EcdhPsiSender.h" 2 | #ifdef ENABLE_ECDH_PSI 3 | 4 | #include "cryptoTools/Crypto/RCurve.h" 5 | #ifndef ENABLE_RELIC 6 | static_assert(0, "ENABLE_RELIC must be defined in libOTe"); 7 | #endif 8 | 9 | #include "cryptoTools/Crypto/RCurve.h" 10 | #include "cryptoTools/Crypto/RandomOracle.h" 11 | #include "cryptoTools/Common/Log.h" 12 | #include "cryptoTools/Network/Channel.h" 13 | 14 | 15 | namespace osuCrypto 16 | { 17 | 18 | 19 | EcdhPsiSender::EcdhPsiSender() 20 | { 21 | } 22 | 23 | 24 | EcdhPsiSender::~EcdhPsiSender() 25 | { 26 | } 27 | void EcdhPsiSender::init(u64 n, u64 secParam, block seed) 28 | { 29 | mN = n; 30 | mSecParam = secParam; 31 | mPrng.SetSeed(seed); 32 | } 33 | 34 | 35 | void EcdhPsiSender::sendInput(std::vector& inputs, span chls) 36 | { 37 | 38 | 39 | //u64 theirInputSize = inputs.size(); 40 | 41 | u64 maskSizeByte = u64(40 + 2*log2(inputs.size())+7) / 8; 42 | 43 | //std::vector thrdPrng(chls.size()); 44 | //for (u64 i = 0; i < thrdPrng.size(); i++) 45 | // thrdPrng[i].SetSeed(mPrng.get()); 46 | 47 | auto RsSeed = mPrng.get(); 48 | 49 | std::vector> sendBuff2(chls.size()); 50 | 51 | auto routine = [&](u64 t) 52 | { 53 | u64 inputStartIdx = inputs.size() * t / chls.size(); 54 | u64 inputEndIdx = inputs.size() * (t + 1) / chls.size(); 55 | u64 subsetInputSize = inputEndIdx - inputStartIdx; 56 | 57 | 58 | auto& chl = chls[t]; 59 | //auto& prng = thrdPrng[t]; 60 | 61 | using Curve = REllipticCurve; 62 | using Point = REccPoint; 63 | //using Brick = REccPoint; 64 | using Number = REccNumber; 65 | Curve curve; 66 | 67 | 68 | 69 | RandomOracle inputHasher(sizeof(block)); 70 | Number a(curve); 71 | Point xa(curve), point(curve), yb(curve), yba(curve); 72 | a.randomize(RsSeed); 73 | 74 | std::vector sendBuff(xa.sizeBytes() * subsetInputSize); 75 | auto sendIter = sendBuff.data(); 76 | sendBuff2[t].resize(maskSizeByte * subsetInputSize); 77 | auto sendIter2 = sendBuff2[t].data(); 78 | 79 | std::vector recvBuff(yb.sizeBytes() * subsetInputSize); 80 | std::vector temp(yba.sizeBytes()); 81 | 82 | //send H(x)^a 83 | for (u64 i = inputStartIdx ; i < inputEndIdx; ++i) 84 | { 85 | block seed; 86 | inputHasher.Reset(); 87 | inputHasher.Update(inputs[i]); 88 | inputHasher.Final(seed); 89 | 90 | point.randomize(seed); 91 | //std::cout << "sp " << point << " " << toBlock(hashOut) << std::endl; 92 | 93 | xa = (point * a); 94 | #ifdef PRINT 95 | if (i == 0) 96 | std::cout << "xa[" << i << "] " << xa << std::endl; 97 | #endif 98 | xa.toBytes(sendIter); 99 | sendIter += xa.sizeBytes(); 100 | } 101 | chl.asyncSend(std::move(sendBuff)); 102 | 103 | 104 | //recv H(y)^b 105 | chl.recv(recvBuff); 106 | if (recvBuff.size() != subsetInputSize * yb.sizeBytes()) 107 | { 108 | std::cout << "error @ " << (LOCATION) << std::endl; 109 | throw std::runtime_error(LOCATION); 110 | } 111 | auto recvIter = recvBuff.data(); 112 | 113 | //send H(y)^b^a 114 | for (u64 i = inputStartIdx; i < inputEndIdx;i++) 115 | { 116 | yb.fromBytes(recvIter); recvIter += yb.sizeBytes(); 117 | yba = yb*a; 118 | 119 | 120 | yba.toBytes(temp.data()); 121 | RandomOracle ro(sizeof(block)); 122 | ro.Update(temp.data(), temp.size()); 123 | block blk; 124 | ro.Final(blk); 125 | memcpy(sendIter2, &blk, maskSizeByte); 126 | #ifdef PRINT 127 | if (i == 0) 128 | { 129 | std::cout << "yba[" << i << "] " << yba << std::endl; 130 | std::cout << "temp[" << i << "] " << toBlock(temp) << std::endl; 131 | std::cout << "sendIter2[" << i << "] " << toBlock(sendIter2) << std::endl; 132 | } 133 | #endif 134 | sendIter2 += maskSizeByte; 135 | } 136 | //std::cout << "dones send H(y)^b^a" << std::endl; 137 | 138 | }; 139 | 140 | 141 | std::vector thrds(chls.size()); 142 | for (u64 i = 0; i < u64(chls.size()); ++i) 143 | { 144 | thrds[i] = std::thread([=] { 145 | routine(i); 146 | }); 147 | } 148 | 149 | 150 | for (auto& thrd : thrds) 151 | thrd.join(); 152 | 153 | for (u64 i = 0; i < u64(chls.size()); ++i) 154 | { 155 | thrds[i] = std::thread([=] { 156 | auto& chl = chls[i]; 157 | chl.asyncSend(std::move(sendBuff2[i])); 158 | }); 159 | } 160 | 161 | 162 | for (auto& thrd : thrds) 163 | thrd.join(); 164 | 165 | //std::cout << "S done" << std::endl; 166 | 167 | } 168 | } 169 | 170 | #endif -------------------------------------------------------------------------------- /libPSI/PSI/ECDH/EcdhPsiSender.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "libPSI/config.h" 3 | #ifdef ENABLE_ECDH_PSI 4 | 5 | 6 | #include "cryptoTools/Common/Defines.h" 7 | 8 | #include "cryptoTools/Network/Channel.h" 9 | #include "cryptoTools/Crypto/PRNG.h" 10 | 11 | 12 | 13 | namespace osuCrypto 14 | { 15 | class EcdhPsiSender 16 | { 17 | public: 18 | EcdhPsiSender(); 19 | ~EcdhPsiSender(); 20 | 21 | 22 | u64 mN, mSecParam; 23 | PRNG mPrng; 24 | 25 | void init(u64 n, u64 secParam, block seed); 26 | //void init(u64 n, u64 statSecParam); 27 | 28 | 29 | void sendInput(std::vector& inputs, span chl); 30 | //void sendInput(std::vector& inputs, std::vector& chl); 31 | }; 32 | 33 | } 34 | 35 | #endif -------------------------------------------------------------------------------- /libPSI/PSI/Kkrt/KkrtPsiReceiver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "libPSI/config.h" 4 | #ifdef ENABLE_KKRT_PSI 5 | 6 | #include "cryptoTools/Common/Defines.h" 7 | #include "cryptoTools/Common/Timer.h" 8 | #include "cryptoTools/Network/Channel.h" 9 | #include "libOTe/NChooseOne/NcoOtExt.h" 10 | #include "libPSI/Tools/CuckooHasher.h" 11 | #include "libPSI/Tools/CuckooIndex2.h" 12 | 13 | namespace osuCrypto 14 | { 15 | 16 | class KkrtPsiReceiver : public TimerAdapter 17 | { 18 | public: 19 | KkrtPsiReceiver(); 20 | ~KkrtPsiReceiver(); 21 | 22 | u64 mRecverSize,mSenderSize,mStatSecParam; 23 | std::vector mIntersection; 24 | old::CuckooIndex mIndex; 25 | 26 | NcoOtExtReceiver * mOtRecv; 27 | 28 | block mHashingSeed; 29 | 30 | void init(u64 senderSize, u64 recverSize, u64 statSecParam, Channel chl0, NcoOtExtReceiver& otRecv, block seed); 31 | void init(u64 senderSize, u64 recverSize, u64 statSecParam, span chls, NcoOtExtReceiver& otRecv, block seed); 32 | void sendInput(span inputs, Channel& chl); 33 | void sendInput(span inputs, span chls); 34 | 35 | }; 36 | 37 | 38 | 39 | 40 | } 41 | #endif -------------------------------------------------------------------------------- /libPSI/PSI/Kkrt/KkrtPsiSender.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "libPSI/config.h" 4 | #ifdef ENABLE_KKRT_PSI 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include "cryptoTools/Crypto/PRNG.h" 11 | #include "libPSI/Tools/CuckooIndex2.h" 12 | 13 | namespace osuCrypto 14 | { 15 | 16 | 17 | class KkrtPsiSender : public TimerAdapter 18 | { 19 | public: 20 | KkrtPsiSender(); 21 | ~KkrtPsiSender(); 22 | 23 | u64 mSenderSize, mRecverSize, mStatSecParam; 24 | PRNG mPrng; 25 | std::vector mPermute, mPermuteInv; 26 | 27 | //SimpleIndex mIndex; 28 | old::CuckooParam mParams; 29 | block mHashingSeed; 30 | 31 | NcoOtExtSender* mOtSender; 32 | 33 | void init(u64 senderSize, u64 recverSize, u64 statSecParam, span chls, NcoOtExtSender& otSender, block seed); 34 | void init(u64 senderSize, u64 recverSize, u64 statSecParam, Channel & chl0, NcoOtExtSender& otSender, block seed); 35 | 36 | void sendInput(span inputs, Channel& chl); 37 | void sendInput(span inputs, span chls); 38 | 39 | 40 | }; 41 | 42 | } 43 | #endif -------------------------------------------------------------------------------- /libPSI/PSI/Prty/Poly/polyFFT.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // This file and the associated implementation has been placed in the public domain, waiving all copyright. No restrictions are placed on its use. 3 | #include "libPSI/config.h" 4 | #ifdef ENABLE_PRTY_PSI 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | //#include 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | using namespace std; 22 | using namespace NTL; 23 | using namespace chrono; 24 | 25 | 26 | namespace osuCrypto 27 | { 28 | 29 | 30 | void print_poly(ZZ_pX& P); 31 | void build_tree_main(ZZ_pX* tree, ZZ_p* points, unsigned int tree_size); 32 | void evaluate_main(ZZ_pX& P, ZZ_pX* tree, ZZ_pX* reminders, unsigned int tree_size, ZZ_p* results); 33 | void test_evaluate(ZZ_pX& P, ZZ_p* points, ZZ_p* results, unsigned int npoints); 34 | void iterative_interpolate_zp_main(ZZ_pX& resultP, ZZ_pX* temp, ZZ_p* y, ZZ_p* a, ZZ_pX* M, unsigned int tree_size); 35 | void test_interpolation_result_zp(ZZ_pX& P, ZZ_p* x, ZZ_p* y, long degree); 36 | 37 | void test_multipoint_eval_zp(ZZ prime, long degree, int numThreads); 38 | void test_interpolate_zp(ZZ prime, long degree, int numThreads); 39 | 40 | void multipoint_evaluate_zp(ZZ_pX& P, ZZ_p* x, ZZ_p* y, long degree, int numThreads, ZZ &prime); 41 | void interpolate_zp(ZZ_pX& resultP, ZZ_p* x, ZZ_p* y, long degree, int numThreads, ZZ &prime); 42 | 43 | void build_tree(ZZ_pX* tree, ZZ_p* points, unsigned int tree_size, int numThreads, ZZ &prime); 44 | void prepareForInterpolate(ZZ_p *x, long degree, ZZ_pX *M, ZZ_p *a, int numThreads, ZZ &prime); 45 | void iterative_interpolate_zp(ZZ_pX& resultP, ZZ_pX* temp, ZZ_p* y, ZZ_p* a, ZZ_pX* M, unsigned int tree_size, int numThreads, ZZ &prime); 46 | void evaluate(ZZ_pX& P, ZZ_pX* tree, ZZ_pX* reminders, unsigned int tree_size, ZZ_p* results, int numThreads, ZZ &prime); 47 | void test_tree(ZZ_pX& final_polynomial, ZZ_p* points, unsigned int npoints); 48 | 49 | 50 | //helper functions for threads 51 | void generateSubTreeArrays(vector> &subArrays, int totalNodes, int firstIndex); 52 | void buildSubTree(ZZ_pX* tree, vector &subTree, ZZ &prime); 53 | void interSubTree(ZZ_pX* temp, ZZ_pX* M, vector &subTree, ZZ &prime); 54 | void evalSubTree(ZZ_pX* reminders, ZZ_pX* tree, vector &subTree, ZZ &prime); 55 | void evalReminder(ZZ_pX *tree, ZZ_pX *reminders, int i, ZZ &prime); 56 | void interSpecific(ZZ_pX *temp, ZZ_pX *M, int i, ZZ &prime); 57 | void buildTreeSpecific(ZZ_pX *tree, int i, ZZ &prime); 58 | 59 | 60 | void BytesToZZ_px(unsigned char *bytesArr, ZZ_pX& poly, long numOfElements, long sizeOfElement); 61 | void ZZ_pxToBytes(ZZ_pX& poly, unsigned char *bytesArr, long numOfElements, long sizeOfElement); 62 | 63 | } 64 | #endif -------------------------------------------------------------------------------- /libPSI/PSI/Prty/Poly/polyFFT2.cpp: -------------------------------------------------------------------------------- 1 | #include "libPSI/config.h" 2 | #ifdef ENABLE_PRTY_PSI 3 | #include "polyFFT2.h" 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | namespace osuCrypto 12 | { 13 | #define LEFT(X) (2*X+1) 14 | #define RIGHT(X) (2*X+2) 15 | #define PAPA(X) ((X-1)/2) 16 | 17 | 18 | void polyFFT2::init(ZZ &prime, u64 numThreads) { 19 | mPrime = prime; 20 | mNumThreads = numThreads; 21 | } 22 | 23 | void build_tree_1thread(ZZ_pX* tree, ZZ_p* points) { 24 | 25 | } 26 | 27 | 28 | void polyFFT2::build_tree(ZZ_pX* tree, ZZ_p* points) { 29 | 30 | } 31 | 32 | } 33 | #endif -------------------------------------------------------------------------------- /libPSI/PSI/Prty/Poly/polyFFT2.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // This file and the associated implementation has been placed in the public domain, waiving all copyright. No restrictions are placed on its use. 3 | #include "libPSI/config.h" 4 | #ifdef ENABLE_PRTY_PSI 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | //#include 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | 22 | 23 | namespace osuCrypto 24 | { 25 | using ZZ_pX = NTL::ZZ_pX; 26 | using ZZ = NTL::ZZ; 27 | using ZZ_p = NTL::ZZ_p; 28 | 29 | class polyFFT2 : public TimerAdapter 30 | { 31 | public: 32 | struct node 33 | { 34 | ZZ_pX data; 35 | struct node* left; 36 | struct node* right; 37 | }; 38 | 39 | ZZ mPrime; 40 | u64 mNumThreads; 41 | void init(ZZ &prime, u64 numThreads); 42 | void build_tree(ZZ_pX* tree, ZZ_p* points); 43 | 44 | }; 45 | 46 | } 47 | #endif -------------------------------------------------------------------------------- /libPSI/PSI/Prty/Poly/polyNTL.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // This file and the associated implementation has been placed in the public domain, waiving all copyright. No restrictions are placed on its use. 3 | #include "libPSI/config.h" 4 | #ifdef ENABLE_PRTY_PSI 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "NTL/GF2EX.h" 13 | #include "NTL/GF2XFactoring.h" 14 | #include 15 | #include "NTL/GF2EX.h" 16 | #include 17 | #include 18 | #include "NTL/GF2EX.h" 19 | #include "NTL/ZZ_p.h" 20 | #include "NTL/GF2EX.h" 21 | #include "NTL/GF2XFactoring.h" 22 | #include "libPSI/PSI/Prty/PrtyDefines.h" 23 | 24 | namespace osuCrypto 25 | { 26 | 27 | class polyNTL : public TimerAdapter 28 | { 29 | public: 30 | 31 | NTL::GF2X mGf2x; 32 | u64 mNumBytes; 33 | 34 | void NtlPolyInit(u64 numBytes); 35 | void GF2EFromBlock(NTL::GF2E &element, block& blk, u64 size); 36 | void GF2EFromBlocks(NTL::GF2E &element, block* blks, u64 size); 37 | 38 | void BlockFromGF2E(block& blk, NTL::GF2E & element, u64 size); 39 | 40 | void getBlkCoefficients(NTL::vec_GF2E& vecX, NTL::vec_GF2E& vecY, std::vector& coeffs); 41 | void getBlkCoefficients(u64 degree, std::vector& setX, std::vector& setY, std::vector& coeffs); 42 | void getSuperBlksCoefficients(u64 degree, std::vector& setX 43 | , std::vector>& setY 44 | , std::vector>& coeffs); 45 | 46 | void evalPolynomial(std::vector& coeffs, block& x, block& y); 47 | void evalPolynomial(std::vector& coeffs, std::vector& setX, std::vector& setY); 48 | void evalPolynomial(std::vector& coeffs, NTL::vec_GF2E& vecX, std::vector& setY); 49 | void evalSuperPolynomial(std::vector>& coeffs 50 | , std::vector& setX, std::vector>& setY); 51 | }; 52 | 53 | } 54 | #endif -------------------------------------------------------------------------------- /libPSI/PSI/Prty/PrtyDefines.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "libPSI/config.h" 3 | #ifdef ENABLE_PRTY_PSI 4 | 5 | #include 6 | #include 7 | #define NTL_Threads 8 | #define DEBUG 9 | #include "PrtyDefines.h" 10 | #include 11 | #include 12 | #include 13 | #include 14 | //using namespace NTL; 15 | #define NTL_Threads_ON 16 | 17 | 18 | 19 | namespace osuCrypto 20 | { 21 | using ZZ =NTL::ZZ; 22 | 23 | static const u64 stepSize(1 << 2); 24 | static const u64 stepSizeMaskSent(1 << 14); 25 | static const u8 numSuperBlocks(4); //wide of T (or field size) 26 | static const u8 first2Slices(2); //2*128 + (436-2*128) 27 | static const u64 recvNumDummies(1); 28 | static const u64 recvMaxBinSize(40); 29 | static std::vector mOneBlocks(128); 30 | static const u64 primeLong(129); 31 | static const u64 fieldSize(440); //TODO 4*sizeof(block) 32 | 33 | static const u64 bIdxForDebug(3), iIdxForDebug(0), hIdxForDebug(0); 34 | 35 | static const ZZ mPrime128 = NTL::to_ZZ("340282366920938463463374607431768211507"); 36 | static const ZZ mPrime160 = NTL::to_ZZ("1461501637330902918203684832716283019655932542983"); //nextprime(2^160) 37 | static const ZZ mPrime164 = NTL::to_ZZ("23384026197294446691258957323460528314494920687733"); //nextprime(2^164) 38 | static const ZZ mPrime168 = NTL::to_ZZ("374144419156711147060143317175368453031918731001943"); //nextprime(2^168) 39 | static const ZZ mPrime172 = NTL::to_ZZ("5986310706507378352962293074805895248510699696029801"); //nextprime(2^172) 40 | static const ZZ mPrime176 = NTL::to_ZZ("95780971304118053647396689196894323976171195136475563"); //nextprime(2^176) 41 | static const ZZ mPrime180 = NTL::to_ZZ("1532495540865888858358347027150309183618739122183602191"); //nextprime(2^180) 42 | static const ZZ mPrime184 = NTL::to_ZZ("24519928653854221733733552434404946937899825954937634843"); //nextprime(2^184) 43 | static const ZZ mPrime188 = NTL::to_ZZ("392318858461667547739736838950479151006397215279002157113"); //nextprime(2^188) 44 | 45 | inline u64 getFieldSizeInBits(u64 setSize) 46 | { 47 | 48 | if (setSize <= (1 << 10)) 49 | return 416; 50 | else if (setSize <= (1 << 12)) 51 | return 420; 52 | else if (setSize <= (1 << 14)) 53 | return 424; 54 | else if (setSize <= (1 << 16)) 55 | return 428; 56 | else if (setSize <= (1 << 18)) 57 | return 432; 58 | else if (setSize <= (1 << 20)) 59 | return 436; 60 | else if (setSize <= (1 << 22)) 61 | return 436; 62 | else if (setSize <= (1 << 24)) 63 | return 444; 64 | 65 | return 444; 66 | } 67 | 68 | 69 | inline ZZ getPrimeLastSlice(u64 fieldSize) 70 | { 71 | u64 lastBit = fieldSize - 2 * 128; 72 | if (lastBit == 160) 73 | return mPrime160; 74 | else if (lastBit == 164) 75 | return mPrime164; 76 | else if (lastBit == 168) 77 | return mPrime168; 78 | else if (lastBit == 172) 79 | return mPrime172; 80 | else if (lastBit == 176) 81 | return mPrime176; 82 | else if (lastBit == 180) 83 | return mPrime180; 84 | else if (lastBit == 184) 85 | return mPrime184; 86 | else if (lastBit == 188) 87 | return mPrime188; 88 | 89 | return mPrime188; 90 | } 91 | 92 | 93 | 94 | inline __m128i mm_bitshift_right(__m128i x, unsigned count) 95 | { 96 | __m128i carry = _mm_slli_si128(x, 8); // old compilers only have the confusingly named _mm_slli_si128 synonym 97 | if (count >= 64) 98 | return _mm_slli_epi64(carry, count - 64); // the non-carry part is all zero, so return early 99 | // else 100 | return _mm_or_si128(_mm_slli_epi64(x, count), _mm_srli_epi64(carry, 64 - count)); 101 | 102 | } 103 | 104 | 105 | inline __m128i mm_bitshift_left(__m128i x, unsigned count) 106 | { 107 | __m128i carry = _mm_srli_si128(x, 8); // old compilers only have the confusingly named _mm_slli_si128 synonym 108 | if (count >= 64) 109 | return _mm_srli_epi64(carry, count - 64); // the non-carry part is all zero, so return early 110 | 111 | return _mm_or_si128(_mm_srli_epi64(x, count), _mm_slli_epi64(carry, 64 - count)); 112 | } 113 | 114 | inline void fillOneBlock(std::vector& blks) 115 | { 116 | for (int i = 0; i < blks.size(); ++i) 117 | blks[i] = mm_bitshift_right(OneBlock, i); 118 | } 119 | 120 | inline void prfOtRows(span inputs, std::vector>& outputs, std::vector& arrAes) 121 | { 122 | std::vector ciphers(inputs.size()); 123 | outputs.resize(inputs.size()); 124 | 125 | for (int j = 0; j < numSuperBlocks - 1; ++j) //1st 3 blocks 126 | for (int i = 0; i < 128; ++i) //for each column 127 | { 128 | arrAes[j * 128 + i].ecbEncBlocks(inputs.data(), inputs.size(), ciphers.data()); //do many aes at the same time for efficeincy 129 | 130 | for (u64 idx = 0; idx < inputs.size(); idx++) 131 | { 132 | ciphers[idx] = ciphers[idx] & mOneBlocks[i]; 133 | outputs[idx][j] = outputs[idx][j] ^ ciphers[idx]; 134 | } 135 | } 136 | 137 | 138 | int j = numSuperBlocks - 1; 139 | for (int i = j * 128; i < arrAes.size(); ++i) 140 | { 141 | arrAes[i].ecbEncBlocks(inputs.data(), inputs.size(), ciphers.data()); //do many aes at the same time for efficeincy 142 | for (u64 idx = 0; idx < inputs.size(); idx++) 143 | { 144 | ciphers[idx] = ciphers[idx] & mOneBlocks[i - j * 128]; 145 | outputs[idx][j] = outputs[idx][j] ^ ciphers[idx]; 146 | } 147 | 148 | } 149 | 150 | } 151 | 152 | inline void prfOtRow(block& input, std::array& output, std::vector arrAes, u64 hIdx = 0) 153 | { 154 | block cipher; 155 | 156 | for (int j = 0; j < numSuperBlocks - 1; ++j) //1st 3 blocks 157 | for (int i = 0; i < 128; ++i) //for each column 158 | { 159 | if (hIdx == 1) 160 | arrAes[j * 128 + i].ecbEncBlock(input ^ OneBlock, cipher); 161 | else 162 | arrAes[j * 128 + i].ecbEncBlock(input, cipher); 163 | 164 | cipher = cipher & mOneBlocks[i]; 165 | output[j] = output[j] ^ cipher; 166 | } 167 | 168 | 169 | int j = numSuperBlocks - 1; 170 | for (int i = 0; i < 128; ++i) 171 | { 172 | if (j * 128 + i < arrAes.size()) { 173 | 174 | if (hIdx == 1) 175 | arrAes[j * 128 + i].ecbEncBlock(input ^ OneBlock, cipher); 176 | else 177 | arrAes[j * 128 + i].ecbEncBlock(input, cipher); 178 | 179 | cipher = cipher & mOneBlocks[i]; 180 | output[j] = output[j] ^ cipher; 181 | } 182 | else { 183 | break; 184 | } 185 | } 186 | 187 | //std::cout << IoStream::lock; 188 | //std::cout << "\t output " << output[0] << "\n"; 189 | //std::cout << IoStream::unlock; 190 | 191 | } 192 | 193 | } 194 | #endif -------------------------------------------------------------------------------- /libPSI/PSI/Prty/PrtyReceiver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // This file and the associated implementation has been placed in the public domain, waiving all copyright. No restrictions are placed on its use. 3 | #include "libPSI/config.h" 4 | #ifdef ENABLE_PRTY_PSI 5 | 6 | #include 7 | #include 8 | #include 9 | #include "libOTe/NChooseOne/Kkrt/KkrtNcoOtSender.h" 10 | #include "libOTe/NChooseOne/Kkrt/KkrtNcoOtReceiver.h" 11 | #include "libOTe/TwoChooseOne/IknpOtExtReceiver.h" 12 | #include "Poly/polyNTL.h" 13 | #include "PrtyDefines.h" 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include "libPSI/Tools/BalancedIndex.h" 19 | 20 | using namespace NTL; 21 | 22 | namespace osuCrypto 23 | { 24 | 25 | class PrtyReceiver : public TimerAdapter 26 | { 27 | public: 28 | 29 | 30 | bool mHasBase; 31 | BalancedIndex mBalance; 32 | 33 | u64 mMyInputSize, mTheirInputSize, mPolyNumBytes, mPolyDegree, mPsiSecParam; 34 | std::vector mS; 35 | KkrtNcoOtSender sendOprf; 36 | KkrtNcoOtReceiver recvOprf; 37 | u64 mFieldSize; 38 | 39 | block mTruncateBlk; 40 | 41 | PRNG mPrng; 42 | ZZ mPrime; 43 | ZZ mPrimeLastSlice; 44 | 45 | ////std::vector> mOtKeys; 46 | std::vector mAesT; 47 | std::vector mAesU; 48 | std::vector mIntersection; //index 49 | 50 | block recvMaskForDebug; 51 | //AES mAesHasher; 52 | 53 | std::array subRowTForDebug; 54 | std::array subRowUForDebug; 55 | 56 | std::vector Outputs; 57 | 58 | void init(u64 myInputSize, u64 theirInputSize, u64 psiSecParam, PRNG& prng, span chls); 59 | void output(span inputs, span chls); 60 | void outputBestComm(span inputs, span chls); 61 | void outputBigPoly(span inputs, span chls); 62 | 63 | 64 | }; 65 | 66 | } 67 | #endif -------------------------------------------------------------------------------- /libPSI/PSI/Prty/PrtySender.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // This file and the associated implementation has been placed in the public domain, waiving all copyright. No restrictions are placed on its use. 3 | #include "libPSI/config.h" 4 | #ifdef ENABLE_PRTY_PSI 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include "libOTe/NChooseOne/Kkrt/KkrtNcoOtReceiver.h" 11 | #include "libOTe/NChooseOne/Kkrt/KkrtNcoOtSender.h" 12 | #include "libOTe/TwoChooseOne/IknpOtExtReceiver.h" 13 | 14 | #include "Poly/polyNTL.h" 15 | #include "PrtyDefines.h" 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include "Poly/polyFFT.h" 22 | #include "Tools/SimpleIndex.h" 23 | 24 | #include 25 | namespace osuCrypto { 26 | 27 | class PrtySender :public TimerAdapter 28 | { 29 | public: 30 | 31 | SimpleIndex simple; 32 | 33 | bool mHasBase; 34 | 35 | u64 mMyInputSize, mTheirInputSize, mPolyNumBytes, mPolyDegree, mStepSize, mPsiSecParam; 36 | std::vector mS; 37 | KkrtNcoOtReceiver recvOprf; 38 | KkrtNcoOtSender sendOprf; //PQET 39 | 40 | u64 mFieldSize; 41 | ZZ mPrime; 42 | ZZ mPrimeLastSlice; 43 | 44 | 45 | 46 | block mTruncateBlk; 47 | 48 | 49 | PRNG mPrng; 50 | 51 | BitVector mOtChoices; 52 | std::vector mAesQ; 53 | 54 | u64 idxPermuteDoneforDebug, hashIdxforDebug; 55 | AES mAesHasher; 56 | 57 | std::array subRowQForDebug; 58 | 59 | 60 | void init(u64 myInputSize, u64 theirInputSize, u64 psiSecParam, PRNG& prng, span chls); 61 | void output(span inputs, span chls); 62 | void outputBestComm(span inputs, span chls); 63 | void outputBigPoly(span inputs, span chls); 64 | 65 | 66 | }; 67 | } 68 | 69 | #endif -------------------------------------------------------------------------------- /libPSI/Tools/BalancedIndex.cpp: -------------------------------------------------------------------------------- 1 | #include "BalancedIndex.h" 2 | #include "cryptoTools/Crypto/PRNG.h" 3 | #include 4 | #include "cryptoTools/Common/Log.h" 5 | #include "cryptoTools/Common/CuckooIndex.h" 6 | #include 7 | //#include 8 | //#include 9 | 10 | namespace osuCrypto 11 | { 12 | 13 | 14 | void BalancedIndex::print(span items) 15 | { 16 | std::cout << "numIters=" << numIters << std::endl; 17 | std::cout << "mNumDummies=" << mNumDummies << std::endl; 18 | std::cout << "mNumBins=" << mNumBins << std::endl; 19 | for (u64 i = 0; i < mBins.size(); ++i) 20 | { 21 | std::cout << "BBin #" << i << " contains " << mBins[i].cnt << " elements" << std::endl; 22 | 23 | for (auto it = mBins[i].values.begin(); it != mBins[i].values.end(); ++it)//for each bin, list all alter light bins 24 | { 25 | for (u64 j = 0; j < it->second.size(); j++) 26 | { 27 | //std::cout << "\t" << it->second[j] << "\t" << items[it->second[j]] << std::endl; 28 | std::cout << "\t" << items[it->second[j].mIdx] <<"\t" << it->second[j].mHashIdx << std::endl; 29 | 30 | } 31 | } 32 | 33 | std::cout << std::endl; 34 | } 35 | 36 | std::cout << std::endl; 37 | } 38 | 39 | void BalancedIndex::init(u64 inputSize, u64 maxBinSize, u64 numDummies, u64 statSecParam) 40 | { 41 | numIters = 0; 42 | mMaxBinSize = maxBinSize; 43 | mNumBins = 1+inputSize/(maxBinSize- numDummies); 44 | mNumDummies = numDummies; 45 | //mMaxBinSize = mNumDummies + inputSize / mNumBins; 46 | mHashSeed = _mm_set_epi32(4253465, 3434565, 234435, 23987025); //hardcode hash 47 | mAesHasher.setKey(mHashSeed); 48 | mBins.resize(mNumBins); 49 | } 50 | 51 | void BalancedIndex::check() 52 | { 53 | for (u64 idxBin = 0; idxBin < mNumBins; ++idxBin) 54 | { 55 | if (mBins[idxBin].cnt > mMaxBinSize) 56 | std::cout << idxBin << "\t cnt:" << mBins[idxBin].cnt << "\t" << "mMaxBinSize " << mMaxBinSize << "\n"; 57 | } 58 | } 59 | void BalancedIndex::insertItems(span items) 60 | { 61 | u64 inputSize = items.size(); 62 | std::vector heavyBins; 63 | 64 | block cipher; 65 | u64 b1, b2; //2 bins index 66 | 67 | //1st pass 68 | for (u64 idxItem = 0; idxItem < inputSize; ++idxItem) 69 | { 70 | cipher = mAesHasher.ecbEncBlock(items[idxItem]); 71 | 72 | b1 = _mm_extract_epi64(cipher, 0) % mNumBins; //1st 64 bits for finding bin location 73 | b2 = _mm_extract_epi64(cipher, 1) % mNumBins; //2nd 64 bits for finding alter bin location 74 | 75 | if (mBins[b1].cnt < mBins[b2].cnt) 76 | { 77 | auto iterB2 = mBins[b1].values.find(b2); //find alter b2 in BIN b1 78 | 79 | if (iterB2 != mBins[b1].values.end()) //if bins[b1] has b2 as unordered_map, insert index of this item 80 | iterB2->second.push_back({ 0,idxItem }); 81 | else 82 | mBins[b1].values.emplace(std::make_pair(b2, std::vector{ {0,idxItem}})); //if not, insert new map 83 | 84 | mBins[b1].cnt++; 85 | } 86 | else 87 | { 88 | auto iterB1 = mBins[b2].values.find(b1); //find alter b2 in BIN b1 89 | 90 | if (iterB1 != mBins[b2].values.end()) //if bins[b1] has b2 as unordered_map, insert index of this item 91 | iterB1->second.push_back({ 1,idxItem }); 92 | else 93 | mBins[b2].values.emplace(std::make_pair(b1, std::vector{ {1, idxItem}})); //if not, insert new map 94 | 95 | mBins[b2].cnt++; 96 | } 97 | 98 | } 99 | 100 | 101 | //find light/heavy bins after 1st pass 102 | for (u64 idxBin = 0; idxBin < mNumBins; ++idxBin) 103 | { 104 | for (auto it = mBins[idxBin].values.begin(); it != mBins[idxBin].values.end(); ++it)//for each bin, list all alter light bins 105 | { 106 | if (mBins[it->first].cnt < mMaxBinSize) 107 | mBins[idxBin].lightBins.emplace_back(it->first); 108 | } 109 | 110 | if (mBins[idxBin].cnt > mMaxBinSize) 111 | heavyBins.emplace_back(idxBin); 112 | } 113 | // std::cout << "heavyBins.size() " << heavyBins.size() << "\n"; 114 | 115 | 116 | 117 | //=====================Self-Balacing-Step========================== 118 | 119 | while (heavyBins.size() > 0 ) 120 | { 121 | //std::cout << numIters << "\t " << heavyBins.size() << "\t"; 122 | 123 | 124 | u64 b1 = heavyBins[rand() % heavyBins.size()]; //choose random bin that is heavy 125 | 126 | //std::cout << mBins[b1].cnt << "\t"; 127 | 128 | 129 | if (mBins[b1].lightBins.size() > 0) 130 | { 131 | u64 i2 = rand() % mBins[b1].lightBins.size(); //choose random alter bin, that is light, to balance 132 | u64 b2 = mBins[b1].lightBins[i2]; 133 | //std::cout << mBins[b2].cnt; 134 | 135 | 136 | if (mBins[b2].cnt < mBins[b1].cnt) //if true, do the balance (double check in some unexpected cases) 137 | { 138 | auto curSubBin = mBins[b1].values.find(b2); 139 | 140 | u64 rB = rand() % 2; 141 | 142 | if (rB == 1 || mBins[b2].cnt + 1 != mBins[b1].cnt) //if not tie 143 | { 144 | 145 | if (curSubBin->second.size() == 0) 146 | continue; 147 | 148 | u64 idxBalanced = rand() % curSubBin->second.size(); //choose random item of bin b1 that can move to b2 149 | 150 | //Remove item 151 | item idxBalancedItem = curSubBin->second[idxBalanced]; 152 | curSubBin->second.erase(curSubBin->second.begin() + idxBalanced); //remove that item from b1 153 | mBins[b1].cnt--; 154 | 155 | if (mBins[b1].cnt < mMaxBinSize) //b1 may no longer a heavy bin 156 | { 157 | auto it = std::find(heavyBins.begin(), heavyBins.end(), b1); 158 | heavyBins.erase(it); 159 | } 160 | 161 | 162 | auto newSubBin = mBins[b2].values.find(b1); //place idxBalancedItem into b2 163 | 164 | if (newSubBin != mBins[b2].values.end()) { //if bins[b2] has b1 as unordered_map, emplace it into 165 | newSubBin->second.emplace_back(idxBalancedItem); 166 | 167 | if (mBins[b1].cnt < mMaxBinSize) 168 | mBins[b2].lightBins.emplace_back(b1); 169 | } 170 | else 171 | { 172 | mBins[b2].values.emplace(std::make_pair(b1, std::vector{idxBalancedItem})); 173 | mBins[b2].lightBins.emplace_back(b1); 174 | } 175 | 176 | mBins[b2].cnt++; 177 | 178 | //b2 may become an heavy bin 179 | if (mBins[b2].cnt > mMaxBinSize) 180 | { 181 | heavyBins.emplace_back(b2); 182 | mBins[b1].lightBins.erase(mBins[b1].lightBins.begin() + i2); 183 | } 184 | } 185 | 186 | numIters++; 187 | } 188 | } 189 | //std::cout << "\n"; 190 | } 191 | //check(); 192 | 193 | 194 | for (u64 idxBin = 0; idxBin < mNumBins; ++idxBin) 195 | { 196 | for (auto it = mBins[idxBin].values.begin(); it != mBins[idxBin].values.end(); ++it)//for each bin, list all alter light bins 197 | { 198 | for (u64 idx = 0; idx < it->second.size(); idx++) 199 | { 200 | if(it->second[idx].mHashIdx==0) 201 | mBins[idxBin].blks.push_back(items[it->second[idx].mIdx]); 202 | else 203 | mBins[idxBin].blks.push_back(items[it->second[idx].mIdx]^OneBlock); 204 | 205 | mBins[idxBin].idxs.push_back(it->second[idx].mIdx); 206 | mBins[idxBin].hashIdxs.push_back((u8)it->second[idx].mHashIdx); 207 | 208 | } 209 | } 210 | 211 | } 212 | 213 | } 214 | 215 | } 216 | -------------------------------------------------------------------------------- /libPSI/Tools/BalancedIndex.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cryptoTools/Common/Defines.h" 3 | #include "cryptoTools/Common/BitVector.h" 4 | #include "cryptoTools/Common/Matrix.h" 5 | #include 6 | 7 | namespace osuCrypto 8 | { 9 | class BalancedIndex 10 | { 11 | public: 12 | 13 | struct item 14 | { 15 | u64 mHashIdx; 16 | u64 mIdx; 17 | }; 18 | 19 | struct Bin 20 | { 21 | std::unordered_map> values; // 22 | std::vector lightBins; //index of alternative light Bins 23 | u64 cnt; 24 | std::vector blks; //index of alternative light Bins 25 | std::vector idxs; //index of alternative light Bins 26 | std::vector hashIdxs; //index of alternative light Bins 27 | }; 28 | 29 | 30 | u64 mNumBalls, mNumBins, mNumDummies, numIters, mMaxBinSize; 31 | 32 | std::vector mBins; 33 | block mHashSeed; 34 | AES mAesHasher; 35 | void print(span items) ; 36 | void init(u64 inputSize, u64 maxBinSize, u64 numDummies, u64 statSecParam = 40); 37 | void insertItems(span items); 38 | void check(); 39 | }; 40 | 41 | } 42 | -------------------------------------------------------------------------------- /libPSI/Tools/CuckooHasher.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cryptoTools/Common/Defines.h" 3 | #include "cryptoTools/Common/Log.h" 4 | #include "cryptoTools/Common/BitVector.h" 5 | 6 | #include "cryptoTools/Common/MatrixView.h" 7 | //#include 8 | #include 9 | 10 | #include 11 | 12 | #define THREAD_SAFE_CUCKOO_HASHER 13 | 14 | namespace osuCrypto 15 | { 16 | //struct CuckooParam 17 | //{ 18 | // u64 mStashSize; 19 | // double mBinScaler; 20 | // u64 mNumHashes, mN;// , mSenderBinSize; 21 | //}; 22 | 23 | 24 | 25 | class CuckooHasher 26 | { 27 | public: 28 | CuckooHasher(); 29 | ~CuckooHasher(); 30 | 31 | struct Bin 32 | { 33 | Bin() :mVal(-1) {} 34 | Bin(u64 idx, u64 hashIdx) : mVal(idx | (hashIdx << 56)) {} 35 | 36 | bool isEmpty() const; 37 | u64 idx() const; 38 | u64 hashIdx() const; 39 | 40 | void swap(u64& idx, u64& hashIdx); 41 | #ifdef THREAD_SAFE_CUCKOO_HASHER 42 | Bin(const Bin& b) : mVal(b.mVal.load(std::memory_order_relaxed)) {} 43 | Bin(Bin&& b) : mVal(b.mVal.load(std::memory_order_relaxed)) {} 44 | std::atomic mVal; 45 | #else 46 | Bin(const Bin& b) : mVal(b.mVal) {} 47 | Bin(Bin&& b) : mVal(b.mVal) {} 48 | u64 mVal; 49 | #endif 50 | }; 51 | struct Workspace 52 | { 53 | Workspace(u64 n) 54 | : curAddrs(n) 55 | , curHashIdxs(n) 56 | , oldVals(n) 57 | //, findAddr(n) 58 | , findVal(n) 59 | {} 60 | 61 | std::vector 62 | curAddrs,// (inputIdxs.size(), 0), 63 | curHashIdxs,// (inputIdxs.size(), 0), 64 | oldVals;// (inputIdxs.size()); 65 | 66 | std::vector> /*findAddr,*/ findVal; 67 | }; 68 | 69 | 70 | bool mPrint = true; 71 | u64 mTotalTries; 72 | 73 | bool operator==(const CuckooHasher& cmp)const; 74 | bool operator!=(const CuckooHasher& cmp)const; 75 | 76 | //std::mutex mStashx; 77 | 78 | CuckooParam mParams; 79 | 80 | void print() const; 81 | void init(u64 n, u64 statSecParam, bool multiThreaded); 82 | void insert(u64 IdxItem, span hashes); 83 | void insertHelper(u64 IdxItem, u64 hashIdx, u64 numTries); 84 | 85 | void insertBatch(span itemIdxs, MatrixView hashs, Workspace& workspace); 86 | 87 | u64 find(span hashes); 88 | u64 findBatch(MatrixView hashes, 89 | span idxs, 90 | Workspace& wordkspace); 91 | 92 | private: 93 | 94 | std::vector mHashes; 95 | MatrixView mHashesView; 96 | 97 | std::vector mBins; 98 | std::vector mStash; 99 | 100 | //std::vector mBins; 101 | //std::vector mStash; 102 | 103 | 104 | //void insertItems(std::array,4>& hashs); 105 | }; 106 | 107 | } 108 | -------------------------------------------------------------------------------- /libPSI/Tools/RandomShuffle.cpp: -------------------------------------------------------------------------------- 1 | #include "RandomShuffle.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | namespace osuCrypto 7 | { 8 | RandomShuffle::RandomShuffle(u64 numTHreads) 9 | { 10 | } 11 | void RandomShuffle::shuffle(span vals, PRNG& prng) 12 | { 13 | std::shuffle(vals.begin(), vals.end(), prng); 14 | } 15 | 16 | void RandomShuffle::parallelShuffle(span vals, u64 t, u64 nt) 17 | { 18 | auto start = vals.size() * t / nt; 19 | auto end = vals.size() * t / nt; 20 | PRNG prng(toBlock(t)); 21 | //std::vector dest(vals.size()); 22 | mergeShuffle({ vals.data() + start, vals.data() + end }, prng); 23 | 24 | 25 | 26 | //memcpy(vals.data(), dest.data(), sizeof(u64) * vals.size()); 27 | } 28 | 29 | void RandomShuffle::mergeShuffle(span src, PRNG & prng) 30 | { 31 | //Expects(src.size() == dest.size()); 32 | 33 | u64 k = 1ull << 20; 34 | 35 | //std::array base{ ZeroBlock, toBlock(0ull,~0ull), toBlock(~0ull,0ull), AllOneBlock }; 36 | //std::array masks; 37 | 38 | //std::array u64Masks{ 0ull, ~0ull }; 39 | 40 | if (src.size() < k) 41 | { 42 | shuffle(src, prng); 43 | } 44 | else 45 | { 46 | block mask = _mm_set_epi8(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1); 47 | std::array expended; 48 | auto bytes = (u8*)expended.data(); 49 | 50 | PRNG prng2( prng.get(), 256); 51 | auto low = src.begin(); 52 | auto mid = src.begin() + src.size() / 2; 53 | auto end = src.end(); 54 | 55 | auto g = std::async([&]() { 56 | mergeShuffle({ low, mid }, prng2); 57 | //gTimer.setTimePoint("s1"); 58 | }); 59 | mergeShuffle({ mid, end }, prng); 60 | //gTimer.setTimePoint("s1"); 61 | g.get(); 62 | 63 | 64 | 65 | while (low != mid && mid != end) 66 | { 67 | //u64 rand = prng.get(); 68 | auto min = std::min(128ll, std::min(mid - low, end - mid)); 69 | auto blk = prng.get(); 70 | expended[0] = mask & _mm_srai_epi16(blk, 0); 71 | expended[1] = mask & _mm_srai_epi16(blk, 1); 72 | expended[2] = mask & _mm_srai_epi16(blk, 2); 73 | expended[3] = mask & _mm_srai_epi16(blk, 3); 74 | expended[4] = mask & _mm_srai_epi16(blk, 4); 75 | expended[5] = mask & _mm_srai_epi16(blk, 5); 76 | expended[6] = mask & _mm_srai_epi16(blk, 6); 77 | expended[7] = mask & _mm_srai_epi16(blk, 7); 78 | 79 | for (u64 i = 0; i < min; ++i) 80 | { 81 | if (bytes[i]) std::swap(*low, *mid); 82 | mid += bytes[i]; 83 | ++low; 84 | } 85 | } 86 | 87 | //gTimer.setTimePoint("merge"); 88 | 89 | shuffle({ low, end }, prng); 90 | 91 | //gTimer.setTimePoint("final"); 92 | 93 | //mergeShuffle({ auxBegin,auxEnd }, prng); 94 | } 95 | 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /libPSI/Tools/RandomShuffle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | namespace osuCrypto 5 | { 6 | 7 | 8 | class RandomShuffle 9 | { 10 | 11 | public: 12 | 13 | RandomShuffle(u64 numTHreads = 1); 14 | 15 | 16 | void shuffle(span vals, PRNG& prng); 17 | void parallelShuffle(span vals, u64 threadIndex, u64 numThreads); 18 | 19 | //void randomPermutation(span dest, u64 threadIndex); 20 | 21 | void mergeShuffle(span vals, PRNG& prng); 22 | 23 | 24 | }; 25 | 26 | } -------------------------------------------------------------------------------- /libPSI/Tools/SimpleHasher.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cryptoTools/Common/Defines.h" 3 | #include "cryptoTools/Common/BitVector.h" 4 | #include "cryptoTools/Common/Matrix.h" 5 | #include 6 | 7 | 8 | namespace osuCrypto 9 | { 10 | //// a list of {{set size, bit size}} 11 | //std::vector> binSizes 12 | //{ 13 | // {1<<12, 18}, 14 | // {1<<16, 19}, 15 | // {1<<20, 20}, 16 | // {1<<24, 21} 17 | //}; 18 | 19 | 20 | class SimpleHasher 21 | { 22 | public: 23 | SimpleHasher(); 24 | ~SimpleHasher(); 25 | 26 | //typedef std::vector MtBin; 27 | //typedef std::vector> MtBin; 28 | 29 | u64 mBinCount , mMaxBinSize, mN; 30 | 31 | Matrix mBins_; 32 | std::unique_ptr[]> mBinSizes; 33 | block mHashSeed; 34 | 35 | inline void push(u64 binIdx, u64 value) 36 | { 37 | auto pos = mBinSizes[binIdx].fetch_add(1,std::memory_order_relaxed); 38 | mBins_(binIdx, pos) = value; 39 | } 40 | 41 | inline span getBin(u64 binIdx) 42 | { 43 | return { mBins_.data(binIdx), getBinSize(binIdx) }; 44 | } 45 | 46 | inline u8 getBinSize(u64 binIdx) 47 | { 48 | return mBinSizes[binIdx].load(std::memory_order_relaxed); 49 | } 50 | 51 | void print() const; 52 | 53 | void init(u64 n, u64 numBits, block hashSeed, u64 secParam, double binScaler); 54 | 55 | //void preHashedInsertItems(ArrayView items, u64 itemIdx); 56 | //void insertItemsWithPhasing(ArrayView items, u64 itemIdx); 57 | }; 58 | 59 | } 60 | -------------------------------------------------------------------------------- /libPSI/Tools/SimpleIndex.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cryptoTools/Common/Defines.h" 3 | #include "cryptoTools/Common/BitVector.h" 4 | #include "cryptoTools/Common/Matrix.h" 5 | 6 | namespace osuCrypto 7 | { 8 | //// a list of {{set size, bit size}} 9 | //std::vector> binSizes 10 | //{ 11 | // {1<<12, 18}, 12 | // {1<<16, 19}, 13 | // {1<<20, 20}, 14 | // {1<<24, 21} 15 | //}; 16 | /*{ 17 | return mVal == u64(-1); 18 | } 19 | 20 | u64 CuckooIndex::Bin::idx() const 21 | { 22 | return mVal & (u64(-1) >> 8); 23 | } 24 | 25 | u64 CuckooIndex::Bin::hashIdx() const 26 | { 27 | return mVal >> 56;*/ 28 | 29 | class SimpleIndex 30 | { 31 | public: 32 | 33 | 34 | struct Item 35 | { 36 | Item() :mVal(-1) {} 37 | 38 | Item& operator=(const Item&) = default; 39 | 40 | bool isEmpty() const { return mVal == u64(-1); } 41 | 42 | // The index is the index of the input that currently 43 | // occupies this bin position. The index is encode in the 44 | // first 7 bytes. 45 | u64 idx() const { return mVal & (u64(-1) >> 8); } 46 | 47 | // The index of the hash function that this item is 48 | // currently using. This in is encoded in the 8th byte. 49 | u64 hashIdx() const { return ((u8*)&mVal)[7] & 127; } 50 | 51 | // Return true if this item was set with a collition. 52 | bool isCollision() const { return (((u8*)&mVal)[7] >> 7) > 0; } 53 | 54 | // The this item to contain the index idx under the given hash index. 55 | // The collision value is also encoded. 56 | void set(u64 idx, u8 hashIdx, bool collision) 57 | { 58 | mVal = idx; 59 | ((u8*)&mVal)[7] = hashIdx | ((collision & 1) << 7); 60 | } 61 | #ifdef THREAD_SAFE_SIMPLE_INDEX 62 | Item(const Item& b) : mVal(b.mVal.load(std::memory_order_relaxed)) {} 63 | Item(Item&& b) : mVal(b.mVal.load(std::memory_order_relaxed)) {} 64 | std::atomic mVal; 65 | #else 66 | Item(const Item& b) : mVal(b.mVal) {} 67 | Item(Item&& b) : mVal(b.mVal) {} 68 | u64 mVal; 69 | #endif 70 | }; 71 | 72 | u64 mMaxBinSize, mNumHashFunctions; 73 | 74 | // The current assignment of items to bins. Only 75 | // the index of the input item is stored in the bin, 76 | // not the actual item itself. 77 | Matrix mBins; 78 | u64 mNumBins; 79 | 80 | // numBalls x mNumHashFunctions matrix, (i,j) contains the i'th items 81 | // hash value under hash index j. 82 | Matrix mItemToBinMap; 83 | 84 | // The some of each bin. 85 | std::vector mBinSizes; 86 | 87 | block mHashSeed; 88 | void print() ; 89 | static u64 get_bin_size(u64 numBins, u64 numBalls, u64 statSecParam); 90 | 91 | 92 | void init(u64 numBins, u64 numBalls, u64 statSecParam = 40, u64 numHashFunction = 3); 93 | void insertItems(span items, block hashingSeed); 94 | }; 95 | 96 | } 97 | -------------------------------------------------------------------------------- /libPSI/Tools/fileBased.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include "cryptoTools/Common/Defines.h" 5 | #include "cryptoTools/Common/CLP.h" 6 | #include "cryptoTools/Network/IOService.h" 7 | #include "cryptoTools/Network/Channel.h" 8 | #include "cryptoTools/Network/Channel.h" 9 | #include "cryptoTools/Crypto/RandomOracle.h" 10 | #include "cryptoTools/Crypto/PRNG.h" 11 | #include 12 | #include 13 | #include 14 | 15 | #include "libPSI/MPSI/Rr17/Rr17a/Rr17aMPsiReceiver.h" 16 | #include "libPSI/MPSI/Rr17/Rr17a/Rr17aMPsiSender.h" 17 | #include "libOTe/NChooseOne/Oos/OosNcoOtReceiver.h" 18 | #include "libOTe/NChooseOne/Oos/OosNcoOtSender.h" 19 | 20 | #include "libPSI/PSI/Kkrt/KkrtPsiReceiver.h" 21 | #include "libPSI/PSI/Kkrt/KkrtPsiSender.h" 22 | #include "libOTe/NChooseOne/Kkrt/KkrtNcoOtReceiver.h" 23 | #include "libOTe/NChooseOne/Kkrt/KkrtNcoOtSender.h" 24 | 25 | #include "libPSI/PSI/ECDH/EcdhPsiReceiver.h" 26 | #include "libPSI/PSI/ECDH/EcdhPsiSender.h" 27 | 28 | #include "libPSI/MPSI/DKT/DktMPsiReceiver.h" 29 | #include "libPSI/MPSI/DKT/DktMPsiSender.h" 30 | 31 | 32 | namespace osuCrypto 33 | { 34 | 35 | 36 | std::ifstream::pos_type filesize(std::ifstream& file); 37 | 38 | bool hasSuffix(std::string const& value, std::string const& ending); 39 | 40 | bool isHexBlock(const std::string& buff); 41 | block hexToBlock(const std::string& buff); 42 | 43 | enum class FileType 44 | { 45 | Bin, 46 | Csv, 47 | Unspecified 48 | }; 49 | 50 | enum class Role { 51 | Sender = 0, 52 | Receiver = 1, 53 | Invalid 54 | }; 55 | 56 | std::vector readSet(const std::string& path, FileType ft, bool debug); 57 | 58 | void writeOutput(std::string outPath, FileType ft, const std::vector& intersection); 59 | 60 | 61 | 62 | void padSmallSet(std::vector& set, u64& theirSize, const CLP& cmd); 63 | 64 | void doFilePSI(const CLP& cmd); 65 | 66 | } 67 | 68 | -------------------------------------------------------------------------------- /libPSI/Version.cpp: -------------------------------------------------------------------------------- 1 | #include "Version.h" 2 | 3 | 4 | // includes the header to run the version check... -------------------------------------------------------------------------------- /libPSI/Version.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | static_assert(CRYPTO_TOOLS_VERSION >= 10500, "please update libOTe and cryptoTools."); 7 | static_assert(LIBOTE_VERSION >= 10000, "please update libOTe and cryptoTools."); 8 | 9 | #define LIB_PSI_VERSION_MAJOR 1 10 | #define LIB_PSI_VERSION_MINOR 1 11 | #define LIB_PSI_VERSION_PATCH 0 12 | #define LIB_PSI_VERSION (LIB_PSI_VERSION_MAJOR * 10000 + LIB_PSI_VERSION_MINOR * 100 + LIB_PSI_VERSION_PATCH) -------------------------------------------------------------------------------- /libPSI/config.h.in: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | #if LIBOTE_VERSION < 10000 6 | Config ERROR: libOTe is too old. 7 | #endif 8 | 9 | #define ON 1 10 | 11 | // build the library with DCW PSI enabled 12 | #cmakedefine ENABLE_DCW_PSI @ENABLE_DCW_PSI@ 13 | 14 | // build the library with DKT PSI enabled 15 | #cmakedefine ENABLE_DKT_PSI @ENABLE_DKT_PSI@ 16 | 17 | // build the library with GRR PSI enabled 18 | #cmakedefine ENABLE_GRR_PSI @ENABLE_GRR_PSI@ 19 | 20 | // build the library with RR16 PSI enabled 21 | #cmakedefine ENABLE_RR16_PSI @ENABLE_RR16_PSI@ 22 | 23 | // build the library with RR17 PSI enabled 24 | #cmakedefine ENABLE_RR17_PSI @ENABLE_RR17_PSI@ 25 | 26 | // build the library with RR17 PSI enabled 27 | #cmakedefine ENABLE_RR17B_PSI @ENABLE_RR17B_PSI@ 28 | 29 | // build the library with KKRT PSI enabled 30 | #cmakedefine ENABLE_KKRT_PSI @ENABLE_KKRT_PSI@ 31 | 32 | // build the library with ECDH PSI enabled 33 | #cmakedefine ENABLE_ECDH_PSI @ENABLE_ECDH_PSI@ 34 | 35 | // build the library with DRRN PSI enabled 36 | #cmakedefine ENABLE_DRRN_PSI @ENABLE_DRRN_PSI@ 37 | 38 | // build the library with PRTY PSI enabled 39 | #cmakedefine ENABLE_PRTY_PSI @ENABLE_PRTY_PSI@ 40 | 41 | 42 | #undef ON -------------------------------------------------------------------------------- /libPSI_Tests/AknBfPsi_Tests.cpp: -------------------------------------------------------------------------------- 1 | #include "AknBfPsi_Tests.h" 2 | 3 | #include "cryptoTools/Network/Endpoint.h" 4 | #include 5 | #include "Common.h" 6 | #include "cryptoTools/Common/Defines.h" 7 | #include "libOTe/TwoChooseOne/KosOtExtReceiver.h" 8 | #include "libOTe/TwoChooseOne/KosOtExtSender.h" 9 | #include "libPSI/MPSI/Rr16/AknBfMPsiReceiver.h" 10 | #include "libPSI/MPSI/Rr16/AknBfMPsiSender.h" 11 | #include "cryptoTools/Common/Log.h" 12 | #include "cryptoTools/Common/TestCollection.h" 13 | // 14 | //#include "cryptopp/aes.h" 15 | //#include "cryptopp/modes.h" 16 | //#include "MyAssert.h" 17 | #include 18 | 19 | using namespace osuCrypto; 20 | 21 | 22 | 23 | void AknBfPsi_EmptySet_Test_Impl() 24 | { 25 | #ifdef ENABLE_RR16_PSI 26 | 27 | u64 setSize = 8, psiSecParam = 40; 28 | PRNG prng(_mm_set_epi32(4253465, 3434565, 234435, 23987045)); 29 | 30 | std::vector sendSet(setSize), recvSet(setSize); 31 | for (u64 i = 0; i < setSize; ++i) 32 | { 33 | sendSet[i] = prng.get(); 34 | recvSet[i] = prng.get(); 35 | } 36 | 37 | std::string name("psi"); 38 | 39 | IOService ios(0); 40 | Endpoint ep0(ios, "localhost", 1212, EpMode::Client, name); 41 | Endpoint ep1(ios, "localhost", 1212, EpMode::Server, name); 42 | 43 | 44 | std::vector recvChl{ ep1.addChannel(name, name) }; 45 | std::vector sendChl{ ep0.addChannel(name, name) }; 46 | 47 | 48 | 49 | KosOtExtReceiver otRecv; 50 | KosOtExtSender otSend; 51 | 52 | auto async = std::async([&]() {PRNG prng(ZeroBlock); otRecv.genBaseOts(prng, recvChl[0]); }); 53 | otSend.genBaseOts(prng, sendChl[0]); 54 | async.get(); 55 | 56 | AknBfMPsiSender send; 57 | AknBfMPsiReceiver recv; 58 | std::thread thrd([&]() { 59 | 60 | send.init(setSize, psiSecParam, otSend, sendChl, prng.get()); 61 | send.sendInput(sendSet, sendChl[0]); 62 | }); 63 | 64 | recv.init(setSize, psiSecParam, otRecv, recvChl[0], ZeroBlock); 65 | recv.sendInput(recvSet, recvChl[0]); 66 | 67 | thrd.join(); 68 | 69 | sendChl[0].close(); 70 | recvChl[0].close(); 71 | 72 | ep0.stop(); 73 | ep1.stop(); 74 | ios.stop(); 75 | #else 76 | throw UnitTestSkipped("Not enabled"); 77 | #endif 78 | } 79 | 80 | 81 | void AknBfPsi_FullSet_Test_Impl() 82 | { 83 | #ifdef ENABLE_RR16_PSI 84 | 85 | setThreadName("CP_Test_Thread"); 86 | u64 setSize = 8, psiSecParam = 40, numThreads(1); 87 | PRNG prng(_mm_set_epi32(4253465, 3434565, 234435, 23987045)); 88 | 89 | 90 | std::vector sendSet(setSize), recvSet(setSize); 91 | for (u64 i = 0; i < setSize; ++i) 92 | { 93 | sendSet[i] = recvSet[i] = prng.get(); 94 | } 95 | 96 | std::shuffle(sendSet.begin(), sendSet.end(), prng); 97 | 98 | 99 | std::string name("psi"); 100 | 101 | IOService ios(0); 102 | Endpoint ep0(ios, "localhost", 1212, EpMode::Client, name); 103 | Endpoint ep1(ios, "localhost", 1212, EpMode::Server, name); 104 | 105 | 106 | std::vector sendChls(numThreads), recvChls(numThreads); 107 | for (u64 i = 0; i < numThreads; ++i) 108 | { 109 | sendChls[i] = ep1.addChannel("chl" + std::to_string(i), "chl" + std::to_string(i)); 110 | recvChls[i] = ep0.addChannel("chl" + std::to_string(i), "chl" + std::to_string(i)); 111 | } 112 | KosOtExtReceiver otRecv; 113 | KosOtExtSender otSend; 114 | 115 | auto async = std::async([&]() {PRNG prng(ZeroBlock); otRecv.genBaseOts(prng, recvChls[0]); }); 116 | otSend.genBaseOts(prng, sendChls[0]); 117 | async.get(); 118 | 119 | AknBfMPsiSender send; 120 | AknBfMPsiReceiver recv; 121 | std::thread thrd([&]() { 122 | 123 | send.init(setSize, psiSecParam, otSend, sendChls, prng.get()); 124 | send.sendInput(sendSet, sendChls); 125 | }); 126 | 127 | recv.init(setSize, psiSecParam, otRecv, recvChls, ZeroBlock); 128 | recv.sendInput(recvSet, recvChls); 129 | thrd.join(); 130 | 131 | 132 | 133 | 134 | for (u64 i = 0; i < numThreads; ++i) 135 | { 136 | sendChls[i].close();// = &ep1.addChannel("chl" + std::to_string(i), "chl" + std::to_string(i)); 137 | recvChls[i].close();// = &ep0.addChannel("chl" + std::to_string(i), "chl" + std::to_string(i)); 138 | } 139 | 140 | ep0.stop(); 141 | ep1.stop(); 142 | ios.stop(); 143 | 144 | 145 | if (recv.mIntersection.size() != setSize) 146 | throw UnitTestFail("Bad intersection size."); 147 | 148 | #else 149 | throw UnitTestSkipped("Not enabled"); 150 | #endif 151 | } 152 | 153 | void AknBfPsi_SingltonSet_Test_Impl() 154 | { 155 | #ifdef ENABLE_RR16_PSI 156 | //Timer& t = gTimer; 157 | 158 | setThreadName("Sender"); 159 | u64 setSize = 1, psiSecParam = 40; 160 | 161 | PRNG prng(_mm_set_epi32(4253465, 34354565, 234435, 23987045)); 162 | 163 | std::vector sendSet(setSize), recvSet(setSize); 164 | for (u64 i = 0; i < setSize; ++i) 165 | { 166 | sendSet[i] = prng.get(); 167 | recvSet[i] = prng.get(); 168 | } 169 | 170 | sendSet[0] = recvSet[0]; 171 | 172 | std::string name("psi"); 173 | IOService ios(0); 174 | Endpoint ep0(ios, "localhost", 1212, EpMode::Client, name); 175 | Endpoint ep1(ios, "localhost", 1212, EpMode::Server, name); 176 | 177 | 178 | Channel recvChl = ep1.addChannel(name, name); 179 | Channel sendChl = ep0.addChannel(name, name); 180 | 181 | 182 | KosOtExtReceiver otRecv; 183 | KosOtExtSender otSend; 184 | 185 | auto async = std::async([&]() {PRNG prng(ZeroBlock); otRecv.genBaseOts(prng, recvChl); }); 186 | otSend.genBaseOts(prng, sendChl); 187 | async.get(); 188 | 189 | AknBfMPsiSender send; 190 | AknBfMPsiReceiver recv; 191 | std::thread thrd([&]() { 192 | 193 | send.init(setSize, psiSecParam, otSend, sendChl, prng.get()); 194 | send.sendInput(sendSet, sendChl); 195 | }); 196 | 197 | recv.init(setSize, psiSecParam, otRecv, recvChl, ZeroBlock); 198 | recv.sendInput(recvSet, recvChl); 199 | 200 | thrd.join(); 201 | 202 | //std::cout << gTimer << std::endl; 203 | 204 | sendChl.close(); 205 | recvChl.close(); 206 | 207 | ep0.stop(); 208 | ep1.stop(); 209 | ios.stop(); 210 | 211 | if (recv.mIntersection.size() != 1 || 212 | recv.mIntersection[0] != 0) 213 | throw UnitTestFail("Bad intersection size"); 214 | 215 | #else 216 | throw UnitTestSkipped("Not enabled"); 217 | #endif 218 | } -------------------------------------------------------------------------------- /libPSI_Tests/AknBfPsi_Tests.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | void AknBfPsi_EmptySet_Test_Impl (); 4 | void AknBfPsi_FullSet_Test_Impl (); 5 | void AknBfPsi_SingltonSet_Test_Impl(); 6 | //void OtBinPsi_SingltonSet_serial_Test_Impl(); 7 | 8 | -------------------------------------------------------------------------------- /libPSI_Tests/BgiPirTests.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | 4 | void BgiPir_keyGen_128_test(); 5 | 6 | void BgiPir_keyGen_test(); 7 | void BgiPir_PIR_test(); 8 | void BgiPir_FullDomain_test(); 9 | void BgiPir_FullDomain_iterator_test(); 10 | void BgiPir_FullDomain_multikey_test(); 11 | -------------------------------------------------------------------------------- /libPSI_Tests/BinOtPsi_Tests.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | void CuckooHasher_Test_Impl(); 3 | void CuckooHasher_parallel_Test_Impl(); 4 | // 5 | //void Rr17a_Kkrt_EmptySet_Test_Impl (); 6 | //void Rr17a_Kkrt_FullSet_Test_Impl (); 7 | //void Rr17a_Kkrt_SingltonSet_Test_Impl(); 8 | // 9 | 10 | 11 | void Rr17a_Oos_EmptySet_Test_Impl(); 12 | void Rr17a_Oos_FullSet_Test_Impl(); 13 | void Rr17a_Oos_parallel_FullSet_Test_Impl(); 14 | void Rr17a_Oos_SingltonSet_Test_Impl(); 15 | //void OtBinPsi_SingltonSet_serial_Test_Impl(); 16 | 17 | 18 | 19 | void Rr17a_SM_EmptySet_Test_Impl(); 20 | void Rr17a_SM_FullSet_Test_Impl(); 21 | void Rr17a_SM_parallel_FullSet_Test_Impl(); 22 | void Rr17a_SM_SingltonSet_Test_Impl(); 23 | 24 | 25 | void Rr17b_Oos_EmptySet_Test_Impl(); 26 | void Rr17b_Oos_FullSet_Test_Impl(); 27 | void Rr17b_Oos_parallel_FullSet_Test_Impl(); 28 | void Rr17b_Oos_SingltonSet_Test_Impl(); 29 | 30 | 31 | 32 | 33 | void Rr17b_SM_EmptySet_Test_Impl(); 34 | void Rr17b_SM_FullSet_Test_Impl(); 35 | void Rr17b_SM_parallel_FullSet_Test_Impl(); 36 | void Rr17b_SM_SingltonSet_Test_Impl(); 37 | 38 | 39 | 40 | void Psi_kkrt_EmptySet_Test_Impl(); 41 | void Psi_kkrt_FullSet_Test_Impl(); 42 | void Psi_kkrt_SingletonSet_Test_Impl(); 43 | -------------------------------------------------------------------------------- /libPSI_Tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | #project(libPSI_Tests) 3 | 4 | 5 | 6 | 7 | ############################################# 8 | # Build libPSI_Tests # 9 | ############################################# 10 | 11 | file(GLOB_RECURSE SRCS *.cpp) 12 | 13 | include_directories(${CMAKE_SOURCE_DIR}/libPSI_Tests) 14 | 15 | 16 | add_library(libPSI_Tests ${SRCS}) 17 | 18 | target_link_libraries(libPSI_Tests libPSI) 19 | 20 | if(MSVC) 21 | target_compile_options(libPSI_Tests PRIVATE $<$:/std:c++${LIBPSI_STD_VER}>) 22 | #target_compile_options(libOTe PRIVATE -openmp:experimental) 23 | else() 24 | target_compile_options(libPSI_Tests PRIVATE $<$:-std=c++${LIBPSI_STD_VER}>) 25 | 26 | endif() -------------------------------------------------------------------------------- /libPSI_Tests/Common.cpp: -------------------------------------------------------------------------------- 1 | //#include "stdafx.h" 2 | #include "Common.h" 3 | #include "cryptoTools/Common/Log.h" 4 | 5 | #include 6 | #include 7 | 8 | using namespace osuCrypto; 9 | 10 | static std::fstream* file = nullptr; 11 | std::string SolutionDir = "../../"; 12 | 13 | void InitDebugPrinting(std::string filePath) 14 | { 15 | std::cout << "changing sink" << std::endl; 16 | 17 | if (file == nullptr) 18 | { 19 | file = new std::fstream; 20 | } 21 | else 22 | { 23 | file->close(); 24 | } 25 | 26 | file->open(filePath, std::ios::trunc | std::ofstream::out); 27 | 28 | if (!file->is_open()) 29 | throw std::runtime_error("failed to open: " + filePath); 30 | 31 | //time_t now = time(0); 32 | 33 | std::cout.rdbuf(file->rdbuf()); 34 | std::cerr.rdbuf(file->rdbuf()); 35 | //SetSink(*file); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /libPSI_Tests/Common.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | // 5 | void InitDebugPrinting(std::string file = "../../testout.txt"); 6 | // 7 | extern std::string SolutionDir; 8 | 9 | //class UnitTestFail : public std::exception 10 | //{ 11 | // std::string mWhat; 12 | //public: 13 | // explicit UnitTestFail(std::string reason) 14 | // :std::exception(), 15 | // mWhat(reason) 16 | // {} 17 | // 18 | // explicit UnitTestFail() 19 | // :std::exception(), 20 | // mWhat("UnitTestFailed exception") 21 | // { 22 | // } 23 | // 24 | // virtual const char* what() const throw() 25 | // { 26 | // return mWhat.c_str(); 27 | // } 28 | //}; 29 | // 30 | -------------------------------------------------------------------------------- /libPSI_Tests/DcwBfPsi_Tests.cpp: -------------------------------------------------------------------------------- 1 | #include "DcwBfPsi_Tests.h" 2 | 3 | #include "Common.h" 4 | #include "cryptoTools/Network/Session.h" 5 | #include "cryptoTools/Network/Channel.h" 6 | #include "cryptoTools/Network/IOService.h" 7 | #include "cryptoTools/Common/Defines.h" 8 | #include "libPSI/PSI/Dcw/DcwRBfPsiReceiver.h" 9 | #include "libPSI/PSI/Dcw/DcwRBfPsiSender.h" 10 | #include "cryptoTools/Common/Log.h" 11 | #include "libOTe/TwoChooseOne/IknpOtExtReceiver.h" 12 | #include "libOTe/TwoChooseOne/IknpOtExtSender.h" 13 | #include "cryptoTools/Common/TestCollection.h" 14 | #include 15 | 16 | using namespace osuCrypto; 17 | 18 | 19 | #ifdef ENABLE_DCW_PSI 20 | 21 | 22 | void DcwRBfPsi_EmptySet_Test_Impl() 23 | { 24 | u64 repeatCount = 1; 25 | u64 setSize = 8, psiSecParam = 40; 26 | PRNG prng(_mm_set_epi32(4253465, 3434565, 234435, 23987045)); 27 | 28 | std::vector sendSet(setSize), recvSet(setSize); 29 | for (u64 i = 0; i < setSize; ++i) 30 | { 31 | sendSet[i] = prng.get(); 32 | recvSet[i] = prng.get(); 33 | } 34 | 35 | IOService ios; 36 | Endpoint ep0(ios, "localhost:1212", EpMode::Client); 37 | Endpoint ep1(ios, "localhost:1212", EpMode::Server); 38 | 39 | std::vector recvChl{ ep1.addChannel() }; 40 | std::vector sendChl{ ep0.addChannel() }; 41 | 42 | IknpOtExtReceiver otRecv; 43 | IknpOtExtSender otSend; 44 | 45 | 46 | std::vector sendPSIs(repeatCount); 47 | std::vector recvPSIs(repeatCount); 48 | std::thread thrd([&]() { 49 | 50 | for (u64 j = 0; j < repeatCount; ++j) 51 | { 52 | sendPSIs[j].init(setSize, psiSecParam, otSend, sendChl, prng.get()); 53 | } 54 | }); 55 | 56 | for (u64 j = 0; j < repeatCount; ++j) 57 | { 58 | recvPSIs[j].init(setSize, psiSecParam, otRecv, recvChl[0], ZeroBlock); 59 | } 60 | 61 | thrd.join(); 62 | 63 | 64 | auto sendThrd = std::thread([&]() { 65 | for (u64 j = 0; j < repeatCount; ++j) 66 | { 67 | auto& sender = sendPSIs[j]; 68 | 69 | sender.sendInput(sendSet, sendChl[0]); 70 | } 71 | }); 72 | 73 | auto recvThrd = std::thread([&]() { 74 | for (u64 j = 0; j < repeatCount; ++j) 75 | { 76 | auto& recv = recvPSIs[j]; 77 | 78 | recv.sendInput(recvSet, recvChl[0]); 79 | 80 | if (recv.mIntersection.size()) 81 | throw UnitTestFail(); 82 | } 83 | }); 84 | sendThrd.join(); 85 | recvThrd.join(); 86 | 87 | 88 | sendChl[0].close(); 89 | recvChl[0].close(); 90 | 91 | ep0.stop(); 92 | ep1.stop(); 93 | ios.stop(); 94 | } 95 | 96 | 97 | void DcwRBfPsi_FullSet_Test_Impl() 98 | { 99 | setThreadName("CP_Test_Thread"); 100 | u64 repeatCount = 1; 101 | u64 setSize = 8, psiSecParam = 40; 102 | PRNG prng(_mm_set_epi32(4253465, 3434565, 234435, 23987045)); 103 | 104 | 105 | std::vector sendSet(setSize), recvSet(setSize); 106 | for (u64 i = 0; i < setSize; ++i) 107 | { 108 | sendSet[i] = recvSet[i] = prng.get(); 109 | } 110 | 111 | std::shuffle(sendSet.begin(), sendSet.end(), prng); 112 | 113 | IOService ios; 114 | Endpoint ep0(ios, "localhost:1212", EpMode::Client); 115 | Endpoint ep1(ios, "localhost:1212", EpMode::Server); 116 | 117 | 118 | std::vector recvChls{ ep1.addChannel() }; 119 | std::vector sendChls{ ep0.addChannel() }; 120 | 121 | 122 | IknpOtExtReceiver otRecv; 123 | IknpOtExtSender otSend; 124 | 125 | 126 | 127 | DcwRBfPsiSender sendPSIs; 128 | DcwRBfPsiReceiver recvPSIs; 129 | std::thread thrd([&]() { 130 | 131 | 132 | sendPSIs.init(setSize, psiSecParam, otSend, sendChls[0], prng.get()); 133 | sendPSIs.sendInput(sendSet, sendChls); 134 | }); 135 | 136 | for (u64 j = 0; j < repeatCount; ++j) 137 | { 138 | recvPSIs.init(setSize, psiSecParam, otRecv, recvChls[0], ZeroBlock); 139 | recvPSIs.sendInput(recvSet, recvChls); 140 | } 141 | 142 | thrd.join(); 143 | 144 | if (recvPSIs.mIntersection.size() != setSize) 145 | { 146 | 147 | throw UnitTestFail("bad size, expected:" + std::to_string(setSize) + 148 | ", actual:" +std::to_string(recvPSIs.mIntersection.size())); 149 | } 150 | } 151 | 152 | void DcwRBfPsi_SingltonSet_Test_Impl() 153 | { 154 | setThreadName("Sender"); 155 | //InitDebugPrinting("..//test.out"); 156 | u64 repeatCount = 1; 157 | u64 setSize = 20, psiSecParam = 30; 158 | 159 | PRNG prng(_mm_set_epi32(4253465, 34354565, 234435, 23987045)); 160 | 161 | std::vector sendSet(setSize), recvSet(setSize); 162 | for (u64 i = 0; i < setSize; ++i) 163 | { 164 | sendSet[i] = prng.get(); 165 | recvSet[i] = prng.get(); 166 | } 167 | 168 | sendSet[0] = recvSet[0]; 169 | 170 | IOService ios; 171 | Endpoint ep0(ios, "localhost:1212", EpMode::Client); 172 | Endpoint ep1(ios, "localhost:1212", EpMode::Server); 173 | 174 | 175 | std::vector recvChl{ ep1.addChannel() }; 176 | std::vector sendChl{ ep0.addChannel() }; 177 | 178 | 179 | IknpOtExtReceiver otRecv; 180 | IknpOtExtSender otSend; 181 | 182 | 183 | 184 | std::vector sendPSIs(repeatCount); 185 | std::vector recvPSIs(repeatCount); 186 | std::thread thrd([&]() { 187 | 188 | for (u64 j = 0; j < repeatCount; ++j) 189 | { 190 | 191 | std::vector cc{ sendChl }; 192 | sendPSIs[j].init(setSize, psiSecParam, otSend, cc, prng.get()); 193 | } 194 | }); 195 | 196 | for (u64 j = 0; j < repeatCount; ++j) 197 | { 198 | recvPSIs[j].init(setSize, psiSecParam, otRecv, recvChl, ZeroBlock); 199 | } 200 | 201 | thrd.join(); 202 | 203 | auto sendThrd = std::thread([&]() { 204 | for (u64 j = 0; j < repeatCount; ++j) 205 | { 206 | DcwRBfPsiSender& sender = sendPSIs[j]; 207 | 208 | sender.sendInput(sendSet, sendChl); 209 | } 210 | }); 211 | 212 | auto recvThrd = std::thread([&]() { 213 | for (u64 j = 0; j < repeatCount; ++j) 214 | { 215 | DcwRBfPsiReceiver& recv = recvPSIs[j]; 216 | 217 | recv.sendInput(recvSet, recvChl); 218 | 219 | if (recv.mIntersection.size() != 1 || 220 | recv.mIntersection[0] != 0) 221 | throw UnitTestFail(); 222 | } 223 | }); 224 | sendThrd.join(); 225 | recvThrd.join(); 226 | 227 | } 228 | #else 229 | 230 | 231 | void DcwRBfPsi_EmptySet_Test_Impl() { throw oc::UnitTestSkipped("ENABLE_DCW_PSI not defined"); } 232 | void DcwRBfPsi_FullSet_Test_Impl() { throw oc::UnitTestSkipped("ENABLE_DCW_PSI not defined"); } 233 | void DcwRBfPsi_SingltonSet_Test_Impl() { throw oc::UnitTestSkipped("ENABLE_DCW_PSI not defined"); } 234 | 235 | #endif -------------------------------------------------------------------------------- /libPSI_Tests/DcwBfPsi_Tests.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | //void DcwBfPsi_EmptySet_Test_Impl(); 4 | //void DcwBfPsi_FullSet_Test_Impl(); 5 | //void DcwBfPsi_SingltonSet_Test_Impl(); 6 | // 7 | 8 | 9 | void DcwRBfPsi_EmptySet_Test_Impl(); 10 | void DcwRBfPsi_FullSet_Test_Impl(); 11 | void DcwRBfPsi_SingltonSet_Test_Impl(); 12 | -------------------------------------------------------------------------------- /libPSI_Tests/DktMPsi_Tests.cpp: -------------------------------------------------------------------------------- 1 | #include "DktMPsi_Tests.h" 2 | 3 | #include "cryptoTools/Network/Endpoint.h" 4 | #include "Common.h" 5 | #include "cryptoTools/Common/Defines.h" 6 | #include "libPSI/MPSI/DKT/DktMPsiReceiver.h" 7 | #include "libPSI/MPSI/DKT/DktMPsiSender.h" 8 | #include "cryptoTools/Common/Log.h" 9 | #include "cryptoTools/Common/TestCollection.h" 10 | #include "cryptoTools/Network/IOService.h" 11 | 12 | // 13 | //#include "cryptopp/aes.h" 14 | //#include "cryptopp/modes.h" 15 | //#include "MyAssert.h" 16 | #include 17 | 18 | using namespace osuCrypto; 19 | 20 | #ifdef ENABLE_DKT_PSI 21 | 22 | void DktMPsi_EmptySet_Test_Impl() 23 | { 24 | u64 setSize = 8, psiSecParam = 40; 25 | PRNG prng(_mm_set_epi32(4253465, 3434565, 234435, 23987045)); 26 | 27 | std::vector sendSet(setSize), recvSet(setSize); 28 | for (u64 i = 0; i < setSize; ++i) 29 | { 30 | sendSet[i] = prng.get(); 31 | recvSet[i] = prng.get(); 32 | } 33 | 34 | std::string name("psi"); 35 | 36 | IOService ios(0); 37 | Endpoint ep0(ios, "localhost", 1212, EpMode::Client, name); 38 | Endpoint ep1(ios, "localhost", 1212, EpMode::Server, name); 39 | 40 | 41 | std::vector recvChl{ ep1.addChannel(name, name) }; 42 | std::vector sendChl{ ep0.addChannel(name, name) }; 43 | 44 | 45 | 46 | DktMPsiSender send; 47 | DktMPsiReceiver recv; 48 | std::thread thrd([&]() { 49 | 50 | send.init(setSize, psiSecParam, prng.get()); 51 | send.sendInput(sendSet, sendChl); 52 | }); 53 | 54 | recv.init(setSize, psiSecParam, ZeroBlock); 55 | recv.sendInput(recvSet, recvChl); 56 | 57 | thrd.join(); 58 | 59 | sendChl[0].close(); 60 | recvChl[0].close(); 61 | 62 | ep0.stop(); 63 | ep1.stop(); 64 | ios.stop(); 65 | } 66 | 67 | void DktMPsi_FullSet_Test_Impl() 68 | { 69 | setThreadName("CP_Test_Thread"); 70 | u64 setSize = 40, psiSecParam = 40, numThreads(2); 71 | PRNG prng(_mm_set_epi32(4253465, 3434565, 234435, 23987045)); 72 | 73 | 74 | std::vector sendSet(setSize), recvSet(setSize); 75 | for (u64 i = 0; i < setSize; ++i) 76 | { 77 | sendSet[i] = recvSet[i] = prng.get(); 78 | } 79 | 80 | std::shuffle(sendSet.begin(), sendSet.end(), prng); 81 | 82 | 83 | std::string name("psi"); 84 | 85 | IOService ios(0); 86 | Endpoint ep0(ios, "localhost", 1212, EpMode::Client, name); 87 | Endpoint ep1(ios, "localhost", 1212, EpMode::Server, name); 88 | 89 | 90 | std::vector sendChls(numThreads), recvChls(numThreads); 91 | for (u64 i = 0; i < numThreads; ++i) 92 | { 93 | sendChls[i] = ep1.addChannel("chl" + std::to_string(i), "chl" + std::to_string(i)); 94 | recvChls[i] = ep0.addChannel("chl" + std::to_string(i), "chl" + std::to_string(i)); 95 | } 96 | 97 | 98 | 99 | DktMPsiSender send; 100 | DktMPsiReceiver recv; 101 | std::thread thrd([&]() { 102 | 103 | send.init(setSize, psiSecParam, prng.get()); 104 | send.sendInput(sendSet, sendChls); 105 | }); 106 | 107 | recv.init(setSize, psiSecParam, ZeroBlock); 108 | recv.sendInput(recvSet, recvChls); 109 | 110 | if (recv.mIntersection.size() != setSize) 111 | throw UnitTestFail(); 112 | 113 | thrd.join(); 114 | 115 | for (u64 i = 0; i < numThreads; ++i) 116 | { 117 | sendChls[i].close();// = &ep1.addChannel("chl" + std::to_string(i), "chl" + std::to_string(i)); 118 | recvChls[i].close();// = &ep0.addChannel("chl" + std::to_string(i), "chl" + std::to_string(i)); 119 | } 120 | 121 | ep0.stop(); 122 | ep1.stop(); 123 | ios.stop(); 124 | 125 | } 126 | 127 | void DktMPsi_SingltonSet_Test_Impl() 128 | { 129 | setThreadName("Sender"); 130 | u64 setSize = 40, psiSecParam = 40; 131 | 132 | PRNG prng(_mm_set_epi32(4253465, 34354565, 234435, 23987045)); 133 | 134 | std::vector sendSet(setSize), recvSet(setSize); 135 | for (u64 i = 0; i < setSize; ++i) 136 | { 137 | sendSet[i] = prng.get(); 138 | recvSet[i] = prng.get(); 139 | } 140 | 141 | sendSet[0] = recvSet[0]; 142 | 143 | std::string name("psi"); 144 | IOService ios(0); 145 | Endpoint ep0(ios, "localhost", 1212, EpMode::Client, name); 146 | Endpoint ep1(ios, "localhost", 1212, EpMode::Server, name); 147 | 148 | 149 | std::vector recvChl = {ep1.addChannel(name, name)}; 150 | std::vector sendChl = {ep0.addChannel(name, name)}; 151 | 152 | 153 | 154 | DktMPsiSender send; 155 | DktMPsiReceiver recv; 156 | std::thread thrd([&]() { 157 | 158 | send.init(setSize, psiSecParam, prng.get()); 159 | send.sendInput(sendSet, sendChl); 160 | }); 161 | 162 | recv.init(setSize, psiSecParam, ZeroBlock); 163 | recv.sendInput(recvSet, recvChl); 164 | 165 | thrd.join(); 166 | 167 | for (u64 i = 0; i < sendChl.size(); ++i) 168 | { 169 | sendChl[0].close(); 170 | recvChl[0].close(); 171 | } 172 | 173 | ep0.stop(); 174 | ep1.stop(); 175 | ios.stop(); 176 | 177 | if (recv.mIntersection.size() != 1 || 178 | recv.mIntersection[0] != 0) 179 | { 180 | 181 | throw UnitTestFail(); 182 | } 183 | 184 | } 185 | #else 186 | 187 | void DktMPsi_EmptySet_Test_Impl() 188 | { 189 | throw UnitTestSkipped("not enabled"); 190 | } 191 | void DktMPsi_FullSet_Test_Impl() 192 | { 193 | throw UnitTestSkipped("not enabled"); 194 | } 195 | void DktMPsi_SingltonSet_Test_Impl() 196 | { 197 | throw UnitTestSkipped("not enabled"); 198 | } 199 | 200 | #endif -------------------------------------------------------------------------------- /libPSI_Tests/DktMPsi_Tests.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | void DktMPsi_EmptySet_Test_Impl (); 4 | void DktMPsi_FullSet_Test_Impl (); 5 | void DktMPsi_SingltonSet_Test_Impl(); 6 | //void OtBinPsi_SingltonSet_serial_Test_Impl(); 7 | 8 | -------------------------------------------------------------------------------- /libPSI_Tests/DrrnPsi_Tests.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | void Psi_drrn_SingletonSet_Test_Impl(); 4 | void Psi_drrn_FullSet_Test_Impl(); 5 | void Psi_drrn_EmptySet_Test_Impl(); 6 | -------------------------------------------------------------------------------- /libPSI_Tests/EcdhPsi_Tests.cpp: -------------------------------------------------------------------------------- 1 | #include "EcdhPsi_Tests.h" 2 | 3 | #include "cryptoTools/Network/Endpoint.h" 4 | #include "Common.h" 5 | #include "cryptoTools/Common/Defines.h" 6 | #include "libPSI/PSI/ECDH/EcdhPsiReceiver.h" 7 | #include "libPSI/PSI/ECDH/EcdhPsiSender.h" 8 | #include "cryptoTools/Common/Log.h" 9 | #include "cryptoTools/Common/TestCollection.h" 10 | #include "cryptoTools/Network/IOService.h" 11 | // 12 | //#include "cryptopp/aes.h" 13 | //#include "cryptopp/modes.h" 14 | //#include "MyAssert.h" 15 | #include 16 | 17 | using namespace osuCrypto; 18 | 19 | 20 | #ifdef ENABLE_ECDH_PSI 21 | void EcdhPsi_EmptySet_Test_Impl() 22 | { 23 | 24 | u64 setSize = 8, psiSecParam = 40; 25 | PRNG prng(_mm_set_epi32(4253465, 3434565, 234435, 23987045)); 26 | 27 | std::vector sendSet(setSize), recvSet(setSize); 28 | for (u64 i = 0; i < setSize; ++i) 29 | { 30 | sendSet[i] = prng.get(); 31 | recvSet[i] = prng.get(); 32 | } 33 | 34 | std::string name("psi"); 35 | 36 | IOService ios(0); 37 | Endpoint ep0(ios, "localhost", 1212, EpMode::Client, name); 38 | Endpoint ep1(ios, "localhost", 1212, EpMode::Server, name); 39 | 40 | 41 | std::vector recvChl{ ep1.addChannel(name, name) }; 42 | std::vector sendChl{ ep0.addChannel(name, name) }; 43 | 44 | EcdhPsiSender send; 45 | EcdhPsiReceiver recv; 46 | std::thread thrd([&]() { 47 | 48 | send.init(setSize, psiSecParam, prng.get()); 49 | send.sendInput(sendSet, sendChl); 50 | }); 51 | 52 | recv.init(setSize, psiSecParam, ZeroBlock); 53 | recv.sendInput(recvSet, recvChl); 54 | 55 | thrd.join(); 56 | 57 | sendChl[0].close(); 58 | recvChl[0].close(); 59 | 60 | ep0.stop(); 61 | ep1.stop(); 62 | ios.stop(); 63 | } 64 | 65 | void EcdhPsi_FullSet_Test_Impl() 66 | { 67 | setThreadName("CP_Test_Thread"); 68 | u64 setSize = 40, psiSecParam = 40, numThreads(2); 69 | PRNG prng(_mm_set_epi32(4253465, 3434565, 234435, 23987045)); 70 | 71 | 72 | std::vector sendSet(setSize), recvSet(setSize); 73 | for (u64 i = 0; i < setSize; ++i) 74 | { 75 | sendSet[i] = recvSet[i] = prng.get(); 76 | } 77 | 78 | std::shuffle(sendSet.begin(), sendSet.end(), prng); 79 | 80 | 81 | std::string name("psi"); 82 | 83 | IOService ios(0); 84 | Endpoint ep0(ios, "localhost", 1212, EpMode::Client, name); 85 | Endpoint ep1(ios, "localhost", 1212, EpMode::Server, name); 86 | 87 | 88 | std::vector sendChls(numThreads), recvChls(numThreads); 89 | for (u64 i = 0; i < numThreads; ++i) 90 | { 91 | sendChls[i] = ep1.addChannel("chl" + std::to_string(i), "chl" + std::to_string(i)); 92 | recvChls[i] = ep0.addChannel("chl" + std::to_string(i), "chl" + std::to_string(i)); 93 | } 94 | 95 | EcdhPsiSender send; 96 | EcdhPsiReceiver recv; 97 | std::thread thrd([&]() { 98 | 99 | send.init(setSize, psiSecParam, prng.get()); 100 | send.sendInput(sendSet, sendChls); 101 | }); 102 | 103 | recv.init(setSize, psiSecParam, ZeroBlock); 104 | recv.sendInput(recvSet, recvChls); 105 | 106 | if (recv.mIntersection.size() != setSize) 107 | throw UnitTestFail(); 108 | 109 | thrd.join(); 110 | 111 | for (u64 i = 0; i < numThreads; ++i) 112 | { 113 | sendChls[i].close();// = &ep1.addChannel("chl" + std::to_string(i), "chl" + std::to_string(i)); 114 | recvChls[i].close();// = &ep0.addChannel("chl" + std::to_string(i), "chl" + std::to_string(i)); 115 | } 116 | 117 | ep0.stop(); 118 | ep1.stop(); 119 | ios.stop(); 120 | 121 | } 122 | 123 | void EcdhPsi_SingltonSet_Test_Impl() 124 | { 125 | setThreadName("Sender"); 126 | u64 setSize = 40, psiSecParam = 40; 127 | 128 | PRNG prng(_mm_set_epi32(4253465, 34354565, 234435, 23987045)); 129 | 130 | std::vector sendSet(setSize), recvSet(setSize); 131 | for (u64 i = 0; i < setSize; ++i) 132 | { 133 | sendSet[i] = prng.get(); 134 | recvSet[i] = prng.get(); 135 | } 136 | 137 | sendSet[0] = recvSet[0]; 138 | 139 | std::string name("psi"); 140 | IOService ios(0); 141 | Endpoint ep0(ios, "localhost", 1212, EpMode::Client, name); 142 | Endpoint ep1(ios, "localhost", 1212, EpMode::Server, name); 143 | 144 | 145 | std::vector recvChl = { ep1.addChannel(name, name) }; 146 | std::vector sendChl = { ep0.addChannel(name, name) }; 147 | 148 | 149 | EcdhPsiSender send; 150 | EcdhPsiReceiver recv; 151 | std::thread thrd([&]() { 152 | 153 | send.init(setSize, psiSecParam, prng.get()); 154 | send.sendInput(sendSet, sendChl); 155 | }); 156 | 157 | recv.init(setSize, psiSecParam, ZeroBlock); 158 | recv.sendInput(recvSet, recvChl); 159 | 160 | thrd.join(); 161 | 162 | for (u64 i = 0; i < sendChl.size(); ++i) 163 | { 164 | sendChl[0].close(); 165 | recvChl[0].close(); 166 | } 167 | 168 | ep0.stop(); 169 | ep1.stop(); 170 | ios.stop(); 171 | 172 | if (recv.mIntersection.size() != 1 || 173 | recv.mIntersection[0] != 0) 174 | { 175 | 176 | throw UnitTestFail(); 177 | } 178 | 179 | } 180 | 181 | #else 182 | 183 | void EcdhPsi_EmptySet_Test_Impl() 184 | { 185 | throw UnitTestSkipped("not enabled"); 186 | } 187 | void EcdhPsi_FullSet_Test_Impl() 188 | { 189 | throw UnitTestSkipped("not enabled"); 190 | } 191 | void EcdhPsi_SingltonSet_Test_Impl() 192 | { 193 | throw UnitTestSkipped("not enabled"); 194 | } 195 | 196 | #endif -------------------------------------------------------------------------------- /libPSI_Tests/EcdhPsi_Tests.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | void EcdhPsi_EmptySet_Test_Impl (); 4 | void EcdhPsi_FullSet_Test_Impl (); 5 | void EcdhPsi_SingltonSet_Test_Impl(); 6 | -------------------------------------------------------------------------------- /libPSI_Tests/FileBase_Tests.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | void filebase_readSet_Test(); 4 | 5 | void filebase_kkrt_bin_Test(); 6 | void filebase_kkrt_csv_Test(); 7 | void filebase_kkrt_csvh_Test(); 8 | void filebase_rr17a_bin_Test(); 9 | void filebase_ecdh_bin_Test(); -------------------------------------------------------------------------------- /libPSI_Tests/Grr18MPSI_Tests.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | void Grr18_Oos_EmptySet_Test_Impl(); 4 | void Grr18_Oos_FullSet_Test_Impl(); 5 | void Grr18_Oos_parallel_FullSet_Test_Impl(); 6 | void Grr18_Oos_SingltonSet_Test_Impl(); -------------------------------------------------------------------------------- /libPSI_Tests/ShamirSSScheme_Tests.cpp: -------------------------------------------------------------------------------- 1 | //#include "stdafx.h" 2 | #ifdef ENABLE_DCW_PSI 3 | #include 4 | #include 5 | #include 6 | 7 | #include "Common.h" 8 | #include "cryptoTools/Common/Defines.h" 9 | #include "libPSI/MPSI/Dcw/ShamirSSScheme.h" 10 | #include "cryptoTools/Common/Log.h" 11 | #include "cryptoTools/Crypto/PRNG.h" 12 | using namespace osuCrypto; 13 | 14 | //namespace osuCrypto_tests 15 | //{ 16 | void ShamirSSScheme_Test() 17 | { 18 | u64 n = 100, k = 50; 19 | 20 | 21 | ShamirSSScheme ss; 22 | auto secret = ss.init(n, k); 23 | 24 | 25 | std::vector shares(n); 26 | 27 | //if(ss.GetSecretParts()[0].size()) 28 | 29 | ss.computeShares(shares); 30 | 31 | //for (u64 i = 0; i < n; ++i) 32 | //{ 33 | // NTL::BytesFromZZ((u8*)&shares[i], ss.GetSecretParts()[i], sizeof(block)); 34 | //} 35 | 36 | 37 | std::vector idxs(k); 38 | std::vector nums(k); 39 | for (u64 i = 0; i < k; ++i) 40 | { 41 | idxs[i] = (u32)i; 42 | //NTL::ZZFromBytes(nums[i], (u8*)&shares[i], sizeof(block)); 43 | nums[i] = shares[i]; 44 | 45 | //std::cout << "shares[" << i << "] = " << shares[i] << std::endl; 46 | } 47 | 48 | //auto recovered = ss.getSecret(idxs, nums); 49 | auto recovered2 = ss.reconstruct(idxs, nums); 50 | 51 | 52 | 53 | 54 | //if( neq(secret, recovered)) 55 | // throw UnitTestFail(); 56 | 57 | 58 | 59 | if (neq(secret, recovered2)) 60 | { 61 | 62 | std::cout << secret << " " << recovered2 << std::endl; 63 | throw UnitTestFail(); 64 | 65 | } 66 | } 67 | #endif -------------------------------------------------------------------------------- /libPSI_Tests/ShamirSSScheme_Tests.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifdef ENABLE_DCW_PSI 3 | 4 | 5 | void ShamirSSScheme_Test(); 6 | #endif -------------------------------------------------------------------------------- /libPSI_Tests/UnitTests.cpp: -------------------------------------------------------------------------------- 1 | #include "cryptoTools/Common/Log.h" 2 | #include 3 | #include "UnitTests.h" 4 | #include "AknBfPsi_Tests.h" 5 | #include "AknBfPsi_Tests.h" 6 | #include "BinOtPsi_Tests.h" 7 | 8 | #include "ShamirSSScheme_Tests.h" 9 | #include "DcwBfPsi_Tests.h" 10 | #include "DktMPsi_Tests.h" 11 | #include "EcdhPsi_Tests.h" 12 | #include "Grr18MPSI_Tests.h" 13 | #include "BgiPirTests.h" 14 | #include "DrrnPsi_Tests.h" 15 | #include "FileBase_Tests.h" 16 | using namespace osuCrypto; 17 | namespace libPSI_Tests 18 | { 19 | 20 | TestCollection Tests([](TestCollection& t) { 21 | 22 | 23 | t.add("Psi_kkrt_EmptySet_Test_Impl ", Psi_kkrt_EmptySet_Test_Impl); 24 | t.add("Psi_kkrt_FullSet_Test_Impl ", Psi_kkrt_FullSet_Test_Impl); 25 | t.add("Psi_kkrt_SingletonSet_Test_Impl ", Psi_kkrt_SingletonSet_Test_Impl); 26 | 27 | t.add("filebase_readSet_Test ", filebase_readSet_Test); 28 | t.add("filebase_kkrt_bin_Test ", filebase_kkrt_bin_Test); 29 | t.add("filebase_kkrt_csv_Test ", filebase_kkrt_csv_Test); 30 | t.add("filebase_kkrt_csvh_Test ", filebase_kkrt_csvh_Test); 31 | 32 | t.add("EcdhPsi_EmptySet_Test_Impl ", EcdhPsi_EmptySet_Test_Impl); 33 | t.add("EcdhPsi_FullSet_Test_Impl ", EcdhPsi_FullSet_Test_Impl); 34 | t.add("EcdhPsi_SingltonSet_Test_Impl ", EcdhPsi_SingltonSet_Test_Impl); 35 | t.add("filebase_ecdh_bin_Test ", filebase_ecdh_bin_Test); 36 | 37 | 38 | t.add("BgiPir_keyGen_128_test ", BgiPir_keyGen_128_test); 39 | t.add("BgiPir_keyGen_test(); ", BgiPir_keyGen_test); 40 | t.add("BgiPir_PIR_test(); ", BgiPir_PIR_test); 41 | t.add("BgiPir_FullDomain_test(); ", BgiPir_FullDomain_test); 42 | t.add("BgiPir_FullDomain_iterator_test(); ", BgiPir_FullDomain_iterator_test); 43 | t.add("BgiPir_FullDomain_multikey_test(); ", BgiPir_FullDomain_multikey_test); 44 | 45 | 46 | t.add("Psi_drrn_SingletonSet_Test_Impl ", Psi_drrn_SingletonSet_Test_Impl); 47 | t.add("Psi_drrn_FullSet_Test_Impl ", Psi_drrn_FullSet_Test_Impl); 48 | t.add("Psi_drrn_EmptySet_Test_Impl ", Psi_drrn_EmptySet_Test_Impl); 49 | 50 | //t.add("DktPsi_EmptySet_Test_Impl ", DktMPsi_EmptySet_Test_Impl); 51 | //t.add("DktPsi_FullSet_Test_Impl ", DktMPsi_FullSet_Test_Impl); 52 | //t.add("DktPsi_SingltonSet_Test_Imp ", DktMPsi_SingltonSet_Test_Impl); 53 | 54 | 55 | t.add("DcwPsi_EmptySet_Test_Impl ", DcwRBfPsi_EmptySet_Test_Impl); 56 | t.add("DcwPsi_FullSet_Test_Impl ", DcwRBfPsi_FullSet_Test_Impl); 57 | t.add("DcwPsi_SingltonSet_Test_Imp ", DcwRBfPsi_SingltonSet_Test_Impl); 58 | 59 | t.add("RR16_EmptySet_Test_Impl ", AknBfPsi_EmptySet_Test_Impl); 60 | t.add("RR16_FullSet_Test_Impl ", AknBfPsi_FullSet_Test_Impl); 61 | t.add("RR16_SingltonSet_Test_Impl ", AknBfPsi_SingltonSet_Test_Impl); 62 | 63 | t.add("CuckooHasher_Test_Impl ", CuckooHasher_Test_Impl); 64 | t.add("CuckooHasher_parallel_Test_Impl ", CuckooHasher_parallel_Test_Impl); 65 | 66 | t.add("Rr17a_Oos_EmptySet_Test_Impl ", Rr17a_Oos_EmptySet_Test_Impl); 67 | t.add("Rr17a_Oos_SingltonSet_Test_Impl ", Rr17a_Oos_SingltonSet_Test_Impl); 68 | t.add("Rr17a_Oos_FullSet_Test_Impl ", Rr17a_Oos_FullSet_Test_Impl); 69 | t.add("Rr17a_Oos_parallel_FullSet_Test_Impl ", Rr17a_Oos_parallel_FullSet_Test_Impl); 70 | 71 | t.add("Rr17a_SM_EmptySet_Test_Impl ", Rr17a_SM_EmptySet_Test_Impl); 72 | t.add("Rr17a_SM_SingltonSet_Test_Impl ", Rr17a_SM_SingltonSet_Test_Impl); 73 | t.add("Rr17a_SM_FullSet_Test_Impl ", Rr17a_SM_FullSet_Test_Impl); 74 | t.add("Rr17a_SM_parallel_FullSet_Test_Impl ", Rr17a_SM_parallel_FullSet_Test_Impl); 75 | t.add("filebase_rr17a_bin_Test ", filebase_rr17a_bin_Test); 76 | 77 | t.add("Rr17b_Oos_EmptySet_Test_Impl ", Rr17b_Oos_EmptySet_Test_Impl); 78 | t.add("Rr17b_Oos_SingltonSet_Test_Impl ", Rr17b_Oos_SingltonSet_Test_Impl); 79 | t.add("Rr17b_Oos_FullSet_Test_Impl ", Rr17b_Oos_FullSet_Test_Impl); 80 | t.add("Rr17b_Oos_parallel_FullSet_Test_Impl ", Rr17b_Oos_parallel_FullSet_Test_Impl); 81 | 82 | 83 | t.add("Grr18_Oos_EmptySet_Test_Impl ", Grr18_Oos_EmptySet_Test_Impl); 84 | t.add("Grr18_Oos_FullSet_Test_Impl ", Grr18_Oos_FullSet_Test_Impl); 85 | t.add("Grr18_Oos_parallel_FullSet_Test_Impl ", Grr18_Oos_parallel_FullSet_Test_Impl); 86 | t.add("Grr18_Oos_SingltonSet_Test_Impl ", Grr18_Oos_SingltonSet_Test_Impl); 87 | 88 | }); 89 | } 90 | -------------------------------------------------------------------------------- /libPSI_Tests/UnitTests.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | namespace libPSI_Tests 5 | { 6 | extern osuCrypto::TestCollection Tests; 7 | } -------------------------------------------------------------------------------- /thirdparty/fetch.cmake: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | if(DEFINED LOG_FILE AND NOT VERBOSE_FETCH) 5 | set(LOG_SETTING OUTPUT_FILE ${LOG_FILE} ERROR_FILE ${LOG_FILE} ${OUTPUT_QUIET}) 6 | else() 7 | unset(LOG_SETTING) 8 | endif() 9 | 10 | function(RUN) 11 | cmake_parse_arguments( 12 | PARSED_ARGS # prefix of parameters 13 | "" # list of names of the boolean arguments (only defined ones will be true) 14 | "WD" # list of names of mono-valued arguments 15 | "CMD;NAME" # list of names of multi-valued arguments (output variables are lists) 16 | ${ARGN} # arguments of the function to parse, here we take the all original ones 17 | ) 18 | message("${PARSED_ARGS_NAME}") 19 | file(APPEND ${LOG_FILE} 20 | "vvvvvvvvvvvvv RUN ${PARSED_ARGS_NAME} vvvvvvvvvvvv\n" 21 | "${PARSED_ARGS_CMD}\n" 22 | "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" 23 | ) 24 | 25 | execute_process( 26 | COMMAND ${PARSED_ARGS_CMD} 27 | WORKING_DIRECTORY ${PARSED_ARGS_WD} 28 | RESULT_VARIABLE RESULT 29 | ) 30 | if(RESULT) 31 | message(FATAL_ERROR "${PARSED_ARGS_NAME} failed") 32 | endif() 33 | endfunction() 34 | 35 | 36 | 37 | if(NOT MSVC AND SUDO_FETCH) 38 | set(SUDO "sudo ") 39 | endif() 40 | 41 | if(NOT DEFINED PARALLEL_FETCH) 42 | include(ProcessorCount) 43 | ProcessorCount(NUM_PROCESSORS) 44 | if(NOT NUM_PROCESSORS EQUAL 0) 45 | set(PARALLEL_FETCH ${NUM_PROCESSORS}) 46 | else() 47 | set(PARALLEL_FETCH 1) 48 | endif() 49 | endif() -------------------------------------------------------------------------------- /thirdparty/getLibOTe.cmake: -------------------------------------------------------------------------------- 1 | 2 | set(DEP_NAME libOTe) 3 | set(GIT_REPOSITORY "https://github.com/osu-crypto/libOTe.git") 4 | set(GIT_TAG "f47217d924ceef9b65e04a9ae680b65a79a4425a") 5 | 6 | set(OUT_DIR "${CMAKE_CURRENT_LIST_DIR}/../out/") 7 | set(CLONE_DIR "${OUT_DIR}/${DEP_NAME}") 8 | set(BUILD_DIR "${CLONE_DIR}/out/build/${LIBPSI_CONFIG}") 9 | set(LOG_FILE "${CMAKE_CURRENT_LIST_DIR}/log-${DEP_NAME}.txt") 10 | 11 | # defines the run(...) function. 12 | include("${CMAKE_CURRENT_LIST_DIR}/fetch.cmake") 13 | 14 | # an extra option to always build libOTe. useful if your also editing the subproject 15 | option(LIBOTE_DEV "always build libOTe" OFF) 16 | 17 | # only download if we haven't already found libOTe 18 | if(NOT EXISTS ${BUILD_DIR} OR NOT ${DEP_NAME}_FOUND OR LIBOTE_DEV) 19 | 20 | # find git 21 | find_program(GIT git REQUIRED) 22 | 23 | # define the commands we need to run: download, checkout, submodule update, cmake config, build, local install 24 | set(DOWNLOAD_CMD ${GIT} clone --recursive ${GIT_REPOSITORY}) 25 | set(CHECKOUT_CMD ${GIT} checkout ${GIT_TAG}) 26 | set(SUBMODULE_CMD ${GIT} submodule update --recursive) 27 | set(CONFIGURE_CMD ${CMAKE_COMMAND} -S ${CLONE_DIR} -B ${BUILD_DIR} -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} 28 | -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE} 29 | -DFETCH_AUTO=ON 30 | -DLIBOTE_STD_VER=${LIBPSI_STD_VER} 31 | -DVERBOSE_FETCH=${VERBOSE_FETCH} 32 | -DENABLE_ALL_OT=ON 33 | -DENABLE_RELIC=${ENABLE_RELIC} 34 | -DENABLE_SODIUM=${ENABLE_SODIUM} 35 | -DENABLE_BOOST=ON 36 | ) 37 | set(BUILD_CMD ${CMAKE_COMMAND} --build ${BUILD_DIR} --config ${CMAKE_BUILD_TYPE}) 38 | 39 | # when we fetch a project, we install it into the local folder LIBPSI_THIRDPARTY_DIR 40 | # if the user later calls "cmake install", then we will install it to their requested 41 | # location too. See below for this. 42 | set(INSTALL_CMD ${CMAKE_COMMAND} --install ${BUILD_DIR} --config ${CMAKE_BUILD_TYPE} --prefix ${LIBPSI_THIRDPARTY_DIR}) 43 | 44 | # execute the fetch commands. 45 | message("============= Building ${DEP_NAME} =============") 46 | if(NOT EXISTS ${CLONE_DIR}) 47 | run(NAME "Cloning ${GIT_REPOSITORY} into ${OUT_DIR}" CMD ${DOWNLOAD_CMD} WD ${OUT_DIR}) 48 | endif() 49 | 50 | run(NAME "Checkout ${GIT_TAG} " CMD ${CHECKOUT_CMD} WD ${CLONE_DIR}) 51 | run(NAME "submodule" CMD ${SUBMODULE_CMD} WD ${CLONE_DIR}) 52 | run(NAME "Configure" CMD ${CONFIGURE_CMD} WD ${CLONE_DIR}) 53 | run(NAME "Build" CMD ${BUILD_CMD} WD ${CLONE_DIR}) 54 | run(NAME "Install" CMD ${INSTALL_CMD} WD ${CLONE_DIR}) 55 | 56 | message("log ${LOG_FILE}\n==========================================") 57 | else() 58 | 59 | # if we already found libOTe, then just print that. 60 | message("${DEP_NAME} already fetched.") 61 | endif() 62 | 63 | # this command gets run when the user calls cmake install. This will install libOTe. 64 | install(CODE " 65 | execute_process( 66 | COMMAND ${SUDO} \${CMAKE_COMMAND} --install \"${BUILD_DIR}\" --config ${CMAKE_BUILD_TYPE} --prefix \${CMAKE_INSTALL_PREFIX} 67 | WORKING_DIRECTORY \"${CLONE_DIR}\" 68 | RESULT_VARIABLE RESULT 69 | COMMAND_ECHO STDOUT 70 | ) 71 | ") 72 | -------------------------------------------------------------------------------- /thirdparty/getSparsehash.cmake: -------------------------------------------------------------------------------- 1 | set(DEP_NAME sparsehash-c11) 2 | set(GIT_REPOSITORY https://github.com/sparsehash/sparsehash-c11.git) 3 | set(GIT_TAG "edd6f1180156e76facc1c0449da245208ab39503" ) 4 | 5 | set(CLONE_DIR "${CMAKE_CURRENT_LIST_DIR}/${DEP_NAME}") 6 | set(LOG_FILE "${CMAKE_CURRENT_LIST_DIR}/log-${DEP_NAME}.txt") 7 | 8 | 9 | # defines the run(...) function. 10 | include("${CMAKE_CURRENT_LIST_DIR}/fetch.cmake") 11 | 12 | # only download if we haven't already found sparsehash 13 | if(NOT EXISTS ${CLONE_DIR} OR NOT SPARSEHASH_FOUND) 14 | 15 | # find git 16 | find_program(GIT git REQUIRED) 17 | 18 | # download the source and check out the right commit. 19 | set(DOWNLOAD_CMD ${GIT} clone ${GIT_REPOSITORY}) 20 | set(CHECKOUT_CMD ${GIT} checkout ${GIT_TAG}) 21 | 22 | # run the commands. 23 | message("============= Building ${DEP_NAME} =============") 24 | if(NOT EXISTS ${CLONE_DIR}) 25 | run(NAME "Cloning ${GIT_REPOSITORY}" CMD ${DOWNLOAD_CMD} WD ${CMAKE_CURRENT_LIST_DIR}) 26 | endif() 27 | run(NAME "Checkout ${GIT_TAG} " CMD ${CHECKOUT_CMD} WD ${CLONE_DIR}) 28 | 29 | # install the headers into our local install directory LIBPSI_THIRDPARTY_DIR. 30 | # If the user calls cmake install, we will also install it to the requested location. 31 | # see below for the code that does this. 32 | file(COPY ${CLONE_DIR}/sparsehash DESTINATION ${LIBPSI_THIRDPARTY_DIR}/include/) 33 | 34 | message("log ${LOG_FILE}\n==========================================") 35 | else() 36 | # dont download and install sparsehash if its already found. 37 | message("${DEP_NAME} already fetched.") 38 | endif() 39 | 40 | # this command gets run when the user calls cmake install. This will install sparsehash. 41 | install( 42 | DIRECTORY "${CLONE_DIR}/sparsehash" 43 | DESTINATION "include") 44 | 45 | --------------------------------------------------------------------------------