Crypto

← Main | 한국어 →


API


Overview

The Sigilaris crypto package provides high-performance cryptographic primitives for blockchain applications. It supports secp256k1 elliptic curve cryptography (ECDSA) and Keccak-256 hashing with a consistent API across both JVM and JavaScript platforms.

Why do we need the crypto package? In blockchain systems, cryptographic operations like transaction signing, signature verification, and public key recovery are essential. This package provides these operations in a type-safe and performance-optimized manner.

Key Features:

Quick Start (30 seconds)

import org.sigilaris.core.crypto.*
import scodec.bits.ByteVector

// Generate a key pair
val keyPair = CryptoOps.generate()

// Hash a message
val message = "Hello, Blockchain!".getBytes
val hash = CryptoOps.keccak256(message)

// Create a signature
val signResult = CryptoOps.sign(keyPair, hash)
val signature = signResult.toOption.get

// Recover public key
val recovered = CryptoOps.recover(signature, hash)
val publicKey = recovered.toOption.get

// Verify the recovered public key matches the original
assert(publicKey == keyPair.publicKey)

That's it! The crypto package automatically:

Documentation

Core Concepts

Main Types

CryptoOps

Platform-specific cryptographic operations implementation:

KeyPair

Data type representing a secp256k1 key pair:

Signature

Recoverable ECDSA signature:

Hash

Type class for type-safe Keccak-256 hashing:

import org.sigilaris.core.crypto.Hash
import org.sigilaris.core.crypto.Hash.ops.*
import org.sigilaris.core.datatype.Utf8

// Hash a UTF-8 string
val utf8Data = Utf8("hello")
val utf8Hash: Hash.Value[Utf8] = utf8Data.toHash

// Hash custom types
case class Order(from: Long, to: Long, amount: Long)
given Hash[Order] = Hash.build[Order]

val order = Order(1L, 2L, 100L)
val orderHash: Hash.Value[Order] = order.toHash

Use Cases

1. Transaction Signing

import org.sigilaris.core.crypto.*
import org.sigilaris.core.codec.byte.ByteEncoder
import org.sigilaris.core.codec.byte.ByteEncoder.ops.*

case class Transaction(from: Long, to: Long, amount: Long, nonce: Long)

val keyPair = CryptoOps.generate()
val tx = Transaction(from = 1L, to = 2L, amount = 100L, nonce = 42L)

// Encode and hash the transaction
val txBytes = ByteEncoder[Transaction].encode(tx).toArray
val txHash = CryptoOps.keccak256(txBytes)

// Create signature
val signature = CryptoOps.sign(keyPair, txHash).toOption.get

2. Signature Verification

// Recover public key from signature
val recoveredPubKey = CryptoOps.recover(signature, txHash).toOption.get

// Verify the recovered public key matches the original
val isValid = recoveredPubKey == keyPair.publicKey

3. Type-Safe Hashing

import org.sigilaris.core.crypto.Hash
import org.sigilaris.core.crypto.Hash.ops.*
import org.sigilaris.core.datatype.Utf8

// Hash a UTF-8 string
val data = Utf8("important data")
val dataHash = data.toHash

// Hash value preserves the source type information
val hashValue: Hash.Value[Utf8] = dataHash

Platform-Specific Implementations

JVM (BouncyCastle)

JavaScript (elliptic.js)

Security Considerations

Low-S Normalization

All signatures are automatically normalized to Low-S form (s ≤ n/2):

Constant-Time Comparison

Uses constant-time algorithms for sensitive data comparison:

Memory Hygiene

Zeroizes memory after using secret data:

Performance Characteristics

JMH Benchmark Results

Recent optimization results (Phase 5):

Memory Usage

Scalability

Type Conventions

Byte Representation

Signature Format

Next Steps

Limitations

References


← Main | 한국어 →