Skip to content

Commit

Permalink
Cairo v0.3.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
liorgold2 committed Aug 15, 2021
1 parent 96480ab commit 76d48b7
Show file tree
Hide file tree
Showing 22 changed files with 692 additions and 48 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ We recommend starting from [Setting up the environment](https://cairo-lang.org/d
# Installation instructions

You should be able to download the python package zip file directly from
[github](https://github.com/starkware-libs/cairo-lang/releases/tag/v0.3.0)
[github](https://github.com/starkware-libs/cairo-lang/releases/tag/v0.3.1)
and install it using ``pip``.
See [Setting up the environment](https://cairo-lang.org/docs/quickstart.html).

Expand Down Expand Up @@ -54,7 +54,7 @@ Once the docker image is built, you can fetch the python package zip file using:

```bash
> container_id=$(docker create cairo)
> docker cp ${container_id}:/app/cairo-lang-0.3.0.zip .
> docker cp ${container_id}:/app/cairo-lang-0.3.1.zip .
> docker rm -v ${container_id}
```

6 changes: 3 additions & 3 deletions src/demo/amm_demo/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def deploy_contract(batch_prover: BatchProver, w3: Web3, operator: eth.Account)
input(
f'AMM demo smart contract successfully deployed to address {contract_address}. '
'You can track the contract state through this link '
f'https://ropsten.etherscan.io/address/{contract_address} .'
f'https://goerli.etherscan.io/address/{contract_address} .'
'Press enter to continue.')

return w3.eth.contract(abi=abi, address=contract_address)
Expand All @@ -102,7 +102,7 @@ def main():

# Connect to an Ethereum node.
node_rpc_url = input(
'Please provide an RPC URL to communicate with an Ethereum node on Ropsten: ')
'Please provide an RPC URL to communicate with an Ethereum node on Goerli: ')
w3 = Web3(HTTPProvider(node_rpc_url))
if not w3.isConnected():
print('Error: could not connect to the Ethereum node.')
Expand All @@ -123,7 +123,7 @@ def main():
# Ask for funds to be transferred to the operator account id its balance is too low.
if w3.eth.getBalance(operator.address) < MIN_OPERATOR_BALANCE:
input(
f'Please send funds (at least {MIN_OPERATOR_BALANCE * 10**-18} Ropsten ETH) '
f'Please send funds (at least {MIN_OPERATOR_BALANCE * 10**-18} Goerli ETH) '
f'to {operator.address} and press enter.')
while w3.eth.getBalance(operator.address) < MIN_OPERATOR_BALANCE:
print('Funds not received yet...')
Expand Down
2 changes: 2 additions & 0 deletions src/starkware/cairo/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ python_lib(cairo_common_lib
hash_state.cairo
hash.cairo
invoke.cairo
keccak.cairo
math_cmp.cairo
math_utils.py
math.cairo
memcpy.cairo
merkle_multi_update.cairo
merkle_update.cairo
pow.cairo
registers.cairo
segments.cairo
serialize.cairo
Expand Down
33 changes: 33 additions & 0 deletions src/starkware/cairo/common/keccak.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Computes the keccak hash.
# This function is unsafe (not sound): there is no validity enforcement that the result is indeed
# keccak, but an honest prover will compute the keccak.
# Args:
# data - an array of words representing the input data. Each word in the array is 16 bytes of the
# input data, except the last word, which may be less.
# length - the number of bytes in the input.
func unsafe_keccak(data : felt*, length : felt) -> (low, high):
alloc_locals
local low
local high
%{
from eth_hash.auto import keccak
data, length = ids.data, ids.length

if '__keccak_max_size' in globals():
assert length <= __keccak_max_size, \
f'unsafe_keccak() can only be used with length<={__keccak_max_size}. ' \
f'Got: length={length}.'

keccak_input = bytearray()
for word_i, byte_i in enumerate(range(0, length, 16)):
word = memory[data + word_i]
n_bytes = min(16, length - byte_i)
assert 0 <= word < 2 ** (8 * n_bytes)
keccak_input += word.to_bytes(n_bytes, 'big')

hashed = keccak(keccak_input)
ids.high = int.from_bytes(hashed[:16], 'big')
ids.low = int.from_bytes(hashed[16:32], 'big')
%}
return (low=low, high=high)
end
51 changes: 51 additions & 0 deletions src/starkware/cairo/common/pow.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from starkware.cairo.common.math import assert_le
from starkware.cairo.common.registers import get_ap, get_fp_and_pc

# Returns base ** exp, for 0 <= exp < 2**251.
func pow{range_check_ptr}(base, exp) -> (res):
struct LoopLocals:
member bit : felt
member temp0 : felt

member res : felt
member base : felt
member exp : felt
end

if exp == 0:
return (1)
end

let initial_locs : LoopLocals* = cast(fp - 2, LoopLocals*)
initial_locs.res = 1; ap++
initial_locs.base = base; ap++
initial_locs.exp = exp; ap++

loop:
let prev_locs : LoopLocals* = cast(ap - LoopLocals.SIZE, LoopLocals*)
let locs : LoopLocals* = cast(ap, LoopLocals*)
locs.base = prev_locs.base * prev_locs.base; ap++
%{ ids.locs.bit = (ids.prev_locs.exp % PRIME) & 1 %}
jmp odd if locs.bit != 0; ap++

even:
locs.exp = prev_locs.exp / 2; ap++
locs.res = prev_locs.res; ap++
# exp cannot be 0 here.
static_assert ap + 1 == locs + LoopLocals.SIZE
jmp loop; ap++

odd:
locs.temp0 = prev_locs.exp - 1
locs.exp = locs.temp0 / 2; ap++
locs.res = prev_locs.res * prev_locs.base; ap++
static_assert ap + 1 == locs + LoopLocals.SIZE
jmp loop if locs.exp != 0; ap++

# Cap the number of steps.
let (__ap__) = get_ap()
let (__fp__, _) = get_fp_and_pc()
let n_steps = (__ap__ - cast(initial_locs, felt)) / LoopLocals.SIZE - 1
assert_le(n_steps, 251)
return (res=locs.res)
end
Loading

0 comments on commit 76d48b7

Please sign in to comment.