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

Implement generic_hash() function to support hashing algorithms (ZKP) #4892

Open
devendran-m opened this issue Sep 17, 2024 · 0 comments
Open
Assignees
Labels
rc-5 Release Candidate 5

Comments

@devendran-m
Copy link
Contributor

As a developer building Zero Knowledge (ZK) software on Casper Network, I need cryptographic utilities to be implemented as host functions to ensure that the execution cost of cryptographic operations is efficient and manageable.

Acceptance Criteria:

  • Implement a host function called generic_hash(input, type) that supports various hash algorithms.
  • Currently supported algorithms:
    • HashAlgoType::Blake2b - utilizes the existing Blake2b implementation.
    • HashAlgoType::Blake3b - introduces the Blake3 library.
  • Future support planned for additional algorithms such as sha256, keccak, and poseidon.
  • The host functions should significantly reduce the execution cost, which in the current state (without host functions) can be excessive (e.g., running Risc0 VM proof verification for a simple sum of squares costs more than 6000 CSPR).

Benefit:

This feature will enable more efficient execution of ZK cryptographic operations like proof verifications, enhancing the usability and scalability of Casper-based applications without incurring prohibitive transaction costs.

@devendran-m devendran-m added the rc-5 Release Candidate 5 label Sep 17, 2024
@devendran-m devendran-m changed the title Support for Cryptographic Utilities as Host Functions Implement generic_hash() function to support hashing algorithms (ZKP) Sep 19, 2024
casperlabs-bors-ng bot added a commit that referenced this issue Oct 15, 2024
4903: Implement generic_hash() host function to support hashing algorithms r=igor-casper a=igor-casper

This PR directly addresses #4892, referencing #4411 as prior work.

Adds host function called generic_hash(input, type) with support for the following types:
- HashAlgorithm::Blake2b - using the existing blake2b implementation,
- HashAlgorithm::Blake3 - introducing the blake3 library,
- HashAlgorithm::Sha256 - introducing the sha2 library,

**Example usage**
```rs
#![no_std]
#![no_main]

extern crate alloc;
use alloc::string::String;

use casper_contract::contract_api::cryptography;
use casper_types::crypto::HashAlgorithm;

#[no_mangle]
pub extern "C" fn call() {
    let data = "sha256 hash test";
    let expected = [0x29, 0xD2, 0xC7, 0x7B, 0x39, 0x7F, 0xF6, 0x9E, 0x25, 0x0D, 0x81, 0xA3, 0xBA, 0xBB, 0x32, 0xDE, 0xFF, 0x3C, 0x2D, 0x06, 0xC9, 0x8E, 0x5E, 0x73, 0x60, 0x54, 0x3C, 0xE4, 0x91, 0xAC, 0x81, 0xCA];

    let hash = cryptography::generic_hash(data, HashAlgorithm::Sha256);
    
    assert_eq!(
        hash, expected,
        "Hash mismatch"
    );
}
```

**Notes**
- Blake2 is implemented in the types crate and it's been that way for some time, ideally it would be moved into the cryptography module where blake3 and sha256 reside
- The costs were referenced from #4411

Co-authored-by: igor-casper <[email protected]>
Co-authored-by: igor-casper <[email protected]>
casperlabs-bors-ng bot added a commit that referenced this issue Oct 15, 2024
4903: Implement generic_hash() host function to support hashing algorithms r=igor-casper a=igor-casper

This PR directly addresses #4892, referencing #4411 as prior work.

Adds host function called generic_hash(input, type) with support for the following types:
- HashAlgorithm::Blake2b - using the existing blake2b implementation,
- HashAlgorithm::Blake3 - introducing the blake3 library,
- HashAlgorithm::Sha256 - introducing the sha2 library,

**Example usage**
```rs
#![no_std]
#![no_main]

extern crate alloc;
use alloc::string::String;

use casper_contract::contract_api::cryptography;
use casper_types::crypto::HashAlgorithm;

#[no_mangle]
pub extern "C" fn call() {
    let data = "sha256 hash test";
    let expected = [0x29, 0xD2, 0xC7, 0x7B, 0x39, 0x7F, 0xF6, 0x9E, 0x25, 0x0D, 0x81, 0xA3, 0xBA, 0xBB, 0x32, 0xDE, 0xFF, 0x3C, 0x2D, 0x06, 0xC9, 0x8E, 0x5E, 0x73, 0x60, 0x54, 0x3C, 0xE4, 0x91, 0xAC, 0x81, 0xCA];

    let hash = cryptography::generic_hash(data, HashAlgorithm::Sha256);
    
    assert_eq!(
        hash, expected,
        "Hash mismatch"
    );
}
```

**Notes**
- Blake2 is implemented in the types crate and it's been that way for some time, ideally it would be moved into the cryptography module where blake3 and sha256 reside
- The costs were referenced from #4411

Co-authored-by: igor-casper <[email protected]>
Co-authored-by: igor-casper <[email protected]>
casperlabs-bors-ng bot added a commit that referenced this issue Oct 18, 2024
4903: Implement generic_hash() host function to support hashing algorithms r=EdHastingsCasperAssociation a=igor-casper

This PR directly addresses #4892, referencing #4411 as prior work.

Adds host function called generic_hash(input, type) with support for the following types:
- HashAlgorithm::Blake2b - using the existing blake2b implementation,
- HashAlgorithm::Blake3 - introducing the blake3 library,
- HashAlgorithm::Sha256 - introducing the sha2 library,

**Example usage**
```rs
#![no_std]
#![no_main]

extern crate alloc;
use alloc::string::String;

use casper_contract::contract_api::cryptography;
use casper_types::crypto::HashAlgorithm;

#[no_mangle]
pub extern "C" fn call() {
    let data = "sha256 hash test";
    let expected = [0x29, 0xD2, 0xC7, 0x7B, 0x39, 0x7F, 0xF6, 0x9E, 0x25, 0x0D, 0x81, 0xA3, 0xBA, 0xBB, 0x32, 0xDE, 0xFF, 0x3C, 0x2D, 0x06, 0xC9, 0x8E, 0x5E, 0x73, 0x60, 0x54, 0x3C, 0xE4, 0x91, 0xAC, 0x81, 0xCA];

    let hash = cryptography::generic_hash(data, HashAlgorithm::Sha256);
    
    assert_eq!(
        hash, expected,
        "Hash mismatch"
    );
}
```

**Notes**
- Blake2 is implemented in the types crate and it's been that way for some time, ideally it would be moved into the cryptography module where blake3 and sha256 reside
- The costs were referenced from #4411

Co-authored-by: igor-casper <[email protected]>
Co-authored-by: igor-casper <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rc-5 Release Candidate 5
Projects
None yet
Development

No branches or pull requests

3 participants