Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Use new cairo sytax #243

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
243 changes: 121 additions & 122 deletions exercises/builtins/bitwise.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,130 +6,129 @@ from starkware.cairo.common.bitwise import bitwise_and, bitwise_xor, bitwise_or
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin
from starkware.cairo.common.pow import pow

# While operations like addition, multiplication and divisions are native for felts,
# bit operations are more difficult to implement for felt.
# The bitwise builtin allows computing logical operations such as XOR, AND and OR on 251-bit felts.
// While operations like addition, multiplication and divisions are native for felts,
// bit operations are more difficult to implement for felt.
// The bitwise builtin allows computing logical operations such as XOR, AND and OR on 251-bit felts.

# Resources:
# - https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/cairo/common/bitwise.cairo
# - https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/cairo/common/cairo_builtins.cairo#L17
// Resources:
// - https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/cairo/common/bitwise.cairo
// - https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/cairo/common/cairo_builtins.cairo#L17

# I AM NOT DONE
// I AM NOT DONE

# TODO: Use a bitwise operation to return the n-th bit of the value parameter
// TODO: Use a bitwise operation to return the n-th bit of the value parameter

func get_nth_bit{bitwise_ptr : BitwiseBuiltin*, range_check_ptr : felt}(value, n) -> (res):
# FILL ME
return (res)
end
func get_nth_bit{bitwise_ptr: BitwiseBuiltin*, range_check_ptr: felt}(value, n) -> (res: felt) {
// FILL ME
return (res,);
}

# TODO: Use a bitwise operation to set the n-th bit of the value parameter to 1
// TODO: Use a bitwise operation to set the n-th bit of the value parameter to 1

func set_nth_bit{bitwise_ptr : BitwiseBuiltin*, range_check_ptr : felt}(value, n) -> (res):
# FILL ME
return (res)
end
func set_nth_bit{bitwise_ptr: BitwiseBuiltin*, range_check_ptr: felt}(value, n) -> (res: felt) {
// FILL ME
return (res,);
}

# TODO: Use a bitwise operation to toggle the n-th bit of the value parameter
// TODO: Use a bitwise operation to toggle the n-th bit of the value parameter

func toggle_nth_bit{bitwise_ptr : BitwiseBuiltin*, range_check_ptr : felt}(value, n) -> (res):
# FILL ME
return (res)
end
func toggle_nth_bit{bitwise_ptr: BitwiseBuiltin*, range_check_ptr: felt}(value, n) -> (res: felt) {
// FILL ME
return (res,);
}

# Let's write a unique function that combines all the functions above.
# This function should use the bitwise_ptr explicitly.
# In particular, do not use any of the functions above.
// Let's write a unique function that combines all the functions above.
// This function should use the bitwise_ptr explicitly.
// In particular, do not use any of the functions above.

# TODO: Write a function that takes as argument
# - a felt `op` in ['get', 'set', 'toggle'],
# - felts `value` and `n`,
# and returns the result of the operation applied to `n`-th bit of value.
# Make sure
# - the argument `n` is within the correct bitwise bounds,
# - the `op` argument is correct.
// TODO: Write a function that takes as argument
// - a felt `op` in ['get', 'set', 'toggle'],
// - felts `value` and `n`,
// and returns the result of the operation applied to `n`-th bit of value.
// Make sure
// - the argument `n` is within the correct bitwise bounds,
// - the `op` argument is correct.

func op_nth_bit{bitwise_ptr : BitwiseBuiltin*, range_check_ptr : felt}(op, value, n) -> (res):
alloc_locals
func op_nth_bit{bitwise_ptr: BitwiseBuiltin*, range_check_ptr: felt}(op, value, n) -> (res: felt) {
alloc_locals;

# Assert op is correct
// Assert op is correct

with_attr error_message("Bad bitwise bounds"):
# Assert n is within bounds
end
with_attr error_message("Bad bitwise bounds") {
// Assert n is within bounds
}

# Compute the operation
# Don't forget to advance bitwise_ptr
// Compute the operation
// Don't forget to advance bitwise_ptr

return (res)
end
return (res,);
}

# Do not modify the tests.
// Do not modify the tests.
@view
func test_get_nth_bit{syscall_ptr : felt*, bitwise_ptr : BitwiseBuiltin*, range_check_ptr : felt}():
alloc_locals
local value = 0xA8
let (local r0) = get_nth_bit(value, 0)
let (local r1) = get_nth_bit(value, 1)
let (local r2) = get_nth_bit(value, 2)
let (local r3) = get_nth_bit(value, 3)
let (local r4) = get_nth_bit(value, 4)
let (local r5) = get_nth_bit(value, 5)
let (local r6) = get_nth_bit(value, 6)
let (local r7) = get_nth_bit(value, 7)
assert r0 * 2 ** 0 + r1 * 2 ** 1 + r2 * 2 ** 2 + r3 * 2 ** 3 + r4 * 2 ** 4 + r5 * 2 ** 5 + r6 * 2 ** 6 + r7 * 2 ** 7 = value
return ()
end
func test_get_nth_bit{syscall_ptr: felt*, bitwise_ptr: BitwiseBuiltin*, range_check_ptr: felt}() {
alloc_locals;
local value = 0xA8;
let (local r0) = get_nth_bit(value, 0);
let (local r1) = get_nth_bit(value, 1);
let (local r2) = get_nth_bit(value, 2);
let (local r3) = get_nth_bit(value, 3);
let (local r4) = get_nth_bit(value, 4);
let (local r5) = get_nth_bit(value, 5);
let (local r6) = get_nth_bit(value, 6);
let (local r7) = get_nth_bit(value, 7);
assert r0 * 2 ** 0 + r1 * 2 ** 1 + r2 * 2 ** 2 + r3 * 2 ** 3 + r4 * 2 ** 4 + r5 * 2 ** 5 + r6 * 2 ** 6 + r7 * 2 ** 7 = value;
return ();
}

@view
func test_set_nth_bit{syscall_ptr : felt*, bitwise_ptr : BitwiseBuiltin*, range_check_ptr : felt}():
alloc_locals
local value = 0
let (local r0) = set_nth_bit(value, 0)
assert 1 = r0
let (local r1) = set_nth_bit(r0, 1)
assert 3 = r1
let (local r2) = set_nth_bit(r1, 2)
assert 7 = r2
let (local r3) = set_nth_bit(r2, 3)
assert 15 = r3
let (local r4) = set_nth_bit(r3, 4)
assert 31 = r4
let (local r5) = set_nth_bit(r4, 5)
assert 63 = r5
let (local r6) = set_nth_bit(r5, 6)
assert 127 = r6
let (local r7) = set_nth_bit(r6, 7)
assert 255 = r7
return ()
end
func test_set_nth_bit{syscall_ptr: felt*, bitwise_ptr: BitwiseBuiltin*, range_check_ptr: felt}() {
alloc_locals;
local value = 0;
let (local r0) = set_nth_bit(value, 0);
assert 1 = r0;
let (local r1) = set_nth_bit(r0, 1);
assert 3 = r1;
let (local r2) = set_nth_bit(r1, 2);
assert 7 = r2;
let (local r3) = set_nth_bit(r2, 3);
assert 15 = r3;
let (local r4) = set_nth_bit(r3, 4);
assert 31 = r4;
let (local r5) = set_nth_bit(r4, 5);
assert 63 = r5;
let (local r6) = set_nth_bit(r5, 6);
assert 127 = r6;
let (local r7) = set_nth_bit(r6, 7);
assert 255 = r7;
return ();
}

@view
func test_toggle_nth_bit{
syscall_ptr : felt*, bitwise_ptr : BitwiseBuiltin*, range_check_ptr : felt
}():
alloc_locals

local value = nondet %{ int('100000011010111', 2) %}
let (res) = toggle_nth_bit(value, 14)
let (res) = toggle_nth_bit(res, 3)
let (res) = toggle_nth_bit(res, 5)
assert res = 2 ** 8 - 1
return ()
end
func test_toggle_nth_bit{syscall_ptr: felt*, bitwise_ptr: BitwiseBuiltin*, range_check_ptr: felt}(
) {
alloc_locals;

local value = nondet %{ int('100000011010111', 2) %};
let (res) = toggle_nth_bit(value, 14);
let (res) = toggle_nth_bit(res, 3);
let (res) = toggle_nth_bit(res, 5);
assert res = 2 ** 8 - 1;
return ();
}

@view
func test_op_nth_bit{syscall_ptr : felt*, bitwise_ptr : BitwiseBuiltin*, range_check_ptr : felt}():
alloc_locals
local v0
local v1
local v2
local n0
local n1
local n2
local r0
local r1
local r2
func test_op_nth_bit{syscall_ptr: felt*, bitwise_ptr: BitwiseBuiltin*, range_check_ptr: felt}() {
alloc_locals;
local v0;
local v1;
local v2;
local n0;
local n1;
local n2;
local r0;
local r1;
local r2;
%{
from random import randint
size = 249
Expand All @@ -144,34 +143,34 @@ func test_op_nth_bit{syscall_ptr : felt*, bitwise_ptr : BitwiseBuiltin*, range_c
ids.r1 = ids.v1 | (1 << ids.n1)
ids.r2 = ids.v2 ^ (1 << ids.n2)
%}
let (val0) = op_nth_bit('get', v0, n0)
assert r0 = val0
let (val1) = op_nth_bit('set', v1, n1)
assert r1 = val1
let (val2) = op_nth_bit('toggle', v2, n2)
assert r2 = val2
let (val0) = op_nth_bit('get', v0, n0);
assert r0 = val0;
let (val1) = op_nth_bit('set', v1, n1);
assert r1 = val1;
let (val2) = op_nth_bit('toggle', v2, n2);
assert r2 = val2;

%{ expect_revert() %}
let (_) = op_nth_bit('rigged', v0, n1)
return ()
end
let (_) = op_nth_bit('rigged', v0, n1);
return ();
}

@view
func test_bitwise_bounds_negative_ko{
syscall_ptr : felt*, bitwise_ptr : BitwiseBuiltin*, range_check_ptr : felt
}():
alloc_locals
syscall_ptr: felt*, bitwise_ptr: BitwiseBuiltin*, range_check_ptr: felt
}() {
alloc_locals;
%{ expect_revert(error_message="Bad bitwise bounds") %}
let (res) = op_nth_bit('set', 1337, -42)
return ()
end
let (res) = op_nth_bit('set', 1337, -42);
return ();
}

@view
func test_bitwise_bounds_too_high_ko{
syscall_ptr : felt*, bitwise_ptr : BitwiseBuiltin*, range_check_ptr : felt
}():
alloc_locals
syscall_ptr: felt*, bitwise_ptr: BitwiseBuiltin*, range_check_ptr: felt
}() {
alloc_locals;
%{ expect_revert(error_message="Bad bitwise bounds") %}
let (res) = op_nth_bit('set', 1337, 251)
return ()
end
let (res) = op_nth_bit('set', 1337, 251);
return ();
}
36 changes: 18 additions & 18 deletions exercises/hints/hints00.cairo
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
%lang starknet

# Hints
// Hints

# Hints are the Cairo way to defer part of the program execution to an external program
# Memory can be accessed and assigned inside a hint by using variables identifiers.
# e.g., inside a hint variable `a` is accessed through `ids.a`
// Hints are the Cairo way to defer part of the program execution to an external program
// Memory can be accessed and assigned inside a hint by using variables identifiers.
// e.g., inside a hint variable `a` is accessed through `ids.a`

# I AM NOT DONE
// I AM NOT DONE

# TODO: Assign the value of `res` inside a hint.
// TODO: Assign the value of `res` inside a hint.

func basic_hint() -> (value : felt):
alloc_locals
local res
# TODO: Insert hint here
return (res)
end
func basic_hint() -> (value: felt) {
alloc_locals;
local res;
// TODO: Insert hint here
return (res,);
}

# Do not change the test
// Do not change the test
@external
func test_basic_hint{syscall_ptr : felt*}():
let (value) = basic_hint()
assert 41 = value - 1
return ()
end
func test_basic_hint{syscall_ptr: felt*}() {
let (value) = basic_hint();
assert 41 = value - 1;
return ();
}
Loading