Skip to content

CryptoLib

Back to Native Contracts

Provides cryptographic hash functions, signature verification, and BLS12-381 curve operations. CryptoLib methods are exposed through Syscalls.sol rather than NativeCalls.sol.

Methods

MethodSignatureReturnSafeDescription
sha256sha256(bytes)bytes32SHA-256 hash.
ripemd160ripemd160(bytes)bytes20RIPEMD-160 hash.
keccak256keccak256(bytes)bytes32Keccak-256 hash (added at Cockatrice hardfork).
murmur32murmur32(bytes,uint32)bytes4Murmur3 32-bit hash with seed.
verifyWithECDsaverifyWithECDsa(bytes32,bytes,bytes,uint8)boolECDSA signature verification.
bls12381Serializebls12381Serialize(bytes)bytesSerialize a BLS12-381 point.
bls12381Deserializebls12381Deserialize(bytes)bytesDeserialize a BLS12-381 point.
bls12381Equalbls12381Equal(bytes,bytes)boolCompare two BLS12-381 points.
bls12381Addbls12381Add(bytes,bytes)bytesAdd two BLS12-381 points.
bls12381Mulbls12381Mul(bytes,bytes,bool)bytesMultiply BLS12-381 point by scalar.
bls12381Pairingbls12381Pairing(bytes,bytes)bytesBLS12-381 pairing check.

Curve Support

The verifyWithECDsa method accepts a curve parameter that selects both the elliptic curve and the hash algorithm:

ValueConstantCurveHashUse Case
22SECP256K1_SHA256secp256k1SHA-256Bitcoin-compatible signatures
23SECP256R1_SHA256secp256r1SHA-256Neo-native signatures
122SECP256K1_KECCAK256secp256k1Keccak-256Ethereum-compatible signatures
123SECP256R1_KECCAK256secp256r1Keccak-256Neo + Keccak signatures

Code Example

solidity
import "devpack/contracts/Syscalls.sol";

contract SignatureVerifier {
    function verifyEthSignature(
        bytes32 messageHash,
        bytes memory publicKey,
        bytes memory signature
    ) public view returns (bool) {
        // secp256k1 + Keccak-256 for Ethereum compatibility
        return Syscalls.verifyWithECDsa(
            messageHash, publicKey, signature,
            Syscalls.SECP256K1_KECCAK256
        );
    }

    function verifyNeoSignature(
        bytes32 messageHash,
        bytes memory publicKey,
        bytes memory signature
    ) public view returns (bool) {
        // secp256r1 + SHA-256 for Neo-native signatures
        return Syscalls.verifyWithECDsa(
            messageHash, publicKey, signature,
            Syscalls.SECP256R1_SHA256
        );
    }

    function hashData(bytes memory data) public view returns (bytes32) {
        return Syscalls.sha256(data);
    }
}

INFO

Solidity's built-in keccak256() and sha256() are automatically lowered to CryptoLib.keccak256 and CryptoLib.sha256 native contract calls by the compiler. The Syscalls.* wrappers are provided for explicit usage when you want to call through the devpack namespace directly.


MIT Licensed