Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof_of_work for non-poseidon hash reverted#5 #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,59 @@ namespace nil {
return ((result & mask) == 0);
}
};

template<typename TranscriptHashType, typename OutType = std::uint32_t, std::uint32_t MASK=0xFFFF0000>
class proof_of_work {
public:
using transcript_hash_type = TranscriptHashType;
using transcript_type = transcript::fiat_shamir_heuristic_sequential<transcript_hash_type>;
using output_type = OutType;

constexpr static std::uint32_t mask = MASK;

static inline boost::property_tree::ptree get_params() {
boost::property_tree::ptree params;
params.put("mask", mask);
return params;
}

static inline OutType generate(transcript_type &transcript) {
output_type proof_of_work = std::rand();
output_type result;
std::vector<std::uint8_t> bytes(4);

while( true ) {
transcript_type tmp_transcript = transcript;
bytes[0] = std::uint8_t((proof_of_work&0xFF000000)>>24);
bytes[1] = std::uint8_t((proof_of_work&0x00FF0000)>>16);
bytes[2] = std::uint8_t((proof_of_work&0x0000FF00)>>8);
bytes[3] = std::uint8_t(proof_of_work&0x000000FF);

tmp_transcript(bytes);
result = tmp_transcript.template int_challenge<output_type>();
if ((result & mask) == 0)
break;
proof_of_work++;
}
transcript(bytes);
result = transcript.template int_challenge<output_type>();
return proof_of_work;
}

static inline bool verify(transcript_type &transcript, output_type proof_of_work) {
std::vector<std::uint8_t> bytes(4);
bytes[0] = std::uint8_t((proof_of_work&0xFF000000)>>24);
bytes[1] = std::uint8_t((proof_of_work&0x00FF0000)>>16);
bytes[2] = std::uint8_t((proof_of_work&0x0000FF00)>>8);
bytes[3] = std::uint8_t(proof_of_work&0x000000FF);
transcript(bytes);
output_type result = transcript.template int_challenge<output_type>();
return ((result & mask) == 0);
}
};
}
}
}
}

#endif // ACTOR_PROOF_OF_WORK_HPP
#endif // ACTOR_PROOF_OF_WORK_HPP