Skip to content

Commit

Permalink
add keccak tree builder (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChickenLover authored Jul 15, 2024
1 parent 7fd9ed1 commit ea71faf
Show file tree
Hide file tree
Showing 74 changed files with 779 additions and 644 deletions.
18 changes: 9 additions & 9 deletions docs/docs/icicle/golang-bindings/keccak.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func main() {
input := createHostSliceFromHexString("1725b6")
outHost256 := make(core.HostSlice[uint8], 32)

cfg := keccak.GetDefaultKeccakConfig()
cfg := keccak.GetDefaultHashConfig()
e := keccak.Keccak256(input, int32(input.Len()), 1, outHost256, &cfg)
if e.CudaErrorCode != cr.CudaSuccess {
panic("Keccak256 hashing failed")
Expand All @@ -49,8 +49,8 @@ func main() {
## Keccak Methods

```go
func Keccak256(input core.HostOrDeviceSlice, inputBlockSize, numberOfBlocks int32, output core.HostOrDeviceSlice, config *KeccakConfig) core.IcicleError
func Keccak512(input core.HostOrDeviceSlice, inputBlockSize, numberOfBlocks int32, output core.HostOrDeviceSlice, config *KeccakConfig) core.IcicleError
func Keccak256(input core.HostOrDeviceSlice, inputBlockSize, numberOfBlocks int32, output core.HostOrDeviceSlice, config *HashConfig) core.IcicleError
func Keccak512(input core.HostOrDeviceSlice, inputBlockSize, numberOfBlocks int32, output core.HostOrDeviceSlice, config *HashConfig) core.IcicleError
```

### Parameters
Expand All @@ -59,18 +59,18 @@ func Keccak512(input core.HostOrDeviceSlice, inputBlockSize, numberOfBlocks int3
- **`inputBlockSize`**: An integer specifying the size of the input data for a single hash.
- **`numberOfBlocks`**: An integer specifying the number of results in the hash batch.
- **`output`**: A slice where the resulting hash will be stored. This slice can be in host or device memory.
- **`config`**: A pointer to a `KeccakConfig` object, which contains various configuration options for the Keccak256 operation.
- **`config`**: A pointer to a `HashConfig` object, which contains various configuration options for the Keccak256 operation.

### Return Value

- **`CudaError`**: Returns a CUDA error code indicating the success or failure of the Keccak256/Keccak512 operation.

## KeccakConfig
## HashConfig

The `KeccakConfig` structure holds configuration parameters for the Keccak256/Keccak512 operation, allowing customization of its behavior to optimize performance based on the specifics of the operation or the underlying hardware.
The `HashConfig` structure holds configuration parameters for the Keccak256/Keccak512 operation, allowing customization of its behavior to optimize performance based on the specifics of the operation or the underlying hardware.

```go
type KeccakConfig struct {
type HashConfig struct {
Ctx cr.DeviceContext
areInputsOnDevice bool
areOutputsOnDevice bool
Expand All @@ -87,8 +87,8 @@ type KeccakConfig struct {

### Default Configuration

Use `GetDefaultKeccakConfig` to obtain a default configuration, which can then be customized as needed.
Use `GetDefaultHashConfig` to obtain a default configuration, which can then be customized as needed.

```go
func GetDefaultKeccakConfig() KeccakConfig
func GetDefaultHashConfig() HashConfig
```
18 changes: 9 additions & 9 deletions docs/docs/icicle/rust-bindings/keccak.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

```rust
use icicle_cuda_runtime::memory::{DeviceVec, HostSlice};
use icicle_hash::keccak::{keccak256, KeccakConfig};
use icicle_hash::keccak::{keccak256, HashConfig};
use rand::{self, Rng};

fn main() {
Expand All @@ -14,7 +14,7 @@ fn main() {
let input = HostSlice::<u8>::from_slice(initial_data.as_slice());
let mut output = DeviceVec::<u8>::cuda_malloc(32).unwrap();

let mut config = KeccakConfig::default();
let mut config = HashConfig::default();
keccak256(input, initial_data.len() as i32, 1, &mut output[..], &mut config).expect("Failed to execute keccak256 hashing");

let mut output_host = vec![0_u8; 32];
Expand All @@ -32,15 +32,15 @@ pub fn keccak256(
input_block_size: i32,
number_of_blocks: i32,
output: &mut (impl HostOrDeviceSlice<u8> + ?Sized),
config: &mut KeccakConfig,
config: &mut HashConfig,
) -> IcicleResult<()>

pub fn keccak512(
input: &(impl HostOrDeviceSlice<u8> + ?Sized),
input_block_size: i32,
number_of_blocks: i32,
output: &mut (impl HostOrDeviceSlice<u8> + ?Sized),
config: &mut KeccakConfig,
config: &mut HashConfig,
) -> IcicleResult<()>
```

Expand All @@ -50,18 +50,18 @@ pub fn keccak512(
- **`input_block_size`**: An integer specifying the size of the input data for a single hash.
- **`number_of_blocks`**: An integer specifying the number of results in the hash batch.
- **`output`**: A slice where the resulting hash will be stored. This slice can be in host or device memory.
- **`config`**: A pointer to a `KeccakConfig` object, which contains various configuration options for the Keccak256 operation.
- **`config`**: A pointer to a `HashConfig` object, which contains various configuration options for the Keccak256 operation.

### Return Value

- **`IcicleResult`**: Returns a CUDA error code indicating the success or failure of the Keccak256/Keccak512 operation.

## KeccakConfig
## HashConfig

The `KeccakConfig` structure holds configuration parameters for the Keccak256/Keccak512 operation, allowing customization of its behavior to optimize performance based on the specifics of the operation or the underlying hardware.
The `HashConfig` structure holds configuration parameters for the Keccak256/Keccak512 operation, allowing customization of its behavior to optimize performance based on the specifics of the operation or the underlying hardware.

```rust
pub struct KeccakConfig<'a> {
pub struct HashConfig<'a> {
pub ctx: DeviceContext<'a>,
pub are_inputs_on_device: bool,
pub are_outputs_on_device: bool,
Expand All @@ -81,7 +81,7 @@ pub struct KeccakConfig<'a> {
Example initialization with default settings:

```rust
let default_config = KeccakConfig::default();
let default_config = HashConfig::default();
```

Customizing the configuration:
Expand Down
2 changes: 1 addition & 1 deletion examples/c++/multi-gpu-poseidon/example.cu
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void threadPoseidon(
std::cerr << "CUDA error: " << cudaGetErrorString(err_result) << std::endl;
return;
}
SpongeConfig column_config = default_sponge_config(ctx);
HashConfig column_config = default_hash_config(ctx);
cudaError_t err = poseidon->hash_many(layers, column_hashes, (size_t) size_partition, size_col, 1, column_config);
checkCudaError(err);
}
Expand Down
8 changes: 4 additions & 4 deletions examples/c++/poseidon/example.cu
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ inline uint32_t tree_index(uint32_t level, uint32_t offset) { return (1 << level

// We assume the tree has leaves already set, compute all other levels
void build_tree(
const uint32_t tree_height, scalar_t* tree, Poseidon<scalar_t> &poseidon, SpongeConfig &config)
const uint32_t tree_height, scalar_t* tree, Poseidon<scalar_t> &poseidon, HashConfig &config)
{
for (uint32_t level = tree_height - 1; level > 0; level--) {
const uint32_t next_level = level - 1;
Expand Down Expand Up @@ -67,7 +67,7 @@ uint32_t validate_proof(
const uint32_t* proof_lr,
const scalar_t* proof_hash,
Poseidon<scalar_t> &poseidon,
SpongeConfig &config)
HashConfig &config)
{
scalar_t hashes_in[2], hash_out[1], level_hash;
level_hash = hash;
Expand Down Expand Up @@ -112,12 +112,12 @@ int main(int argc, char* argv[])
std::cout << "Hashing blocks into tree leaves..." << std::endl;

Poseidon<scalar_t> poseidon(data_arity, ctx);
SpongeConfig config = default_sponge_config(ctx);
HashConfig config = default_hash_config(ctx);
poseidon.hash_many(data, &tree[tree_index(leaf_level, 0)], tree_width, data_arity, 1, config);

std::cout << "3. Building Merkle tree" << std::endl;
Poseidon<scalar_t> tree_poseidon(tree_arity, ctx);
SpongeConfig tree_config = default_sponge_config(ctx);
HashConfig tree_config = default_hash_config(ctx);
build_tree(tree_height, tree, tree_poseidon, tree_config);

std::cout << "4. Generate membership proof" << std::endl;
Expand Down
4 changes: 2 additions & 2 deletions examples/rust/poseidon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use icicle_bls12_381::curve::ScalarField as F;

use icicle_cuda_runtime::device_context::DeviceContext;

use icicle_core::hash::{SpongeHash, SpongeConfig};
use icicle_core::hash::{SpongeHash, HashConfig};
use icicle_core::poseidon::Poseidon;
use icicle_core::traits::FieldImpl;
use icicle_cuda_runtime::memory::HostSlice;
Expand Down Expand Up @@ -32,7 +32,7 @@ fn main() {
);
let ctx = DeviceContext::default();
let poseidon = Poseidon::load(arity, &ctx).unwrap();
let config = SpongeConfig::default();
let config = HashConfig::default();

println!(
"---------------------- Input size 2^{}={} ------------------------",
Expand Down
10 changes: 5 additions & 5 deletions icicle/include/api/babybear.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extern "C" cudaError_t babybear_poseidon2_hash_many_cuda(
unsigned int number_of_states,
unsigned int input_block_len,
unsigned int output_len,
hash::SpongeConfig& cfg);
hash::HashConfig& cfg);

extern "C" cudaError_t
babybear_poseidon2_delete_cuda(poseidon2::Poseidon2<babybear::scalar_t>* poseidon, device_context::DeviceContext& ctx);
Expand All @@ -59,16 +59,16 @@ extern "C" cudaError_t babybear_build_merkle_tree(
babybear::scalar_t* digests,
unsigned int height,
unsigned int input_block_len,
const hash::SpongeHasher<babybear::scalar_t, babybear::scalar_t>* compression,
const hash::SpongeHasher<babybear::scalar_t, babybear::scalar_t>* bottom_layer,
const hash::Hasher<babybear::scalar_t, babybear::scalar_t>* compression,
const hash::Hasher<babybear::scalar_t, babybear::scalar_t>* bottom_layer,
const merkle_tree::TreeBuilderConfig& tree_config);

extern "C" cudaError_t babybear_mmcs_commit_cuda(
const matrix::Matrix<babybear::scalar_t>* leaves,
unsigned int number_of_inputs,
babybear::scalar_t* digests,
const hash::SpongeHasher<babybear::scalar_t, babybear::scalar_t>* hasher,
const hash::SpongeHasher<babybear::scalar_t, babybear::scalar_t>* compression,
const hash::Hasher<babybear::scalar_t, babybear::scalar_t>* hasher,
const hash::Hasher<babybear::scalar_t, babybear::scalar_t>* compression,
const merkle_tree::TreeBuilderConfig& tree_config);

extern "C" cudaError_t babybear_mul_cuda(
Expand Down
10 changes: 5 additions & 5 deletions icicle/include/api/bls12_377.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ extern "C" cudaError_t bls12_377_build_merkle_tree(
bls12_377::scalar_t* digests,
unsigned int height,
unsigned int input_block_len,
const hash::SpongeHasher<bls12_377::scalar_t, bls12_377::scalar_t>* compression,
const hash::SpongeHasher<bls12_377::scalar_t, bls12_377::scalar_t>* bottom_layer,
const hash::Hasher<bls12_377::scalar_t, bls12_377::scalar_t>* compression,
const hash::Hasher<bls12_377::scalar_t, bls12_377::scalar_t>* bottom_layer,
const merkle_tree::TreeBuilderConfig& tree_config);

extern "C" cudaError_t bls12_377_mmcs_commit_cuda(
const matrix::Matrix<bls12_377::scalar_t>* leaves,
unsigned int number_of_inputs,
bls12_377::scalar_t* digests,
const hash::SpongeHasher<bls12_377::scalar_t, bls12_377::scalar_t>* hasher,
const hash::SpongeHasher<bls12_377::scalar_t, bls12_377::scalar_t>* compression,
const hash::Hasher<bls12_377::scalar_t, bls12_377::scalar_t>* hasher,
const hash::Hasher<bls12_377::scalar_t, bls12_377::scalar_t>* compression,
const merkle_tree::TreeBuilderConfig& tree_config);

extern "C" cudaError_t bls12_377_poseidon_create_cuda(
Expand Down Expand Up @@ -108,7 +108,7 @@ extern "C" cudaError_t bls12_377_poseidon_hash_many_cuda(
unsigned int number_of_states,
unsigned int input_block_len,
unsigned int output_len,
hash::SpongeConfig& cfg);
hash::HashConfig& cfg);

extern "C" cudaError_t
bls12_377_poseidon_delete_cuda(poseidon::Poseidon<bls12_377::scalar_t>* poseidon);
Expand Down
10 changes: 5 additions & 5 deletions icicle/include/api/bls12_381.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ extern "C" cudaError_t bls12_381_build_merkle_tree(
bls12_381::scalar_t* digests,
unsigned int height,
unsigned int input_block_len,
const hash::SpongeHasher<bls12_381::scalar_t, bls12_381::scalar_t>* compression,
const hash::SpongeHasher<bls12_381::scalar_t, bls12_381::scalar_t>* bottom_layer,
const hash::Hasher<bls12_381::scalar_t, bls12_381::scalar_t>* compression,
const hash::Hasher<bls12_381::scalar_t, bls12_381::scalar_t>* bottom_layer,
const merkle_tree::TreeBuilderConfig& tree_config);

extern "C" cudaError_t bls12_381_mmcs_commit_cuda(
const matrix::Matrix<bls12_381::scalar_t>* leaves,
unsigned int number_of_inputs,
bls12_381::scalar_t* digests,
const hash::SpongeHasher<bls12_381::scalar_t, bls12_381::scalar_t>* hasher,
const hash::SpongeHasher<bls12_381::scalar_t, bls12_381::scalar_t>* compression,
const hash::Hasher<bls12_381::scalar_t, bls12_381::scalar_t>* hasher,
const hash::Hasher<bls12_381::scalar_t, bls12_381::scalar_t>* compression,
const merkle_tree::TreeBuilderConfig& tree_config);

extern "C" cudaError_t bls12_381_poseidon_create_cuda(
Expand Down Expand Up @@ -108,7 +108,7 @@ extern "C" cudaError_t bls12_381_poseidon_hash_many_cuda(
unsigned int number_of_states,
unsigned int input_block_len,
unsigned int output_len,
hash::SpongeConfig& cfg);
hash::HashConfig& cfg);

extern "C" cudaError_t
bls12_381_poseidon_delete_cuda(poseidon::Poseidon<bls12_381::scalar_t>* poseidon);
Expand Down
12 changes: 6 additions & 6 deletions icicle/include/api/bn254.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ extern "C" cudaError_t bn254_poseidon2_hash_many_cuda(
unsigned int number_of_states,
unsigned int input_block_len,
unsigned int output_len,
hash::SpongeConfig& cfg);
hash::HashConfig& cfg);

extern "C" cudaError_t
bn254_poseidon2_delete_cuda(poseidon2::Poseidon2<bn254::scalar_t>* poseidon, device_context::DeviceContext& ctx);
Expand All @@ -107,16 +107,16 @@ extern "C" cudaError_t bn254_build_merkle_tree(
bn254::scalar_t* digests,
unsigned int height,
unsigned int input_block_len,
const hash::SpongeHasher<bn254::scalar_t, bn254::scalar_t>* compression,
const hash::SpongeHasher<bn254::scalar_t, bn254::scalar_t>* bottom_layer,
const hash::Hasher<bn254::scalar_t, bn254::scalar_t>* compression,
const hash::Hasher<bn254::scalar_t, bn254::scalar_t>* bottom_layer,
const merkle_tree::TreeBuilderConfig& tree_config);

extern "C" cudaError_t bn254_mmcs_commit_cuda(
const matrix::Matrix<bn254::scalar_t>* leaves,
unsigned int number_of_inputs,
bn254::scalar_t* digests,
const hash::SpongeHasher<bn254::scalar_t, bn254::scalar_t>* hasher,
const hash::SpongeHasher<bn254::scalar_t, bn254::scalar_t>* compression,
const hash::Hasher<bn254::scalar_t, bn254::scalar_t>* hasher,
const hash::Hasher<bn254::scalar_t, bn254::scalar_t>* compression,
const merkle_tree::TreeBuilderConfig& tree_config);

extern "C" cudaError_t bn254_poseidon_create_cuda(
Expand Down Expand Up @@ -144,7 +144,7 @@ extern "C" cudaError_t bn254_poseidon_hash_many_cuda(
unsigned int number_of_states,
unsigned int input_block_len,
unsigned int output_len,
hash::SpongeConfig& cfg);
hash::HashConfig& cfg);

extern "C" cudaError_t
bn254_poseidon_delete_cuda(poseidon::Poseidon<bn254::scalar_t>* poseidon);
Expand Down
10 changes: 5 additions & 5 deletions icicle/include/api/bw6_761.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ extern "C" cudaError_t bw6_761_build_merkle_tree(
bw6_761::scalar_t* digests,
unsigned int height,
unsigned int input_block_len,
const hash::SpongeHasher<bw6_761::scalar_t, bw6_761::scalar_t>* compression,
const hash::SpongeHasher<bw6_761::scalar_t, bw6_761::scalar_t>* bottom_layer,
const hash::Hasher<bw6_761::scalar_t, bw6_761::scalar_t>* compression,
const hash::Hasher<bw6_761::scalar_t, bw6_761::scalar_t>* bottom_layer,
const merkle_tree::TreeBuilderConfig& tree_config);

extern "C" cudaError_t bw6_761_mmcs_commit_cuda(
const matrix::Matrix<bw6_761::scalar_t>* leaves,
unsigned int number_of_inputs,
bw6_761::scalar_t* digests,
const hash::SpongeHasher<bw6_761::scalar_t, bw6_761::scalar_t>* hasher,
const hash::SpongeHasher<bw6_761::scalar_t, bw6_761::scalar_t>* compression,
const hash::Hasher<bw6_761::scalar_t, bw6_761::scalar_t>* hasher,
const hash::Hasher<bw6_761::scalar_t, bw6_761::scalar_t>* compression,
const merkle_tree::TreeBuilderConfig& tree_config);

extern "C" cudaError_t bw6_761_poseidon_create_cuda(
Expand Down Expand Up @@ -108,7 +108,7 @@ extern "C" cudaError_t bw6_761_poseidon_hash_many_cuda(
unsigned int number_of_states,
unsigned int input_block_len,
unsigned int output_len,
hash::SpongeConfig& cfg);
hash::HashConfig& cfg);

extern "C" cudaError_t
bw6_761_poseidon_delete_cuda(poseidon::Poseidon<bw6_761::scalar_t>* poseidon);
Expand Down
10 changes: 5 additions & 5 deletions icicle/include/api/grumpkin.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ extern "C" cudaError_t grumpkin_build_merkle_tree(
grumpkin::scalar_t* digests,
unsigned int height,
unsigned int input_block_len,
const hash::SpongeHasher<grumpkin::scalar_t, grumpkin::scalar_t>* compression,
const hash::SpongeHasher<grumpkin::scalar_t, grumpkin::scalar_t>* bottom_layer,
const hash::Hasher<grumpkin::scalar_t, grumpkin::scalar_t>* compression,
const hash::Hasher<grumpkin::scalar_t, grumpkin::scalar_t>* bottom_layer,
const merkle_tree::TreeBuilderConfig& tree_config);

extern "C" cudaError_t grumpkin_mmcs_commit_cuda(
const matrix::Matrix<grumpkin::scalar_t>* leaves,
unsigned int number_of_inputs,
grumpkin::scalar_t* digests,
const hash::SpongeHasher<grumpkin::scalar_t, grumpkin::scalar_t>* hasher,
const hash::SpongeHasher<grumpkin::scalar_t, grumpkin::scalar_t>* compression,
const hash::Hasher<grumpkin::scalar_t, grumpkin::scalar_t>* hasher,
const hash::Hasher<grumpkin::scalar_t, grumpkin::scalar_t>* compression,
const merkle_tree::TreeBuilderConfig& tree_config);

extern "C" cudaError_t grumpkin_poseidon_create_cuda(
Expand Down Expand Up @@ -81,7 +81,7 @@ extern "C" cudaError_t grumpkin_poseidon_hash_many_cuda(
unsigned int number_of_states,
unsigned int input_block_len,
unsigned int output_len,
hash::SpongeConfig& cfg);
hash::HashConfig& cfg);

extern "C" cudaError_t
grumpkin_poseidon_delete_cuda(poseidon::Poseidon<grumpkin::scalar_t>* poseidon);
Expand Down
Loading

0 comments on commit ea71faf

Please sign in to comment.