Digest algorithm implementations
The near-digest crate gives NEAR contracts and off-chain Rust code one common interface for selected digest algorithms. It keeps hash output and digest crate behavior stable across build targets while using NEAR host services on-chain where a matching service exists.
Algorithm families
The crate exposes SHA-2, SHA-3, and RIPEMD families as optional modules. Each public hasher represents one fixed-size digest and uses shared digest traits, so higher-level code can keep the same hashing flow in contract and non-contract builds.
| Family | Public hashers | Contract execution | Other targets |
|---|---|---|---|
| SHA-2 | Sha256 | NEAR sha256_array host service | Rust SHA-2 implementation |
| Keccak | Keccak256, Keccak512 | NEAR keccak256_array or keccak512_array host service | Rust SHA-3 implementation |
| SHA-3 | Sha3_256, Sha3_384, Sha3_512 | NEAR sha3_256, sha3_384, or sha3_512 host service | Rust SHA-3 implementation |
| RIPEMD | Ripemd160 | NEAR ripemd160_array host service | Rust RIPEMD implementation |
The sha2, sha3, and ripemd Cargo features control which families are available. No family is enabled by default. The zeroize feature adds clearing for sensitive hasher state, while unstable marks APIs or backends that may change between minor releases.
References: near-digest/…/lib.rs• L39-59 near-digest/…/mod.rs• L1-17 near-digest/…/mod.rs• L1-66 near-digest/…/mod.rs• L1-18 near-digest/Cargo.toml• L28-41
Target-compatible hashing
Target-compatible hashing means that a public hasher type stays the same while its work is selected at compile time. A NEAR contract delegates supported algorithms to host services. Native tools, tests, and other builds use the matching Rust implementation. Both paths define the same digest contract for the caller.
The host service is the preferred path for supported on-chain algorithms because it avoids doing the hash work inside the contract's Wasm module. The near build setting selects this path; cargo near build sets it for a contract build.
This diagram shows how one public hasher maps input to the target-specific hashing path and then returns the same digest shape.
flowchart LR
A[Contract or Rust caller] --> B[Shared digest hasher]
B --> C{Build target}
C -->|NEAR contract| D[NEAR host hash service]
C -->|Other target| E[Rust hash implementation]
D --> F[Fixed-size digest]
E --> F
References: near-digest/…/lib.rs• L1-21 near-digest/…/lib.rs• L89-202 near-digest/…/mod.rs• L5-16 near-digest/…/mod.rs• L5-65 near-digest/…/mod.rs• L5-17 near-digest/Cargo.toml• L19-26
Digest interface
The digest interface defines how callers supply data and receive a result. The public types implement the digest crate's common traits, which lets generic code accept these hashers in the same way as RustCrypto hashers.
Callers can hash one byte slice with the convenience Digest::digest operation or add data in steps with Update. Finalization returns a fixed-size output. Reset and finalize_reset clear the current input so the same hasher can process a new message.
| Operation | Contract for callers |
|---|---|
| One-shot digest | Hash a complete byte slice and return its fixed-size output |
| Incremental update | Add message parts before finalization |
| Finalize | Consume the hasher and produce the digest |
| Reset | Discard current input and restore the initial state |
| Finalize and reset | Produce the current digest and prepare the hasher for new input |
References: near-digest/…/lib.rs• L18-37 near-digest/…/lib.rs• L121-198 near-digest/…/mod.rs• L19-43 near-digest/…/mod.rs• L68-164 near-digest/…/mod.rs• L20-44
On-chain message buffering
The NEAR host services accept a complete message in one call, while the shared digest interface also supports incremental updates. The on-chain adapter bridges these models by retaining updated bytes and calling the host service only when the caller finalizes.
This keeps the public interface compatible with incremental hashing, but on-chain use does not stream data into the host service. The zeroize feature adds drop-time clearing for buffered hasher input and forwards zeroization support to the digest dependencies.
References: near-digest/…/lib.rs• L61-67 near-digest/…/utils.rs• L6-50 near-digest/…/lib.rs• L151-152 near-digest/Cargo.toml• L17-18 near-digest/Cargo.toml• L33-39
Algorithm coverage
The algorithm modules separate family-specific public types from the shared target-selection contract. This keeps each family clear to use while applying one compatibility model across all supported hashes.
SHA-2 coverage
SHA-2 support exposes Sha256. On a NEAR contract it uses the NEAR SHA-256 host service. On other targets it uses the Rust SHA-2 implementation, while preserving the same 32-byte output and digest operations.
References: near-digest/…/mod.rs• L1-17 near-digest/…/near.rs• L1-16
SHA-3 coverage
SHA-3 support distinguishes Keccak from the standardized FIPS-202 SHA-3 variants. Keccak256 and Keccak512 use their matching host services on-chain. Sha3_256, Sha3_384, and Sha3_512 now use their matching NEAR host services as well as the Rust implementation on other targets. These SHA-3 host services require nearcore protocol version 87 or later, shipped in nearcore 2.14.
References: near-digest/…/mod.rs• L1-66 near-digest/…/near.rs• L1-71
RIPEMD coverage
RIPEMD support exposes Ripemd160. It uses the NEAR RIPEMD-160 host service for contract builds and the Rust RIPEMD implementation elsewhere. The public type keeps a 20-byte result across both paths.
References: near-digest/…/mod.rs• L1-18 near-digest/…/near.rs• L1-16
Contract compatibility
Contract compatibility comes from matching three boundaries: the public digest type, the fixed output size, and the selected target backend. The family module declares the public type, the shared wrapper forwards digest behavior, and the target adapter supplies either a host result or a Rust result.
The crate checks known digest values and reset behavior for each exposed algorithm. These checks protect the main cross-target promise: the same input and algorithm produce the same output and state behavior on-chain and off-chain.
For related value types and hash helpers, see Cryptographic hash utilities. For cryptography that consumes digest-based primitives, see BLS12-381 cryptography. Use Cryptography and host interfaces for the parent overview. Low-level host binding context is in NEAR host ABI bindings.
References: near-digest/…/lib.rs• L15-21 near-digest/…/lib.rs• L89-202 near-digest/…/lib.rs• L204-228 near-digest/…/mod.rs• L26-42 near-digest/…/mod.rs• L75-164 near-digest/…/mod.rs• L27-43
Change checklist
When these files or sections are changed, remember to consider these:
- A new or changed NEAR host service must stay paired with the matching non-NEAR algorithm and public output size.
- A change to the shared target wrapper must preserve the
digesttrait behavior for every family that uses it. - A change to the one-shot host adapter must be checked against incremental update, reset, and finalize-reset behavior.
- A change to feature gates must keep public module availability and dependency selection aligned.
- A change to SHA-3 host support must keep the documented nearcore protocol requirement and the contract target mapping aligned.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A supported on-chain type points to the wrong host service | The contract returns a valid-looking digest for the wrong algorithm | Keep the family mapping and known-value checks aligned |
| On-chain buffering is changed without clearing on reset | Later messages can include bytes from an earlier message | Preserve reset and finalize-reset coverage for each hasher |
| Host and Rust paths use different output sizes | Generic digest code can fail to compile or consume the wrong result shape | Keep target adapters and public type output sizes matched |
| A contract uses SHA-3 host services on a network without the required protocol support | The contract cannot use the selected host operation | Check the network supports protocol version 87 or later before relying on these services |
Key files
| File | Why to read it |
|---|---|
| near-digest/…/lib.rs | Defines the crate promise, feature surface, and shared target-selection wrapper |
| near-digest/…/utils.rs | Shows how one-shot host services support the shared incremental digest interface |
| near-digest/…/mod.rs | Defines the SHA-256 public contract and its target mapping |
| near-digest/…/mod.rs | Defines the Keccak and standardized SHA-3 coverage and protocol boundary |
| near-digest/…/mod.rs | Defines the RIPEMD-160 public contract and target mapping |
| near-digest/…/near.rs | Shows the SHA-256 host-service adapter |
| near-digest/…/near.rs | Shows the Keccak and SHA-3 host-service adapters |
| near-digest/…/near.rs | Shows the RIPEMD-160 host-service adapter |
| near-digest/Cargo.toml | Defines optional algorithm and zeroization features plus target-specific dependencies |