Skip to content

Commit

Permalink
All ints in quality calculation, quality to quality string
Browse files Browse the repository at this point in the history
  • Loading branch information
mariano54 committed Feb 10, 2020
1 parent edf4ce8 commit 0a49ad0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
10 changes: 4 additions & 6 deletions python-bindings/chiapos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@ PYBIND11_MODULE(chiapos, m) {
const uint8_t* challenge_ptr = reinterpret_cast<const uint8_t*>(challenge_str.data());
std::vector<LargeBits> qualities = dp.GetQualitiesForChallenge(challenge_ptr);
std::vector<py::bytes> ret;
uint8_t* quality_buf = new uint8_t[Util::ByteAlign(2 * dp.GetSize()) / 8];
uint8_t* quality_buf = new uint8_t[32];
for (LargeBits quality : qualities) {
quality.ToBytes(quality_buf);
py::bytes quality_py = py::bytes(reinterpret_cast<char*>(quality_buf),
Util::ByteAlign(2 * dp.GetSize()) / 8);
py::bytes quality_py = py::bytes(reinterpret_cast<char*>(quality_buf), 32);
ret.push_back(quality_py);
}
delete[] quality_buf;
Expand Down Expand Up @@ -113,10 +112,9 @@ PYBIND11_MODULE(chiapos, m) {
if (quality.GetSize() == 0) {
return stdx::optional<py::bytes>();
}
uint8_t* quality_buf = new uint8_t[Util::ByteAlign(2 * k) / 8];
uint8_t* quality_buf = new uint8_t[32];
quality.ToBytes(quality_buf);
py::bytes quality_py = py::bytes(reinterpret_cast<char*>(quality_buf),
Util::ByteAlign(2 * k) / 8);
py::bytes quality_py = py::bytes(reinterpret_cast<char*>(quality_buf), 32);
delete[] quality_buf;
return stdx::optional<py::bytes>(quality_py);
});
Expand Down
2 changes: 1 addition & 1 deletion src/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ int main(int argc, char *argv[]) {
DiskProver prover(filename);
vector<LargeBits> qualities = prover.GetQualitiesForChallenge(challenge_bytes);
for (uint32_t i = 0; i < qualities.size(); i++) {
k = qualities[i].GetSize() / 2;
k = prover.GetSize() / 2;
uint8_t proof_data[8 * k];
LargeBits proof = prover.GetFullProof(challenge_bytes, i);
proof.ToBytes(proof_data);
Expand Down
12 changes: 9 additions & 3 deletions src/prover_disk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "encoding.hpp"
#include "calculate_bucket.hpp"
#include "plotter_disk.hpp"
#include "../lib/include/picosha2.hpp"

// The DiskProver, given a correctly formatted plot file, can efficiently generate valid proofs
// of space, for a given challenge.
Expand Down Expand Up @@ -127,7 +128,7 @@ class DiskProver {
return k;
}

// Given a challenge, returns a quality string, which is 2 adjecent x values,
// Given a challenge, returns a quality string, which is sha256(challenge + 2 adjecent x values),
// from the 64 value proof. Note that this is more efficient than fetching all
// 64 x values, which are in different parts of the disk.
std::vector<LargeBits> GetQualitiesForChallenge(const uint8_t* challenge) {
Expand Down Expand Up @@ -165,8 +166,13 @@ class DiskProver {
uint128_t new_line_point = ReadLinePoint(1, position);
auto x1x2 = Encoding::LinePointToSquare(new_line_point);

// The final two x values (which are stored in the same location) are returned.
qualities.push_back(LargeBits(x1x2.second, k) + LargeBits(x1x2.first, k));
// The final two x values (which are stored in the same location) are hashed
vector<unsigned char> hash_input(32 + Util::ByteAlign(2 * k) / 8, 0);
memcpy(hash_input.data(), challenge, 32);
(LargeBits(x1x2.second, k) + LargeBits(x1x2.first, k)).ToBytes(hash_input.data() + 32);
vector<unsigned char> hash(picosha2::k_digest_size);
picosha2::hash256(hash_input.begin(), hash_input.end(), hash.begin(), hash.end());
qualities.push_back(LargeBits(hash.data(), 32, 256));
}
disk_file.clear();
disk_file.sync();
Expand Down
19 changes: 12 additions & 7 deletions src/verifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

class Verifier {
public:
// Gets the quality string from a proof in proof ordering. The quality string is two
// adjacent values, determined by the quality index (1-32), and the proof in plot
// ordering.
LargeBits GetQualityString(uint8_t k, LargeBits proof, uint16_t quality_index) {
// Gets the quality string from a proof in proof ordering. The quality string is sha256 of
// the challenge + two adjacent values, determined by the quality index (1-32),
// and the proof in plot ordering.
LargeBits GetQualityString(uint8_t k, LargeBits proof, uint16_t quality_index, const uint8_t* challenge) {
// Converts the proof from proof ordering to plot ordering
for (uint8_t table_index = 1; table_index < 7; table_index++) {
LargeBits new_proof;
Expand All @@ -40,8 +40,13 @@ class Verifier {
}
proof = new_proof;
}
// Returns two of the x values, based on the quality index
return proof.Slice(k * quality_index, k * (quality_index + 2));
// Hashes two of the x values, based on the quality index
vector<unsigned char> hash_input(32 + Util::ByteAlign(2 * k) / 8, 0);
memcpy(hash_input.data(), challenge, 32);
proof.Slice(k * quality_index, k * (quality_index + 2)).ToBytes(hash_input.data() + 32);
vector<unsigned char> hash(picosha2::k_digest_size);
picosha2::hash256(hash_input.begin(), hash_input.end(), hash.begin(), hash.end());
return LargeBits(hash.data(), 32, 256);
}

// Validates a proof of space, and returns the quality string if the proof is valid for the given
Expand Down Expand Up @@ -107,7 +112,7 @@ class Verifier {
// Makes sure the output is equal to the first k bits of the challenge
if (challenge_bits.Slice(0, k) == ys[0].Slice(0, k)) {
// Returns quality string, which requires changing proof to plot ordering
return GetQualityString(k, proof_bits, quality_index);
return GetQualityString(k, proof_bits, quality_index, challenge);
} else {
return LargeBits();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ void TestProofOfSpace(std::string filename, uint32_t iterations, uint8_t k, uint
proof.ToBytes(proof_data);

LargeBits quality = verifier.ValidateProof(plot_id, k, hash.data(), proof_data, k*8);
REQUIRE(quality.GetSize() == 2 * k);
REQUIRE(quality.GetSize() == 256);
REQUIRE(quality == qualities[index]);
success += 1;

Expand Down

0 comments on commit 0a49ad0

Please sign in to comment.