8000 GitHub - distributed-lab/op_rand: Trustless Randomness Generation on Bitcoin
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

distributed-lab/op_rand

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

55 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

OP_RAND: VRF on Bitcoin

Paper

This is a method of emulation of OP_RAND opcode on Bitcoin through a trustless interactive game between transaction counterparties. The game result is probabilistic and doesnโ€™t allow any party to cheat, increasing their chance of winning on any protocol step. The protocol is organized in a way unrecognizable to any external party and doesnโ€™t require any specific scripts or Bitcoin protocol updates.

๐Ÿ“– Overview

OP_RAND allows two (currently) users to create the set of transactions, the UTXO of the final one of which can be spent with some probability by each counterparty. For that, OP_RAND uses:

  • Commitments: The protocol allows the challenger to create commitments on random values, only one of each is used for the final address formation. An acceptor also mast create the commitment for their final public key, but without the knowledge if that can be spent.
  • Zero-Knowledge Proofs: For proving the correctness of all actions (with hiding the secret data) between challenger and acceptor it uses Noir circuits with Barretenberg backend.
  • Bitcoin Script: OP_RAND doesn't require and update of the Bitcoin protocol or appearance of new op codes
  • Interactive Protocol: Two-party commit-reveal scheme

Key Features

  • ๐ŸŽฒ True Randomness: Cryptographically secure 50/50 outcomes
  • ๐Ÿ”’ Trustless: No third parties or oracles required
  • ๐Ÿ•ต๏ธ Private: Commitment selection hidden until revelation
  • โœ… Verifiable: All parties can verify proof correctness
  • ๐Ÿƒ Fast: Efficient zero-knowledge proof generation and verification
  • ๐Ÿ’ฐ Economic: Winner-takes-all incentive mechanism
  • ๐Ÿ‘ป Stealthy: Appears as normal Bitcoin transactions

๐Ÿ—๏ธ Architecture

The project consists of several key components:

Core Crates

  • op-rand-types - Fundamental data structures and commitment types
  • op-rand-prover - Zero-knowledge proof generation and verification using Barretenberg
  • op-rand-transaction-builder - Bitcoin transaction construction utilities

Applications

  • apps/cli - Full-featured command-line interface for protocol interaction

Circuits

  • circuits/crates/challenger_circuit - ZK circuit for challenger proofs
  • circuits/crates/acceptor_circuit - ZK circuit for acceptor proofs
  • circuits/crates/common - Shared cryptographic utilities

๐Ÿš€ Installation

From Source

# Clone the repository
git clone https://github.com/distributed-lab/op_rand
cd op_rand

# Build the project
cargo build --release

# Install the CLI globally
cargo install --path apps/cli

Verify Installation

op-rand-cli --help

๐ŸŽฎ Quick Start

1. Setup Configuration

Create a config.toml file:

# Your Bitcoin private key (WIF format)
private_key = "cVt4o7BGAig1UXywgGSmARhxMdzP5qvQsxKkSsc1XEkw3tDTQFpy"

# Esplora API endpoint
esplora_url = "https://blockstream.info/testnet/api"

# Bitcoin network (testnet, regtest, bitcoin)
network = "testnet"

โš ๏ธ Security Warning: Never use mainnet private keys with real funds in development environments.

2. Complete Workflow Example

As Challenger (Party A):

# Create a 100,000 satoshi challenge
op-rand-cli create-challenge --amount 100000 --locktime 144

# This creates:
# - challenger.json (share with acceptor)
# - private_challenger.json (keep secret)

As Acceptor (Party B):

# Inspect the challenge first
op-rand-cli info --challenge-file challenger.json

# Accept the challenge by selecting a commitment
op-rand-cli accept-challenge \
  --challenge-file challenger.json \
  --selected-commitment 0

# This creates:
# - acceptor.json (send back to challenger)

Complete the Challenge (Challenger):

# Finalize and broadcast the challenge
op-rand-cli complete-challenge \
  --challenger-file challenger.json \
  --challenger-private-file private_challenger.json \
  --acceptor-file acceptor.json

# Returns: Transaction ID and reveals the random outcome

Claim Winnings:

# The winner can spend the locked funds
op-rand-cli try-spend \
  --challenge-tx "transaction_hex_from_previous_step" \
  --challenger  # or --acceptor depending on who won

๐Ÿ“š Documentation

๐Ÿ”ฌ How It Works

Protocol Overview

  1. Commitment Phase: Challenger generates cryptographic commitments to secret values
  2. Challenge Creation: Zero-knowledge proof demonstrates commitment validity
  3. Acceptance Phase: Acceptor blindly selects one commitment and provides their own proof
  4. Revelation Phase: Challenger reveals selected commitment, determining the winner
  5. Settlement Phase: Winner can claim the locked Bitcoin funds

Cryptographic Guarantees

  • Unpredictability: Neither party can predict the outcome
  • Fairness: Each party has exactly 50% probability of winning
  • Binding: Commitments cannot be changed after creation
  • Hiding: Commitment selection remains private until revelation
  • Verifiability: All proofs can be independently verified

Zero-Knowledge Circuits

The protocol uses two main ZK circuits:

  • Challenger Circuit: Proves knowledge of commitment secrets without revealing them
  • Acceptor Circuit: Proves valid signature and commitment selection

๐Ÿ› ๏ธ Development

Project Structure

op_rand/
โ”œโ”€โ”€ apps/
โ”‚   โ””โ”€โ”€ cli/                    # Command-line interface
โ”œโ”€โ”€ crates/
โ”‚   โ”œโ”€โ”€ types/                  # Core data structures
โ”‚   โ”œโ”€โ”€ prover/                 # ZK proof system
โ”‚   โ””โ”€โ”€ transaction-builder/    # Bitcoin transaction utilities
โ”œโ”€โ”€ circuits/
โ”‚   โ””โ”€โ”€ crates/
โ”‚       โ”œโ”€โ”€ challenger_circuit/ # Challenger ZK circuit
โ”‚       โ”œโ”€โ”€ acceptor_circuit/   # Acceptor ZK circuit
โ”‚       โ””โ”€โ”€ common/             # Shared circuit utilities
โ””โ”€โ”€ target/                     # Build artifacts

๐Ÿข About

Developed by Distributed Lab

๐Ÿ”— Links

๐ŸŽฏ TODO

  • Support of P2TR addresses
  • Implement flexible probability (better than 1/n)

๐Ÿ™ Acknowledgments

Special thanks to passport-zk-circuits-noir contributors for secp256k1 circuits which were instrumental in implementing the cryptographic primitives for this project.


Build trustless randomness on Bitcoin with cryptographic guarantees.

0