Skip to Content
DevelopersExamplesComputational Squaring

Computational Squaring Example

This example demonstrates how to use off-chain computation with on-chain verification through a service. It introduces a simple squaring function (n^2) to illustrate the core mechanics of SatLayer’s BVS model. Although squaring a number is a trivial task, this pattern generalizes to more complex use cases involving cryptographic proofs, simulations, or data aggregation.

The key idea is to offload heavy computation from the blockchain, while still enabling trustless, verifiable results that can trigger slashing if misbehavior occurs.

Lifecycle Overview

The example follows a simple lifecycle:

  • Requests are sent to the contract without the need for heavy on-chain computation.
  • The off-chain service, ran by the operators, computes the square of a given number and submits the result to the contract.
  • Slashing is triggered if the operator submits an incorrect result.

This approach enables computational efficiency while maintaining crypto-economic accountability.

Getting started

Prerequisites for this example:

  1. Install Rust and Cargo: https://www.rust-lang.org/tools/install 
  2. Basic understanding of CosmWasm contracts
  3. Node.js for development of the off-chain service: https://nodejs.org/en/download/ 
  4. Docker to run off-chain node: https://docs.docker.com/get-docker/ 

Project structure:

squaring/ ├── contract/ <- CosmWasm contract ├── service/ <- Off-chain service ├── compose.yml <- Docker Compose file for running the service └── README.md <- This file

How to run

Clone and copy contents of ./examples/squaring from GitHub  to your local machine. Install with npm install and npm run test to set up the environment.

run.sh
npm run install npm run test

How to build the contract

To build the CosmWasm contract, you will need to have Docker running, and then you can run the following command:

build.sh
docker run --rm -v "$(pwd)":/code \ --mount type=volume,source="$(basename "$(pwd)")_cache",target=/target \ --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \ cosmwasm/optimizer:0.16.0

Project overview

This project demonstrates a Bitcoin Validated Service (BVS) using a simple squaring function as an example. It consists of two main components: the on-chain contract, and the off-chain service.

We use squaring as a simple example to demonstrate the concept of off-chain computation with on-chain verification. Squaring is n^2, where n is the input number, and the result is the square of that number.

While this example is simple and not meant for production, it illustrates the principles of off-chain computation and on-chain verification.

The general request respond lifecycle is as follows:

  1. Users submit computation requests to the contract
  2. Operators (running the off-chain service) detect these requests
  3. Operators perform the computation off-chain
  4. Operators submit the results back to the contract
  5. The contract verifies the results and slashes operators who submit incorrect answers

On-chain Contract (CosmWasm)

Located in the contract/ directory, this Rust-based smart contract:

  • Accepts computation requests from users
  • Verifies responses from operators to prove fault
  • Manages operator registration and slashing mechanisms
  • Provides query endpoints for retrieving computation results

Off-chain Service (TypeScript)

Located in the service/ directory, this Node.js service:

  • Continuously monitors the blockchain for new computation requests
  • Performs the actual computation (i.e. squaring numbers) off-chain
  • Submits results back to the on-chain contract
Last updated on