-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add APIs for hashing and merkle trees commit+verify
- Loading branch information
Showing
38 changed files
with
1,967 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
|
||
#include "icicle/backend/hash/keccak_backend.h" | ||
|
||
namespace icicle { | ||
|
||
class KeccakBackend : public HashBackend | ||
{ | ||
public: | ||
KeccakBackend(uint64_t input_chunk_size, uint64_t output_size, uint64_t rate, int padding_const) | ||
: HashBackend(output_size, input_chunk_size) | ||
{ | ||
} | ||
|
||
eIcicleError hash(const std::byte* input, uint64_t size, const HashConfig& config, std::byte* output) const override | ||
{ | ||
ICICLE_LOG_INFO << "Keccak CPU hash() called"; | ||
// TODO implement | ||
return eIcicleError::SUCCESS; | ||
} | ||
}; | ||
|
||
const int KECCAK_256_RATE = 136; | ||
const int KECCAK_256_DIGEST = 4; | ||
const int KECCAK_512_RATE = 72; | ||
const int KECCAK_512_DIGEST = 8; | ||
const int KECCAK_STATE_SIZE = 25; | ||
const int KECCAK_PADDING_CONST = 1; | ||
const int SHA3_PADDING_CONST = 6; | ||
|
||
class Keccak256Backend : public KeccakBackend | ||
{ | ||
public: | ||
Keccak256Backend(int input_chunk_size) | ||
: KeccakBackend( | ||
input_chunk_size, | ||
KECCAK_256_DIGEST * sizeof(uint64_t) / sizeof(std::byte), | ||
KECCAK_256_RATE, | ||
KECCAK_PADDING_CONST) | ||
{ | ||
} | ||
}; | ||
|
||
class Keccak512Backend : public KeccakBackend | ||
{ | ||
public: | ||
Keccak512Backend(int input_chunk_size) | ||
: KeccakBackend( | ||
input_chunk_size, | ||
KECCAK_512_DIGEST * sizeof(uint64_t) / sizeof(std::byte), | ||
KECCAK_512_RATE, | ||
KECCAK_PADDING_CONST) | ||
{ | ||
} | ||
}; | ||
|
||
class Sha3_256Backend : public KeccakBackend | ||
{ | ||
public: | ||
Sha3_256Backend(int input_chunk_size) | ||
: KeccakBackend( | ||
input_chunk_size, | ||
KECCAK_256_DIGEST * sizeof(uint64_t) / sizeof(std::byte), | ||
KECCAK_256_RATE, | ||
SHA3_PADDING_CONST) | ||
{ | ||
} | ||
}; | ||
|
||
class Sha3_512Backend : public KeccakBackend | ||
{ | ||
public: | ||
Sha3_512Backend(int input_chunk_size) | ||
: KeccakBackend( | ||
input_chunk_size, | ||
KECCAK_512_DIGEST * sizeof(uint64_t) / sizeof(std::byte), | ||
KECCAK_512_RATE, | ||
SHA3_PADDING_CONST) | ||
{ | ||
} | ||
}; | ||
|
||
/************************ Keccak 256 registration ************************/ | ||
eIcicleError | ||
create_keccak_256_hash_backend(const Device& device, uint64_t input_chunk_size, std::shared_ptr<HashBackend>& backend) | ||
{ | ||
backend = std::make_shared<Keccak256Backend>(input_chunk_size); | ||
return eIcicleError::SUCCESS; | ||
} | ||
|
||
REGISTER_KECCAK_256_FACTORY_BACKEND("CPU", create_keccak_256_hash_backend); | ||
|
||
/************************ Keccak 512 registration ************************/ | ||
eIcicleError | ||
create_keccak_512_hash_backend(const Device& device, uint64_t input_chunk_size, std::shared_ptr<HashBackend>& backend) | ||
{ | ||
backend = std::make_shared<Keccak512Backend>(input_chunk_size); | ||
return eIcicleError::SUCCESS; | ||
} | ||
|
||
REGISTER_KECCAK_512_FACTORY_BACKEND("CPU", create_keccak_512_hash_backend); | ||
|
||
/************************ SHA3 256 registration ************************/ | ||
eIcicleError | ||
create_sha3_256_hash_backend(const Device& device, uint64_t input_chunk_size, std::shared_ptr<HashBackend>& backend) | ||
{ | ||
backend = std::make_shared<Sha3_256Backend>(input_chunk_size); | ||
return eIcicleError::SUCCESS; | ||
} | ||
|
||
REGISTER_SHA3_256_FACTORY_BACKEND("CPU", create_sha3_256_hash_backend); | ||
|
||
/************************ SHA3 512 registration ************************/ | ||
eIcicleError | ||
create_sha3_512_hash_backend(const Device& device, uint64_t input_chunk_size, std::shared_ptr<HashBackend>& backend) | ||
{ | ||
backend = std::make_shared<Sha3_512Backend>(input_chunk_size); | ||
return eIcicleError::SUCCESS; | ||
} | ||
|
||
REGISTER_SHA3_512_FACTORY_BACKEND("CPU", create_sha3_512_hash_backend); | ||
|
||
} // namespace icicle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "icicle/backend/merkle/merkle_tree_backend.h" | ||
#include "icicle/errors.h" | ||
#include "icicle/utils/log.h" | ||
|
||
namespace icicle { | ||
|
||
class CPUMerkleTreeBackend : public MerkleTreeBackend | ||
{ | ||
public: | ||
CPUMerkleTreeBackend( | ||
const std::vector<Hash>& layer_hashes, uint64_t leaf_element_size, uint64_t output_store_min_layer = 0) | ||
: MerkleTreeBackend(layer_hashes, leaf_element_size, output_store_min_layer) | ||
{ | ||
} | ||
|
||
eIcicleError build(const std::byte* leaves, uint64_t size, const MerkleTreeConfig& config) override | ||
{ | ||
ICICLE_LOG_INFO << "CPU CPUMerkleTreeBackend::build() called with " << size << " bytes of leaves"; | ||
return eIcicleError::SUCCESS; // TODO: Implement tree-building logic | ||
} | ||
|
||
eIcicleError get_merkle_root(std::byte* root, uint64_t root_size) const override | ||
{ | ||
ICICLE_LOG_INFO << "CPU CPUMerkleTreeBackend::get_merkle_root() called"; | ||
return eIcicleError::SUCCESS; // TODO: Implement root retrieval logic | ||
} | ||
|
||
eIcicleError get_merkle_proof( | ||
const std::byte* leaves, | ||
uint64_t element_idx, | ||
const MerkleTreeConfig& config, | ||
MerkleProof& merkle_proof) const override | ||
{ | ||
ICICLE_LOG_INFO << "CPU CPUMerkleTreeBackend::get_merkle_proof() called for element index " << element_idx; | ||
return eIcicleError::SUCCESS; // TODO: Implement proof generation logic | ||
} | ||
}; | ||
|
||
eIcicleError create_merkle_tree_backend( | ||
const Device& device, | ||
const std::vector<Hash>& layer_hashes, | ||
uint64_t leaf_element_size, | ||
uint64_t output_store_min_layer, | ||
std::shared_ptr<MerkleTreeBackend>& backend) | ||
{ | ||
ICICLE_LOG_INFO << "Creating CPU MerkleTreeBackend"; | ||
backend = std::make_shared<CPUMerkleTreeBackend>(layer_hashes, leaf_element_size, output_store_min_layer); | ||
return eIcicleError::SUCCESS; | ||
} | ||
|
||
REGISTER_MERKLE_TREE_FACTORY_BACKEND("CPU", create_merkle_tree_backend); | ||
|
||
} // namespace icicle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include "icicle/backend/hash/poseidon_backend.h" | ||
#include "icicle/utils/utils.h" | ||
|
||
namespace icicle { | ||
|
||
template <typename S> | ||
class PoseidonConstantsCPU : PoseidonConstants<S> | ||
{ | ||
// TODO add field here | ||
S* m_dummy_poseidon_constant; | ||
}; | ||
|
||
static eIcicleError cpu_poseidon_init_constants( | ||
const Device& device, | ||
unsigned arity, | ||
unsigned alpha, | ||
unsigned nof_partial_rounds, | ||
unsigned nof_upper_full_rounds, | ||
unsigned nof_end_full_rounds, | ||
const scalar_t* rounds_constants, | ||
const scalar_t* mds_matrix, | ||
const scalar_t* pre_matrix, | ||
const scalar_t* sparse_matrix, | ||
std::shared_ptr<PoseidonConstants<scalar_t>>& constants /*out*/) | ||
{ | ||
ICICLE_LOG_INFO << "in cpu_poseidon_init_constants()"; | ||
// TODO implement | ||
return eIcicleError::SUCCESS; | ||
} | ||
|
||
REGISTER_POSEIDON_INIT_CONSTANTS_BACKEND("CPU", cpu_poseidon_init_constants); | ||
|
||
static eIcicleError cpu_poseidon_init_default_constants( | ||
const Device& device, unsigned arity, std::shared_ptr<PoseidonConstants<scalar_t>>& constants /*out*/) | ||
{ | ||
ICICLE_LOG_INFO << "in cpu_poseidon_init_default_constants()"; | ||
// TODO implement | ||
return eIcicleError::SUCCESS; | ||
} | ||
|
||
REGISTER_POSEIDON_INIT_DEFAULT_CONSTANTS_BACKEND("CPU", cpu_poseidon_init_default_constants); | ||
|
||
template <typename S> | ||
class PoseidonBackendCPU : public HashBackend | ||
{ | ||
public: | ||
PoseidonBackendCPU(std::shared_ptr<PoseidonConstants<S>> constants) | ||
: HashBackend(sizeof(S), 0 /*TODO get from constants arity of whatever*/), m_constants{constants} | ||
{ | ||
} | ||
|
||
eIcicleError hash(const std::byte* input, uint64_t size, const HashConfig& config, std::byte* output) const override | ||
{ | ||
ICICLE_LOG_INFO << "Poseidon CPU hash() " << size << " bytes, for type " << demangle<S>(); | ||
// TODO implement | ||
return eIcicleError::SUCCESS; | ||
} | ||
|
||
private: | ||
std::shared_ptr<PoseidonConstants<S>> m_constants = nullptr; | ||
}; | ||
|
||
static eIcicleError create_cpu_poseidon_hash_backend( | ||
const Device& device, | ||
std::shared_ptr<PoseidonConstants<scalar_t>> constants, | ||
std::shared_ptr<HashBackend>& backend /*OUT*/) | ||
{ | ||
backend = std::make_shared<PoseidonBackendCPU<scalar_t>>(constants); | ||
return eIcicleError::SUCCESS; | ||
} | ||
|
||
REGISTER_CREATE_POSEIDON_BACKEND("CPU", create_cpu_poseidon_hash_backend); | ||
|
||
} // namespace icicle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
|
||
function(setup_hash_target) | ||
add_library(icicle_hash SHARED) | ||
target_sources(icicle_hash PRIVATE | ||
src/hash/keccak.cpp | ||
src/hash/merkle_tree.cpp | ||
) | ||
|
||
target_link_libraries(icicle_hash PUBLIC icicle_device) | ||
|
||
install(TARGETS icicle_hash | ||
RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/" | ||
LIBRARY DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/" | ||
ARCHIVE DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/") | ||
endfunction() | ||
|
Oops, something went wrong.