Skip to content

feat(soroban): implement sha256 and keccak256 builtins#1919

Open
aryanbaranwal001 wants to merge 10 commits into
hyperledger-solang:mainfrom
aryanbaranwal001:feat/soroban-hash-builtins
Open

feat(soroban): implement sha256 and keccak256 builtins#1919
aryanbaranwal001 wants to merge 10 commits into
hyperledger-solang:mainfrom
aryanbaranwal001:feat/soroban-hash-builtins

Conversation

@aryanbaranwal001

Copy link
Copy Markdown

Description

The Solidity sha256 and keccak256 builtins were unimplemented on the Soroban target. This PR adds support for both, the following code now compiles and works as expected.

contract Hasher {
    function sha(bytes memory input) public pure returns (bytes32) {
        return sha256(input);
    }
}

Key Changes

  • codegen/mod.rs: added HostFunctions::ComputeSha256 ("c._") and ComputeKeccak256 ("c.1").
  • emit/soroban/mod.rs: registered both in function_signature and added them to declare_externals.
  • codegen/expression.rs: lower Builtin::Sha256 / Builtin::Keccak256 on Soroban, pass the input bytes (args[0]) through soroban_encode_arg to get a BytesObject handle, call the host fn, then decode the returned digest with soroban_decode_arg.

Tests

  • Added 9 tests in tests/soroban_testcases/hash.rs: digest fixture asserts for both builtins, determinism and distinguishability, large input handling, cross algorithm divergence (sha256 != keccak256), and Sha256 / Keccak256 padding boundary fixtures.

Note

Other targets (solana, polkadot) implement hashing in TargetRuntime::hash, which takes a (vector_bytes, vector_len) ptr/len pair. We could do the same in SorobanTarget::hash, but on Soroban bytes memory is an i64 BytesObject handle, so we'd first need a wrapper that copies the BytesObject contents into linear memory just to feed hash the pointer it expects. Cleaner approach is to intercept the builtin one stage earlier in codegen and lower it directly, wiring it with the Soroban hashing host functions. This follows the pattern that the existing Soroban arms for RequireAuth, ExtendTtl follows.

… and codec

Signed-off-by: Aryan Baranwal <aryanbaranwal131214@gmail.com>
Signed-off-by: Aryan Baranwal <aryanbaranwal131214@gmail.com>
…alues

Signed-off-by: Aryan Baranwal <aryanbaranwal131214@gmail.com>
Signed-off-by: Aryan Baranwal <aryanbaranwal131214@gmail.com>
Signed-off-by: Aryan Baranwal <aryanbaranwal131214@gmail.com>
…, bytes32

Signed-off-by: Aryan Baranwal <aryanbaranwal131214@gmail.com>
…bytes_subscript

Signed-off-by: Aryan Baranwal <aryanbaranwal131214@gmail.com>
Signed-off-by: Aryan Baranwal <aryanbaranwal131214@gmail.com>
Signed-off-by: Aryan Baranwal <aryanbaranwal131214@gmail.com>
Signed-off-by: Aryan Baranwal <aryanbaranwal131214@gmail.com>
@aryanbaranwal001

Copy link
Copy Markdown
Author

This PR is stacked on #1918 because it includes the changes from #1908 and #1904, which add support for bytesN & bytes types respectively which aren't supported by soroban main. Both are required by the canonical Solidity signature for sha256 / keccak256, which is bytes memory → bytes32. I kept this signature to stay consistent with Polkadot / Solana counterparts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant