Byte Codec

← Main | 한국어 →


API | Type Rules | Examples | RLP Comparison


Overview

The Sigilaris byte codec provides deterministic binary encoding and decoding for blockchain applications. When signing transactions or computing block hashes, data must first be converted to a deterministic byte sequence—the same data must always produce the same bytes to ensure hash and signature correctness.

Why Deterministic Encoding?

In blockchain systems:

Any non-deterministic encoding (e.g., random collection ordering) breaks consensus.

Key Features

Quick Start (30 seconds)

import org.sigilaris.core.codec.byte.*
import scodec.bits.ByteVector

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

val tx = Transaction(from = 1L, to = 2L, amount = 100L)
val encoded: ByteVector = ByteEncoder[Transaction].encode(tx)
val decoded = ByteDecoder[Transaction].decode(encoded)

That's it! The codec automatically derives instances for case classes.

Documentation

What's Included

Basic Types

Collections

Automatic Derivation

import org.sigilaris.core.codec.byte.*

case class Block(height: Long, txCount: Long)

// Instances automatically derived
val block = Block(height = 1L, txCount = 10L)
ByteEncoder[Block].encode(block)

Design Philosophy

Separation of Concerns

This codec library handles byte encoding only. Hashing and signing are separate modules:

Determinism Guarantee

Performance

Example: Transaction Encoding

import org.sigilaris.core.codec.byte.*
import scodec.bits.ByteVector

case class Address(id: Long)
case class Transaction(
  from: Address,
  to: Address,
  amount: Long,
  nonce: Long
)
val tx = Transaction(
  from = Address(100L),
  to = Address(200L),
  amount = 5000L,
  nonce = 42L
)

val bytes = ByteEncoder[Transaction].encode(tx)
val roundtrip = ByteDecoder[Transaction].decode(bytes)

The encoded bytes can now be hashed or signed (using future crypto modules).

Next Steps

  1. API Reference: Learn about contramap, emap, flatMap combinators
  2. Type Rules: Understand BigNat/BigInt variable-length encoding
  3. Examples: See complete blockchain data structures
  4. RLP Comparison: Compare with Ethereum's RLP encoding

Limitations and Scope

Performance Characteristics


← Main | 한국어 →