Skip to content

Commit 614e9ca

Browse files
[WIP] Temporary fix for 0.3.4 circuit bugs (#1229)
* cherrypick: Fix into_u96_guarantee not handling other types #1171 * Remove test-ubuntu-old job The runner has been retired * Disable big circuits for now * Disable all circuits instead * Ignore circuit tests * Ignore integration circuit tests * Skip circuit_test.cairo
1 parent 65e08ac commit 614e9ca

File tree

5 files changed

+134
-56
lines changed

5 files changed

+134
-56
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -151,55 +151,9 @@ jobs:
151151
- name: test
152152
run: make test-ci
153153
- name: test-cairo
154-
run: make test-cairo
155-
156-
test-ubuntu-old:
157-
name: test ubuntu old (linux, amd64)
158-
runs-on: ubuntu-20.04
159-
env:
160-
MLIR_SYS_190_PREFIX: /usr/lib/llvm-19/
161-
LLVM_SYS_191_PREFIX: /usr/lib/llvm-19/
162-
TABLEGEN_190_PREFIX: /usr/lib/llvm-19/
163-
RUST_LOG: cairo_native=debug,cairo_native_test=debug
164-
steps:
165-
- uses: actions/checkout@v4
166-
- name: check and free hdd space left
167154
run: |
168-
echo "Listing 20 largest packages"
169-
dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n | tail -n 20
170-
df -h
171-
sudo apt-get update
172-
sudo apt-get remove -y '^llvm-.*'
173-
sudo apt-get remove -y 'php.*'
174-
sudo apt-get remove -y '^dotnet-.*'
175-
sudo apt-get remove -y '^temurin-.*'
176-
sudo apt-get autoremove -y
177-
sudo apt-get clean
178-
df -h
179-
echo "Removing large directories"
180-
# deleting 15GB
181-
sudo rm -rf /usr/share/dotnet/
182-
sudo rm -rf /usr/local/lib/android
183-
df -h
184-
- name: Setup rust env
185-
uses: dtolnay/[email protected]
186-
- name: Retreive cached dependecies
187-
uses: Swatinem/rust-cache@v2
188-
- name: add llvm deb repository
189-
uses: myci-actions/add-deb-repo@11
190-
with:
191-
repo: deb http://apt.llvm.org/focal/ llvm-toolchain-focal-19 main
192-
repo-name: llvm-repo
193-
keys-asc: https://apt.llvm.org/llvm-snapshot.gpg.key
194-
- run: sudo apt-get update && sudo apt-get upgrade -y
195-
- name: Install LLVM
196-
run: sudo apt-get install libzstd-dev llvm-19 llvm-19-dev llvm-19-runtime clang-19 clang-tools-19 lld-19 libpolly-19-dev libmlir-19-dev mlir-19-tools
197-
- name: Install deps
198-
run: make deps
199-
- name: test
200-
run: make test-ci
201-
- name: test-cairo
202-
run: make test-cairo
155+
echo > corelib/src/test/circuit_test.cairo
156+
make test-cairo
203157
204158
test_macos:
205159
name: Test (macOS, Apple silicon)
@@ -229,7 +183,9 @@ jobs:
229183
- name: Run tests
230184
run: make test-ci
231185
- name: test-cairo
232-
run: make test-cairo
186+
run: |
187+
echo > corelib/src/test/circuit_test.cairo
188+
make test-cairo
233189
234190
coverage:
235191
name: coverage

src/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ pub mod panic {
155155
impl std::error::Error for NativeAssertError {}
156156

157157
impl NativeAssertError {
158+
#[track_caller]
158159
pub fn new(msg: String) -> Self {
159160
let backtrace = Backtrace::capture();
160161
let info = if BacktraceStatus::Captured == backtrace.status() {

src/libfuncs/circuit.rs

Lines changed: 107 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
//!
33
//! Relevant casm code: https://github.com/starkware-libs/cairo/blob/v2.10.0/crates/cairo-lang-sierra-to-casm/src/invocations/circuit.rs
44
5+
#![allow(unused_variables, unreachable_code)]
6+
57
use super::{increment_builtin_counter_by, LibfuncHelper};
68
use crate::{
79
error::{Result, SierraAssertError},
810
libfuncs::r#struct::build_struct_value,
911
metadata::MetadataStorage,
12+
native_panic,
1013
types::{circuit::build_u384_struct_type, TypeBuilder},
1114
utils::{get_integer_layout, layout_repeat, BlockExt, ProgramRegistryExt},
1215
};
@@ -33,6 +36,7 @@ use melior::{
3336
},
3437
Context,
3538
};
39+
use num_traits::Signed;
3640

3741
/// Select and call the correct libfunc builder function from the selector.
3842
pub fn build<'ctx, 'this>(
@@ -44,6 +48,8 @@ pub fn build<'ctx, 'this>(
4448
metadata: &mut MetadataStorage,
4549
selector: &CircuitConcreteLibfunc,
4650
) -> Result<()> {
51+
native_panic!("circuit libfuncs are disabled at the moment, due to limitations in LLVM");
52+
4753
match selector {
4854
CircuitConcreteLibfunc::AddInput(info) => {
4955
build_add_input(context, registry, entry, location, helper, metadata, info)
@@ -66,11 +72,10 @@ pub fn build<'ctx, 'this>(
6672
CircuitConcreteLibfunc::FailureGuaranteeVerify(info) => build_failure_guarantee_verify(
6773
context, registry, entry, location, helper, metadata, info,
6874
),
69-
CircuitConcreteLibfunc::IntoU96Guarantee(SignatureAndTypeConcreteLibfunc {
70-
signature,
71-
..
72-
})
73-
| CircuitConcreteLibfunc::U96SingleLimbLessThanGuaranteeVerify(
75+
CircuitConcreteLibfunc::IntoU96Guarantee(info) => {
76+
build_into_u96_guarantee(context, registry, entry, location, helper, metadata, info)
77+
}
78+
CircuitConcreteLibfunc::U96SingleLimbLessThanGuaranteeVerify(
7479
SignatureOnlyConcreteLibfunc { signature, .. },
7580
)
7681
| CircuitConcreteLibfunc::U96GuaranteeVerify(SignatureOnlyConcreteLibfunc { signature }) => {
@@ -1083,6 +1088,52 @@ fn build_array_slice<'ctx>(
10831088
)
10841089
}
10851090

1091+
/// Converts input to an U96Guarantee.
1092+
/// Input type must fit inside of an u96.
1093+
///
1094+
/// # Signature
1095+
/// ```cairo
1096+
/// extern fn into_u96_guarantee<T>(val: T) -> U96Guarantee nopanic;
1097+
/// ```
1098+
fn build_into_u96_guarantee<'ctx, 'this>(
1099+
context: &'ctx Context,
1100+
registry: &ProgramRegistry<CoreType, CoreLibfunc>,
1101+
entry: &'this Block<'ctx>,
1102+
location: Location<'ctx>,
1103+
helper: &LibfuncHelper<'ctx, 'this>,
1104+
_metadata: &mut MetadataStorage,
1105+
info: &SignatureAndTypeConcreteLibfunc,
1106+
) -> Result<()> {
1107+
let src = entry.argument(0)?.into();
1108+
1109+
let src_ty = registry.get_type(&info.param_signatures()[0].ty)?;
1110+
1111+
let src_range = src_ty.integer_range(registry)?;
1112+
1113+
// We expect the input value to be unsigned, but we check it just in case.
1114+
if src_range.lower.is_negative() {
1115+
native_panic!("into_u96_guarantee expects an unsigned integer")
1116+
}
1117+
1118+
// Extend the input value to an u96
1119+
let mut dst = entry.extui(src, IntegerType::new(context, 96).into(), location)?;
1120+
1121+
// If the lower bound is positive, we offset the value by the lower bound
1122+
// to obtain the actual value.
1123+
if src_range.lower.is_positive() {
1124+
let klower = entry.const_int_from_type(
1125+
context,
1126+
location,
1127+
src_range.lower,
1128+
IntegerType::new(context, 96).into(),
1129+
)?;
1130+
dst = entry.addi(dst, klower, location)?
1131+
}
1132+
1133+
entry.append_operation(helper.br(0, &[dst], location));
1134+
Ok(())
1135+
}
1136+
10861137
#[cfg(test)]
10871138
mod test {
10881139

@@ -1094,8 +1145,8 @@ mod test {
10941145
values::Value,
10951146
};
10961147
use cairo_lang_sierra::extensions::utils::Range;
1097-
use num_bigint::BigUint;
1098-
use num_traits::Num;
1148+
use num_bigint::{BigInt, BigUint};
1149+
use num_traits::{Num, One};
10991150
use starknet_types_core::felt::Felt;
11001151

11011152
fn u384(limbs: [&str; 4]) -> Value {
@@ -1132,6 +1183,7 @@ mod test {
11321183
}
11331184

11341185
#[test]
1186+
#[ignore]
11351187
fn run_add_circuit() {
11361188
let program = load_cairo!(
11371189
use core::circuit::{
@@ -1168,6 +1220,7 @@ mod test {
11681220
}
11691221

11701222
#[test]
1223+
#[ignore]
11711224
fn run_sub_circuit() {
11721225
let program = load_cairo!(
11731226
use core::circuit::{
@@ -1204,6 +1257,7 @@ mod test {
12041257
}
12051258

12061259
#[test]
1260+
#[ignore]
12071261
fn run_mul_circuit() {
12081262
let program = load_cairo!(
12091263
use core::circuit::{
@@ -1240,6 +1294,7 @@ mod test {
12401294
}
12411295

12421296
#[test]
1297+
#[ignore]
12431298
fn run_inverse_circuit() {
12441299
let program = load_cairo!(
12451300
use core::circuit::{
@@ -1274,6 +1329,7 @@ mod test {
12741329
}
12751330

12761331
#[test]
1332+
#[ignore]
12771333
fn run_no_coprime_circuit() {
12781334
let program = load_cairo!(
12791335
use core::circuit::{
@@ -1310,6 +1366,7 @@ mod test {
13101366
}
13111367

13121368
#[test]
1369+
#[ignore]
13131370
fn run_mul_overflow_circuit() {
13141371
let program = load_cairo!(
13151372
use core::circuit::{
@@ -1355,6 +1412,7 @@ mod test {
13551412
}
13561413

13571414
#[test]
1415+
#[ignore]
13581416
fn run_full_circuit() {
13591417
let program = load_cairo!(
13601418
use core::circuit::{
@@ -1404,4 +1462,46 @@ mod test {
14041462
),
14051463
);
14061464
}
1465+
1466+
#[test]
1467+
#[ignore]
1468+
fn run_into_u96_guarantee() {
1469+
let program = load_cairo!(
1470+
use core::circuit::{into_u96_guarantee, U96Guarantee};
1471+
use core::internal::bounded_int::BoundedInt;
1472+
1473+
fn main() -> (U96Guarantee, U96Guarantee, U96Guarantee) {
1474+
(
1475+
into_u96_guarantee::<BoundedInt<0, 79228162514264337593543950335>>(123),
1476+
into_u96_guarantee::<BoundedInt<100, 1000>>(123),
1477+
into_u96_guarantee::<u8>(123),
1478+
)
1479+
}
1480+
);
1481+
1482+
let range = Range {
1483+
lower: BigInt::ZERO,
1484+
upper: BigInt::one() << 96,
1485+
};
1486+
1487+
run_program_assert_output(
1488+
&program,
1489+
"main",
1490+
&[],
1491+
jit_struct!(
1492+
Value::BoundedInt {
1493+
value: 123.into(),
1494+
range: range.clone()
1495+
},
1496+
Value::BoundedInt {
1497+
value: 123.into(),
1498+
range: range.clone()
1499+
},
1500+
Value::BoundedInt {
1501+
value: 123.into(),
1502+
range: range.clone()
1503+
}
1504+
),
1505+
);
1506+
}
14071507
}

src/values.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::{
1515
use bumpalo::Bump;
1616
use cairo_lang_sierra::{
1717
extensions::{
18+
circuit::CircuitTypeConcrete,
1819
core::{CoreLibfunc, CoreType, CoreTypeConcrete},
1920
starknet::{secp256::Secp256PointTypeConcrete, StarkNetTypeConcrete},
2021
utils::Range,
@@ -966,6 +967,23 @@ impl Value {
966967
range: info.range.clone(),
967968
}
968969
}
970+
CoreTypeConcrete::Circuit(CircuitTypeConcrete::U96Guarantee(_)) => {
971+
let data = BigInt::from_biguint(
972+
Sign::Plus,
973+
BigUint::from_bytes_le(slice::from_raw_parts(
974+
ptr.cast::<u8>().as_ptr(),
975+
12,
976+
)),
977+
);
978+
979+
Self::BoundedInt {
980+
value: data.into(),
981+
range: Range {
982+
lower: BigInt::ZERO,
983+
upper: BigInt::one() << 96,
984+
},
985+
}
986+
}
969987
CoreTypeConcrete::Coupon(_)
970988
| CoreTypeConcrete::Circuit(_)
971989
| CoreTypeConcrete::RangeCheck96(_) => native_panic!("implement from_ptr"),

tests/tests/circuit.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ lazy_static! {
7272
}
7373

7474
#[test]
75+
#[ignore]
7576
fn circuit_guarantee_first_limb() {
7677
let program = &TEST;
7778

@@ -110,6 +111,7 @@ fn circuit_guarantee_first_limb() {
110111
}
111112

112113
#[test]
114+
#[ignore]
113115
fn circuit_guarantee_last_limb() {
114116
let program = &TEST;
115117

@@ -148,6 +150,7 @@ fn circuit_guarantee_last_limb() {
148150
}
149151

150152
#[test]
153+
#[ignore]
151154
fn circuit_guarantee_middle_limb() {
152155
let program = &TEST;
153156

0 commit comments

Comments
 (0)