Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove all Yul references in EVM placeholder base #46

Draft
wants to merge 26 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,159 @@ npx hardhat deploy --network localhost

Hardhat re-uses old deployments, to force re-deploy add the `--reset` flag above


# Warp Instructions

Clone the warp transpiler repository
```
git clone [email protected]:NethermindEth/warp.git
cd warp
```

## Launch the test network

Check if this is the right docker file contents (not commited to develop on warp yet)

```
version: '3.8'

services:
warp:
image: nethermind/warp:v2.5.1
links:
- devnet
volumes:
- .:/dapp
environment:
- STARKNET_NETWORK=alpha-goerli
- STARKNET_WALLET=starkware.starknet.wallets.open_zeppelin.OpenZeppelinAccount
- STARKNET_GATEWAY_URL=http://devnet:5050
- STARKNET_FEEDER_GATEWAY_URL=http://devnet:5050
entrypoint: tail -F anything

devnet:
image: shardlabs/starknet-devnet:0.4.4-seed0
ports:
- '5050:5050'
```

Bring up docker containers

```
docker-compose up
```

## Bash into docker
Find the container id and bash into the warp container.

```shell
➜ warp git:(develop) ✗ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9f156f579a4e nethermind/warp:v2.5.1 "tail -F anything" 24 hours ago Up 24 seconds warp2-warp-1
69c639ee81e8 shardlabs/starknet-devnet:0.4.4-seed0 "starknet-devnet --h…" 24 hours ago Up 24 seconds 0.0.0.0:5050->5050/tcp, :::5050->5050/tcp warp2-devnet-1
➜ warp2 git:(develop) ✗ docker exec -it 9f156f579a4e /bin/bash
```

All interactions here on are carried out in the docker environment.

### Create an account

You need to create an account and fund it and deploy it before being able to deploy contracts/test.

```
starknet new_account --feeder_gateway_url http://devnet:5050 --gateway_url http://devnet:5050
```

Add some tokens to your address.
```
curl http://devnet:5050/mint -H "Content-Type: application/json" -d '{"amount": 1000000000000000000, "address": "<account-address>"}'
```

Deploy the account

```
starknet deploy_account --feeder_gateway_url http://devnet:5050 --gateway_url http://devnet:5050
```

If you get a below error

```
Error:
BadRequest: HTTP error ocurred. Status: 500.
Text: {"code":"StarknetErrorCode.UNINITIALIZED_CONTRACT","message":"Requested contract
address 0x7b66ab9b86126568a44856c1a548977b11ad67f18c760b02d83e9ed14bca134 is not deployed."}
```

This could happen if your account was not funded. Try re-funding and re-deploying but this needs
a force flag.

```
starknet deploy_account --feeder_gateway_url http://devnet:5050 --gateway_url http://devnet:5050 --force
```


## Transpiling/Compiling/Deploying

## Transpiling contracts

Copy the `contracts/` directory from `evm-placeholder-verification` to `exampleContracts/` in warp. Ex



```
warp transpile exampleContracts/ERC20.sol
```

Transpilation outputs to `warp_output/exampleContracts/ERC20.sol/` or similar directories based on
your contract name.

## Compile

```
root@9f156f579a4e:/dapp# warp compile warp_output/exampleContracts/ERC20.sol/WARP.cairo
Running starknet compile with cairoPath /usr/src/warp-stable
starknet-compile output written to warp_output/exampleContracts/ERC20.sol/WARP_compiled.json
```
Compilation outputs a json file

To transpile the base verifier , copy the `contracts/` directory
from `evm-placeholder-verification` to `exampleContracts/` in warp.

### Deploy

Deployment on testnet is done via the following two steps

1. Create a class hash
```
starknet declare --contract WARP_compiled.json --feeder_gateway_url http://devnet:5050 --gateway_url http://devnet:5050
```

2. Deploy
```
starknet deploy --class_hash 0x5c341f52e9284d3a3925440ce43d1a825962d26e82731997cd3b3aedec81b3f --gateway_url http://devnet:5050 --feeder_gateway_url http://devnet:5050
```

### Invoking/Calling contracts

Invoking contracts involves state changes (working assumption)

```
starknet invoke --address 0x077487db16639d5c4a0c66f66ccda78a54ebaed04d9dd4b4f4d87d0adfd49d5b \
--abi warp_output/exampleContracts/TestWarp.sol/TestWarp_abi.json \
--function fmul_51a017f7 --inputs 10 0 10 0 10 0 --feeder_gateway_url http://devnet:5050 --gateway_url http://devnet:5050
```


Calling contracts does not involve state changes(working assumption)
```
starknet call --address 0x01f54865c30ee9b8f7f0b841b45990772ef04ca479592aa7073b3dd518cf2754 --abi \
warp_output/exampleContracts/TestWarp.sol/TestWarp_abi.json \
--function fmul_51a017f7 --inputs 10 0 10 0 3 0 --feeder_gateway_url http://devnet:5050 --gateway_url http://devnet:5050
```




## Community

Issue reports are preferred to be done with Github Issues in here: https://github.com/NilFoundation/evm-placeholder-verification/issues.
Expand Down
135 changes: 0 additions & 135 deletions contracts/algebra/bn254.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,139 +25,4 @@ import {types} from "../types.sol";
library bn254_crypto {
uint256 constant p_mod = 21888242871839275222246405745257275088696311157297823662689037894645226208583;
uint256 constant r_mod = 21888242871839275222246405745257275088548364400416034343698204186575808495617;

// Perform a modular exponentiation. This method is ideal for small exponents (~64 bits or less), as
// it is cheaper than using the pow precompile
function pow_small(uint256 base, uint256 exponent, uint256 modulus) internal pure returns (uint256) {
uint256 result = 1;
uint256 input = base;
uint256 count = 1;

assembly {
let endpoint := add(exponent, 0x01)
for {} lt(count, endpoint) {count := add(count, count)}
{
if and(exponent, count) {
result := mulmod(result, input, modulus)
}
input := mulmod(input, input, modulus)
}
}

return result;
}

function invert(uint256 fr) internal view returns (uint256) {
uint256 output;
bool success;
uint256 p = r_mod;
assembly {
let mPtr := mload(0x40)
mstore(mPtr, 0x20)
mstore(add(mPtr, 0x20), 0x20)
mstore(add(mPtr, 0x40), 0x20)
mstore(add(mPtr, 0x60), fr)
mstore(add(mPtr, 0x80), sub(p, 2))
mstore(add(mPtr, 0xa0), p)
success := staticcall(gas(), 0x05, mPtr, 0xc0, 0x00, 0x20)
output := mload(0x00)
}
require(success, "pow precompile call failed!");
return output;
}

function new_g1(uint256 x, uint256 y) internal pure returns (types.g1_point memory) {
uint256 xValue;
uint256 yValue;
assembly {
xValue := mod(x, r_mod)
yValue := mod(y, r_mod)
}
return types.g1_point(xValue, yValue);
}

function new_g2(uint256 x0, uint256 x1, uint256 y0, uint256 y1) internal pure returns (types.g2_point memory) {
return types.g2_point(x0, x1, y0, y1);
}

function P1() internal pure returns (types.g1_point memory) {
return types.g1_point(1, 2);
}

function P2() internal pure returns (types.g2_point memory) {
return types.g2_point({
x0 : 0x198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2,
x1 : 0x1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed,
y0 : 0x090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b,
y1 : 0x12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa
});
}


/// Evaluate the following pairing product:
/// e(a1, a2).e(-b1, b2) == 1
function pairingProd2(
types.g1_point memory a1,
types.g2_point memory a2,
types.g1_point memory b1,
types.g2_point memory b2
) internal view returns (bool) {
validateG1Point(a1);
validateG1Point(b1);
bool success;
uint256 out;
assembly {
let mPtr := mload(0x40)
mstore(mPtr, mload(a1))
mstore(add(mPtr, 0x20), mload(add(a1, 0x20)))
mstore(add(mPtr, 0x40), mload(a2))
mstore(add(mPtr, 0x60), mload(add(a2, 0x20)))
mstore(add(mPtr, 0x80), mload(add(a2, 0x40)))
mstore(add(mPtr, 0xa0), mload(add(a2, 0x60)))

mstore(add(mPtr, 0xc0), mload(b1))
mstore(add(mPtr, 0xe0), mload(add(b1, 0x20)))
mstore(add(mPtr, 0x100), mload(b2))
mstore(add(mPtr, 0x120), mload(add(b2, 0x20)))
mstore(add(mPtr, 0x140), mload(add(b2, 0x40)))
mstore(add(mPtr, 0x160), mload(add(b2, 0x60)))
success := staticcall(
gas(),
8,
mPtr,
0x180,
0x00,
0x20
)
out := mload(0x00)
}
require(success, "Pairing check failed!");
return (out != 0);
}

/**
* validate the following:
* x != 0
* y != 0
* x < p
* y < p
* y^2 = x^3 + 3 mod p
*/
function validateG1Point(types.g1_point memory point) internal pure {
bool is_well_formed;
uint256 p = p_mod;
assembly {
let x := mload(point)
let y := mload(add(point, 0x20))

is_well_formed := and(
and(
and(lt(x, p), lt(y, p)),
not(or(iszero(x), iszero(y)))
),
eq(mulmod(y, y, p), addmod(mulmod(x, mulmod(x, x, p), p), 3, p))
)
}
require(is_well_formed, "Bn254: G1 point not on curve, or is malformed");
}
}
Loading