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

Implement circuits. #4

Merged
merged 19 commits into from
Aug 27, 2024
Merged
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
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ version = "0.1.0"
edition = "2021"

[dependencies]
cairo-lang-sierra = "2.7.1"
cairo-lang-utils = "2.7.1"
cairo-lang-sierra = "=2.7.1"
cairo-lang-utils = "=2.7.1"
clap = { version = "4.5.16", features = ["derive"] }
k256 = "0.13.3"
keccak = "0.1.5"
Expand All @@ -25,8 +25,8 @@ tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }

[dev-dependencies]
cairo-lang-compiler = "2.7.0"
cairo-lang-starknet = "2.7.0"
cairo-lang-compiler = "=2.7.0"
cairo-lang-starknet = "=2.7.0"

# On dev optimize dependencies a bit so it's not as slow.
[profile.dev.package."*"]
Expand Down
10 changes: 8 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use self::args::CmdArgs;
use cairo_lang_sierra::{
extensions::{core::CoreTypeConcrete, starknet::StarkNetTypeConcrete},
extensions::{
circuit::CircuitTypeConcrete, core::CoreTypeConcrete, starknet::StarkNetTypeConcrete,
},
ProgramParser,
};
use clap::Parser;
Expand Down Expand Up @@ -65,10 +67,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
CoreTypeConcrete::Felt252(_) => Value::parse_felt(&iter.next().unwrap()),
CoreTypeConcrete::GasBuiltin(_) => Value::U128(args.available_gas.unwrap()),
CoreTypeConcrete::RangeCheck(_)
| CoreTypeConcrete::RangeCheck96(_)
| CoreTypeConcrete::Bitwise(_)
| CoreTypeConcrete::Pedersen(_)
| CoreTypeConcrete::Poseidon(_)
| CoreTypeConcrete::SegmentArena(_) => Value::Unit,
| CoreTypeConcrete::SegmentArena(_)
| CoreTypeConcrete::Circuit(
CircuitTypeConcrete::AddMod(_) | CircuitTypeConcrete::MulMod(_),
) => Value::Unit,
CoreTypeConcrete::StarkNet(inner) => match inner {
StarkNetTypeConcrete::System(_) => Value::Unit,
_ => todo!(),
Expand Down
28 changes: 26 additions & 2 deletions src/value.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use cairo_lang_sierra::{
extensions::{
circuit::CircuitTypeConcrete,
core::{CoreLibfunc, CoreType, CoreTypeConcrete},
starknet::StarkNetTypeConcrete,
ConcreteType,
},
ids::ConcreteTypeId,
program_registry::ProgramRegistry,
};
use num_bigint::BigInt;
use num_bigint::{BigInt, BigUint};
use serde::Serialize;
use starknet_types_core::felt::Felt;
use std::{collections::HashMap, fmt::Debug, ops::Range};
Expand All @@ -22,6 +23,9 @@ pub enum Value {
range: Range<BigInt>,
value: BigInt,
},
Circuit(Vec<BigUint>),
CircuitModulus(BigUint),
CircuitOutputs(Vec<BigUint>),
Enum {
self_ty: ConcreteTypeId,
index: usize,
Expand Down Expand Up @@ -50,6 +54,7 @@ pub enum Value {
},
I8(i8),
Struct(Vec<Self>),
U256(u128, u128),
U128(u128),
U16(u16),
U32(u32),
Expand Down Expand Up @@ -82,6 +87,9 @@ impl Value {
CoreTypeConcrete::Array(info) => {
matches!(self, Self::Array { ty, .. } if *ty == info.ty)
}
CoreTypeConcrete::BoundedInt(info) => {
matches!(self, Self::BoundedInt { range, .. } if range.start == info.range.lower && range.end == info.range.upper)
}
CoreTypeConcrete::Enum(_) => {
matches!(self, Self::Enum { self_ty, .. } if self_ty == type_id)
}
Expand All @@ -94,6 +102,12 @@ impl Value {
CoreTypeConcrete::NonZero(info) => self.is(registry, &info.ty),
CoreTypeConcrete::Sint8(_) => matches!(self, Self::I8(_)),
CoreTypeConcrete::Snapshot(info) => self.is(registry, &info.ty),
CoreTypeConcrete::StarkNet(
StarkNetTypeConcrete::ClassHash(_)
| StarkNetTypeConcrete::ContractAddress(_)
| StarkNetTypeConcrete::StorageBaseAddress(_)
| StarkNetTypeConcrete::StorageAddress(_),
) => matches!(self, Self::Felt(_)),
CoreTypeConcrete::Struct(info) => {
matches!(self, Self::Struct(members)
if members.len() == info.members.len()
Expand All @@ -105,9 +119,19 @@ impl Value {
}
CoreTypeConcrete::Uint8(_) => matches!(self, Self::U8(_)),
CoreTypeConcrete::Uint32(_) => matches!(self, Self::U32(_)),
CoreTypeConcrete::Uint128(_)
| CoreTypeConcrete::Circuit(CircuitTypeConcrete::U96Guarantee(_)) => {
matches!(self, Self::U128(_))
}

// Unused builtins (mapped to `Value::Unit`).
CoreTypeConcrete::RangeCheck(_) | CoreTypeConcrete::SegmentArena(_) => {
CoreTypeConcrete::RangeCheck(_)
| CoreTypeConcrete::SegmentArena(_)
| CoreTypeConcrete::RangeCheck96(_)
| CoreTypeConcrete::Circuit(
CircuitTypeConcrete::AddMod(_) | CircuitTypeConcrete::MulMod(_),
)
| CoreTypeConcrete::StarkNet(StarkNetTypeConcrete::System(_)) => {
matches!(self, Self::Unit)
}

Expand Down
3 changes: 2 additions & 1 deletion src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mod r#box;
mod branch_align;
mod bytes31;
mod cast;
mod circuit;
mod r#const;
mod drop;
mod dup;
Expand Down Expand Up @@ -290,7 +291,7 @@ fn eval<'a>(
CoreConcreteLibfunc::BranchAlign(info) => self::branch_align::eval(registry, info, args),
CoreConcreteLibfunc::Bytes31(selector) => self::bytes31::eval(registry, selector, args),
CoreConcreteLibfunc::Cast(selector) => self::cast::eval(registry, selector, args),
CoreConcreteLibfunc::Circuit(_) => todo!(),
CoreConcreteLibfunc::Circuit(selector) => self::circuit::eval(registry, selector, args),
CoreConcreteLibfunc::Const(selector) => self::r#const::eval(registry, selector, args),
CoreConcreteLibfunc::Coupon(_) => todo!(),
CoreConcreteLibfunc::CouponCall(_) => todo!(),
Expand Down
Loading