Skip to content

Commit da60be2

Browse files
committed
[Sapling] init zk-snarks params.
1 parent dce8d15 commit da60be2

File tree

2 files changed

+112
-1
lines changed

2 files changed

+112
-1
lines changed

src/sapling/util.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,110 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include "sapling/util.h"
7+
#include "sync.h"
78
#include <algorithm>
9+
#include <librustzcash.h>
810
#include <stdexcept>
11+
#include <iostream>
12+
13+
static boost::filesystem::path zc_paramsPathCached;
14+
static RecursiveMutex csPathCached;
15+
16+
static boost::filesystem::path ZC_GetBaseParamsDir()
17+
{
18+
// Copied from GetDefaultDataDir and adapter for zcash params.
19+
20+
namespace fs = boost::filesystem;
21+
// Windows < Vista: C:\Documents and Settings\Username\Application Data\ZcashParams
22+
// Windows >= Vista: C:\Users\Username\AppData\Roaming\ZcashParams
23+
// Mac: ~/Library/Application Support/ZcashParams
24+
// Unix: ~/.zcash-params
25+
#ifdef WIN32
26+
// Windows
27+
return GetSpecialFolderPath(CSIDL_APPDATA) / "ZcashParams";
28+
#else
29+
fs::path pathRet;
30+
char* pszHome = getenv("HOME");
31+
if (pszHome == NULL || strlen(pszHome) == 0)
32+
pathRet = fs::path("/");
33+
else
34+
pathRet = fs::path(pszHome);
35+
#ifdef MAC_OSX
36+
// Mac
37+
pathRet /= "Library/Application Support";
38+
TryCreateDirectory(pathRet);
39+
return pathRet / "ZcashParams";
40+
#else
41+
// Unix
42+
return pathRet / ".zcash-params";
43+
#endif
44+
#endif
45+
}
46+
47+
const boost::filesystem::path &ZC_GetParamsDir()
48+
{
49+
namespace fs = boost::filesystem;
50+
51+
LOCK(csPathCached); // Reuse the same lock as upstream.
52+
53+
fs::path &path = zc_paramsPathCached;
54+
55+
// This can be called during exceptions by LogPrintf(), so we cache the
56+
// value so we don't have to do memory allocations after that.
57+
if (!path.empty())
58+
return path;
59+
60+
path = ZC_GetBaseParamsDir();
61+
62+
return path;
63+
}
64+
65+
/**
66+
* Ignores exceptions thrown by Boost's create_directory if the requested directory exists.
67+
* Specifically handles case where path p exists, but it wasn't possible for the user to
68+
* write to the parent directory.
69+
*/
70+
bool TryCreateDirectory(const boost::filesystem::path& p)
71+
{
72+
try {
73+
return boost::filesystem::create_directory(p);
74+
} catch (const boost::filesystem::filesystem_error&) {
75+
if (!boost::filesystem::exists(p) || !boost::filesystem::is_directory(p))
76+
throw;
77+
}
78+
79+
// create_directory didn't create the directory, it had to have existed already
80+
return false;
81+
}
82+
83+
void initZKSNARKS()
84+
{
85+
boost::filesystem::path sapling_spend = ZC_GetParamsDir() / "sapling-spend.params";
86+
boost::filesystem::path sapling_output = ZC_GetParamsDir() / "sapling-output.params";
87+
boost::filesystem::path sprout_groth16 = ZC_GetParamsDir() / "sprout-groth16.params";
88+
89+
if (!(boost::filesystem::exists(sapling_spend) &&
90+
boost::filesystem::exists(sapling_output) &&
91+
boost::filesystem::exists(sprout_groth16)
92+
)) {
93+
throw std::runtime_error("Sapling params don't exist");
94+
}
95+
96+
std::string sapling_spend_str = sapling_spend.string();
97+
std::string sapling_output_str = sapling_output.string();
98+
std::string sprout_groth16_str = sprout_groth16.string();
99+
100+
librustzcash_init_zksnark_params(
101+
sapling_spend_str.c_str(),
102+
"8270785a1a0d0bc77196f000ee6d221c9c9894f55307bd9357c3f0105d31ca63991ab91324160d8f53e2bbd3c2633a6eb8bdf5205d822e7f3f73edac51b2b70c",
103+
sapling_output_str.c_str(),
104+
"657e3d38dbb5cb5e7dd2970e8b03d69b4787dd907285b5a7f0790dcc8072f60bf593b32cc2d1c030e00ff5ae64bf84c5c3beb84ddc841d48264b4a171744d028",
105+
sprout_groth16_str.c_str(),
106+
"e9b238411bd6c0ec4791e9d04245ec350c9c5744f5610dfcce4365d5ca49dfefd5054e371842b3f88fa1b9d7e8e075249b3ebabd167fa8b0f3161292d36c180a"
107+
);
108+
109+
std::cout << "### Sapling params initialized ###" << std::endl;
110+
}
9111

10112
std::vector<unsigned char> convertIntToVectorLE(const uint64_t val_int) {
11113
std::vector<unsigned char> bytes;

src/sapling/util.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,20 @@
88

99
#include "sapling/uint252.h"
1010
#include "uint256.h"
11-
#include <sodium.h>
1211

12+
#include <sodium.h>
13+
#include <boost/filesystem.hpp>
14+
#include <boost/filesystem/fstream.hpp>
1315
#include <vector>
1416
#include <cstdint>
1517

18+
// Init sapling library
19+
void initZKSNARKS();
20+
21+
// Sapling network dir
22+
const boost::filesystem::path &ZC_GetParamsDir();
23+
bool TryCreateDirectory(const boost::filesystem::path& p);
24+
1625
std::vector<unsigned char> convertIntToVectorLE(const uint64_t val_int);
1726
std::vector<bool> convertBytesVectorToVector(const std::vector<unsigned char>& bytes);
1827
uint64_t convertVectorToInt(const std::vector<bool>& v);

0 commit comments

Comments
 (0)