Skip to content

Commit

Permalink
Merge branch 'main' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
victorMagdesian authored Dec 2, 2024
2 parents 4e7d53d + 5ff4928 commit ffc8964
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 16 deletions.
29 changes: 28 additions & 1 deletion corelib/src/circuit.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ mod conversions {
use crate::internal::{
bounded_int, bounded_int::{BoundedInt, AddHelper, MulHelper, DivRemHelper},
};
use crate::integer::upcast;
use crate::integer::{upcast, downcast};

use super::{u384, u96};

Expand Down Expand Up @@ -507,6 +507,14 @@ mod conversions {
u384 { limb0, limb1, limb2: upcast(limb2), limb3: 0 }
}

pub fn felt252_try_into_two_u96(value: felt252) -> Option<(u96, u96)> {
let v: u256 = value.into();
let (limb1_low32, limb0) = bounded_int::div_rem(v.low, NZ_POW96_TYPED);
let limb1_high64: BoundedInt<0, { POW64 - 1 }> = downcast(v.high)?;
let limb1 = bounded_int::add(bounded_int::mul(limb1_high64, POW32_TYPED), limb1_low32);
Option::Some((limb0, limb1))
}

pub fn try_into_u128(value: u384) -> Option<u128> {
if value.limb2 != 0 || value.limb3 != 0 {
return Option::None;
Expand Down Expand Up @@ -540,6 +548,10 @@ mod conversions {
},
)
}

pub fn two_u96_into_felt252(limb0: u96, limb1: u96) -> felt252 {
limb0.into() + limb1.into() * POW96
}
}

impl U128IntoU384 of Into<u128, u384> {
Expand Down Expand Up @@ -572,6 +584,21 @@ impl U384TryIntoU256 of TryInto<u384, u256> {
}
}

impl U384Serde of Serde<u384> {
fn serialize(self: @u384, ref output: Array<felt252>) {
output.append(conversions::two_u96_into_felt252(*self.limb0, *self.limb1));
output.append(conversions::two_u96_into_felt252(*self.limb2, *self.limb3));
}

fn deserialize(ref serialized: Span<felt252>) -> Option<u384> {
let [l01, l23] = (*serialized.multi_pop_front::<2>()?).unbox();
let (limb0, limb1) = conversions::felt252_try_into_two_u96(l01)?;
let (limb2, limb3) = conversions::felt252_try_into_two_u96(l23)?;

return Option::Some(u384 { limb0, limb1, limb2, limb3 });
}
}

impl U384Zero of crate::num::traits::Zero<u384> {
fn zero() -> u384 {
u384 { limb0: 0, limb1: 0, limb2: 0, limb3: 0 }
Expand Down
8 changes: 8 additions & 0 deletions corelib/src/pedersen.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub impl PedersenImpl of PedersenTrait {
/// # Examples
///
/// ```
/// use core::pedersen::PedersenTrait;
///
/// let mut state = PedersenTrait::new(0);
/// assert!(state.state == 0);
/// ```
Expand All @@ -65,6 +67,9 @@ impl HashStateImpl of crate::hash::HashStateTrait<HashState> {
/// # Examples
///
/// ```
/// use core::hash::HashStateTrait;
/// use core::pedersen::PedersenTrait;
///
/// let mut state = PedersenTrait::new(0);
/// state = state.update(1);
/// ```
Expand All @@ -80,6 +85,9 @@ impl HashStateImpl of crate::hash::HashStateTrait<HashState> {
/// # Examples
///
/// ```
/// use core::hash::HashStateTrait;
/// use core::pedersen::PedersenTrait;
///
/// let mut state = PedersenTrait::new(0);
/// state = state.update(1);
/// let hash = state.finalize();
Expand Down
21 changes: 21 additions & 0 deletions corelib/src/test/circuit_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,27 @@ fn test_fill_inputs_loop() {
circuit_inputs.done().eval(modulus).unwrap();
}

#[test]
fn test_u384_serde() {
let value = u384 {
limb0: 0xb000000cd000000ef0000000,
limb1: 0x50000006700000089000000a,
limb2: 0x100000023000000450000000,
limb3: 0x80000009a000000bc0000000,
};
let serialized = array![
0x50000006700000089000000ab000000cd000000ef0000000,
0x80000009a000000bc0000000100000023000000450000000,
];
let mut buffer = array![];
value.serialize(ref buffer);
assert!(buffer == serialized);

let mut serialized = serialized.span();

assert!(Serde::<u384>::deserialize(ref serialized) == Option::Some(value));
}

#[test]
fn test_u384_zero() {
assert_eq!(Zero::zero(), u384 { limb0: 0, limb1: 0, limb2: 0, limb3: 0 });
Expand Down
15 changes: 11 additions & 4 deletions crates/cairo-lang-filesystem/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::sync::Arc;

use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use cairo_lang_utils::{LookupIntern, Upcast};
use salsa::Durability;
use semver::Version;
use serde::{Deserialize, Serialize};
use smol_str::{SmolStr, ToSmolStr};
Expand Down Expand Up @@ -313,10 +314,16 @@ fn crate_config(db: &dyn FilesGroup, crt: CrateId) -> Option<CrateConfiguration>

fn priv_raw_file_content(db: &dyn FilesGroup, file: FileId) -> Option<Arc<str>> {
match file.lookup_intern(db) {
FileLongId::OnDisk(path) => match fs::read_to_string(path) {
Ok(content) => Some(content.into()),
Err(_) => None,
},
FileLongId::OnDisk(path) => {
// This does not result in performance cost due to OS caching and the fact that salsa
// will re-execute only this single query if the file content did not change.
db.salsa_runtime().report_synthetic_read(Durability::LOW);

match fs::read_to_string(path) {
Ok(content) => Some(content.into()),
Err(_) => None,
}
}
FileLongId::Virtual(virt) => Some(virt.content),
FileLongId::External(external_id) => Some(db.ext_as_virtual(external_id).content),
}
Expand Down
1 change: 1 addition & 0 deletions crates/cairo-lang-semantic/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,7 @@ impl SemanticDiagnosticKind {
error_code!(E0002)
}
Self::MissingMember(_) => error_code!(E0003),
Self::MissingItemsInImpl(_) => error_code!(E0004),
_ => return None,
})
}
Expand Down
8 changes: 4 additions & 4 deletions crates/cairo-lang-semantic/src/items/tests/trait
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ error: Type not found.
fn param_test(ref a: u128) -> bad_type nopanic;
^******^

error: Not all trait items are implemented. Missing: 'param_test', 'no_ret_ty'.
error[E0004]: Not all trait items are implemented. Missing: 'param_test', 'no_ret_ty'.
--> lib.cairo:7:6
impl MyImpl of MyTrait::<A>;
^****^
Expand Down Expand Up @@ -425,7 +425,7 @@ trait MyTrait {
impl MyImpl of MyTrait;

//! > expected_diagnostics
error: Not all trait items are implemented. Missing: 'foo1', 'foo2'.
error[E0004]: Not all trait items are implemented. Missing: 'foo1', 'foo2'.
--> lib.cairo:5:6
impl MyImpl of MyTrait;
^****^
Expand Down Expand Up @@ -456,7 +456,7 @@ impl MyImpl of MyTrait;
// TODO(TomerStarkware): improve diagnostics for missing impls.

//! > expected_diagnostics
error: Not all trait items are implemented. Missing: 'foo1', 'foo2', 'X', 'C'.
error[E0004]: Not all trait items are implemented. Missing: 'foo1', 'foo2', 'X', 'C'.
--> lib.cairo:9:6
impl MyImpl of MyTrait;
^****^
Expand Down Expand Up @@ -509,7 +509,7 @@ impl MyImpl of MyTrait {
}

//! > expected_diagnostics
error: Not all trait items are implemented. Missing: 'foo1', 'foo3', 'Y', 'D'.
error[E0004]: Not all trait items are implemented. Missing: 'foo1', 'foo3', 'Y', 'D'.
--> lib.cairo:16:6
impl MyImpl of MyTrait {
^****^
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-semantic/src/items/tests/trait_const
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ trait MyTrait {
impl MyImpl of MyTrait {}

//! > expected_diagnostics
error: Not all trait items are implemented. Missing: 'Y'.
error[E0004]: Not all trait items are implemented. Missing: 'Y'.
--> lib.cairo:4:6
impl MyImpl of MyTrait {}
^****^
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-semantic/src/items/tests/trait_type
Original file line number Diff line number Diff line change
Expand Up @@ -2276,7 +2276,7 @@ trait MyTrait {
impl MyImpl of MyTrait {}

//! > expected_diagnostics
error: Not all trait items are implemented. Missing: 'ty'.
error[E0004]: Not all trait items are implemented. Missing: 'ty'.
--> lib.cairo:4:6
impl MyImpl of MyTrait {}
^****^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1674,7 +1674,7 @@ warning: Plugin diagnostic: Impls with the embeddable attribute must implement a
#[embeddable_as(MyImpl)]
^**********************^

error: Not all trait items are implemented. Missing: 'no_self', 'self_of_wrong_type'.
error[E0004]: Not all trait items are implemented. Missing: 'no_self', 'self_of_wrong_type'.
--> lib.cairo:8:21
#[embeddable_as(MyImpl)]
^****^
Expand Down
8 changes: 4 additions & 4 deletions docs/reference/readme.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
The docs are written in [AsciiDoc](https://asciidoc.org/)(`.adoc`). You can view the docs locally in many ways.

- View on IDE
- View in Html
- View in HTML
- View in PDF

## View on IDE

- Install `AsciiDoc`plugins in `VsCode` or `Intellij IDEA`

## View in Html
## View in HTML
### Install `asciidoctor`

- Mac OS
Expand All @@ -21,7 +21,7 @@ brew install asciidoctor
apt-get install asciidoctor
```

### Convert to Html
### Convert to HTML
> Tips:`pwd` is `cairo/docs/reference`
- Convert a single file
Expand All @@ -34,7 +34,7 @@ asciidoctor src/Summary.adoc
asciidoctor src/*.adoc
```

Now `Html` versions of the documentation will be generated.
Now `HTML` versions of the documentation will be generated.

## View in PDF
### Install `asciidoctor`
Expand Down

0 comments on commit ffc8964

Please sign in to comment.