June 2024

# Access Massive Blockchain Data From Your Contract Using Maru Platform

## TL;DR

As the blockchain ecosystem continues to evolve, one of the critical challenges developers face is efficiently accessing and utilizing historical blockchain state data. This is where the Maru Platform steps in, revolutionizing the way dApps interact with Ethereum’s historical state using zero-knowledge proofs (ZKPs). Let’s dive into what makes the Maru Platform a game-changer for the blockchain community.

## Understanding the State Accessibility

While the execution environment of the blockchain primarily deals with the current state, a vast amount of valuable data is stored in the historical or parallel state (of other rollup or L1). Until recently, using this data in a trustless and efficient manner posed significant challenges. The main limitation is the massive amount of data commitments required to be verified in order to trust this data, which exceeds the capacity of on-chain VMs. The novel approach is to use ZKPs to prove data commitments and operations over it off-chain and verify the output in an on-chain execution environment. While some projects are using an optimistic approach with an economic driven security model, Maru releases our approach of proving state data for web3 apps.

## Leveraging Zero-Knowledge Proofs with Maru

Zero-knowledge proofs have introduced a new paradigm in ensuring data integrity and trustlessness. Maru Platform harnesses the power of ZKPs to prove each block, enabling developers to query and process necessary data through a comprehensive SDK. This facilitates the development of dApps that can trustlessly distribute rewards, create rebate systems, and incentivize users based on their on-chain activities as specified in smart contracts.

## How Maru Platform Works

Maru Platform employs a four-step process to handle blockchain data:

- Prove and Cache Each Block: Every block is proven and cached to ensure data availability.
- Provable Query Execution: Queries are run to extract and filter the required data.
- Provable Computation: Computations are performed over the retrieved data.
- Aggregation and Proof Generation: The results are aggregated into a single output with a corresponding proof.

This methodology ensures that all operations are transparent, trustless, and efficient.

## Key Components of Maru Platform

### Merkle Proof Aggregation

Maru utilizes massive Merkle proof aggregation to cache all block data. This ensures that the platform is constantly updated with the latest on-chain commits.

### zkEVM Integration

The zkEVM component allows developers to query contracts directly using the ABI. This simplifies the interaction with smart contracts, avoiding the complexities of memory slots and contract storage. The main advantages of this tool:

- Security: you don’t have to convince users in correctness of your implementation, just use standard or widely used and audited smart contract and its interface;
- Flexibility: to process the data in the same way it is done in the existing web3 apps (you don’t need to deal with raw state formats researching each contract architecture);
- Adoption: Solidity is the most used stack in the web3 world.

### zkMapReduce

zkMapReduce is a computational architecture that naturally arises from the blockchain structure. By splitting calculations into block-oriented Map part and chain-oriented Reduce part we can both make calculations more simple and efficient. This approach enables us to employ wide-scale parallelism to dramatically improve performance on historical datasets, when we need to prove calculation for thousands blocks at a time. We are using a special calculation-oriented DSL to code both parts in a low-level yet understandable manner. Programs written in this DSL yield faster proofs. Alternatively, the MapReduce part can be implemented in Solidity bringing every advantage of a popular high-level programming language to the table for a price of a bit slower proofs.

zkMapReduce provides the capability to perform custom computations over filtered data, catering to various dApp requirements such as volume calculations, user leaderboards, and funding rates.

## Practical Use Cases

Maru Platform's robust architecture and versatile functionalities open the door to numerous practical applications:

- DeFi Interest Rates and Earnings Data: For instance, an auto-rebalancing yield generation contract can utilize daily DeFi interest rates and earnings data. A prime example is AAVE Deposits and Borrows v2, where Maru Platform ensures seamless data integration.
- Explore more: AAVE Deposits and Borrows v2

- Volume Weighted Average Price (VWAP): By leveraging data from AMM pools, dApps can compute VWAP without relying on third-party oracles. The Curve tricrypto2 ETH VWAP is a testament to Maru Platform's capabilities in this area.
- Learn more: Curve tricrypto2 ETH VWAP

### Requesting Data

You can easily define what data you need using Maru API or Portal:

```
{
"user_inputs":{
"block_number_start":19213400,
"block_number_end":19213510,
"value":117420373259851,
"hash":"4553b38eea91182c1d2507648822eb5bf3fd909a8106c0bd652dcd1ae50c06f2"
},
"plonky2_inputs": {
"extract":{
"type":"extract4",
"offsets":[0,32,64,96],
"aux_inputs":{
"19213400":{"value":"256439480000"},
… ,
"19213509":{"value":"259878762000"},
"19213510":{"value":"259878762000"}}},
"map":{
"type":"4to1with1lookup",
"lookup":[1,1000000,1000000000],
"formula":[{"formula":"op1 aux0 mul 1000000000000000000 div"}]},
"reduce":{
"type":"1to1sum",
"expressions":{"formula":"acc0 op0 add"}},
"id":"1",
"verify_inputs":{
"19213401":{"value":"0"},
…,
"19213510":{"value":"117420373259851"}},
"start_block":19213400,
"end_block":19213510,
"filter":{
"contract":"0x7d2768dE32b0b80b7a3454c06BdAc94A69DDc7A9",
"topics":["0xde6857219544bb5b7746f48ed30be6386fefc61b2f864cacf559893bf50fd951",
"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"]},
"hash":"4553b38eea91182c1d2507648822eb5bf3fd909a8106c0bd652dcd1ae50c06f2"
},
"verifier_inputs": {
"block_start":"0x189651567ae148ba3ff8aa6ba23b3434d6764b010cd489bf459a29c8cbfb09b9",
"block_end":"0x93704246d9f10f7475341dba453e42297c5e76e5ca463e503d9d37aed942fb49",
"value":"0x00000000000000000000000000000000000000000000000000006acb0fa3364b",
"hash":"4553b38eea91182c1d2507648822eb5bf3fd909a8106c0bd652dcd1ae50c06f2"
}
}
```

Where "formula" is an expression represented in reverse Polish notation and “hash” is used to identify various expressions in the proving system and is calculated as follows:

```
hash = sha256("{contract}{topics}{keccak(offsets)}{keccak(map)}{keccak(reduce)}")
```

While "contract" contains the address of the contract that has generated the expression events, "topics" contains an array of event topics that interpret the indexed event parameters.

### Verification

After the proof is generated, it can be verified on-chain in the Sepolia Testnet network, in which contracts for proof verification are pre-deployed. A proof verification transaction is passed to the on-chain verifier and saves the result in the VerificationGateway contract storage. After a successful transaction Maru returns a link for it:

https://sepolia.etherscan.io/tx/0x2ed94332839a3fd37abee0c569209167300a93aadac832b3467c7929ceda6198

and other smart contracts can check whether Inputs with their corresponding proofs have been checked previously using the following functions:

```
function isProofed(
uint256[8] calldata input
) external view returns (bool);
function isProofedUserInput(
bytes32 blockhash_prev,
bytes32 blockhash_end,
bytes32 hash,
bytes32 value
) external view returns (bool);
```

For this check, other smart contracts do not need to know the proof, but only need to pass the correct input data, which contains information about the period, expression, and result of the calculation.

The codebase for Maru Platform has been open-sourced here, and instructions for how to engage with this testnet can be found on Maru Docs.

## Next Steps

While this is already significantly more efficient and faster than typical competitors, we have many planned updates that will further improve the performance as well as the user experience:

- Index L2 rollups state;
- Integrate SP1, Valida & Jolt verifier into Maru proof stack (to run arbitrary rust programs);
- Gas as a metric proving cost calculation.

And of course adding node operators to our testnet to run proof generations will be evaluated and implemented soon. Stay tuned!