|
| 1 | +/* |
| 2 | + This file is part of cpp-ethereum. |
| 3 | +
|
| 4 | + cpp-ethereum is free software: you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU General Public License as published by |
| 6 | + the Free Software Foundation, either version 3 of the License, or |
| 7 | + (at your option) any later version. |
| 8 | +
|
| 9 | + cpp-ethereum is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + GNU General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU General Public License |
| 15 | + along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | +/** |
| 18 | + * @file LibSnark.cpp |
| 19 | + */ |
| 20 | + |
| 21 | +#include <libdevcrypto/LibSnark.h> |
| 22 | +// libsnark has to be compiled with exactly the same switches: |
| 23 | +// sudo PREFIX=/usr/local make NO_PROCPS=1 NO_GTEST=1 NO_DOCS=1 CURVE=ALT_BN128 FEATUREFLAGS="-DBINARY_OUTPUT=1 -DMONTGOMERY_OUTPUT=1 -DNO_PT_COMPRESSION=1" lib install |
| 24 | +#define BINARY_OUTPUT 1 |
| 25 | +#define MONTGOMERY_OUTPUT 1 |
| 26 | +#define NO_PT_COMPRESSION 1 |
| 27 | + |
| 28 | +#include <libsnark/algebra/curves/alt_bn128/alt_bn128_g1.hpp> |
| 29 | +#include <libsnark/algebra/curves/alt_bn128/alt_bn128_g2.hpp> |
| 30 | +#include <libsnark/algebra/curves/alt_bn128/alt_bn128_pairing.hpp> |
| 31 | +#include <libsnark/algebra/curves/alt_bn128/alt_bn128_pp.hpp> |
| 32 | +#include <libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/r1cs_ppzksnark.hpp> |
| 33 | + |
| 34 | +#include <libdevcrypto/Exceptions.h> |
| 35 | + |
| 36 | +#include <libdevcore/CommonIO.h> |
| 37 | +#include <libdevcore/Log.h> |
| 38 | +#include <libdevcore/FixedHash.h> |
| 39 | + |
| 40 | +#include <fstream> |
| 41 | + |
| 42 | +using namespace std; |
| 43 | +using namespace dev; |
| 44 | +using namespace dev::crypto; |
| 45 | + |
| 46 | +namespace |
| 47 | +{ |
| 48 | + |
| 49 | +void initLibSnark() |
| 50 | +{ |
| 51 | + static bool initialized = 0; |
| 52 | + if (!initialized) |
| 53 | + { |
| 54 | + libsnark::alt_bn128_pp::init_public_params(); |
| 55 | + // Otherwise the library would output profiling info for each run. |
| 56 | + libsnark::inhibit_profiling_counters = true; |
| 57 | + libsnark::inhibit_profiling_info = true; |
| 58 | + initialized = true; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +libsnark::bigint<libsnark::alt_bn128_q_limbs> toLibsnarkBigint(h256 const& _x) |
| 63 | +{ |
| 64 | + libsnark::bigint<libsnark::alt_bn128_q_limbs> x; |
| 65 | + for (unsigned i = 0; i < 4; i++) |
| 66 | + for (unsigned j = 0; j < 8; j++) |
| 67 | + x.data[3 - i] |= uint64_t(_x[i * 8 + j]) << (8 * (7 - j)); |
| 68 | + return x; |
| 69 | +} |
| 70 | + |
| 71 | +h256 fromLibsnarkBigint(libsnark::bigint<libsnark::alt_bn128_q_limbs> _x) |
| 72 | +{ |
| 73 | + h256 x; |
| 74 | + for (unsigned i = 0; i < 4; i++) |
| 75 | + for (unsigned j = 0; j < 8; j++) |
| 76 | + x[i * 8 + j] = uint8_t(uint64_t(_x.data[3 - i]) >> (8 * (7 - j))); |
| 77 | + return x; |
| 78 | +} |
| 79 | + |
| 80 | +libsnark::alt_bn128_Fq decodeFqElement(dev::bytesConstRef _data) |
| 81 | +{ |
| 82 | + // h256::AlignLeft ensures that the h256 is zero-filled on the right if _data |
| 83 | + // is too short. |
| 84 | + h256 xbin(_data, h256::AlignLeft); |
| 85 | + if (u256(xbin) >= u256(fromLibsnarkBigint(libsnark::alt_bn128_Fq::mod))) |
| 86 | + BOOST_THROW_EXCEPTION(InvalidEncoding()); |
| 87 | + return toLibsnarkBigint(xbin); |
| 88 | +} |
| 89 | + |
| 90 | +libsnark::alt_bn128_G1 decodePointG1(dev::bytesConstRef _data) |
| 91 | +{ |
| 92 | + libsnark::alt_bn128_Fq X = decodeFqElement(_data.cropped(0)); |
| 93 | + libsnark::alt_bn128_Fq Y = decodeFqElement(_data.cropped(32)); |
| 94 | + if (X == libsnark::alt_bn128_Fq::zero() && Y == libsnark::alt_bn128_Fq::zero()) |
| 95 | + return libsnark::alt_bn128_G1::zero(); |
| 96 | + libsnark::alt_bn128_G1 p(X, Y, libsnark::alt_bn128_Fq::one()); |
| 97 | + if (!p.is_well_formed()) |
| 98 | + BOOST_THROW_EXCEPTION(InvalidEncoding()); |
| 99 | + return p; |
| 100 | +} |
| 101 | + |
| 102 | +bytes encodePointG1(libsnark::alt_bn128_G1 _p) |
| 103 | +{ |
| 104 | + if (_p.is_zero()) |
| 105 | + return h256().asBytes() + h256().asBytes(); |
| 106 | + _p.to_affine_coordinates(); |
| 107 | + return |
| 108 | + fromLibsnarkBigint(_p.X.as_bigint()).asBytes() + |
| 109 | + fromLibsnarkBigint(_p.Y.as_bigint()).asBytes(); |
| 110 | +} |
| 111 | + |
| 112 | +libsnark::alt_bn128_Fq2 decodeFq2Element(dev::bytesConstRef _data) |
| 113 | +{ |
| 114 | + // Encoding: c1 (256 bits) c0 (256 bits) |
| 115 | + // "Big endian", just like the numbers |
| 116 | + return libsnark::alt_bn128_Fq2( |
| 117 | + decodeFqElement(_data.cropped(32)), |
| 118 | + decodeFqElement(_data.cropped(0)) |
| 119 | + ); |
| 120 | +} |
| 121 | + |
| 122 | + |
| 123 | +libsnark::alt_bn128_G2 decodePointG2(dev::bytesConstRef _data) |
| 124 | +{ |
| 125 | + libsnark::alt_bn128_Fq2 X = decodeFq2Element(_data); |
| 126 | + libsnark::alt_bn128_Fq2 Y = decodeFq2Element(_data.cropped(64)); |
| 127 | + if (X == libsnark::alt_bn128_Fq2::zero() && Y == libsnark::alt_bn128_Fq2::zero()) |
| 128 | + return libsnark::alt_bn128_G2::zero(); |
| 129 | + libsnark::alt_bn128_G2 p(X, Y, libsnark::alt_bn128_Fq2::one()); |
| 130 | + if (!p.is_well_formed()) |
| 131 | + BOOST_THROW_EXCEPTION(InvalidEncoding()); |
| 132 | + return p; |
| 133 | +} |
| 134 | + |
| 135 | +} |
| 136 | + |
| 137 | +pair<bool, bytes> dev::crypto::alt_bn128_pairing_product(dev::bytesConstRef _in) |
| 138 | +{ |
| 139 | + // Input: list of pairs of G1 and G2 points |
| 140 | + // Output: 1 if pairing evaluates to 1, 0 otherwise (left-padded to 32 bytes) |
| 141 | + |
| 142 | + bool result = true; |
| 143 | + size_t const pairSize = 2 * 32 + 2 * 64; |
| 144 | + size_t const pairs = _in.size() / pairSize; |
| 145 | + if (pairs * pairSize != _in.size()) |
| 146 | + { |
| 147 | + // Invalid length. |
| 148 | + return make_pair(false, bytes()); |
| 149 | + } |
| 150 | + try |
| 151 | + { |
| 152 | + initLibSnark(); |
| 153 | + libsnark::alt_bn128_Fq12 x = libsnark::alt_bn128_Fq12::one(); |
| 154 | + for (size_t i = 0; i < pairs; ++i) |
| 155 | + { |
| 156 | + dev::bytesConstRef pair = _in.cropped(i * pairSize, pairSize); |
| 157 | + libsnark::alt_bn128_G2 p = decodePointG2(pair.cropped(2 * 32)); |
| 158 | + if (-libsnark::alt_bn128_G2::scalar_field::one() * p + p != libsnark::alt_bn128_G2::zero()) |
| 159 | + // p is not an element of the group (has wrong order) |
| 160 | + return {false, bytes()}; |
| 161 | + x = x * libsnark::alt_bn128_miller_loop( |
| 162 | + libsnark::alt_bn128_precompute_G1(decodePointG1(pair)), |
| 163 | + libsnark::alt_bn128_precompute_G2(p) |
| 164 | + ); |
| 165 | + } |
| 166 | + result = libsnark::alt_bn128_final_exponentiation(x) == libsnark::alt_bn128_GT::one(); |
| 167 | + } |
| 168 | + catch (InvalidEncoding const&) |
| 169 | + { |
| 170 | + return make_pair(false, bytes()); |
| 171 | + } |
| 172 | + catch (...) |
| 173 | + { |
| 174 | + cwarn << "Internal exception from libsnark. Forwarding as precompiled contract failure."; |
| 175 | + return make_pair(false, bytes()); |
| 176 | + } |
| 177 | + |
| 178 | + bytes res(32, 0); |
| 179 | + res[31] = unsigned(result); |
| 180 | + return {true, res}; |
| 181 | +} |
| 182 | + |
| 183 | +pair<bool, bytes> dev::crypto::alt_bn128_G1_add(dev::bytesConstRef _in) |
| 184 | +{ |
| 185 | + try |
| 186 | + { |
| 187 | + initLibSnark(); |
| 188 | + libsnark::alt_bn128_G1 p1 = decodePointG1(_in); |
| 189 | + libsnark::alt_bn128_G1 p2 = decodePointG1(_in.cropped(32 * 2)); |
| 190 | + |
| 191 | + return {true, encodePointG1(p1 + p2)}; |
| 192 | + } |
| 193 | + catch (InvalidEncoding const&) |
| 194 | + { |
| 195 | + } |
| 196 | + catch (...) |
| 197 | + { |
| 198 | + cwarn << "Internal exception from libsnark. Forwarding as precompiled contract failure."; |
| 199 | + } |
| 200 | + return make_pair(false, bytes()); |
| 201 | +} |
| 202 | + |
| 203 | +pair<bool, bytes> dev::crypto::alt_bn128_G1_mul(dev::bytesConstRef _in) |
| 204 | +{ |
| 205 | + try |
| 206 | + { |
| 207 | + initLibSnark(); |
| 208 | + libsnark::alt_bn128_G1 p = decodePointG1(_in.cropped(0)); |
| 209 | + |
| 210 | + libsnark::alt_bn128_G1 result = toLibsnarkBigint(h256(_in.cropped(64), h256::AlignLeft)) * p; |
| 211 | + |
| 212 | + return {true, encodePointG1(result)}; |
| 213 | + } |
| 214 | + catch (InvalidEncoding const&) |
| 215 | + { |
| 216 | + } |
| 217 | + catch (...) |
| 218 | + { |
| 219 | + cwarn << "Internal exception from libsnark. Forwarding as precompiled contract failure."; |
| 220 | + } |
| 221 | + return make_pair(false, bytes()); |
| 222 | +} |
0 commit comments