Skip to content

Commit

Permalink
StarkPerpetual v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gkaempfer committed Feb 21, 2021
1 parent 210284a commit b2cc6f9
Show file tree
Hide file tree
Showing 143 changed files with 10,964 additions and 5,801 deletions.
1 change: 1 addition & 0 deletions common-contracts/src/components/Governance.sol
1 change: 1 addition & 0 deletions common-contracts/src/components/GovernanceStorage.sol
1 change: 1 addition & 0 deletions common-contracts/src/interfaces/FactRegistry.sol
1 change: 1 addition & 0 deletions common-contracts/src/interfaces/IFactRegistry.sol
1 change: 1 addition & 0 deletions common-contracts/src/interfaces/IQueryableFactRegistry.sol
1 change: 1 addition & 0 deletions common-contracts/src/interfaces/Identity.sol
1 change: 1 addition & 0 deletions common-contracts/src/interfaces/MGovernance.sol
1 change: 1 addition & 0 deletions common-contracts/src/libraries/Common.sol
94 changes: 94 additions & 0 deletions common-contracts/src/upgrade/CallProxy.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.6.11;

import "./StorageSlots.sol";
import "../libraries/Common.sol";

/**
CallProxy is a 'call' based proxy.
It is a facade to a real implementation,
only that unlike the Proxy pattern, it uses call and not delegatecall,
so that the state is recorded on the called contract.
This contract is expected to be placed behind the regular proxy,
thus:
1. Implementation address is stored in a hashed slot (other than proxy's one...).
2. No state variable is allowed in low address ranges.
3. Setting of implementation is done in initialize.
4. isFrozen and initialize are implemented, to be compliant with Proxy.
This implementation is intentionally minimal,
and has no management or governance.
The assumption is that if a different implementation is needed, it will be performed
in an upgradeTo a new deployed CallProxy, pointing to a new implementation.
*/
contract CallProxy is StorageSlots {

using Addresses for address;

// Proxy client - initialize & isFrozen.
// NOLINTNEXTLINE: external-function.
function isFrozen() public pure returns(bool) {
return false;
}

function initialize(bytes calldata data) external {
require(data.length == 32, "INCORRECT_DATA_SIZE");
address impl = abi.decode(data, (address));
require(impl.isContract(), "ADDRESS_NOT_CONTRACT");
setCallProxyImplementation(impl);
}

/*
Returns the call proxy implementation address.
*/
function callProxyImplementation() public view returns (address _implementation) {
bytes32 slot = CALL_PROXY_IMPL_SLOT;
assembly {
_implementation := sload(slot)
}
}

/*
Sets the call proxy implementation address.
*/
function setCallProxyImplementation(address newImplementation) private {
bytes32 slot = CALL_PROXY_IMPL_SLOT;
assembly {
sstore(slot, newImplementation)
}
}

/*
Contract's default function. Pass execution to the implementation contract (using call).
It returns back to the external caller whatever the implementation called code returns.
*/
// NOLINTNEXTLINE: locked-ether.
fallback() external payable {
address _implementation = callProxyImplementation();
require(_implementation != address(0x0), "MISSING_IMPLEMENTATION");
uint256 value = msg.value;
assembly {
// Copy msg.data. We take full control of memory in this inline assembly
// block because it will not return to Solidity code. We overwrite the
// Solidity scratch pad at memory position 0.
calldatacopy(0, 0, calldatasize())

// Call the implementation.
// out and outsize are 0 for now, as we don't know the out size yet.
let result := call(gas(), _implementation, value, 0, calldatasize(), 0, 0)

// Copy the returned data.
returndatacopy(0, 0, returndatasize())

switch result
// delegatecall returns 0 on error.
case 0 {
revert(0, returndatasize())
}
default {
return(0, returndatasize())
}
}
}
}
1 change: 1 addition & 0 deletions common-contracts/src/upgrade/Proxy.sol
1 change: 1 addition & 0 deletions common-contracts/src/upgrade/ProxyGovernance.sol
1 change: 1 addition & 0 deletions common-contracts/src/upgrade/ProxyStorage.sol
1 change: 1 addition & 0 deletions common-contracts/src/upgrade/StorageSlots.sol
1 change: 0 additions & 1 deletion evm-verifier/solidity/contracts/FactRegistry.sol

This file was deleted.

8 changes: 4 additions & 4 deletions evm-verifier/solidity/contracts/Fri.sol.ref
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pragma solidity ^0.5.2;
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.6.11;

import "./MemoryMap.sol";
import "./MemoryAccessUtils.sol";
Expand All @@ -10,7 +11,6 @@ import "../../HornerEvaluator.sol";
by evaluating the fully committed polynomial, and requires specific handling.
*/
contract Fri is MemoryMap, MemoryAccessUtils, HornerEvaluator, FriLayer {
event LogGas(string name, uint256 val);

function verifyLastLayer(uint256[] memory ctx, uint256 nPoints)
internal view {
Expand Down Expand Up @@ -42,7 +42,7 @@ contract Fri is MemoryMap, MemoryAccessUtils, HornerEvaluator, FriLayer {
*/
function friVerifyLayers(
uint256[] memory ctx)
internal view
internal view virtual
{

uint256 friCtx = getPtr(ctx, MM_FRI_CTX);
Expand Down Expand Up @@ -87,7 +87,7 @@ contract Fri is MemoryMap, MemoryAccessUtils, HornerEvaluator, FriLayer {
// Layer is done, verify the current layer and move to next layer.
// ctx[mmMerkleQueue: merkleQueueIdx) holds the indices
// and values of the merkle leaves that need verification.
verify(
verifyMerkle(
channelPtr, merkleQueuePtr, bytes32(ctx[MM_FRI_COMMITMENTS + friStep - 1]),
nLiveQueries);

Expand Down
3 changes: 2 additions & 1 deletion evm-verifier/solidity/contracts/FriLayer.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pragma solidity ^0.5.2;
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.6.11;

import "./MerkleVerifier.sol";
import "./PrimeFieldElement0.sol";
Expand Down
7 changes: 4 additions & 3 deletions evm-verifier/solidity/contracts/FriStatementContract.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pragma solidity ^0.5.2;
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.6.11;

import "./FactRegistry.sol";
import "./interfaces/FactRegistry.sol";
import "./FriLayer.sol";

contract FriStatementContract is FriLayer, FactRegistry {
Expand Down Expand Up @@ -93,7 +94,7 @@ contract FriStatementContract is FriLayer, FactRegistry {
2**friStepSize, /* friCosetSize = 2**friStepSize */
friCtx);

verify(channelPtr, merkleQueuePtr, bytes32(expectedRoot), nQueries);
verifyMerkle(channelPtr, merkleQueuePtr, bytes32(expectedRoot), nQueries);

bytes32 factHash;
assembly {
Expand Down
13 changes: 8 additions & 5 deletions evm-verifier/solidity/contracts/FriStatementVerifier.sol.ref
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pragma solidity ^0.5.2;
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.6.11;

import "./MemoryMap.sol";
import "./MemoryAccessUtils.sol";
Expand All @@ -11,9 +12,11 @@ import "../../VerifierChannel.sol";
The first layer is computed from decommitments, the last layer is computed by evaluating the
fully committed polynomial, and the mid-layers are provided in the proof only as hashed data.
*/
contract FriStatementVerifier is MemoryMap, MemoryAccessUtils, VerifierChannel, HornerEvaluator {
event LogGas(string name, uint256 val);

abstract contract FriStatementVerifier is
MemoryMap,
MemoryAccessUtils,
VerifierChannel,
HornerEvaluator {
FriStatementContract friStatementContract;

constructor(address friStatementContractAddress) internal {
Expand Down Expand Up @@ -65,7 +68,7 @@ contract FriStatementVerifier is MemoryMap, MemoryAccessUtils, VerifierChannel,
*/
function friVerifyLayers(
uint256[] memory ctx)
internal view
internal view virtual
{
uint256 channelPtr = getChannelPtr(ctx);
uint256 nQueries = ctx[MM_N_UNIQUE_QUERIES];
Expand Down
3 changes: 2 additions & 1 deletion evm-verifier/solidity/contracts/HornerEvaluator.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pragma solidity ^0.5.2;
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.6.11;

import "./PrimeFieldElement0.sol";

Expand Down
9 changes: 5 additions & 4 deletions evm-verifier/solidity/contracts/IMerkleVerifier.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
pragma solidity ^0.5.2;
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.6.11;

contract IMerkleVerifier {
abstract contract IMerkleVerifier {
uint256 constant internal MAX_N_MERKLE_VERIFIER_QUERIES = 128;

function verify(
function verifyMerkle(
uint256 channelPtr,
uint256 queuePtr,
bytes32 root,
uint256 n)
internal view
internal view virtual
returns (bytes32 hash);
}
3 changes: 2 additions & 1 deletion evm-verifier/solidity/contracts/MemoryAccessUtils.sol.ref
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pragma solidity ^0.5.2;
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.6.11;

import "./MemoryMap.sol";

Expand Down
7 changes: 4 additions & 3 deletions evm-verifier/solidity/contracts/MerkleStatementContract.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pragma solidity ^0.5.2;
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.6.11;

import "./FactRegistry.sol";
import "./interfaces/FactRegistry.sol";
import "./MerkleVerifier.sol";

contract MerkleStatementContract is MerkleVerifier, FactRegistry {
Expand Down Expand Up @@ -82,7 +83,7 @@ contract MerkleStatementContract is MerkleVerifier, FactRegistry {
mstore(0x40, add(dataToHashPtr, 0x20))
}
require(badInput == 0, "INVALID_MERKLE_INDICES");
bytes32 resRoot = verify(channelPtr, merkleQueuePtr, bytes32(expectedRoot), nQueries);
bytes32 resRoot = verifyMerkle(channelPtr, merkleQueuePtr, bytes32(expectedRoot), nQueries);
bytes32 factHash;
assembly {
// Append the resulted root (should be the return value of verify) to dataToHashPtr.
Expand Down
10 changes: 6 additions & 4 deletions evm-verifier/solidity/contracts/MerkleStatementVerifier.sol
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
pragma solidity ^0.5.2;
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.6.11;

import "./MerkleStatementContract.sol";

contract MerkleStatementVerifier is IMerkleVerifier {
abstract contract MerkleStatementVerifier is IMerkleVerifier {
MerkleStatementContract merkleStatementContract;

constructor(address merkleStatementContractAddress) internal {
constructor(address merkleStatementContractAddress) public {
merkleStatementContract = MerkleStatementContract(merkleStatementContractAddress);
}

// Computes the hash of the Merkle statement, and verifies that it is registered in the
// Merkle Fact Registry. Receives as input the queuePtr (as address), its length
// the numbers of queries n, and the root. The channelPtr is is ignored.
function verify(uint256 /*channelPtr*/, uint256 queuePtr, bytes32 root, uint256 n) internal view
function verifyMerkle(uint256 /*channelPtr*/, uint256 queuePtr, bytes32 root, uint256 n)
internal view virtual override
returns(bytes32) {
bytes32 statement;
require(n <= MAX_N_MERKLE_VERIFIER_QUERIES, "TOO_MANY_MERKLE_QUERIES");
Expand Down
7 changes: 4 additions & 3 deletions evm-verifier/solidity/contracts/MerkleVerifier.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pragma solidity ^0.5.2;
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.6.11;

import "./IMerkleVerifier.sol";

Expand All @@ -19,12 +20,12 @@ contract MerkleVerifier is IMerkleVerifier {
The input data is destroyed during verification.
*/
function verify(
function verifyMerkle(
uint256 channelPtr,
uint256 queuePtr,
bytes32 root,
uint256 n)
internal view
internal view virtual override
returns (bytes32 hash)
{
uint256 lhashMask = getHashMask();
Expand Down
6 changes: 4 additions & 2 deletions evm-verifier/solidity/contracts/PrimeFieldElement0.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pragma solidity ^0.5.2;
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.6.11;


contract PrimeFieldElement0 {
uint256 constant internal K_MODULUS =
Expand Down Expand Up @@ -86,7 +88,7 @@ contract PrimeFieldElement0 {
mstore(add(p, 0x80), exponent) // Exponent.
mstore(add(p, 0xa0), modulus) // Modulus.
// Call modexp precompile.
if iszero(staticcall(gas, 0x05, p, 0xc0, p, 0x20)) {
if iszero(staticcall(gas(), 0x05, p, 0xc0, p, 0x20)) {
revert(0, 0)
}
res := mload(p)
Expand Down
3 changes: 2 additions & 1 deletion evm-verifier/solidity/contracts/Prng.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pragma solidity ^0.5.2;
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.6.11;

import "./PrimeFieldElement0.sol";

Expand Down
Loading

0 comments on commit b2cc6f9

Please sign in to comment.