diff --git a/.github/ci.yml b/.github/ci.yml new file mode 100644 index 000000000..b4df574e7 --- /dev/null +++ b/.github/ci.yml @@ -0,0 +1,13 @@ +name: CI +on: + push: + merge_group: + pull_request: +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: software-mansion/setup-scarb@v1 + - run: scarb fmt --check + - run: scarb test diff --git a/.tool-versions b/.tool-versions index 4fd758a0a..21cfc8077 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1,2 +1 @@ -scarb 2.3.1 -starknet-foundry 0.11.0 +scarb 2.4.0 diff --git a/Cargo.lock b/Cargo.lock deleted file mode 100644 index 0f487ace1..000000000 --- a/Cargo.lock +++ /dev/null @@ -1,7 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "test_sandbox" -version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml deleted file mode 100644 index fa0da3c9c..000000000 --- a/Cargo.toml +++ /dev/null @@ -1,18 +0,0 @@ -[workspace] -resolver = "2" -members = [ - "src/test_sandbox" -] - -[workspace.package] -edition = "2021" -version = "0.1.0" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[workspace.dependencies] -starknet = { git = "https://github.com/xJonathanLEI/starknet-rs" } -tokio = { version = "1.0", features = ["macros"] } -thiserror = "1" -cairo-lang-starknet = "2" -lazy_static = "1.4.0" \ No newline at end of file diff --git a/README.md b/README.md index 7bb724506..c0816eb2c 100644 --- a/README.md +++ b/README.md @@ -3,23 +3,19 @@ ## Build ``` sh scarb build -cargo build ``` ## Test ``` sh -snforge test -cargo test +scarb test ``` ## Format ``` sh scarb fmt -cargo fmt ``` ## Clean ``` sh scarb clean -cargo clean -``` \ No newline at end of file +``` diff --git a/Scarb.lock b/Scarb.lock index c679b4329..0743bc011 100644 --- a/Scarb.lock +++ b/Scarb.lock @@ -2,48 +2,5 @@ version = 1 [[package]] -name = "cairo_blake2s" +name = "cairo_verifier" version = "0.1.0" -source = "git+https://github.com/HerodotusDev/cairo-blake2s.git#7aea89a1a9fbdfb689faa6086c8dd68abae0b5cd" - -[[package]] -name = "channel" -version = "0.1.0" -dependencies = [ - "cairo_blake2s", - "common", -] - -[[package]] -name = "channel_contract" -version = "0.1.0" -dependencies = [ - "channel", - "openzeppelin", - "snforge_std", -] - -[[package]] -name = "common" -version = "0.1.0" - -[[package]] -name = "openzeppelin" -version = "0.8.0" -source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.8.0#c23e8e96de60e6e3159b1ff8591a1187269c0eb7" - -[[package]] -name = "snforge_std" -version = "0.1.0" -source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.11.0#5465c41541c44a7804d16318fab45a2f0ccec9e7" - -[[package]] -name = "structs" -version = "0.1.0" - -[[package]] -name = "verifier" -version = "0.1.0" -dependencies = [ - "structs", -] diff --git a/Scarb.toml b/Scarb.toml index fc7b341f6..73d345695 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -1,28 +1,3 @@ -[workspace] -members = [ - "src/structs", - "src/verifier", - "src/channel", - "src/common", - "contract/channel_contract", -] +[package] name = "cairo_verifier" version = "0.1.0" - -[workspace.dependencies] -openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.8.0" } -snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.11.0" } -cairo_blake2s = { git = "https://github.com/HerodotusDev/cairo-blake2s.git" } -starknet = "2.3.1" -structs = { path = "src/structs" } -verifier = { path = "src/verifier" } -channel = { path = "src/channel" } -common = { path = "src/common" } - -[tool.snforge] -exit_first = true - -# Default profile -[tool.sncast] -account = "cairo1_verifier" -url = "https://devnet.starknet.visoft.solutions" \ No newline at end of file diff --git a/contract/channel_contract/Scarb.toml b/contract/channel_contract/Scarb.toml deleted file mode 100644 index e4f4f5534..000000000 --- a/contract/channel_contract/Scarb.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "channel_contract" -version = "0.1.0" - -[dependencies] -openzeppelin = { workspace = true } -snforge_std = { workspace = true } -starknet = { workspace = true } -channel = { workspace = true } - -[[target.starknet-contract]] -sierra = true -casm = true \ No newline at end of file diff --git a/contract/channel_contract/src/lib.cairo b/contract/channel_contract/src/lib.cairo deleted file mode 100644 index 8b9fe58b8..000000000 --- a/contract/channel_contract/src/lib.cairo +++ /dev/null @@ -1,17 +0,0 @@ -#[starknet::interface] -trait IChannelContract { - fn add(ref self: TContractState, a: felt252, b: felt252) -> felt252; -} - -#[starknet::contract] -mod ChannelContract { - #[storage] - struct Storage {} - - #[external(v0)] - impl ChannelContractImpl of super::IChannelContract { - fn add(ref self: ContractState, a: felt252, b: felt252) -> felt252 { - 0 - } - } -} diff --git a/contract/channel_contract/tests/test_contract.cairo b/contract/channel_contract/tests/test_contract.cairo deleted file mode 100644 index 65778ce2c..000000000 --- a/contract/channel_contract/tests/test_contract.cairo +++ /dev/null @@ -1,19 +0,0 @@ -use starknet::ContractAddress; - -use snforge_std::{declare, ContractClassTrait}; - -use channel_contract::IChannelContractSafeDispatcher; -use channel_contract::IChannelContractSafeDispatcherTrait; - -fn deploy_contract(name: felt252) -> ContractAddress { - let contract = declare(name); - contract.deploy(@ArrayTrait::new()).unwrap() -} - -#[test] -fn test_add() { - let contract_address = deploy_contract('ChannelContract'); - let safe_dispatcher = IChannelContractSafeDispatcher { contract_address }; - let result = safe_dispatcher.add(42, 11).unwrap(); - assert(result == 0, 'Invalid value'); -} diff --git a/rust-toolchain.toml b/rust-toolchain.toml deleted file mode 100644 index 0394d23e5..000000000 --- a/rust-toolchain.toml +++ /dev/null @@ -1,4 +0,0 @@ -[toolchain] -channel = "1.74.0" -components = ["rustfmt", "clippy"] -profile = "minimal" \ No newline at end of file diff --git a/rustfmt.toml b/rustfmt.toml deleted file mode 100644 index 2d2a18c96..000000000 --- a/rustfmt.toml +++ /dev/null @@ -1,20 +0,0 @@ -edition = "2021" -newline_style = "Unix" -use_field_init_shorthand = true -use_small_heuristics = "Max" -use_try_shorthand = true -max_width = 100 - -# Unstable features below -# unstable_features = true -# version = "Two" -# comment_width = 100 -# format_code_in_doc_comments = true -# format_macro_bodies = true -# format_macro_matchers = true -# format_strings = true -# imports_granularity = "Module" -# group_imports = "StdExternalCrate" -# normalize_comments = true -# normalize_doc_attributes = true -# wrap_comments = true \ No newline at end of file diff --git a/src/channel.cairo b/src/channel.cairo new file mode 100644 index 000000000..1cfd407d4 --- /dev/null +++ b/src/channel.cairo @@ -0,0 +1,4 @@ +mod channel; + +#[cfg(test)] +mod tests; diff --git a/src/channel/Scarb.toml b/src/channel/Scarb.toml deleted file mode 100644 index e0a0c254b..000000000 --- a/src/channel/Scarb.toml +++ /dev/null @@ -1,7 +0,0 @@ -[package] -name = "channel" -version = "0.1.0" - -[dependencies] -cairo_blake2s = { workspace = true } -common = { workspace = true } \ No newline at end of file diff --git a/src/channel/src/lib.cairo b/src/channel/channel.cairo similarity index 66% rename from src/channel/src/lib.cairo rename to src/channel/channel.cairo index 0dc9e6e66..1a4997130 100644 --- a/src/channel/src/lib.cairo +++ b/src/channel/channel.cairo @@ -1,17 +1,14 @@ -use common::flip_endiannes::FlipEndiannessTrait; -use core::option::OptionTrait; -use core::traits::TryInto; -use core::array::ArrayTrait; -use core::traits::Into; -use common::to_array::ToArrayTrait; -use common::blake2s::blake2s; +use cairo_verifier::common::{ + flip_endiannes::FlipEndiannessTrait, to_array::ToArrayTrait, blake2s::blake2s +}; const C_PRIME_AS_UINT256_LOW: u128 = 31; const C_PRIME_AS_UINT256_HIGH: u128 = 329648542954659146201578277794459156480; // 31 * 0x8000000000000110000000000000000; const STARK_PRIME: u256 = 3618502788666131213697322783095070105623107215331596699973092056135872020481; -const INVERSE_2_TO_256_MOD_STARK_PRIME: felt252 = 113078212145816603762751633895895194930089271709401121343797004406777446400; +const INVERSE_2_TO_256_MOD_STARK_PRIME: felt252 = + 113078212145816603762751633895895194930089271709401121343797004406777446400; #[derive(Drop)] struct Channel { @@ -19,18 +16,7 @@ struct Channel { counter: u256, } -trait ChannelTrait { - fn new(digest: u256) -> Channel; - - // Generate randomness. - fn random_uint256_to_prover(ref self: Channel) -> u256; - fn random_felts_to_prover(ref self: Channel, n: felt252) -> Array; -// // Reads a 64bit integer from the prover. -// fn read_uint256_from_prover(ref self: Channel) -> u256; -// // Reads a field elements from the prover -// fn read_felts_from_prover(ref self: Channel, n: felt252) -> Array; -} - +#[generate_trait] impl ChannelImpl of ChannelTrait { fn new(digest: u256) -> Channel { Channel { digest: digest, counter: 0 } diff --git a/src/channel/tests.cairo b/src/channel/tests.cairo new file mode 100644 index 000000000..3b2083492 --- /dev/null +++ b/src/channel/tests.cairo @@ -0,0 +1 @@ +mod test_channel; diff --git a/src/channel/tests/test_channel.cairo b/src/channel/tests/test_channel.cairo new file mode 100644 index 000000000..273ba3a9c --- /dev/null +++ b/src/channel/tests/test_channel.cairo @@ -0,0 +1,33 @@ +use cairo_verifier::channel::channel::ChannelTrait; + +#[test] +#[available_gas(9999999999)] +fn test_random_felts_to_prover() { + let mut channel = ChannelTrait::new( + u256 { low: 0xf7685ebd40e852b164633a4acbd3244c, high: 0xe8e77626586f73b955364c7b4bbf0bb7 } + ); + let random = channel.random_felts_to_prover(3); + assert( + *random[0] == 3199910790894706855027093840383592257502485581126271436027309705477370004002, + 'invalid random felts[0]' + ); + assert( + *random[1] == 2678311171676075552444787698918310126938416157877134200897080931937186268438, + 'invalid random felts[1]' + ); + assert( + *random[2] == 2409925148191156067407217062797240658947927224212800962983204460004996362724, + 'invalid random felts[2]' + ); +} + +#[test] +#[available_gas(9999999999)] +fn test_random_uint256_to_prover() { + let mut channel = ChannelTrait::new(0); + let random = channel.random_uint256_to_prover(); + assert( + random == 0xae09db7cd54f42b490ef09b6bc541af688e4959bb8c53f359a6f56e38ab454a3, + 'invalid random uint256' + ); +} diff --git a/src/channel/tests/tests.cairo b/src/channel/tests/tests.cairo deleted file mode 100644 index 553122b83..000000000 --- a/src/channel/tests/tests.cairo +++ /dev/null @@ -1,2 +0,0 @@ -mod random_felts_to_prover; -mod random_uint256_to_prover; diff --git a/src/channel/tests/tests/random_felts_to_prover.cairo b/src/channel/tests/tests/random_felts_to_prover.cairo deleted file mode 100644 index 6fa30407d..000000000 --- a/src/channel/tests/tests/random_felts_to_prover.cairo +++ /dev/null @@ -1,14 +0,0 @@ -use core::debug::PrintTrait; -use core::array::ArrayTrait; -use channel::ChannelTrait; - -#[test] -fn test_random_felts_to_prover_0() { - let mut channel = ChannelTrait::new( - u256 { low: 0xf7685ebd40e852b164633a4acbd3244c, high: 0xe8e77626586f73b955364c7b4bbf0bb7 } - ); - let random = channel.random_felts_to_prover(3); - assert(*random[0] == 3199910790894706855027093840383592257502485581126271436027309705477370004002, 'invalid random felts[0]'); - assert(*random[1] == 2678311171676075552444787698918310126938416157877134200897080931937186268438, 'invalid random felts[1]'); - assert(*random[2] == 2409925148191156067407217062797240658947927224212800962983204460004996362724, 'invalid random felts[2]'); -} diff --git a/src/channel/tests/tests/random_uint256_to_prover.cairo b/src/channel/tests/tests/random_uint256_to_prover.cairo deleted file mode 100644 index 182eecb12..000000000 --- a/src/channel/tests/tests/random_uint256_to_prover.cairo +++ /dev/null @@ -1,10 +0,0 @@ -use core::debug::PrintTrait; -use core::array::ArrayTrait; -use channel::ChannelTrait; - -#[test] -fn test_random_uint256_to_prover_0() { - let mut channel = ChannelTrait::new(0); - let random = channel.random_uint256_to_prover(); - assert(random == 0xae09db7cd54f42b490ef09b6bc541af688e4959bb8c53f359a6f56e38ab454a3, 'invalid random uint256'); -} diff --git a/src/common/src/lib.cairo b/src/common.cairo similarity index 63% rename from src/common/src/lib.cairo rename to src/common.cairo index 121f3642b..95b44e817 100644 --- a/src/common/src/lib.cairo +++ b/src/common.cairo @@ -1,5 +1,8 @@ +mod blake2s; +mod flip_endiannes; +mod from_span; mod horner_eval; mod to_array; -mod from_span; -mod flip_endiannes; -mod blake2s; \ No newline at end of file + +#[cfg(test)] +mod tests; diff --git a/src/common/Scarb.lock b/src/common/Scarb.lock deleted file mode 100644 index 059d4672b..000000000 --- a/src/common/Scarb.lock +++ /dev/null @@ -1,6 +0,0 @@ -# Code generated by scarb DO NOT EDIT. -version = 1 - -[[package]] -name = "common" -version = "0.1.0" diff --git a/src/common/Scarb.toml b/src/common/Scarb.toml deleted file mode 100644 index ca62b419e..000000000 --- a/src/common/Scarb.toml +++ /dev/null @@ -1,5 +0,0 @@ -[package] -name = "common" -version = "0.1.0" - -[dependencies] \ No newline at end of file diff --git a/src/common/src/blake2s.cairo b/src/common/blake2s.cairo similarity index 83% rename from src/common/src/blake2s.cairo rename to src/common/blake2s.cairo index 3a321a033..c49248194 100644 --- a/src/common/src/blake2s.cairo +++ b/src/common/blake2s.cairo @@ -87,19 +87,12 @@ fn blake2s_init() -> blake2s_state { i += 1; }; - blake2s_state { - h: blake2s_IV, - t0: 0, - t1: 0, - f0: 0, - buf: buf, - buflen: 0 - } + blake2s_state { h: blake2s_IV, t0: 0, t1: 0, f0: 0, buf: buf, buflen: 0 } } fn blake2s_compress(mut s: blake2s_state, m: Array) -> blake2s_state { assert(m.len() == 16, 'in array must have length 16'); - + let mut v0: u32 = *s.h[0]; let mut v1: u32 = *s.h[1]; let mut v2: u32 = *s.h[2]; @@ -139,21 +132,45 @@ fn blake2s_compress(mut s: blake2s_state, m: Array) -> blake2s_state { break; } if i == 0 { - a = v0; b = v4; c = v8; d = v12; + a = v0; + b = v4; + c = v8; + d = v12; } else if i == 1 { - a = v1; b = v5; c = v9; d = v13; + a = v1; + b = v5; + c = v9; + d = v13; } else if i == 2 { - a = v2; b = v6; c = v10; d = v14; + a = v2; + b = v6; + c = v10; + d = v14; } else if i == 3 { - a = v3; b = v7; c = v11; d = v15; + a = v3; + b = v7; + c = v11; + d = v15; } else if i == 4 { - a = v0; b = v5; c = v10; d = v15; + a = v0; + b = v5; + c = v10; + d = v15; } else if i == 5 { - a = v1; b = v6; c = v11; d = v12; + a = v1; + b = v6; + c = v11; + d = v12; } else if i == 6 { - a = v2; b = v7; c = v8; d = v13; + a = v2; + b = v7; + c = v8; + d = v13; } else if i == 7 { - a = v3; b = v4; c = v9; d = v14; + a = v3; + b = v4; + c = v9; + d = v14; }; // G function begin @@ -181,21 +198,45 @@ fn blake2s_compress(mut s: blake2s_state, m: Array) -> blake2s_state { // G function end if i == 0 { - v0 = a; v4 = b; v8 = c; v12 = d; + v0 = a; + v4 = b; + v8 = c; + v12 = d; } else if i == 1 { - v1 = a; v5 = b; v9 = c; v13 = d; + v1 = a; + v5 = b; + v9 = c; + v13 = d; } else if i == 2 { - v2 = a; v6 = b; v10 = c; v14 = d; + v2 = a; + v6 = b; + v10 = c; + v14 = d; } else if i == 3 { - v3 = a; v7 = b; v11 = c; v15 = d; + v3 = a; + v7 = b; + v11 = c; + v15 = d; } else if i == 4 { - v0 = a; v5 = b; v10 = c; v15 = d; + v0 = a; + v5 = b; + v10 = c; + v15 = d; } else if i == 5 { - v1 = a; v6 = b; v11 = c; v12 = d; + v1 = a; + v6 = b; + v11 = c; + v12 = d; } else if i == 6 { - v2 = a; v7 = b; v8 = c; v13 = d; + v2 = a; + v7 = b; + v8 = c; + v13 = d; } else if i == 7 { - v3 = a; v4 = b; v9 = c; v14 = d; + v3 = a; + v4 = b; + v9 = c; + v14 = d; }; i += 1; @@ -364,4 +405,4 @@ fn blake2s_final(mut s: blake2s_state) -> u256 { }; result -} \ No newline at end of file +} diff --git a/src/common/src/flip_endiannes.cairo b/src/common/flip_endiannes.cairo similarity index 83% rename from src/common/src/flip_endiannes.cairo rename to src/common/flip_endiannes.cairo index e0cde4a9c..ef2d27020 100644 --- a/src/common/src/flip_endiannes.cairo +++ b/src/common/flip_endiannes.cairo @@ -1,6 +1,4 @@ -use common::from_span::FromSpanTrait; -use core::array::ArrayTrait; -use common::to_array::ToArrayTrait; +use cairo_verifier::common::{from_span::FromSpanTrait, to_array::ToArrayTrait}; trait FlipEndiannessTrait { fn flip_endiannes(self: F) -> F; @@ -20,4 +18,4 @@ impl FlipEndiannessU128 of FlipEndiannessTrait { self.to_array_be(ref data); data.span().from_span_le() } -} \ No newline at end of file +} diff --git a/src/common/src/from_span.cairo b/src/common/from_span.cairo similarity index 94% rename from src/common/src/from_span.cairo rename to src/common/from_span.cairo index 7a66b586f..a7d5e0f39 100644 --- a/src/common/src/from_span.cairo +++ b/src/common/from_span.cairo @@ -1,6 +1,3 @@ -use core::array::SpanTrait; -use core::traits::Into; - // 2^8 = 256 const U128maxU8: u128 = 256; const U64maxU8: u64 = 256; diff --git a/src/common/src/horner_eval.cairo b/src/common/horner_eval.cairo similarity index 100% rename from src/common/src/horner_eval.cairo rename to src/common/horner_eval.cairo diff --git a/src/common/tests.cairo b/src/common/tests.cairo new file mode 100644 index 000000000..d944d16ca --- /dev/null +++ b/src/common/tests.cairo @@ -0,0 +1,5 @@ +mod test_blake2s; +mod test_flip_endiannes; +mod test_from_span; +mod test_horner_eval; +mod test_to_array; diff --git a/src/common/tests/test_blake2s.cairo b/src/common/tests/test_blake2s.cairo new file mode 100644 index 000000000..03c0b27dd --- /dev/null +++ b/src/common/tests/test_blake2s.cairo @@ -0,0 +1,193 @@ +use cairo_verifier::common::blake2s::{blake2s, load32}; + +fn get_arr_v1(n: u32) -> Array { + let mut arr = ArrayTrait::new(); + let mut i: u32 = 1; + loop { + arr.append((i % 256).try_into().unwrap()); + if i == 4 * n { + break; + } + i += 1; + }; + let mut out = ArrayTrait::new(); + i = 0; + loop { + out.append(load32(*arr[4 * i], *arr[4 * i + 1], *arr[4 * i + 2], *arr[4 * i + 3])); + i += 1; + if i == n { + break; + }; + }; + out +} + +fn get_arr_v2(n: u32) -> Array { + let mut arr = ArrayTrait::new(); + let mut s: u32 = 1; + let mut i: u32 = 1; + loop { + s *= 17; + s = s ^ i; + s %= 256; + arr.append(s.try_into().unwrap()); + if i == 4 * n { + break; + } + i += 1; + }; + let mut out = ArrayTrait::new(); + i = 0; + loop { + out.append(load32(*arr[4 * i], *arr[4 * i + 1], *arr[4 * i + 2], *arr[4 * i + 3])); + i += 1; + if i == n { + break; + }; + }; + out +} + +#[test] +#[available_gas(9999999999)] +fn test_blake2s_v1() { + assert( + blake2s( + get_arr_v1(1) + ) == 0x035c8c55b225b3cad27dec93997fb528978127b9aa3c145c4308b8b6a4b0c7d4, + 'invalid hash (1)' + ); + assert( + blake2s( + get_arr_v1(2) + ) == 0x676da142c9e15751cf6c94e96ebc05925408612bbcf56437adf6fb21822fca4b, + 'invalid hash (2)' + ); + assert( + blake2s( + get_arr_v1(16) + ) == 0xc7fa21bb08b0bd19600ad212c0fa0f7ff332f415ae1527282a939406413299aa, + 'invalid hash (16)' + ); + assert( + blake2s( + get_arr_v1(17) + ) == 0x6acb015d7514d821091ec780120b89ba4663f65e6ff6588d458ef333fe8c8a39, + 'invalid hash (17)' + ); + assert( + blake2s( + get_arr_v1(32) + ) == 0x5651036b64f7affbe498f0409950e06a352bcae03f5a79b78fec58a4cebe10d5, + 'invalid hash (32)' + ); + assert( + blake2s( + get_arr_v1(33) + ) == 0x42d5eeff1aa4972630bcca469f37bbe8c2f8014937e28cbedbc671571d3eb87c, + 'invalid hash (33)' + ); + assert( + blake2s( + get_arr_v1(250) + ) == 0x33fc848fc73514d8bc3f338b23ba684d945081da37e5a8e490db5032eac34630, + 'invalid hash (250)' + ); + assert( + blake2s( + get_arr_v1(272) + ) == 0x1b5ad0d1b82600127a6add8e1cf604a075843c3d35bbe31d636fa071674c9432, + 'invalid hash (272)' + ); +} + +#[test] +#[available_gas(9999999999)] +fn test_blake2s_v2() { + assert( + blake2s( + get_arr_v2(1) + ) == 0x3becbdec8344113fbee53542a4ef696e97db25efb96cef60d2919bb4dd00ed3e, + 'invalid hash (1)' + ); + assert( + blake2s( + get_arr_v2(2) + ) == 0x5229f5d506302edae36f9cac3f5d176cd9b6aa8420da6d74d7956789099faf70, + 'invalid hash (2)' + ); + assert( + blake2s( + get_arr_v2(16) + ) == 0xdfe91aa5523f1df5e6549d98121e9bdbac4cbba4375e93d812ef487d0fe562f6, + 'invalid hash (16)' + ); + assert( + blake2s( + get_arr_v2(17) + ) == 0x49f2c2bb269d275a111a04c459c847838a0cf7c488d6366577a21f75620243e6, + 'invalid hash (17)' + ); + assert( + blake2s( + get_arr_v2(32) + ) == 0x83e9b2b70274d9198b6b77a1760ebacfd1f0fe232a0ed78f1c722e154ee72362, + 'invalid hash (32)' + ); + assert( + blake2s( + get_arr_v2(33) + ) == 0x93a7f68b8ea17374c11e1da719885513b598c4e191825fb584e399206c05ae15, + 'invalid hash (33)' + ); + assert( + blake2s( + get_arr_v2(250) + ) == 0xe4e6bd453ba2eb5a378d7933576dbf697b6d31cf38061c550ea36f6843a9bf43, + 'invalid hash (250)' + ); + assert( + blake2s( + get_arr_v2(272) + ) == 0x5906fef89f21466142323029000040f6c25be2ff87d581a8f752b94ad3662762, + 'invalid hash (272)' + ); +} + +#[test] +#[available_gas(9999999999)] +fn test_blake2s_big() { + let mut sum: u256 = 0; + let mut arr = ArrayTrait::new(); + let mut i: u32 = 0; + loop { + arr.append(i); + i += 1; + if i == 512 { + break; + }; + }; + loop { + let mut new_arr: Array = ArrayTrait::new(); + let arr_span = arr.span(); + let mut j = 0; + loop { + new_arr.append(*arr_span[j]); + j += 1; + if j == arr_span.len() { + break; + }; + }; + let res = blake2s(new_arr); + sum = sum ^ res; + let to_append: u32 = (res % 0x100000000).try_into().unwrap(); + arr.append(to_append); + i += 1; + if i == 529 { + break; + }; + }; + assert( + sum == 0x75dab924b6592ce2d9e0173c0d69ed45b4d8125da10a9db234fbdac721477df5, 'invalid hash' + ); +} diff --git a/src/common/tests/tests/flip_endiannes.cairo b/src/common/tests/test_flip_endiannes.cairo similarity index 83% rename from src/common/tests/tests/flip_endiannes.cairo rename to src/common/tests/test_flip_endiannes.cairo index fb826a025..3a99549f9 100644 --- a/src/common/tests/tests/flip_endiannes.cairo +++ b/src/common/tests/test_flip_endiannes.cairo @@ -1,50 +1,57 @@ -use common::flip_endiannes::FlipEndiannessTrait; -use core::debug::PrintTrait; +use cairo_verifier::common::flip_endiannes::FlipEndiannessTrait; #[test] +#[available_gas(9999999999)] fn test_flip_endiannes_0() { let le_val: u128 = 0x10000000000000000000009123092121; assert(le_val.flip_endiannes() == 0x21210923910000000000000000000010, 'Invalid value'); } #[test] +#[available_gas(9999999999)] fn test_flip_endiannes_1() { let le_val: u128 = 0x9123092121; assert(le_val.flip_endiannes().flip_endiannes() == le_val, 'Invalid value'); } #[test] +#[available_gas(9999999999)] fn test_flip_endiannes_2() { let le_val: u128 = 0x98071087308702187408710847087; assert(le_val.flip_endiannes().flip_endiannes() == le_val, 'Invalid value'); } #[test] +#[available_gas(9999999999)] fn test_flip_endiannes_3() { let le_val: u128 = 0xABCDEF0123456789ABCDEF0123456789; assert(le_val.flip_endiannes() == 0x8967452301EFCDAB8967452301EFCDAB, 'Invalid value'); } #[test] +#[available_gas(9999999999)] fn test_flip_endiannes_4() { let le_val: u128 = 0x00000000000000000000ABCDEF012345; assert(le_val.flip_endiannes() == 0x452301EFCDAB00000000000000000000, 'Invalid value'); } #[test] +#[available_gas(9999999999)] fn test_flip_endiannes_5() { let le_val: u128 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; assert(le_val.flip_endiannes() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, 'Invalid value'); } #[test] +#[available_gas(9999999999)] fn test_flip_endiannes_6() { let le_val: u128 = 0x00000000000000000000000000000000; assert(le_val.flip_endiannes() == 0x00000000000000000000000000000000, 'Invalid value'); } #[test] +#[available_gas(9999999999)] fn test_flip_endiannes_7() { let le_val: u128 = 0x1234567890ABCDEF1234567890ABCDEF; assert(le_val.flip_endiannes().flip_endiannes() == le_val, 'Invalid value'); -} \ No newline at end of file +} diff --git a/src/common/tests/tests/from_span.cairo b/src/common/tests/test_from_span.cairo similarity index 90% rename from src/common/tests/tests/from_span.cairo rename to src/common/tests/test_from_span.cairo index 92e7cdd15..a6c377ba4 100644 --- a/src/common/tests/tests/from_span.cairo +++ b/src/common/tests/test_from_span.cairo @@ -1,8 +1,7 @@ -use core::debug::PrintTrait; -use core::array::ArrayTrait; -use common::from_span::FromSpanTrait; +use cairo_verifier::common::from_span::FromSpanTrait; #[test] +#[available_gas(9999999999)] fn test_from_array_1() { let mut array = ArrayTrait::::new(); array.append(0x6cebe487); @@ -13,6 +12,7 @@ fn test_from_array_1() { } #[test] +#[available_gas(9999999999)] fn test_from_array_2() { let mut array = ArrayTrait::::new(); array.append(0x5e0352b5); @@ -23,6 +23,7 @@ fn test_from_array_2() { } #[test] +#[available_gas(9999999999)] fn test_from_array_3() { let mut array = ArrayTrait::::new(); array.append(0xb49552b5); @@ -40,6 +41,7 @@ fn test_from_array_3() { } #[test] +#[available_gas(9999999999)] fn test_from_array_4() { let mut array = ArrayTrait::::new(); array.append(0x6f254ac7); diff --git a/src/common/tests/tests/horner_eval.cairo b/src/common/tests/test_horner_eval.cairo similarity index 87% rename from src/common/tests/tests/horner_eval.cairo rename to src/common/tests/test_horner_eval.cairo index 8108e74cf..674bc06ef 100644 --- a/src/common/tests/tests/horner_eval.cairo +++ b/src/common/tests/test_horner_eval.cairo @@ -1,8 +1,7 @@ -use core::debug::PrintTrait; -use core::array::ArrayTrait; -use common::horner_eval::horner_eval; +use cairo_verifier::common::horner_eval::horner_eval; #[test] +#[available_gas(9999999999)] fn test_horner_eval_0() { let mut coefs = ArrayTrait::::new(); let eval = horner_eval(coefs, 1); @@ -10,6 +9,7 @@ fn test_horner_eval_0() { } #[test] +#[available_gas(9999999999)] fn test_horner_eval_1() { let mut coefs = ArrayTrait::::new(); coefs.append(1); @@ -18,6 +18,7 @@ fn test_horner_eval_1() { } #[test] +#[available_gas(9999999999)] fn test_horner_eval_2() { let mut coefs = ArrayTrait::::new(); coefs.append(4); @@ -30,6 +31,7 @@ fn test_horner_eval_2() { } #[test] +#[available_gas(9999999999)] fn test_horner_eval_3() { let mut coefs = ArrayTrait::::new(); coefs.append(4); diff --git a/src/common/tests/tests/to_array.cairo b/src/common/tests/test_to_array.cairo similarity index 91% rename from src/common/tests/tests/to_array.cairo rename to src/common/tests/test_to_array.cairo index e0e313e64..e2247a2b3 100644 --- a/src/common/tests/tests/to_array.cairo +++ b/src/common/tests/test_to_array.cairo @@ -1,8 +1,7 @@ -use core::debug::PrintTrait; -use core::array::ArrayTrait; -use common::to_array::ToArrayTrait; +use cairo_verifier::common::to_array::ToArrayTrait; #[test] +#[available_gas(9999999999)] fn test_to_array_le_1() { let value = 1827398791_u128; // = 0x6cebe487 let mut result = ArrayTrait::::new(); @@ -12,6 +11,7 @@ fn test_to_array_le_1() { } #[test] +#[available_gas(9999999999)] fn test_to_array_le_2() { let value = 18273987910128309_u128; // = 0x40ec185e0352b5 let mut result = ArrayTrait::::new(); @@ -21,6 +21,7 @@ fn test_to_array_le_2() { } #[test] +#[available_gas(9999999999)] fn test_to_array_le_3() { let value = 182739879101283091827398791012830918273987910128309_u256; // = 0x7d09239523c3e2d590e3500b26c941117ab49552b5 @@ -35,6 +36,7 @@ fn test_to_array_le_3() { } #[test] +#[available_gas(9999999999)] fn test_to_array_le_4() { let value = 18273987910112830918273987910128309910128309182739879101283099101283091827399_u256; // = 0x2866b5eb89ac02463b066d1405c0e20f5a19ba600c90767713f2ae1b6f254ac7 diff --git a/src/common/tests/tests.cairo b/src/common/tests/tests.cairo deleted file mode 100644 index 121f3642b..000000000 --- a/src/common/tests/tests.cairo +++ /dev/null @@ -1,5 +0,0 @@ -mod horner_eval; -mod to_array; -mod from_span; -mod flip_endiannes; -mod blake2s; \ No newline at end of file diff --git a/src/common/tests/tests/blake2s.cairo b/src/common/tests/tests/blake2s.cairo deleted file mode 100644 index 30659cc1f..000000000 --- a/src/common/tests/tests/blake2s.cairo +++ /dev/null @@ -1,112 +0,0 @@ -use common::blake2s::{blake2s, load32}; - - -fn get_arr_v1(n: u32) -> Array { - let mut arr = ArrayTrait::new(); - let mut i: u32 = 1; - loop { - arr.append((i%256).try_into().unwrap()); - if i == 4*n { - break; - } - i += 1; - }; - let mut out = ArrayTrait::new(); - i = 0; - loop { - out.append(load32(*arr[4*i], *arr[4*i+1], *arr[4*i+2], *arr[4*i+3])); - i += 1; - if i == n { - break; - }; - }; - out -} - -fn get_arr_v2(n: u32) -> Array { - let mut arr = ArrayTrait::new(); - let mut s: u32 = 1; - let mut i: u32 = 1; - loop { - s *= 17; - s = s^i; - s %= 256; - arr.append(s.try_into().unwrap()); - if i == 4*n { - break; - } - i += 1; - }; - let mut out = ArrayTrait::new(); - i = 0; - loop { - out.append(load32(*arr[4*i], *arr[4*i+1], *arr[4*i+2], *arr[4*i+3])); - i += 1; - if i == n { - break; - }; - }; - out -} - -#[test] -#[available_gas(9999999999)] -fn test_blake2s_v1() { - assert(blake2s(get_arr_v1(1)) == 0x035c8c55b225b3cad27dec93997fb528978127b9aa3c145c4308b8b6a4b0c7d4, 'invalid hash (1)'); - assert(blake2s(get_arr_v1(2)) == 0x676da142c9e15751cf6c94e96ebc05925408612bbcf56437adf6fb21822fca4b, 'invalid hash (2)'); - assert(blake2s(get_arr_v1(16)) == 0xc7fa21bb08b0bd19600ad212c0fa0f7ff332f415ae1527282a939406413299aa, 'invalid hash (16)'); - assert(blake2s(get_arr_v1(17)) == 0x6acb015d7514d821091ec780120b89ba4663f65e6ff6588d458ef333fe8c8a39, 'invalid hash (17)'); - assert(blake2s(get_arr_v1(32)) == 0x5651036b64f7affbe498f0409950e06a352bcae03f5a79b78fec58a4cebe10d5, 'invalid hash (32)'); - assert(blake2s(get_arr_v1(33)) == 0x42d5eeff1aa4972630bcca469f37bbe8c2f8014937e28cbedbc671571d3eb87c, 'invalid hash (33)'); - assert(blake2s(get_arr_v1(250)) == 0x33fc848fc73514d8bc3f338b23ba684d945081da37e5a8e490db5032eac34630, 'invalid hash (250)'); - assert(blake2s(get_arr_v1(272)) == 0x1b5ad0d1b82600127a6add8e1cf604a075843c3d35bbe31d636fa071674c9432, 'invalid hash (272)'); -} - -#[test] -#[available_gas(9999999999)] -fn test_blake2s_v2() { - assert(blake2s(get_arr_v2(1)) == 0x3becbdec8344113fbee53542a4ef696e97db25efb96cef60d2919bb4dd00ed3e, 'invalid hash (1)'); - assert(blake2s(get_arr_v2(2)) == 0x5229f5d506302edae36f9cac3f5d176cd9b6aa8420da6d74d7956789099faf70, 'invalid hash (2)'); - assert(blake2s(get_arr_v2(16)) == 0xdfe91aa5523f1df5e6549d98121e9bdbac4cbba4375e93d812ef487d0fe562f6, 'invalid hash (16)'); - assert(blake2s(get_arr_v2(17)) == 0x49f2c2bb269d275a111a04c459c847838a0cf7c488d6366577a21f75620243e6, 'invalid hash (17)'); - assert(blake2s(get_arr_v2(32)) == 0x83e9b2b70274d9198b6b77a1760ebacfd1f0fe232a0ed78f1c722e154ee72362, 'invalid hash (32)'); - assert(blake2s(get_arr_v2(33)) == 0x93a7f68b8ea17374c11e1da719885513b598c4e191825fb584e399206c05ae15, 'invalid hash (33)'); - assert(blake2s(get_arr_v2(250)) == 0xe4e6bd453ba2eb5a378d7933576dbf697b6d31cf38061c550ea36f6843a9bf43, 'invalid hash (250)'); - assert(blake2s(get_arr_v2(272)) == 0x5906fef89f21466142323029000040f6c25be2ff87d581a8f752b94ad3662762, 'invalid hash (272)'); -} - -#[test] -#[available_gas(9999999999)] -fn test_blake2s_big() { - let mut sum: u256 = 0; - let mut arr = ArrayTrait::new(); - let mut i: u32 = 0; - loop { - arr.append(i); - i += 1; - if i == 512 { - break; - }; - }; - loop { - let mut new_arr: Array = ArrayTrait::new(); - let arr_span = arr.span(); - let mut j = 0; - loop { - new_arr.append(*arr_span[j]); - j += 1; - if j == arr_span.len() { - break; - }; - }; - let res = blake2s(new_arr); - sum = sum ^ res; - let to_append: u32 = (res % 0x100000000).try_into().unwrap(); - arr.append(to_append); - i += 1; - if i == 529 { - break; - }; - }; - assert(sum == 0x75dab924b6592ce2d9e0173c0d69ed45b4d8125da10a9db234fbdac721477df5, 'invalid hash'); -} \ No newline at end of file diff --git a/src/common/src/to_array.cairo b/src/common/to_array.cairo similarity index 80% rename from src/common/src/to_array.cairo rename to src/common/to_array.cairo index f72207ed1..1c377ab94 100644 --- a/src/common/src/to_array.cairo +++ b/src/common/to_array.cairo @@ -1,8 +1,3 @@ -use core::DivRem; -use core::traits::TryInto; -use core::zeroable::NonZero; -use core::array::ArrayTrait; - // 2^8 = 256 const U128maxU8: u128 = 256; const U64maxU8: u64 = 256; @@ -57,15 +52,16 @@ impl U128ToArrayU32 of ToArrayTrait { loop { if i != 0 { i -= 1; - output.append( - (*array.at(i) % 256) * 16777216 + - (*array.at(i) / 256 % 256) * 65536 + - (*array.at(i) / 65536 % 256) * 256 + - (*array.at(i) / 16777216 % 256) - ); + output + .append( + (*array.at(i) % 256) * 16777216 + + (*array.at(i) / 256 % 256) * 65536 + + (*array.at(i) / 65536 % 256) * 256 + + (*array.at(i) / 16777216 % 256) + ); } else { break; } } } -} \ No newline at end of file +} diff --git a/src/lib.cairo b/src/lib.cairo new file mode 100644 index 000000000..3e032ff8d --- /dev/null +++ b/src/lib.cairo @@ -0,0 +1,3 @@ +mod channel; +mod common; +mod structs; diff --git a/src/structs/src/lib.cairo b/src/structs.cairo similarity index 81% rename from src/structs/src/lib.cairo rename to src/structs.cairo index bcd6e2ad4..b978c4505 100644 --- a/src/structs/src/lib.cairo +++ b/src/structs.cairo @@ -1,7 +1,9 @@ mod fri_config; -mod stark_proof; -mod stark_config; -mod traces_config; mod proof_of_work_config; +mod stark_config; +mod stark_proof; mod table_commitment_config; +mod traces_config; +mod unsent_commitment; mod vector_commitment_config; +mod witness; diff --git a/src/structs/Scarb.toml b/src/structs/Scarb.toml deleted file mode 100644 index 8b76aea14..000000000 --- a/src/structs/Scarb.toml +++ /dev/null @@ -1,5 +0,0 @@ -[package] -name = "structs" -version = "0.1.0" - -[dependencies] \ No newline at end of file diff --git a/src/structs/src/fri_config.cairo b/src/structs/fri_config.cairo similarity index 87% rename from src/structs/src/fri_config.cairo rename to src/structs/fri_config.cairo index 4528b9438..35ebe0816 100644 --- a/src/structs/src/fri_config.cairo +++ b/src/structs/fri_config.cairo @@ -1,4 +1,4 @@ -use structs::table_commitment_config::TableCommitmentConfig; +use cairo_verifier::structs::table_commitment_config::TableCommitmentConfig; struct FriConfig { // Log2 of the size of the input layer to FRI. diff --git a/src/structs/src/proof_of_work_config.cairo b/src/structs/proof_of_work_config.cairo similarity index 100% rename from src/structs/src/proof_of_work_config.cairo rename to src/structs/proof_of_work_config.cairo diff --git a/src/structs/src/public_input.cairo b/src/structs/public_input.cairo similarity index 100% rename from src/structs/src/public_input.cairo rename to src/structs/public_input.cairo diff --git a/src/structs/src/stark_config.cairo b/src/structs/stark_config.cairo similarity index 69% rename from src/structs/src/stark_config.cairo rename to src/structs/stark_config.cairo index 549bf7b03..af63fec11 100644 --- a/src/structs/src/stark_config.cairo +++ b/src/structs/stark_config.cairo @@ -1,7 +1,7 @@ -use structs::traces_config::TracesConfig; -use structs::table_commitment_config::TableCommitmentConfig; -use structs::fri_config::FriConfig; -use structs::proof_of_work_config::ProofOfWorkConfig; +use cairo_verifier::structs::traces_config::TracesConfig; +use cairo_verifier::structs::table_commitment_config::TableCommitmentConfig; +use cairo_verifier::structs::fri_config::FriConfig; +use cairo_verifier::structs::proof_of_work_config::ProofOfWorkConfig; struct StarkConfig { traces: TracesConfig, diff --git a/src/structs/src/stark_proof.cairo b/src/structs/stark_proof.cairo similarity index 72% rename from src/structs/src/stark_proof.cairo rename to src/structs/stark_proof.cairo index 662484a27..dd1a97d85 100644 --- a/src/structs/src/stark_proof.cairo +++ b/src/structs/stark_proof.cairo @@ -1,4 +1,4 @@ -use structs::stark_config::StarkConfig; +use cairo_verifier::structs::stark_config::StarkConfig; struct StarkProof { config: StarkConfig, diff --git a/src/structs/src/table_commitment_config.cairo b/src/structs/table_commitment_config.cairo similarity index 53% rename from src/structs/src/table_commitment_config.cairo rename to src/structs/table_commitment_config.cairo index 4dd9ac349..45ed8906b 100644 --- a/src/structs/src/table_commitment_config.cairo +++ b/src/structs/table_commitment_config.cairo @@ -1,4 +1,4 @@ -use structs::vector_commitment_config::VectorCommitmentConfig; +use cairo_verifier::structs::vector_commitment_config::VectorCommitmentConfig; struct TableCommitmentConfig { columns: felt252, diff --git a/src/structs/src/traces_config.cairo b/src/structs/traces_config.cairo similarity index 56% rename from src/structs/src/traces_config.cairo rename to src/structs/traces_config.cairo index 423736692..f3c741fed 100644 --- a/src/structs/src/traces_config.cairo +++ b/src/structs/traces_config.cairo @@ -1,4 +1,4 @@ -use structs::table_commitment_config::TableCommitmentConfig; +use cairo_verifier::structs::table_commitment_config::TableCommitmentConfig; struct TracesConfig { original: TableCommitmentConfig, diff --git a/src/structs/src/unsent_commitment.cairo b/src/structs/unsent_commitment.cairo similarity index 100% rename from src/structs/src/unsent_commitment.cairo rename to src/structs/unsent_commitment.cairo diff --git a/src/structs/src/vector_commitment_config.cairo b/src/structs/vector_commitment_config.cairo similarity index 100% rename from src/structs/src/vector_commitment_config.cairo rename to src/structs/vector_commitment_config.cairo diff --git a/src/structs/src/witness.cairo b/src/structs/witness.cairo similarity index 100% rename from src/structs/src/witness.cairo rename to src/structs/witness.cairo diff --git a/src/test_sandbox/Cargo.toml b/src/test_sandbox/Cargo.toml deleted file mode 100644 index 3dfbc1685..000000000 --- a/src/test_sandbox/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "test_sandbox" -edition.workspace = true -version.workspace = true - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] diff --git a/src/test_sandbox/src/main.rs b/src/test_sandbox/src/main.rs deleted file mode 100644 index e7a11a969..000000000 --- a/src/test_sandbox/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("Hello, world!"); -} diff --git a/src/verifier/Scarb.toml b/src/verifier/Scarb.toml deleted file mode 100644 index 2200f00b0..000000000 --- a/src/verifier/Scarb.toml +++ /dev/null @@ -1,6 +0,0 @@ -[package] -name = "verifier" -version = "0.1.0" - -[dependencies] -structs = { workspace = true } \ No newline at end of file diff --git a/src/verifier/src/lib.cairo b/src/verifier/src/lib.cairo deleted file mode 100644 index 5167119e7..000000000 --- a/src/verifier/src/lib.cairo +++ /dev/null @@ -1,9 +0,0 @@ -mod models; - -use structs::stark_proof::StarkProof; -use models::CairoVerifierOutput; - -fn verify_proof(ref stark_proof: StarkProof) -> CairoVerifierOutput { - // TODO verify proof - CairoVerifierOutput { program_hash: 0, output_hash: 0 } -} diff --git a/src/verifier/src/models.cairo b/src/verifier/src/models.cairo deleted file mode 100644 index 31574382e..000000000 --- a/src/verifier/src/models.cairo +++ /dev/null @@ -1,4 +0,0 @@ -struct CairoVerifierOutput { - program_hash: felt252, - output_hash: felt252, -}