Skip to content

Commit

Permalink
chore: prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
auryn-macmillan committed Jun 6, 2023
1 parent 4f5b421 commit b74bbf1
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 69 deletions.
59 changes: 38 additions & 21 deletions packages/evm/contracts/axiom/AxiomV02.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// DO NOT USE THIS CONTRACT FOR PRODUCTION
pragma solidity ^0.8.12;

import {IAxiomV0} from "./IAxiomV0.sol";
import {Ownable} from "./Ownable.sol";
import { IAxiomV0 } from "./IAxiomV0.sol";
import { Ownable } from "./Ownable.sol";

uint8 constant TREE_DEPTH = 10;
uint32 constant NUM_LEAVES = 2 ** 10;
Expand Down Expand Up @@ -77,7 +77,7 @@ contract AxiomV02 is IAxiomV0, Ownable {
}

function verifyRaw(bytes calldata input) private returns (bool) {
(bool success,) = verifierAddress.call(input);
(bool success, ) = verifierAddress.call(input);
return success;
}

Expand All @@ -99,25 +99,28 @@ contract AxiomV02 is IAxiomV0, Ownable {
}

// The ZKP has block headers for [startBlockNumber, endBlockNumber] blocks. We extract some common information from the calldata.
function getBoundaryBlockData(bytes calldata proofData)
function getBoundaryBlockData(
bytes calldata proofData
)
internal
pure
returns (bytes32 prevHash, bytes32 endHash, uint32 startBlockNumber, uint32 endBlockNumber, bytes32 root)
{
prevHash = bytes32(
uint256(bytes32(proofData[PUBLIC_BYTES_START_IDX:PUBLIC_BYTES_START_IDX + 32])) << 128
| uint128(bytes16(proofData[PUBLIC_BYTES_START_IDX + 32 + 16:PUBLIC_BYTES_START_IDX + 2 * 32]))
(uint256(bytes32(proofData[PUBLIC_BYTES_START_IDX:PUBLIC_BYTES_START_IDX + 32])) << 128) |
uint128(bytes16(proofData[PUBLIC_BYTES_START_IDX + 32 + 16:PUBLIC_BYTES_START_IDX + 2 * 32]))
);
endHash = bytes32(
uint256(bytes32(proofData[PUBLIC_BYTES_START_IDX + 2 * 32:PUBLIC_BYTES_START_IDX + 3 * 32])) << 128
| uint128(bytes16(proofData[PUBLIC_BYTES_START_IDX + 3 * 32 + 16:PUBLIC_BYTES_START_IDX + 4 * 32]))
(uint256(bytes32(proofData[PUBLIC_BYTES_START_IDX + 2 * 32:PUBLIC_BYTES_START_IDX + 3 * 32])) << 128) |
uint128(bytes16(proofData[PUBLIC_BYTES_START_IDX + 3 * 32 + 16:PUBLIC_BYTES_START_IDX + 4 * 32]))
);
startBlockNumber = uint32(
bytes4(proofData[PUBLIC_BYTES_START_IDX + 5 * 32 - 8:PUBLIC_BYTES_START_IDX + 5 * 32 - 4])
);
startBlockNumber =
uint32(bytes4(proofData[PUBLIC_BYTES_START_IDX + 5 * 32 - 8:PUBLIC_BYTES_START_IDX + 5 * 32 - 4]));
endBlockNumber = uint32(bytes4(proofData[PUBLIC_BYTES_START_IDX + 5 * 32 - 4:PUBLIC_BYTES_START_IDX + 5 * 32]));
root = bytes32(
uint256(bytes32(proofData[ROOT_BYTES_START_IDX:ROOT_BYTES_START_IDX + 32])) << 128
| uint128(bytes16(proofData[ROOT_BYTES_START_IDX + 48:ROOT_BYTES_START_IDX + 64]))
(uint256(bytes32(proofData[ROOT_BYTES_START_IDX:ROOT_BYTES_START_IDX + 32])) << 128) |
uint128(bytes16(proofData[ROOT_BYTES_START_IDX + 48:ROOT_BYTES_START_IDX + 64]))
);
}

Expand All @@ -126,14 +129,22 @@ contract AxiomV02 is IAxiomV0, Ownable {
// * roots[idx] is the root of a Merkle tree of height 2**(TREE_DEPTH - idx) in a Merkle mountain
// range which stores block hashes in the interval [startBlockNumber, endBlockNumber]
function updateRecent(bytes calldata proofData) external {
(bytes32 prevHash, bytes32 endHash, uint32 startBlockNumber, uint32 endBlockNumber, bytes32 root) =
getBoundaryBlockData(proofData);
(
bytes32 prevHash,
bytes32 endHash,
uint32 startBlockNumber,
uint32 endBlockNumber,
bytes32 root
) = getBoundaryBlockData(proofData);
bytes32[TREE_DEPTH] memory roots;
for (uint256 idx = 1; idx <= TREE_DEPTH; idx++) {
roots[idx - 1] = bytes32(
uint256(bytes32(proofData[ROOT_BYTES_START_IDX + idx * 64:ROOT_BYTES_START_IDX + idx * 64 + 32])) << 128
| uint128(
bytes16(proofData[ROOT_BYTES_START_IDX + idx * 64 + 16 + 32:ROOT_BYTES_START_IDX + idx * 64 + 64])
(uint256(bytes32(proofData[ROOT_BYTES_START_IDX + idx * 64:ROOT_BYTES_START_IDX + idx * 64 + 32])) <<
128) |
uint128(
bytes16(
proofData[ROOT_BYTES_START_IDX + idx * 64 + 16 + 32:ROOT_BYTES_START_IDX + idx * 64 + 64]
)
)
);
}
Expand Down Expand Up @@ -163,14 +174,20 @@ contract AxiomV02 is IAxiomV0, Ownable {
// update older blocks in "backwards" direction, anchoring on more recent trusted blockhash
// must be batch of NUM_LEAVES blocks
function updateOld(bytes32 nextRoot, uint32 nextNumFinal, bytes calldata proofData) external {
(bytes32 prevHash, bytes32 endHash, uint32 startBlockNumber, uint32 endBlockNumber, bytes32 root) =
getBoundaryBlockData(proofData);
(
bytes32 prevHash,
bytes32 endHash,
uint32 startBlockNumber,
uint32 endBlockNumber,
bytes32 root
) = getBoundaryBlockData(proofData);

require(startBlockNumber % NUM_LEAVES == 0, "aa"); // "startBlockNumber not a multiple of NUM_LEAVES");
require(endBlockNumber - startBlockNumber == NUM_LEAVES - 1, "bb"); // "Updating with incorrect number of blocks");

require(
historicalRoots(endBlockNumber + 1) == keccak256(abi.encodePacked(endHash, nextRoot, nextNumFinal)), "cc"
historicalRoots(endBlockNumber + 1) == keccak256(abi.encodePacked(endHash, nextRoot, nextNumFinal)),
"cc"
);
// "endHash does not match"
require(verifyRaw(proofData)); // "ZKP does not verify")
Expand Down Expand Up @@ -203,4 +220,4 @@ contract AxiomV02 is IAxiomV0, Ownable {
}
return (merkleRoot == keccak256(abi.encodePacked(witness.prevHash, root, witness.numFinal)));
}
}
}
70 changes: 37 additions & 33 deletions packages/evm/contracts/axiom/AxiomV02StoragePf.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
// DO NOT USE THIS CONTRACT FOR PRODUCTION
pragma solidity ^0.8.12;

import {IAxiomV0} from "./IAxiomV0.sol";
import {IAxiomV0StoragePf} from "./IAxiomV0StoragePf.sol";
import { IAxiomV0 } from "./IAxiomV0.sol";
import { IAxiomV0StoragePf } from "./IAxiomV0StoragePf.sol";
// import {Ownable} from "openzeppelin-contracts/access/Ownable.sol";
import {Ownable} from "./Ownable.sol";
import {IHashi} from "../interfaces/IHashi.sol";
import {IOracleAdapter} from "../interfaces/IOracleAdapter.sol";
import { Ownable } from "./Ownable.sol";
import { IHashi } from "../interfaces/IHashi.sol";
import { IOracleAdapter } from "../interfaces/IOracleAdapter.sol";

uint8 constant SLOT_NUMBER = 10;

Expand Down Expand Up @@ -45,11 +45,12 @@ contract AxiomV02StoragePf is Ownable, IAxiomV0StoragePf {
emit UpdateSnarkVerifierAddress(_verifierAddress);
}

function isSlotAttestationValid(uint32 blockNumber, address addr, uint256 slot, uint256 slotValue)
external
view
returns (bool)
{
function isSlotAttestationValid(
uint32 blockNumber,
address addr,
uint256 slot,
uint256 slotValue
) external view returns (bool) {
return slotAttestations[keccak256(abi.encodePacked(blockNumber, addr, slot, slotValue))];
}

Expand All @@ -65,24 +66,25 @@ contract AxiomV02StoragePf is Ownable, IAxiomV0StoragePf {
}

// Extract instances from proof
uint256 _blockHash = (uint256(bytes32(proof[384:384 + 32])) << 128) | uint128(bytes16(proof[384 + 48:384 + 64]));
uint256 _blockHash = (uint256(bytes32(proof[384:384 + 32])) << 128) |
uint128(bytes16(proof[384 + 48:384 + 64]));
uint256 _blockNumber = uint256(bytes32(proof[384 + 64:384 + 96]));
address account = address(bytes20(proof[384 + 108:384 + 128]));

// Check block hash and block number
require(_blockHash == uint256(blockData.claimedBlockHash), "Invalid block hash in instance");
require(_blockNumber == blockData.blockNumber, "Invalid block number in instance");

(bool success,) = verifierAddress.call(proof);
(bool success, ) = verifierAddress.call(proof);
if (!success) {
revert("Proof verification failed");
}

for (uint16 i = 0; i < SLOT_NUMBER; i++) {
uint256 slot = (uint256(bytes32(proof[384 + 128 + 128 * i:384 + 160 + 128 * i])) << 128)
| uint128(bytes16(proof[384 + 176 + 128 * i:384 + 192 + 128 * i]));
uint256 slotValue = (uint256(bytes32(proof[384 + 192 + 128 * i:384 + 224 + 128 * i])) << 128)
| uint128(bytes16(proof[384 + 240 + 128 * i:384 + 256 + 128 * i]));
uint256 slot = (uint256(bytes32(proof[384 + 128 + 128 * i:384 + 160 + 128 * i])) << 128) |
uint128(bytes16(proof[384 + 176 + 128 * i:384 + 192 + 128 * i]));
uint256 slotValue = (uint256(bytes32(proof[384 + 192 + 128 * i:384 + 224 + 128 * i])) << 128) |
uint128(bytes16(proof[384 + 240 + 128 * i:384 + 256 + 128 * i]));
slotAttestations[keccak256(abi.encodePacked(blockData.blockNumber, account, slot, slotValue))] = true;
emit SlotAttestationEvent(blockData.blockNumber, account, slot, slotValue);
}
Expand All @@ -100,31 +102,32 @@ contract AxiomV02StoragePf is Ownable, IAxiomV0StoragePf {
require(hashFromHashi == blockHash, "block hash mismatch with hash block hash");

// Extract instances from proof
uint256 _blockHash = (uint256(bytes32(proof[384:384 + 32])) << 128) | uint128(bytes16(proof[384 + 48:384 + 64]));
uint256 _blockHash = (uint256(bytes32(proof[384:384 + 32])) << 128) |
uint128(bytes16(proof[384 + 48:384 + 64]));
uint256 _blockNumber = uint256(bytes32(proof[384 + 64:384 + 96]));
address account = address(bytes20(proof[384 + 108:384 + 128]));

// Check block hash and block number
require(_blockHash == uint256(blockHash), "Invalid block hash in instance");
require(_blockNumber == blockNumber, "Invalid block number in instance");

(bool success,) = verifierAddress.call(proof);
(bool success, ) = verifierAddress.call(proof);
if (!success) {
revert("Proof verification failed");
}

for (uint16 i = 0; i < SLOT_NUMBER; i++) {
uint256 slot = (uint256(bytes32(proof[384 + 128 + 128 * i:384 + 160 + 128 * i])) << 128)
| uint128(bytes16(proof[384 + 176 + 128 * i:384 + 192 + 128 * i]));
uint256 slotValue = (uint256(bytes32(proof[384 + 192 + 128 * i:384 + 224 + 128 * i])) << 128)
| uint128(bytes16(proof[384 + 240 + 128 * i:384 + 256 + 128 * i]));
(bytes32 hashedVal) = keccak256(abi.encodePacked(blockNumber, account, slot, slotValue));
uint256 slot = (uint256(bytes32(proof[384 + 128 + 128 * i:384 + 160 + 128 * i])) << 128) |
uint128(bytes16(proof[384 + 176 + 128 * i:384 + 192 + 128 * i]));
uint256 slotValue = (uint256(bytes32(proof[384 + 192 + 128 * i:384 + 224 + 128 * i])) << 128) |
uint128(bytes16(proof[384 + 240 + 128 * i:384 + 256 + 128 * i]));
bytes32 hashedVal = keccak256(abi.encodePacked(blockNumber, account, slot, slotValue));
slotAttestations[hashedVal] = true;
emit SlotAttestationEvent(blockNumber, account, slot, slotValue);
}
}

// Verify a storage proof for 10 storage slots in a single account at a single block
// Verify a storage proof for 10 storage slots in a single account at a single block
function attestCryptoPunk420AddressWithHashi(
bytes calldata proof,
uint256 domain,
Expand All @@ -136,32 +139,33 @@ contract AxiomV02StoragePf is Ownable, IAxiomV0StoragePf {
require(hashFromHashi == blockHash, "block hash mismatch with hash block hash");

// Extract instances from proof
uint256 _blockHash = (uint256(bytes32(proof[384:384 + 32])) << 128) | uint128(bytes16(proof[384 + 48:384 + 64]));
uint256 _blockHash = (uint256(bytes32(proof[384:384 + 32])) << 128) |
uint128(bytes16(proof[384 + 48:384 + 64]));
uint256 _blockNumber = uint256(bytes32(proof[384 + 64:384 + 96]));
address account = address(bytes20(proof[384 + 108:384 + 128]));

// Check block hash and block number
require(_blockHash == uint256(blockHash), "Invalid block hash in instance");
require(_blockNumber == blockNumber, "Invalid block number in instance");

(bool success,) = verifierAddress.call(proof);
(bool success, ) = verifierAddress.call(proof);
if (!success) {
revert("Proof verification failed");
}

uint256 slot = (uint256(bytes32(proof[384 + 128:384 + 160])) << 128)
| uint128(bytes16(proof[384 + 176:384 + 192]));
uint256 slotValue = (uint256(bytes32(proof[384 + 192:384 + 224])) << 128)
| uint128(bytes16(proof[384 + 240:384 + 256]));
(bytes32 hashedVal) = keccak256(abi.encodePacked(blockNumber, account, slot, slotValue));
uint256 slot = (uint256(bytes32(proof[384 + 128:384 + 160])) << 128) |
uint128(bytes16(proof[384 + 176:384 + 192]));
uint256 slotValue = (uint256(bytes32(proof[384 + 192:384 + 224])) << 128) |
uint128(bytes16(proof[384 + 240:384 + 256]));
bytes32 hashedVal = keccak256(abi.encodePacked(blockNumber, account, slot, slotValue));
slotAttestations[hashedVal] = true;
cryptoPunk420OwnerAtBlock10Mil = address(uint160(slotValue));
// CryptoPunk#420 address at the slot for the given block
return cryptoPunk420OwnerAtBlock10Mil;
}

// Return the stored CryptoPunk#420 owner at block 10,000,000 on Ethereum
function getCryptoPunk420OwnerAtBlock10Mil() view external returns (address) {
function getCryptoPunk420OwnerAtBlock10Mil() external view returns (address) {
return cryptoPunk420OwnerAtBlock10Mil;
}
}
}
3 changes: 2 additions & 1 deletion packages/evm/contracts/axiom/IAxiomV0.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ interface IAxiomV0 {
function updateOld(bytes32 nextRoot, uint32 nextNumFinal, bytes calldata proofData) external;

function isRecentBlockHashValid(uint32 blockNumber, bytes32 claimedBlockHash) external view returns (bool);

function isBlockHashValid(BlockHashWitness calldata witness) external view returns (bool);
}
}
10 changes: 7 additions & 3 deletions packages/evm/contracts/axiom/IAxiomV0StoragePf.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ interface IAxiomV0StoragePf {

event SlotAttestationEvent(uint32 blockNumber, address addr, uint256 slot, uint256 slotValue);

function isSlotAttestationValid(uint32 blockNumber, address addr, uint256 slot, uint256 slotValue)
external view returns (bool);
function isSlotAttestationValid(
uint32 blockNumber,
address addr,
uint256 slot,
uint256 slotValue
) external view returns (bool);

// Verify a storage proof for 10 storage slots in a single account at a single block
function attestSlots(IAxiomV0.BlockHashWitness calldata blockData, bytes calldata proof) external;
}
}
2 changes: 1 addition & 1 deletion packages/evm/contracts/axiom/Ownable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ abstract contract Ownable is Context {
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
}
2 changes: 1 addition & 1 deletion packages/evm/contracts/axiom/utils/Context.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ abstract contract Context {
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
}
Loading

0 comments on commit b74bbf1

Please sign in to comment.