From 307e37627e6d89ac6102917a68c16d7d27ce240c Mon Sep 17 00:00:00 2001 From: Ammar Arif Date: Thu, 20 Jun 2024 13:37:51 -0400 Subject: [PATCH] migrate to the new felt type --- Cargo.lock | 224 ++++--------------- Cargo.toml | 2 +- crates/cairo-serde/src/call.rs | 2 +- crates/cairo-serde/src/error.rs | 7 +- crates/cairo-serde/src/lib.rs | 10 +- crates/cairo-serde/src/types/array.rs | 68 +++--- crates/cairo-serde/src/types/array_legacy.rs | 18 +- crates/cairo-serde/src/types/boolean.rs | 16 +- crates/cairo-serde/src/types/byte_array.rs | 93 ++++---- crates/cairo-serde/src/types/felt.rs | 31 +-- crates/cairo-serde/src/types/integers.rs | 71 +++--- crates/cairo-serde/src/types/mod.rs | 78 +++---- crates/cairo-serde/src/types/non_zero.rs | 18 +- crates/cairo-serde/src/types/option.rs | 64 +++--- crates/cairo-serde/src/types/result.rs | 48 ++-- crates/cairo-serde/src/types/starknet.rs | 90 ++++---- crates/cairo-serde/src/types/tuple.rs | 44 ++-- crates/cairo-serde/src/types/u256.rs | 29 ++- 18 files changed, 372 insertions(+), 541 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e6a55b0..f9f87f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -106,70 +106,6 @@ version = "1.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519" -[[package]] -name = "ark-ff" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec847af850f44ad29048935519032c33da8aa03340876d351dfab5660d2966ba" -dependencies = [ - "ark-ff-asm", - "ark-ff-macros", - "ark-serialize", - "ark-std", - "derivative", - "digest", - "itertools", - "num-bigint", - "num-traits", - "paste", - "rustc_version", - "zeroize", -] - -[[package]] -name = "ark-ff-asm" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ed4aa4fe255d0bc6d79373f7e31d2ea147bcf486cba1be5ba7ea85abdb92348" -dependencies = [ - "quote", - "syn 1.0.109", -] - -[[package]] -name = "ark-ff-macros" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7abe79b0e4288889c4574159ab790824d0033b9fdcb2a112a3182fac2e514565" -dependencies = [ - "num-bigint", - "num-traits", - "proc-macro2", - "quote", - "syn 1.0.109", -] - -[[package]] -name = "ark-serialize" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adb7b85a02b83d2f22f89bd5cac66c9c89474240cb6207cb1efc16d098e822a5" -dependencies = [ - "ark-std", - "digest", - "num-bigint", -] - -[[package]] -name = "ark-std" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94893f1e0c6eeab764ade8dc4c0db24caf4fe7cbbaafc0eba0a9030f447b5185" -dependencies = [ - "num-traits", - "rand", -] - [[package]] name = "arrayvec" version = "0.7.4" @@ -231,18 +167,6 @@ version = "0.21.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" -[[package]] -name = "bigdecimal" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6773ddc0eafc0e509fb60e48dff7f450f8e674a0686ae8605e8d9901bd5eefa" -dependencies = [ - "num-bigint", - "num-integer", - "num-traits", - "serde", -] - [[package]] name = "bitflags" version = "1.3.2" @@ -595,17 +519,6 @@ dependencies = [ "serde", ] -[[package]] -name = "derivative" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "digest" version = "0.10.7" @@ -617,12 +530,6 @@ dependencies = [ "subtle", ] -[[package]] -name = "either" -version = "1.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a47c1c47d2f5964e29c61246e81db715514cd532db6b5116a25ea3c03d6780a2" - [[package]] name = "encoding_rs" version = "0.8.34" @@ -1041,15 +948,6 @@ version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" -[[package]] -name = "itertools" -version = "0.10.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" -dependencies = [ - "either", -] - [[package]] name = "itoa" version = "1.0.11" @@ -1074,6 +972,12 @@ dependencies = [ "cpufeatures", ] +[[package]] +name = "lambdaworks-math" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "358e172628e713b80a530a59654154bfc45783a6ed70ea284839800cebdf8f97" + [[package]] name = "lazy_static" version = "1.4.0" @@ -1268,12 +1172,6 @@ dependencies = [ "windows-targets 0.48.5", ] -[[package]] -name = "paste" -version = "1.0.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" - [[package]] name = "pbkdf2" version = "0.11.0" @@ -1541,15 +1439,6 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e75f6a532d0fd9f7f13144f392b6ad56a32696bfcd9c78f797f16bbb6f072d6" -[[package]] -name = "rustc_version" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" -dependencies = [ - "semver", -] - [[package]] name = "rustls" version = "0.21.11" @@ -1624,12 +1513,6 @@ dependencies = [ "untrusted", ] -[[package]] -name = "semver" -version = "1.0.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92d43fe69e652f3df9bdc2b85b2854a0825b86e4fb76bc44d945137d053639ca" - [[package]] name = "serde" version = "1.0.198" @@ -1784,15 +1667,14 @@ checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" [[package]] name = "starknet" -version = "0.10.0" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20b9a7b7bfd87287af85854f7458b8170ba6aa59c39113436532b7ff3d2fcbd8" +checksum = "1e633a772f59214c296d5037c95c36b72792c9360323818da2b625c7b4ec4b49" dependencies = [ "starknet-accounts", "starknet-contract", "starknet-core", "starknet-crypto", - "starknet-ff", "starknet-macros", "starknet-providers", "starknet-signers", @@ -1800,13 +1682,14 @@ dependencies = [ [[package]] name = "starknet-accounts" -version = "0.9.0" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2095d7584608ae1707bd1cf2889368ab3734d9f54e4fcef4765cba1f3b3f7618" +checksum = "eee8a6b588a22c7e79f5d8d4e33413387db63a8beb98be8610138541794cc0a5" dependencies = [ "async-trait", "auto_impl", "starknet-core", + "starknet-crypto", "starknet-providers", "starknet-signers", "thiserror", @@ -1814,9 +1697,9 @@ dependencies = [ [[package]] name = "starknet-contract" -version = "0.9.0" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb3b73d437b4d62241612d13fce612602de6684c149cccf696e76a20757e2156" +checksum = "a5f91344f1e0b81873b6dc235c50ae4d084c6ea4dd4a1e3e27ad895803adb610" dependencies = [ "serde", "serde_json", @@ -1829,11 +1712,12 @@ dependencies = [ [[package]] name = "starknet-core" -version = "0.10.0" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ed286d637e34fb8ae1cd2f9615120ec8ff38d1cffd311ed7fdd497cdd2bd01f" +checksum = "415c55b2384be74b4d9d92346bf4eccc570618dd97d9596ebcdd41776cb6e482" dependencies = [ "base64 0.21.7", + "crypto-bigint", "flate2", "hex", "serde", @@ -1842,14 +1726,14 @@ dependencies = [ "serde_with", "sha3", "starknet-crypto", - "starknet-ff", + "starknet-types-core", ] [[package]] name = "starknet-crypto" -version = "0.6.2" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e2c30c01e8eb0fc913c4ee3cf676389fffc1d1182bfe5bb9670e4e72e968064" +checksum = "7cc4ac48f3df819dbd02bbc912f8005edc6aebe03df3158370e486bc94c1caae" dependencies = [ "crypto-bigint", "hex", @@ -1861,50 +1745,35 @@ dependencies = [ "sha2", "starknet-crypto-codegen", "starknet-curve", - "starknet-ff", + "starknet-types-core", "zeroize", ] [[package]] name = "starknet-crypto-codegen" -version = "0.3.3" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbc159a1934c7be9761c237333a57febe060ace2bc9e3b337a59a37af206d19f" +checksum = "2e179dedc3fa6da064e56811d3e05d446aa2f7459e4eb0e3e49378a337235437" dependencies = [ "starknet-curve", - "starknet-ff", + "starknet-types-core", "syn 2.0.60", ] [[package]] name = "starknet-curve" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1c383518bb312751e4be80f53e8644034aa99a0afb29d7ac41b89a997db875b" -dependencies = [ - "starknet-ff", -] - -[[package]] -name = "starknet-ff" -version = "0.3.7" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7abf1b44ec5b18d87c1ae5f54590ca9d0699ef4dd5b2ffa66fc97f24613ec585" +checksum = "56935b306dcf0b8f14bb2a1257164b8478bb8be4801dfae0923f5b266d1b457c" dependencies = [ - "ark-ff", - "bigdecimal", - "crypto-bigint", - "getrandom", - "hex", - "num-bigint", - "serde", + "starknet-types-core", ] [[package]] name = "starknet-macros" -version = "0.1.7" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95d549d3078bdbe775d0deaa8ddb57a19942989ce7c1f2dfd60beeb322bb4945" +checksum = "f4fe4f8d615329410578cbedcdbaa4a36c7f28f68c3f3ac56006cfbdaeaa2b41" dependencies = [ "starknet-core", "syn 2.0.60", @@ -1912,14 +1781,15 @@ dependencies = [ [[package]] name = "starknet-providers" -version = "0.10.0" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6abf40ffcbe3b887b4d5cfc8ab73170c816b4aa78d1d4ad59abd3fb3b0f53cd" +checksum = "59c85e0a0f4563ae95dfeae14ea0f0c70610efc0ec2462505c64eff5765e7b97" dependencies = [ "async-trait", "auto_impl", "ethereum-types", "flate2", + "getrandom", "log", "reqwest", "serde", @@ -1932,20 +1802,34 @@ dependencies = [ [[package]] name = "starknet-signers" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e9a2bd4fd66090003c3b7f0d76476e5b63cd44f6a49ede2442673f4427d5a40" +checksum = "c17da2139119dbe3aacf1d5d4338798a5c489d17f424916ceb9d2efd83554f87" dependencies = [ "async-trait", "auto_impl", "crypto-bigint", "eth-keystore", + "getrandom", "rand", "starknet-core", "starknet-crypto", "thiserror", ] +[[package]] +name = "starknet-types-core" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe29a53d28ff630e4c7827788f14b28f9386d27cb9d05186a5f2e73218c34677" +dependencies = [ + "lambdaworks-math", + "num-bigint", + "num-integer", + "num-traits", + "serde", +] + [[package]] name = "static_assertions" version = "1.1.0" @@ -2657,17 +2541,3 @@ name = "zeroize" version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "63381fa6624bf92130a6b87c0d07380116f80b565c42cf0d754136f0238359ef" -dependencies = [ - "zeroize_derive", -] - -[[package]] -name = "zeroize_derive" -version = "1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.60", -] diff --git a/Cargo.toml b/Cargo.toml index 496489e..4aae544 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ camino = { version = "1.1.2", features = [ "serde1" ] } convert_case = "0.6.0" serde = { version = "1.0", default-features = false, features = ["alloc"] } serde_json = { version = "1.0", default-features = false, features = ["std"] } -starknet = "0.10.0" +starknet = "0.11.0" thiserror = "1.0" tracing = "0.1.34" tracing-subscriber = { version = "0.3.16", features = [ "env-filter", "json" ] } diff --git a/crates/cairo-serde/src/call.rs b/crates/cairo-serde/src/call.rs index 9c10b92..310bfa2 100644 --- a/crates/cairo-serde/src/call.rs +++ b/crates/cairo-serde/src/call.rs @@ -44,7 +44,7 @@ where T::cairo_deserialize(&r, 0) } - pub async fn raw_call(self) -> CairoResult> { + pub async fn raw_call(self) -> CairoResult> { self.provider .call(self.call_raw, self.block_id) .await diff --git a/crates/cairo-serde/src/error.rs b/crates/cairo-serde/src/error.rs index 62f689c..593069d 100644 --- a/crates/cairo-serde/src/error.rs +++ b/crates/cairo-serde/src/error.rs @@ -1,7 +1,6 @@ use super::CairoSerde; -use starknet::core::types::FieldElement; -use starknet::providers::ProviderError; +use starknet::{core::types::Felt, providers::ProviderError}; /// Cairo types result. pub type Result = core::result::Result; @@ -26,11 +25,11 @@ pub enum Error { impl CairoSerde for Error { type RustType = Self; - fn cairo_serialize(_rust: &Self::RustType) -> Vec { + fn cairo_serialize(_rust: &Self::RustType) -> Vec { vec![] } - fn cairo_deserialize(_felts: &[FieldElement], _offset: usize) -> Result { + fn cairo_deserialize(_felts: &[Felt], _offset: usize) -> Result { Ok(Error::Deserialize( "Error cairotype deserialized?".to_string(), )) diff --git a/crates/cairo-serde/src/lib.rs b/crates/cairo-serde/src/lib.rs index 73de809..9bb7648 100644 --- a/crates/cairo-serde/src/lib.rs +++ b/crates/cairo-serde/src/lib.rs @@ -18,7 +18,7 @@ pub use types::starknet::*; pub use types::u256::*; pub use types::*; -use ::starknet::core::types::FieldElement; +use ::starknet::core::types::Felt; /// Basic cairo structs that are already implemented inside /// this crate and hence skipped during ABI generation. @@ -56,12 +56,12 @@ pub trait CairoSerde { Self::SERIALIZED_SIZE.unwrap() } - /// Serializes the given type into a FieldElement sequence. - fn cairo_serialize(rust: &Self::RustType) -> Vec; + /// Serializes the given type into a Felt sequence. + fn cairo_serialize(rust: &Self::RustType) -> Vec; - /// TODO: add `serialize_to(rust: &Self::RustType, out: &mut Vec)`. + /// TODO: add `serialize_to(rust: &Self::RustType, out: &mut Vec)`. /// for large buffers optimization. /// Deserializes an array of felts into the given type. - fn cairo_deserialize(felts: &[FieldElement], offset: usize) -> Result; + fn cairo_deserialize(felts: &[Felt], offset: usize) -> Result; } diff --git a/crates/cairo-serde/src/types/array.rs b/crates/cairo-serde/src/types/array.rs index c4b5063..d7afb72 100644 --- a/crates/cairo-serde/src/types/array.rs +++ b/crates/cairo-serde/src/types/array.rs @@ -1,7 +1,7 @@ //! CairoSerde implementation for `Vec`. //! They are used for Array and Span cairo types. use crate::{CairoSerde, Error, Result}; -use starknet::core::types::FieldElement; +use starknet::core::types::Felt; impl CairoSerde for Vec where @@ -18,13 +18,13 @@ where 1 + data.iter().map(T::cairo_serialized_size).sum::() } - fn cairo_serialize(rust: &Self::RustType) -> Vec { - let mut out: Vec = vec![rust.len().into()]; + fn cairo_serialize(rust: &Self::RustType) -> Vec { + let mut out: Vec = vec![rust.len().into()]; rust.iter().for_each(|r| out.extend(T::cairo_serialize(r))); out } - fn cairo_deserialize(felts: &[FieldElement], offset: usize) -> Result { + fn cairo_deserialize(felts: &[Felt], offset: usize) -> Result { if offset >= felts.len() { return Err(Error::Deserialize(format!( "Buffer too short to deserialize an array: offset ({}) : buffer {:?}", @@ -70,19 +70,15 @@ mod tests { let v: Vec = vec![1, 2, 3]; let felts = Vec::::cairo_serialize(&v); assert_eq!(felts.len(), 4); - assert_eq!(felts[0], FieldElement::from(3_u32)); - assert_eq!(felts[1], FieldElement::ONE); - assert_eq!(felts[2], FieldElement::TWO); - assert_eq!(felts[3], FieldElement::THREE); + assert_eq!(felts[0], Felt::from(3_u32)); + assert_eq!(felts[1], Felt::ONE); + assert_eq!(felts[2], Felt::TWO); + assert_eq!(felts[3], Felt::THREE); } #[test] fn test_deserialize_array() { - let felts: Vec = vec![ - FieldElement::from(2_u32), - FieldElement::from(123_u32), - FieldElement::from(9988_u32), - ]; + let felts: Vec = vec![Felt::from(2_u32), Felt::from(123_u32), Felt::from(9988_u32)]; let vals = Vec::::cairo_deserialize(&felts, 0).unwrap(); assert_eq!(vals.len(), 2); @@ -95,23 +91,23 @@ mod tests { let v: Vec> = vec![vec![1, 2], vec![3]]; let felts = Vec::>::cairo_serialize(&v); assert_eq!(felts.len(), 6); - assert_eq!(felts[0], FieldElement::TWO); - assert_eq!(felts[1], FieldElement::TWO); - assert_eq!(felts[2], FieldElement::ONE); - assert_eq!(felts[3], FieldElement::TWO); - assert_eq!(felts[4], FieldElement::ONE); - assert_eq!(felts[5], FieldElement::THREE); + assert_eq!(felts[0], Felt::TWO); + assert_eq!(felts[1], Felt::TWO); + assert_eq!(felts[2], Felt::ONE); + assert_eq!(felts[3], Felt::TWO); + assert_eq!(felts[4], Felt::ONE); + assert_eq!(felts[5], Felt::THREE); } #[test] fn test_deserialize_array_nested() { - let felts: Vec = vec![ - FieldElement::TWO, - FieldElement::TWO, - FieldElement::ONE, - FieldElement::TWO, - FieldElement::ONE, - FieldElement::THREE, + let felts: Vec = vec![ + Felt::TWO, + Felt::TWO, + Felt::ONE, + Felt::TWO, + Felt::ONE, + Felt::THREE, ]; let vals = Vec::>::cairo_deserialize(&felts, 0).unwrap(); @@ -122,24 +118,20 @@ mod tests { #[test] fn test_serialize_array_tuple() { - let v: Vec<(u32, FieldElement)> = vec![(12, FieldElement::TWO)]; - let felts = Vec::<(u32, FieldElement)>::cairo_serialize(&v); + let v: Vec<(u32, Felt)> = vec![(12, Felt::TWO)]; + let felts = Vec::<(u32, Felt)>::cairo_serialize(&v); assert_eq!(felts.len(), 3); - assert_eq!(felts[0], FieldElement::from(1_u32)); - assert_eq!(felts[1], FieldElement::from(12_u32)); - assert_eq!(felts[2], FieldElement::TWO); + assert_eq!(felts[0], Felt::from(1_u32)); + assert_eq!(felts[1], Felt::from(12_u32)); + assert_eq!(felts[2], Felt::TWO); } #[test] fn test_deserialize_array_tuple() { - let felts: Vec = vec![ - FieldElement::from(1_u32), - FieldElement::from(12_u32), - FieldElement::TWO, - ]; + let felts: Vec = vec![Felt::from(1_u32), Felt::from(12_u32), Felt::TWO]; - let vals = Vec::<(u32, FieldElement)>::cairo_deserialize(&felts, 0).unwrap(); + let vals = Vec::<(u32, Felt)>::cairo_deserialize(&felts, 0).unwrap(); assert_eq!(vals.len(), 1); - assert_eq!(vals[0], (12, FieldElement::TWO)); + assert_eq!(vals[0], (12, Felt::TWO)); } } diff --git a/crates/cairo-serde/src/types/array_legacy.rs b/crates/cairo-serde/src/types/array_legacy.rs index db46fce..06e9be4 100644 --- a/crates/cairo-serde/src/types/array_legacy.rs +++ b/crates/cairo-serde/src/types/array_legacy.rs @@ -1,6 +1,6 @@ //! Dedicated struct for cairo 0 arrays, where len is not prefixed. use crate::{CairoSerde, Error, Result}; -use starknet::core::types::FieldElement; +use starknet::core::types::Felt; #[derive(Debug, Clone, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)] pub struct CairoArrayLegacy(pub Vec); @@ -40,15 +40,15 @@ where data.iter().map(T::cairo_serialized_size).sum::() } - fn cairo_serialize(rust: &Self::RustType) -> Vec { - let mut out: Vec = vec![]; + fn cairo_serialize(rust: &Self::RustType) -> Vec { + let mut out: Vec = vec![]; rust.0 .iter() .for_each(|r| out.extend(T::cairo_serialize(r))); out } - fn cairo_deserialize(felts: &[FieldElement], offset: usize) -> Result { + fn cairo_deserialize(felts: &[Felt], offset: usize) -> Result { if offset >= felts.len() { // As the length of cairo 0 arrays is not included in the serialized form of the array, // we don't have much choice here to return an empty array instead of an error. @@ -59,7 +59,7 @@ where let mut offset = offset; let len = felts[offset - 1]; - if FieldElement::from(offset) + len > FieldElement::from(felts.len()) { + if Felt::from(offset) + len > Felt::from(felts.len()) { return Err(Error::Deserialize(format!( "Buffer too short to deserialize an array of length {}: offset ({}) : buffer {:?}", len, offset, felts, @@ -67,7 +67,7 @@ where } loop { - if FieldElement::from(out.len()) == len { + if Felt::from(out.len()) == len { break; } @@ -88,7 +88,7 @@ mod tests { #[test] fn array_offset_len_ok() { let serialized = vec![felt!("4"), felt!("1"), felt!("2"), felt!("3"), felt!("4")]; - let a = CairoArrayLegacy::::cairo_deserialize(&serialized, 1).unwrap(); + let a = CairoArrayLegacy::::cairo_deserialize(&serialized, 1).unwrap(); assert_eq!(a.len(), 4); assert_eq!(a.0[0], felt!("1")); assert_eq!(a.0[1], felt!("2")); @@ -100,7 +100,7 @@ mod tests { fn empty_array() { // Array with only the length that is 0 (an other field as we're in cairo 0). // So the deserialization of the array starts at index 1. - let serialized = vec![FieldElement::ZERO]; - let _a = CairoArrayLegacy::::cairo_deserialize(&serialized, 1).unwrap(); + let serialized = vec![Felt::ZERO]; + let _a = CairoArrayLegacy::::cairo_deserialize(&serialized, 1).unwrap(); } } diff --git a/crates/cairo-serde/src/types/boolean.rs b/crates/cairo-serde/src/types/boolean.rs index 624b45d..58fd27d 100644 --- a/crates/cairo-serde/src/types/boolean.rs +++ b/crates/cairo-serde/src/types/boolean.rs @@ -1,15 +1,15 @@ //! CairoSerde implementation for bool. use crate::{CairoSerde, Error, Result}; -use starknet::core::types::FieldElement; +use starknet::core::types::Felt; impl CairoSerde for bool { type RustType = Self; - fn cairo_serialize(rust: &Self::RustType) -> Vec { - vec![FieldElement::from(*rust as u32)] + fn cairo_serialize(rust: &Self::RustType) -> Vec { + vec![Felt::from(*rust as u32)] } - fn cairo_deserialize(felts: &[FieldElement], offset: usize) -> Result { + fn cairo_deserialize(felts: &[Felt], offset: usize) -> Result { if offset >= felts.len() { return Err(Error::Deserialize(format!( "Buffer too short to deserialize a boolean: offset ({}) : buffer {:?}", @@ -17,7 +17,7 @@ impl CairoSerde for bool { ))); } - if felts[offset] == FieldElement::ONE { + if felts[offset] == Felt::ONE { Ok(true) } else { Ok(false) @@ -34,17 +34,17 @@ mod tests { let v = true; let felts = bool::cairo_serialize(&v); assert_eq!(felts.len(), 1); - assert_eq!(felts[0], FieldElement::ONE); + assert_eq!(felts[0], Felt::ONE); let v = false; let felts = bool::cairo_serialize(&v); assert_eq!(felts.len(), 1); - assert_eq!(felts[0], FieldElement::ZERO); + assert_eq!(felts[0], Felt::ZERO); } #[test] fn test_deserialize_bool() { - let felts = vec![FieldElement::ZERO, FieldElement::ONE, FieldElement::TWO]; + let felts = vec![Felt::ZERO, Felt::ONE, Felt::TWO]; assert!(!bool::cairo_deserialize(&felts, 0).unwrap()); assert!(bool::cairo_deserialize(&felts, 1).unwrap()); assert!(!bool::cairo_deserialize(&felts, 2).unwrap()); diff --git a/crates/cairo-serde/src/types/byte_array.rs b/crates/cairo-serde/src/types/byte_array.rs index 820ffdd..3ced2fb 100644 --- a/crates/cairo-serde/src/types/byte_array.rs +++ b/crates/cairo-serde/src/types/byte_array.rs @@ -14,14 +14,14 @@ use std::{ string::FromUtf8Error, }; -use starknet::core::types::FieldElement; +use starknet::core::types::Felt; use crate::error::{Error, Result as CainomeResult}; use crate::CairoSerde; const MAX_WORD_LEN: usize = 31; -pub const BYTES31_MAX: FieldElement = FieldElement::from_mont([ +pub const BYTES31_MAX: Felt = Felt::from_raw([ 18446744062762287141, 20123647, 18446744073709514624, @@ -31,10 +31,10 @@ pub const BYTES31_MAX: FieldElement = FieldElement::from_mont([ #[derive( Debug, Clone, Eq, PartialEq, PartialOrd, Default, serde::Serialize, serde::Deserialize, )] -pub struct Bytes31(FieldElement); +pub struct Bytes31(Felt); impl Bytes31 { - pub fn new(felt: FieldElement) -> CainomeResult { + pub fn new(felt: Felt) -> CainomeResult { if felt > BYTES31_MAX { Err(Error::Bytes31OutOfRange) } else { @@ -42,21 +42,21 @@ impl Bytes31 { } } - pub fn felt(&self) -> FieldElement { + pub fn felt(&self) -> Felt { self.0 } } -impl From for FieldElement { +impl From for Felt { fn from(value: Bytes31) -> Self { value.felt() } } -impl TryFrom for Bytes31 { +impl TryFrom for Bytes31 { type Error = Error; - fn try_from(value: FieldElement) -> Result { + fn try_from(value: Felt) -> Result { Self::new(value) } } @@ -64,11 +64,11 @@ impl TryFrom for Bytes31 { impl CairoSerde for Bytes31 { type RustType = Self; - fn cairo_serialize(rust: &Self::RustType) -> Vec { + fn cairo_serialize(rust: &Self::RustType) -> Vec { vec![rust.felt()] } - fn cairo_deserialize(felts: &[FieldElement], offset: usize) -> CainomeResult { + fn cairo_deserialize(felts: &[Felt], offset: usize) -> CainomeResult { Self::new(felts[offset]) } } @@ -78,7 +78,7 @@ impl CairoSerde for Bytes31 { )] pub struct ByteArray { pub data: Vec, - pub pending_word: FieldElement, + pub pending_word: Felt, pub pending_word_len: usize, } @@ -90,25 +90,25 @@ impl CairoSerde for ByteArray { fn cairo_serialized_size(rust: &Self::RustType) -> usize { let mut size = 0; size += Vec::::cairo_serialized_size(&rust.data); - size += FieldElement::cairo_serialized_size(&rust.pending_word); + size += Felt::cairo_serialized_size(&rust.pending_word); size += u32::cairo_serialized_size(&(rust.pending_word_len as u32)); size } - fn cairo_serialize(rust: &Self::RustType) -> Vec { - let mut out: Vec = vec![]; + fn cairo_serialize(rust: &Self::RustType) -> Vec { + let mut out: Vec = vec![]; out.extend(Vec::::cairo_serialize(&rust.data)); - out.extend(FieldElement::cairo_serialize(&rust.pending_word)); + out.extend(Felt::cairo_serialize(&rust.pending_word)); out.extend(u32::cairo_serialize(&(rust.pending_word_len as u32))); out } - fn cairo_deserialize(felts: &[FieldElement], offset: usize) -> CainomeResult { + fn cairo_deserialize(felts: &[Felt], offset: usize) -> CainomeResult { let mut offset = offset; let data = Vec::::cairo_deserialize(felts, offset)?; offset += Vec::::cairo_serialized_size(&data); - let pending_word = FieldElement::cairo_deserialize(felts, offset)?; - offset += FieldElement::cairo_serialized_size(&pending_word); + let pending_word = Felt::cairo_deserialize(felts, offset)?; + offset += Felt::cairo_serialized_size(&pending_word); let pending_word_len = u32::cairo_deserialize(felts, offset)?; Ok(ByteArray { @@ -147,19 +147,18 @@ impl ByteArray { let len = r.len(); ( // Safe to unwrap as pending word always fit in a felt. - FieldElement::from_byte_slice_be(&r).unwrap(), + // Felt::from_byte_slice_be(&r).unwrap(), + Felt::from_bytes_be_slice(&r), len, ) } else { - (FieldElement::ZERO, 0) + (Felt::ZERO, 0) }; let mut data = Vec::new(); for chunk in full_chunks { // Safe to unwrap as full chunks are 31 bytes long, always fit in a felt. - data.push(Bytes31::new( - FieldElement::from_byte_slice_be(chunk).unwrap(), - )?) + data.push(Bytes31::new(Felt::from_bytes_be_slice(chunk))?) } Ok(Self { @@ -197,7 +196,7 @@ impl ByteArray { /// * `len` - The number of bytes in the felt, at most 31. In the context /// of `ByteArray`, we don't need to check `len` as the `MAX_WORD_LEN` /// already protect against that. -fn felt_to_utf8(felt: &FieldElement, len: usize) -> Result { +fn felt_to_utf8(felt: &Felt, len: usize) -> Result { let mut buffer = Vec::new(); // ByteArray always enforce to have the first byte equal to 0. @@ -228,7 +227,7 @@ impl TryFrom<&str> for ByteArray { #[cfg(test)] mod tests { use super::ByteArray; - use starknet::core::types::FieldElement; + use starknet::core::types::Felt; #[test] fn test_from_string_empty_string_default() { @@ -243,7 +242,7 @@ mod tests { b, ByteArray { data: vec![], - pending_word: FieldElement::from_hex_be( + pending_word: Felt::from_hex( "0x0000000000000000000000000000000000000000000000000000000041424344" ) .unwrap(), @@ -261,7 +260,7 @@ mod tests { b, ByteArray { data: vec![], - pending_word: FieldElement::from_hex_be( + pending_word: Felt::from_hex( "0x00004142434445464748494a4b4c4d4e4f505152535455565758595a31323334" ) .unwrap(), @@ -277,13 +276,13 @@ mod tests { assert_eq!( b, ByteArray { - data: vec![FieldElement::from_hex_be( + data: vec![Felt::from_hex( "0x004142434445464748494a4b4c4d4e4f505152535455565758595a3132333435" ) .unwrap() .try_into() .unwrap()], - pending_word: FieldElement::ZERO, + pending_word: Felt::ZERO, pending_word_len: 0, } ); @@ -300,20 +299,20 @@ mod tests { b, ByteArray { data: vec![ - FieldElement::from_hex_be( + Felt::from_hex( "0x004142434445464748494a4b4c4d4e4f505152535455565758595a3132333435" ) .unwrap() .try_into() .unwrap(), - FieldElement::from_hex_be( + Felt::from_hex( "0x004142434445464748494a4b4c4d4e4f505152535455565758595a3132333435" ) .unwrap() .try_into() .unwrap(), ], - pending_word: FieldElement::ZERO, + pending_word: Felt::ZERO, pending_word_len: 0, } ); @@ -330,20 +329,20 @@ mod tests { b, ByteArray { data: vec![ - FieldElement::from_hex_be( + Felt::from_hex( "0x004142434445464748494a4b4c4d4e4f505152535455565758595a3132333435" ) .unwrap() .try_into() .unwrap(), - FieldElement::from_hex_be( + Felt::from_hex( "0x004142434445464748494a4b4c4d4e4f505152535455565758595a3132333435" ) .unwrap() .try_into() .unwrap(), ], - pending_word: FieldElement::from_hex_be( + pending_word: Felt::from_hex( "0x0000000000000000000000000000000000000000000000000000000041424344" ) .unwrap(), @@ -362,7 +361,7 @@ mod tests { fn test_to_string_only_pending_word() { let b = ByteArray { data: vec![], - pending_word: FieldElement::from_hex_be( + pending_word: Felt::from_hex( "0x0000000000000000000000000000000000000000000000000000000041424344", ) .unwrap(), @@ -376,7 +375,7 @@ mod tests { fn test_to_string_max_pending_word_len() { let b = ByteArray { data: vec![], - pending_word: FieldElement::from_hex_be( + pending_word: Felt::from_hex( "0x00004142434445464748494a4b4c4d4e4f505152535455565758595a31323334", ) .unwrap(), @@ -389,13 +388,13 @@ mod tests { #[test] fn test_to_string_data_only() { let b = ByteArray { - data: vec![FieldElement::from_hex_be( + data: vec![Felt::from_hex( "0x004142434445464748494a4b4c4d4e4f505152535455565758595a3132333435", ) .unwrap() .try_into() .unwrap()], - pending_word: FieldElement::ZERO, + pending_word: Felt::ZERO, pending_word_len: 0, }; @@ -406,20 +405,20 @@ mod tests { fn test_to_string_data_only_multiple() { let b = ByteArray { data: vec![ - FieldElement::from_hex_be( + Felt::from_hex( "0x004142434445464748494a4b4c4d4e4f505152535455565758595a3132333435", ) .unwrap() .try_into() .unwrap(), - FieldElement::from_hex_be( + Felt::from_hex( "0x004142434445464748494a4b4c4d4e4f505152535455565758595a3132333435", ) .unwrap() .try_into() .unwrap(), ], - pending_word: FieldElement::ZERO, + pending_word: Felt::ZERO, pending_word_len: 0, }; @@ -433,20 +432,20 @@ mod tests { fn test_to_string_data_and_pending_word() { let b = ByteArray { data: vec![ - FieldElement::from_hex_be( + Felt::from_hex( "0x004142434445464748494a4b4c4d4e4f505152535455565758595a3132333435", ) .unwrap() .try_into() .unwrap(), - FieldElement::from_hex_be( + Felt::from_hex( "0x004142434445464748494a4b4c4d4e4f505152535455565758595a3132333435", ) .unwrap() .try_into() .unwrap(), ], - pending_word: FieldElement::from_hex_be( + pending_word: Felt::from_hex( "0x0000000000000000000000000000000000000000000000000000000041424344", ) .unwrap(), @@ -464,7 +463,7 @@ mod tests { fn test_to_string_invalid_utf8() { let b = ByteArray { data: vec![], - pending_word: FieldElement::from_hex_be( + pending_word: Felt::from_hex( "0x00000000000000000000000000000000000000000000000000000000ffffffff", ) .unwrap(), @@ -482,7 +481,7 @@ mod tests { b, ByteArray { data: vec![], - pending_word: FieldElement::from_hex_be( + pending_word: Felt::from_hex( "0x000000000000000000000000000000000000000000000000f09fa680f09f8c9f", ) .unwrap(), diff --git a/crates/cairo-serde/src/types/felt.rs b/crates/cairo-serde/src/types/felt.rs index 92c50a4..d22090e 100644 --- a/crates/cairo-serde/src/types/felt.rs +++ b/crates/cairo-serde/src/types/felt.rs @@ -1,14 +1,14 @@ use crate::{CairoSerde, Error, Result}; -use starknet::core::types::FieldElement; +use starknet::core::types::Felt; -impl CairoSerde for FieldElement { +impl CairoSerde for Felt { type RustType = Self; - fn cairo_serialize(rust: &Self::RustType) -> Vec { + fn cairo_serialize(rust: &Self::RustType) -> Vec { vec![*rust] } - fn cairo_deserialize(felts: &[FieldElement], offset: usize) -> Result { + fn cairo_deserialize(felts: &[Felt], offset: usize) -> Result { if offset >= felts.len() { return Err(Error::Deserialize(format!( "Buffer too short to deserialize a felt: offset ({}) : buffer {:?}", @@ -26,26 +26,17 @@ mod tests { #[test] fn test_serialize_field_element() { - let f = FieldElement::ZERO; - let felts = FieldElement::cairo_serialize(&f); + let f = Felt::ZERO; + let felts = Felt::cairo_serialize(&f); assert_eq!(felts.len(), 1); - assert_eq!(felts[0], FieldElement::ZERO); + assert_eq!(felts[0], Felt::ZERO); } #[test] fn test_deserialize_field_element() { - let felts = vec![FieldElement::ZERO, FieldElement::ONE, FieldElement::TWO]; - assert_eq!( - FieldElement::cairo_deserialize(&felts, 0).unwrap(), - FieldElement::ZERO - ); - assert_eq!( - FieldElement::cairo_deserialize(&felts, 1).unwrap(), - FieldElement::ONE - ); - assert_eq!( - FieldElement::cairo_deserialize(&felts, 2).unwrap(), - FieldElement::TWO - ); + let felts = vec![Felt::ZERO, Felt::ONE, Felt::TWO]; + assert_eq!(Felt::cairo_deserialize(&felts, 0).unwrap(), Felt::ZERO); + assert_eq!(Felt::cairo_deserialize(&felts, 1).unwrap(), Felt::ONE); + assert_eq!(Felt::cairo_deserialize(&felts, 2).unwrap(), Felt::TWO); } } diff --git a/crates/cairo-serde/src/types/integers.rs b/crates/cairo-serde/src/types/integers.rs index dc2decb..093a8af 100644 --- a/crates/cairo-serde/src/types/integers.rs +++ b/crates/cairo-serde/src/types/integers.rs @@ -1,17 +1,17 @@ //! CairoSerde implementation for integers (signed/unsigned). use crate::{CairoSerde, Error, Result}; -use starknet::core::types::FieldElement; +use starknet::core::types::Felt; macro_rules! implement_trait_for_unsigned { ($type:ty) => { impl CairoSerde for $type { type RustType = Self; - fn cairo_serialize(rust: &Self::RustType) -> Vec { - vec![FieldElement::from(*rust)] + fn cairo_serialize(rust: &Self::RustType) -> Vec { + vec![Felt::from(*rust)] } - fn cairo_deserialize(felts: &[FieldElement], offset: usize) -> Result { + fn cairo_deserialize(felts: &[Felt], offset: usize) -> Result { if offset >= felts.len() { return Err(Error::Deserialize(format!( "Buffer too short to deserialize a unsigned integer: offset ({}) : buffer {:?}", @@ -32,11 +32,11 @@ macro_rules! implement_trait_for_signed { impl CairoSerde for $type { type RustType = Self; - fn cairo_serialize(rust: &Self::RustType) -> Vec { - vec![FieldElement::from(*rust as usize)] + fn cairo_serialize(rust: &Self::RustType) -> Vec { + vec![Felt::from(*rust as usize)] } - fn cairo_deserialize(felts: &[FieldElement], offset: usize) -> Result { + fn cairo_deserialize(felts: &[Felt], offset: usize) -> Result { if offset >= felts.len() { return Err(Error::Deserialize(format!( "Buffer too short to deserialize a signed integer: offset ({}) : buffer {:?}", @@ -75,12 +75,12 @@ mod tests { let v = 12_u8; let felts = u8::cairo_serialize(&v); assert_eq!(felts.len(), 1); - assert_eq!(felts[0], FieldElement::from(12_u8)); + assert_eq!(felts[0], Felt::from(12_u8)); } #[test] fn test_deserialize_u8() { - let felts = vec![FieldElement::from(12_u8), FieldElement::from(10_u8)]; + let felts = vec![Felt::from(12_u8), Felt::from(10_u8)]; assert_eq!(u8::cairo_deserialize(&felts, 0).unwrap(), 12); assert_eq!(u8::cairo_deserialize(&felts, 1).unwrap(), 10); } @@ -90,12 +90,12 @@ mod tests { let v = 12_u16; let felts = u16::cairo_serialize(&v); assert_eq!(felts.len(), 1); - assert_eq!(felts[0], FieldElement::from(12_u16)); + assert_eq!(felts[0], Felt::from(12_u16)); } #[test] fn test_deserialize_u16() { - let felts = vec![FieldElement::from(12_u16), FieldElement::from(10_u8)]; + let felts = vec![Felt::from(12_u16), Felt::from(10_u8)]; assert_eq!(u16::cairo_deserialize(&felts, 0).unwrap(), 12); assert_eq!(u16::cairo_deserialize(&felts, 1).unwrap(), 10); } @@ -105,12 +105,12 @@ mod tests { let v = 123_u32; let felts = u32::cairo_serialize(&v); assert_eq!(felts.len(), 1); - assert_eq!(felts[0], FieldElement::from(123_u32)); + assert_eq!(felts[0], Felt::from(123_u32)); } #[test] fn test_deserialize_u32() { - let felts = vec![FieldElement::from(123_u32), FieldElement::from(99_u32)]; + let felts = vec![Felt::from(123_u32), Felt::from(99_u32)]; assert_eq!(u32::cairo_deserialize(&felts, 0).unwrap(), 123); assert_eq!(u32::cairo_deserialize(&felts, 1).unwrap(), 99); } @@ -120,12 +120,12 @@ mod tests { let v = 123_u64; let felts = u64::cairo_serialize(&v); assert_eq!(felts.len(), 1); - assert_eq!(felts[0], FieldElement::from(123_u64)); + assert_eq!(felts[0], Felt::from(123_u64)); } #[test] fn test_deserialize_u64() { - let felts = vec![FieldElement::from(123_u64), FieldElement::from(99_u64)]; + let felts = vec![Felt::from(123_u64), Felt::from(99_u64)]; assert_eq!(u64::cairo_deserialize(&felts, 0).unwrap(), 123); assert_eq!(u64::cairo_deserialize(&felts, 1).unwrap(), 99); } @@ -135,12 +135,12 @@ mod tests { let v = 123_u128; let felts = u128::cairo_serialize(&v); assert_eq!(felts.len(), 1); - assert_eq!(felts[0], FieldElement::from(123_u128)); + assert_eq!(felts[0], Felt::from(123_u128)); } #[test] fn test_deserialize_u128() { - let felts = vec![FieldElement::from(123_u128), FieldElement::from(99_u128)]; + let felts = vec![Felt::from(123_u128), Felt::from(99_u128)]; assert_eq!(u128::cairo_deserialize(&felts, 0).unwrap(), 123); assert_eq!(u128::cairo_deserialize(&felts, 1).unwrap(), 99); } @@ -150,12 +150,12 @@ mod tests { let v = 123; let felts = usize::cairo_serialize(&v); assert_eq!(felts.len(), 1); - assert_eq!(felts[0], FieldElement::from(123_u128)); + assert_eq!(felts[0], Felt::from(123_u128)); } #[test] fn test_deserialize_usize() { - let felts = vec![FieldElement::from(123_u128), FieldElement::from(99_u64)]; + let felts = vec![Felt::from(123_u128), Felt::from(99_u64)]; assert_eq!(usize::cairo_deserialize(&felts, 0).unwrap(), 123); assert_eq!(usize::cairo_deserialize(&felts, 1).unwrap(), 99); } @@ -165,15 +165,12 @@ mod tests { let v = i8::MAX; let felts = i8::cairo_serialize(&v); assert_eq!(felts.len(), 1); - assert_eq!(felts[0], FieldElement::from(i8::MAX as u8)); + assert_eq!(felts[0], Felt::from(i8::MAX as u8)); } #[test] fn test_deserialize_i8() { - let felts = vec![ - FieldElement::from(i8::MAX as u8), - FieldElement::from(i8::MAX as u8), - ]; + let felts = vec![Felt::from(i8::MAX as u8), Felt::from(i8::MAX as u8)]; assert_eq!(i8::cairo_deserialize(&felts, 0).unwrap(), i8::MAX); assert_eq!(i8::cairo_deserialize(&felts, 1).unwrap(), i8::MAX); } @@ -183,15 +180,12 @@ mod tests { let v = i16::MAX; let felts = i16::cairo_serialize(&v); assert_eq!(felts.len(), 1); - assert_eq!(felts[0], FieldElement::from(i16::MAX as u16)); + assert_eq!(felts[0], Felt::from(i16::MAX as u16)); } #[test] fn test_deserialize_i16() { - let felts = vec![ - FieldElement::from(i16::MAX as u16), - FieldElement::from(i16::MAX as u16), - ]; + let felts = vec![Felt::from(i16::MAX as u16), Felt::from(i16::MAX as u16)]; assert_eq!(i16::cairo_deserialize(&felts, 0).unwrap(), i16::MAX); assert_eq!(i16::cairo_deserialize(&felts, 1).unwrap(), i16::MAX); } @@ -201,15 +195,12 @@ mod tests { let v = i32::MAX; let felts = i32::cairo_serialize(&v); assert_eq!(felts.len(), 1); - assert_eq!(felts[0], FieldElement::from(i32::MAX as u32)); + assert_eq!(felts[0], Felt::from(i32::MAX as u32)); } #[test] fn test_deserialize_i32() { - let felts = vec![ - FieldElement::from(i32::MAX as u32), - FieldElement::from(i32::MAX as u32), - ]; + let felts = vec![Felt::from(i32::MAX as u32), Felt::from(i32::MAX as u32)]; assert_eq!(i32::cairo_deserialize(&felts, 0).unwrap(), i32::MAX); assert_eq!(i32::cairo_deserialize(&felts, 1).unwrap(), i32::MAX); } @@ -219,25 +210,19 @@ mod tests { let v = i64::MAX; let felts = i64::cairo_serialize(&v); assert_eq!(felts.len(), 1); - assert_eq!(felts[0], FieldElement::from(i64::MAX as u64)); + assert_eq!(felts[0], Felt::from(i64::MAX as u64)); } #[test] fn test_deserialize_i64() { - let felts = vec![ - FieldElement::from(i64::MAX as u64), - FieldElement::from(i64::MAX as u64), - ]; + let felts = vec![Felt::from(i64::MAX as u64), Felt::from(i64::MAX as u64)]; assert_eq!(i64::cairo_deserialize(&felts, 0).unwrap(), i64::MAX); assert_eq!(i64::cairo_deserialize(&felts, 1).unwrap(), i64::MAX); } #[test] fn test_deserialize_i128() { - let felts = vec![ - FieldElement::from(i128::MAX as u128), - FieldElement::from(i128::MAX as u128), - ]; + let felts = vec![Felt::from(i128::MAX as u128), Felt::from(i128::MAX as u128)]; assert_eq!(i128::cairo_deserialize(&felts, 0).unwrap(), i128::MAX); assert_eq!(i128::cairo_deserialize(&felts, 1).unwrap(), i128::MAX); } diff --git a/crates/cairo-serde/src/types/mod.rs b/crates/cairo-serde/src/types/mod.rs index 6310b8a..3719775 100644 --- a/crates/cairo-serde/src/types/mod.rs +++ b/crates/cairo-serde/src/types/mod.rs @@ -14,28 +14,28 @@ pub mod u256; #[cfg(test)] mod tests { use crate::CairoSerde; - use ::starknet::core::types::FieldElement; + use ::starknet::core::types::Felt; #[test] fn test_serialize_several_values() { let o = Some(u32::MAX); - let r = Err(FieldElement::TWO); + let r = Err(Felt::TWO); let a: Vec = vec![1, 2, 3]; let mut felts = vec![]; felts.extend(Option::::cairo_serialize(&o)); - felts.extend(Result::::cairo_serialize(&r)); + felts.extend(Result::::cairo_serialize(&r)); felts.extend(Vec::::cairo_serialize(&a)); assert_eq!(felts.len(), 8); - assert_eq!(felts[0], FieldElement::ZERO); - assert_eq!(felts[1], FieldElement::from(u32::MAX)); - assert_eq!(felts[2], FieldElement::ONE); - assert_eq!(felts[3], FieldElement::TWO); - assert_eq!(felts[4], FieldElement::THREE); - assert_eq!(felts[5], FieldElement::ONE); - assert_eq!(felts[6], FieldElement::TWO); - assert_eq!(felts[7], FieldElement::THREE); + assert_eq!(felts[0], Felt::ZERO); + assert_eq!(felts[1], Felt::from(u32::MAX)); + assert_eq!(felts[2], Felt::ONE); + assert_eq!(felts[3], Felt::TWO); + assert_eq!(felts[4], Felt::THREE); + assert_eq!(felts[5], Felt::ONE); + assert_eq!(felts[6], Felt::TWO); + assert_eq!(felts[7], Felt::THREE); } #[test] @@ -46,30 +46,30 @@ mod tests { let mut felts = vec![]; felts.extend(Option::::cairo_serialize(&o)); - felts.extend(Result::<(), FieldElement>::cairo_serialize(&r)); + felts.extend(Result::<(), Felt>::cairo_serialize(&r)); felts.extend(Vec::::cairo_serialize(&a)); assert_eq!(felts.len(), 7); - assert_eq!(felts[0], FieldElement::ZERO); - assert_eq!(felts[1], FieldElement::from(u32::MAX)); - assert_eq!(felts[2], FieldElement::ZERO); - assert_eq!(felts[3], FieldElement::THREE); - assert_eq!(felts[4], FieldElement::ONE); - assert_eq!(felts[5], FieldElement::TWO); - assert_eq!(felts[6], FieldElement::THREE); + assert_eq!(felts[0], Felt::ZERO); + assert_eq!(felts[1], Felt::from(u32::MAX)); + assert_eq!(felts[2], Felt::ZERO); + assert_eq!(felts[3], Felt::THREE); + assert_eq!(felts[4], Felt::ONE); + assert_eq!(felts[5], Felt::TWO); + assert_eq!(felts[6], Felt::THREE); } #[test] fn test_deserialize_several_values() { let felts = vec![ - FieldElement::ZERO, - FieldElement::from(u32::MAX), - FieldElement::ONE, - FieldElement::TWO, - FieldElement::THREE, - FieldElement::ONE, - FieldElement::TWO, - FieldElement::THREE, + Felt::ZERO, + Felt::from(u32::MAX), + Felt::ONE, + Felt::TWO, + Felt::THREE, + Felt::ONE, + Felt::TWO, + Felt::THREE, ]; let mut offset = 0; @@ -77,14 +77,14 @@ mod tests { let o = Option::::cairo_deserialize(&felts, offset).unwrap(); offset += Option::::cairo_serialized_size(&o); - let r = Result::::cairo_deserialize(&felts, offset).unwrap(); - offset += Result::::cairo_serialized_size(&r); + let r = Result::::cairo_deserialize(&felts, offset).unwrap(); + offset += Result::::cairo_serialized_size(&r); let a = Vec::::cairo_deserialize(&felts, offset).unwrap(); offset += Vec::::cairo_serialized_size(&a); assert_eq!(o, Some(u32::MAX)); - assert_eq!(r, Err(FieldElement::TWO)); + assert_eq!(r, Err(Felt::TWO)); assert_eq!(a, vec![1, 2, 3]); assert_eq!(offset, felts.len()); } @@ -92,13 +92,13 @@ mod tests { #[test] fn test_deserialize_several_values_with_unit() { let felts = vec![ - FieldElement::ZERO, - FieldElement::from(u32::MAX), - FieldElement::ZERO, - FieldElement::THREE, - FieldElement::ONE, - FieldElement::TWO, - FieldElement::THREE, + Felt::ZERO, + Felt::from(u32::MAX), + Felt::ZERO, + Felt::THREE, + Felt::ONE, + Felt::TWO, + Felt::THREE, ]; let mut offset = 0; @@ -106,8 +106,8 @@ mod tests { let o = Option::::cairo_deserialize(&felts, offset).unwrap(); offset += Option::::cairo_serialized_size(&o); - let r = Result::<(), FieldElement>::cairo_deserialize(&felts, offset).unwrap(); - offset += Result::<(), FieldElement>::cairo_serialized_size(&r); + let r = Result::<(), Felt>::cairo_deserialize(&felts, offset).unwrap(); + offset += Result::<(), Felt>::cairo_serialized_size(&r); let a = Vec::::cairo_deserialize(&felts, offset).unwrap(); offset += Vec::::cairo_serialized_size(&a); diff --git a/crates/cairo-serde/src/types/non_zero.rs b/crates/cairo-serde/src/types/non_zero.rs index c9b38d6..0c66d14 100644 --- a/crates/cairo-serde/src/types/non_zero.rs +++ b/crates/cairo-serde/src/types/non_zero.rs @@ -4,7 +4,7 @@ //! //! use crate::{CairoSerde, ContractAddress, Result, U256}; -use starknet::core::types::FieldElement; +use starknet::core::types::Felt; #[derive(Debug, PartialEq, PartialOrd, Clone, serde::Serialize, serde::Deserialize)] pub struct NonZero(T); @@ -47,11 +47,11 @@ where T::cairo_serialized_size(&rust.0) } - fn cairo_serialize(rust: &Self::RustType) -> Vec { + fn cairo_serialize(rust: &Self::RustType) -> Vec { T::cairo_serialize(&rust.0) } - fn cairo_deserialize(felts: &[FieldElement], offset: usize) -> Result { + fn cairo_deserialize(felts: &[Felt], offset: usize) -> Result { NonZero::new(T::cairo_deserialize(felts, offset)?).ok_or(crate::Error::ZeroedNonZero) } } @@ -89,15 +89,15 @@ impl Zeroable for U256 { } } -impl Zeroable for FieldElement { +impl Zeroable for Felt { fn is_zero(&self) -> bool { - *self == FieldElement::ZERO + *self == Felt::ZERO } } impl Zeroable for ContractAddress { fn is_zero(&self) -> bool { - self.0 == FieldElement::ZERO + self.0 == Felt::ZERO } } @@ -112,19 +112,19 @@ mod tests { let non_zero = NonZero(1_u32); let felts = NonZero::::cairo_serialize(&non_zero); assert_eq!(felts.len(), 1); - assert_eq!(felts[0], FieldElement::from(1_u32)); + assert_eq!(felts[0], Felt::from(1_u32)); } #[test] fn test_non_zero_cairo_deserialize() { - let felts = vec![FieldElement::from(1_u32)]; + let felts = vec![Felt::from(1_u32)]; let non_zero = NonZero::::cairo_deserialize(&felts, 0).unwrap(); assert_eq!(non_zero, NonZero(1_u32)) } #[test] fn test_non_zero_cairo_deserialize_zero() { - let felts = vec![FieldElement::ZERO, FieldElement::ZERO]; + let felts = vec![Felt::ZERO, Felt::ZERO]; let non_zero = NonZero::::cairo_deserialize(&felts, 0); match non_zero { Err(Error::ZeroedNonZero) => (), diff --git a/crates/cairo-serde/src/types/option.rs b/crates/cairo-serde/src/types/option.rs index b28b3f2..73d470f 100644 --- a/crates/cairo-serde/src/types/option.rs +++ b/crates/cairo-serde/src/types/option.rs @@ -5,7 +5,7 @@ //! //! use crate::{CairoSerde, Error, Result}; -use starknet::core::types::FieldElement; +use starknet::core::types::Felt; impl CairoSerde for Option where @@ -21,21 +21,21 @@ where } } - fn cairo_serialize(rust: &Self::RustType) -> Vec { + fn cairo_serialize(rust: &Self::RustType) -> Vec { let mut out = vec![]; match rust { Some(r) => { - out.push(FieldElement::ZERO); + out.push(Felt::ZERO); out.extend(T::cairo_serialize(r)); } - None => out.push(FieldElement::ONE), + None => out.push(Felt::ONE), }; out } - fn cairo_deserialize(felts: &[FieldElement], offset: usize) -> Result { + fn cairo_deserialize(felts: &[Felt], offset: usize) -> Result { if offset >= felts.len() { return Err(Error::Deserialize(format!( "Buffer too short to deserialize an Option: offset ({}) : buffer {:?}", @@ -45,10 +45,10 @@ where let idx = felts[offset]; - if idx == FieldElement::ZERO { + if idx == Felt::ZERO { // + 1 as the offset value is the index of the enum. Ok(Option::Some(T::cairo_deserialize(felts, offset + 1)?)) - } else if idx == FieldElement::ONE { + } else if idx == Felt::ONE { Ok(Option::None) } else { Err(Error::Deserialize( @@ -61,28 +61,24 @@ where #[cfg(test)] mod tests { use super::*; - use starknet::core::types::FieldElement; + use starknet::core::types::Felt; #[test] fn test_option_some_cairo_serialize() { let o = Some(u32::MAX); let felts = Option::::cairo_serialize(&o); assert_eq!(felts.len(), 2); - assert_eq!(felts[0], FieldElement::ZERO); - assert_eq!(felts[1], FieldElement::from(u32::MAX)); + assert_eq!(felts[0], Felt::ZERO); + assert_eq!(felts[1], Felt::from(u32::MAX)); } #[test] fn test_option_some_cairo_deserialize() { - let felts = vec![FieldElement::ZERO, FieldElement::from(u32::MAX)]; + let felts = vec![Felt::ZERO, Felt::from(u32::MAX)]; let o = Option::::cairo_deserialize(&felts, 0).unwrap(); assert_eq!(o, Some(u32::MAX)); - let felts = vec![ - FieldElement::THREE, - FieldElement::ZERO, - FieldElement::from(u32::MAX), - ]; + let felts = vec![Felt::THREE, Felt::ZERO, Felt::from(u32::MAX)]; let o = Option::::cairo_deserialize(&felts, 1).unwrap(); assert_eq!(o, Some(u32::MAX)); } @@ -92,12 +88,12 @@ mod tests { let o = Some(()); let felts = Option::<()>::cairo_serialize(&o); assert_eq!(felts.len(), 1); - assert_eq!(felts[0], FieldElement::ZERO); + assert_eq!(felts[0], Felt::ZERO); } #[test] fn test_option_some_unit_cairo_deserialize() { - let felts = vec![FieldElement::ZERO]; + let felts = vec![Felt::ZERO]; let o = Option::<()>::cairo_deserialize(&felts, 0).unwrap(); assert_eq!(o, Some(())); } @@ -107,29 +103,29 @@ mod tests { let o = Some(vec![u32::MAX, u32::MAX]); let felts = Option::>::cairo_serialize(&o); assert_eq!(felts.len(), 4); - assert_eq!(felts[0], FieldElement::ZERO); - assert_eq!(felts[1], FieldElement::from(2_u32)); - assert_eq!(felts[2], FieldElement::from(u32::MAX)); - assert_eq!(felts[3], FieldElement::from(u32::MAX)); + assert_eq!(felts[0], Felt::ZERO); + assert_eq!(felts[1], Felt::from(2_u32)); + assert_eq!(felts[2], Felt::from(u32::MAX)); + assert_eq!(felts[3], Felt::from(u32::MAX)); } #[test] fn test_option_some_array_cairo_deserialize() { let felts = vec![ - FieldElement::ZERO, - FieldElement::from(2_u32), - FieldElement::from(u32::MAX), - FieldElement::from(u32::MAX), + Felt::ZERO, + Felt::from(2_u32), + Felt::from(u32::MAX), + Felt::from(u32::MAX), ]; let o = Option::>::cairo_deserialize(&felts, 0).unwrap(); assert_eq!(o, Some(vec![u32::MAX, u32::MAX])); let felts = vec![ - FieldElement::THREE, - FieldElement::ZERO, - FieldElement::from(2_u32), - FieldElement::from(u32::MAX), - FieldElement::from(u32::MAX), + Felt::THREE, + Felt::ZERO, + Felt::from(2_u32), + Felt::from(u32::MAX), + Felt::from(u32::MAX), ]; let o = Option::>::cairo_deserialize(&felts, 1).unwrap(); assert_eq!(o, Some(vec![u32::MAX, u32::MAX])); @@ -140,16 +136,16 @@ mod tests { let o: Option = None; let felts = Option::::cairo_serialize(&o); assert_eq!(felts.len(), 1); - assert_eq!(felts[0], FieldElement::ONE); + assert_eq!(felts[0], Felt::ONE); } #[test] fn test_option_none_cairo_deserialize() { - let felts = vec![FieldElement::ONE]; + let felts = vec![Felt::ONE]; let o = Option::::cairo_deserialize(&felts, 0).unwrap(); assert_eq!(o, None); - let felts = vec![FieldElement::THREE, FieldElement::ONE]; + let felts = vec![Felt::THREE, Felt::ONE]; let o = Option::::cairo_deserialize(&felts, 1).unwrap(); assert_eq!(o, None); } diff --git a/crates/cairo-serde/src/types/result.rs b/crates/cairo-serde/src/types/result.rs index 90ae464..33a10ce 100644 --- a/crates/cairo-serde/src/types/result.rs +++ b/crates/cairo-serde/src/types/result.rs @@ -2,7 +2,7 @@ //! //! use crate::{CairoSerde, Error as CairoError, Result as CairoResult}; -use starknet::core::types::FieldElement; +use starknet::core::types::Felt; impl CairoSerde for Result where @@ -19,16 +19,16 @@ where } } - fn cairo_serialize(rust: &Self::RustType) -> Vec { + fn cairo_serialize(rust: &Self::RustType) -> Vec { let mut out = vec![]; match rust { Result::Ok(r) => { - out.push(FieldElement::ZERO); + out.push(Felt::ZERO); out.extend(T::cairo_serialize(r)); } Result::Err(e) => { - out.push(FieldElement::ONE); + out.push(Felt::ONE); out.extend(E::cairo_serialize(e)); } }; @@ -36,7 +36,7 @@ where out } - fn cairo_deserialize(felts: &[FieldElement], offset: usize) -> CairoResult { + fn cairo_deserialize(felts: &[Felt], offset: usize) -> CairoResult { if offset >= felts.len() { return Err(CairoError::Deserialize(format!( "Buffer too short to deserialize a Result: offset ({}) : buffer {:?}", @@ -46,10 +46,10 @@ where let idx = felts[offset]; - if idx == FieldElement::ZERO { + if idx == Felt::ZERO { // + 1 as the offset value is the index of the enum. CairoResult::Ok(Ok(T::cairo_deserialize(felts, offset + 1)?)) - } else if idx == FieldElement::ONE { + } else if idx == Felt::ONE { CairoResult::Ok(Err(E::cairo_deserialize(felts, offset + 1)?)) } else { Err(CairoError::Deserialize( @@ -62,52 +62,52 @@ where #[cfg(test)] mod tests { use super::*; - use starknet::core::types::FieldElement; + use starknet::core::types::Felt; #[test] fn test_result_ok_cairo_serialize() { let r = Ok(u32::MAX); - let felts = Result::::cairo_serialize(&r); + let felts = Result::::cairo_serialize(&r); assert_eq!(felts.len(), 2); - assert_eq!(felts[0], FieldElement::ZERO); - assert_eq!(felts[1], FieldElement::from(u32::MAX)); + assert_eq!(felts[0], Felt::ZERO); + assert_eq!(felts[1], Felt::from(u32::MAX)); } #[test] fn test_result_ok_cairo_deserialize() { - let felts = vec![FieldElement::ZERO, FieldElement::from(u32::MAX)]; - let r = Result::::cairo_deserialize(&felts, 0).unwrap(); + let felts = vec![Felt::ZERO, Felt::from(u32::MAX)]; + let r = Result::::cairo_deserialize(&felts, 0).unwrap(); assert_eq!(r, Ok(u32::MAX)); } #[test] fn test_result_ok_unit_cairo_serialize() { let r = Ok(()); - let felts = Result::<(), FieldElement>::cairo_serialize(&r); + let felts = Result::<(), Felt>::cairo_serialize(&r); assert_eq!(felts.len(), 1); - assert_eq!(felts[0], FieldElement::ZERO); + assert_eq!(felts[0], Felt::ZERO); } #[test] fn test_result_ok_unit_cairo_deserialize() { - let felts = vec![FieldElement::ZERO]; - let r = Result::<(), FieldElement>::cairo_deserialize(&felts, 0).unwrap(); + let felts = vec![Felt::ZERO]; + let r = Result::<(), Felt>::cairo_deserialize(&felts, 0).unwrap(); assert_eq!(r, Ok(())); } #[test] fn test_result_err_cairo_serialize() { - let r = Err(FieldElement::ONE); - let felts = Result::::cairo_serialize(&r); + let r = Err(Felt::ONE); + let felts = Result::::cairo_serialize(&r); assert_eq!(felts.len(), 2); - assert_eq!(felts[0], FieldElement::ONE); - assert_eq!(felts[1], FieldElement::ONE); + assert_eq!(felts[0], Felt::ONE); + assert_eq!(felts[1], Felt::ONE); } #[test] fn test_result_err_cairo_deserialize() { - let felts = vec![FieldElement::ONE, FieldElement::ONE]; - let r = Result::::cairo_deserialize(&felts, 0).unwrap(); - assert_eq!(r, Err(FieldElement::ONE)); + let felts = vec![Felt::ONE, Felt::ONE]; + let r = Result::::cairo_deserialize(&felts, 0).unwrap(); + assert_eq!(r, Err(Felt::ONE)); } } diff --git a/crates/cairo-serde/src/types/starknet.rs b/crates/cairo-serde/src/types/starknet.rs index aab2882..269bd8f 100644 --- a/crates/cairo-serde/src/types/starknet.rs +++ b/crates/cairo-serde/src/types/starknet.rs @@ -1,20 +1,20 @@ //! CairoSerde implementation for starknet types. //! -//! They are alf `FieldElement` under the hood. +//! They are alf `Felt` under the hood. use crate::{CairoSerde, Error, Result}; -use starknet::core::types::FieldElement; +use starknet::core::types::Felt; /// ContractAddress. #[derive(Debug, Copy, Clone, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)] -pub struct ContractAddress(pub FieldElement); +pub struct ContractAddress(pub Felt); -impl From for ContractAddress { - fn from(item: FieldElement) -> Self { +impl From for ContractAddress { + fn from(item: Felt) -> Self { Self(item) } } -impl From for FieldElement { +impl From for Felt { fn from(item: ContractAddress) -> Self { item.0 } @@ -23,11 +23,11 @@ impl From for FieldElement { impl CairoSerde for ContractAddress { type RustType = Self; - fn cairo_serialize(rust: &Self::RustType) -> Vec { - FieldElement::cairo_serialize(&rust.0) + fn cairo_serialize(rust: &Self::RustType) -> Vec { + Felt::cairo_serialize(&rust.0) } - fn cairo_deserialize(felts: &[FieldElement], offset: usize) -> Result { + fn cairo_deserialize(felts: &[Felt], offset: usize) -> Result { if offset >= felts.len() { return Err(Error::Deserialize(format!( "Buffer too short to deserialize a ContractAddress: offset ({}) : buffer {:?}", @@ -35,23 +35,21 @@ impl CairoSerde for ContractAddress { ))); } - Ok(ContractAddress(FieldElement::cairo_deserialize( - felts, offset, - )?)) + Ok(ContractAddress(Felt::cairo_deserialize(felts, offset)?)) } } /// ClassHash. #[derive(Debug, Copy, Clone, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)] -pub struct ClassHash(pub FieldElement); +pub struct ClassHash(pub Felt); -impl From for ClassHash { - fn from(item: FieldElement) -> Self { +impl From for ClassHash { + fn from(item: Felt) -> Self { Self(item) } } -impl From for FieldElement { +impl From for Felt { fn from(item: ClassHash) -> Self { item.0 } @@ -60,11 +58,11 @@ impl From for FieldElement { impl CairoSerde for ClassHash { type RustType = Self; - fn cairo_serialize(rust: &Self::RustType) -> Vec { - FieldElement::cairo_serialize(&rust.0) + fn cairo_serialize(rust: &Self::RustType) -> Vec { + Felt::cairo_serialize(&rust.0) } - fn cairo_deserialize(felts: &[FieldElement], offset: usize) -> Result { + fn cairo_deserialize(felts: &[Felt], offset: usize) -> Result { if offset >= felts.len() { return Err(Error::Deserialize(format!( "Buffer too short to deserialize a ClassHash: offset ({}) : buffer {:?}", @@ -72,21 +70,21 @@ impl CairoSerde for ClassHash { ))); } - Ok(ClassHash(FieldElement::cairo_deserialize(felts, offset)?)) + Ok(ClassHash(Felt::cairo_deserialize(felts, offset)?)) } } /// EthAddress. #[derive(Debug, Copy, Clone, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)] -pub struct EthAddress(pub FieldElement); +pub struct EthAddress(pub Felt); -impl From for EthAddress { - fn from(item: FieldElement) -> Self { +impl From for EthAddress { + fn from(item: Felt) -> Self { Self(item) } } -impl From for FieldElement { +impl From for Felt { fn from(item: EthAddress) -> Self { item.0 } @@ -95,11 +93,11 @@ impl From for FieldElement { impl CairoSerde for EthAddress { type RustType = Self; - fn cairo_serialize(rust: &Self::RustType) -> Vec { - FieldElement::cairo_serialize(&rust.0) + fn cairo_serialize(rust: &Self::RustType) -> Vec { + Felt::cairo_serialize(&rust.0) } - fn cairo_deserialize(felts: &[FieldElement], offset: usize) -> Result { + fn cairo_deserialize(felts: &[Felt], offset: usize) -> Result { if offset >= felts.len() { return Err(Error::Deserialize(format!( "Buffer too short to deserialize an EthAddress: offset ({}) : buffer {:?}", @@ -107,7 +105,7 @@ impl CairoSerde for EthAddress { ))); } - Ok(EthAddress(FieldElement::cairo_deserialize(felts, offset)?)) + Ok(EthAddress(Felt::cairo_deserialize(felts, offset)?)) } } @@ -117,64 +115,64 @@ mod tests { #[test] fn test_contract_address_cairo_serialize() { - let contract_address = ContractAddress(FieldElement::from(1_u32)); + let contract_address = ContractAddress(Felt::from(1_u32)); let felts = ContractAddress::cairo_serialize(&contract_address); assert_eq!(felts.len(), 1); - assert_eq!(felts[0], FieldElement::from(1_u32)); + assert_eq!(felts[0], Felt::from(1_u32)); } #[test] fn test_contract_address_cairo_deserialize() { - let felts = vec![FieldElement::from(1_u32)]; + let felts = vec![Felt::from(1_u32)]; let contract_address = ContractAddress::cairo_deserialize(&felts, 0).unwrap(); - assert_eq!(contract_address, ContractAddress(FieldElement::from(1_u32))) + assert_eq!(contract_address, ContractAddress(Felt::from(1_u32))) } #[test] fn test_class_hash_cairo_serialize() { - let class_hash = ClassHash(FieldElement::from(1_u32)); + let class_hash = ClassHash(Felt::from(1_u32)); let felts = ClassHash::cairo_serialize(&class_hash); assert_eq!(felts.len(), 1); - assert_eq!(felts[0], FieldElement::from(1_u32)); + assert_eq!(felts[0], Felt::from(1_u32)); } #[test] fn test_class_hash_cairo_deserialize() { - let felts = vec![FieldElement::from(1_u32)]; + let felts = vec![Felt::from(1_u32)]; let class_hash = ClassHash::cairo_deserialize(&felts, 0).unwrap(); - assert_eq!(class_hash, ClassHash(FieldElement::from(1_u32))) + assert_eq!(class_hash, ClassHash(Felt::from(1_u32))) } #[test] fn test_eth_address_cairo_serialize() { - let eth_address = EthAddress(FieldElement::from(1_u32)); + let eth_address = EthAddress(Felt::from(1_u32)); let felts = EthAddress::cairo_serialize(ð_address); assert_eq!(felts.len(), 1); - assert_eq!(felts[0], FieldElement::from(1_u32)); + assert_eq!(felts[0], Felt::from(1_u32)); } #[test] fn test_eth_address_cairo_deserialize() { - let felts = vec![FieldElement::from(1_u32)]; + let felts = vec![Felt::from(1_u32)]; let eth_address = EthAddress::cairo_deserialize(&felts, 0).unwrap(); - assert_eq!(eth_address, EthAddress(FieldElement::from(1_u32))) + assert_eq!(eth_address, EthAddress(Felt::from(1_u32))) } #[test] fn test_contract_address_from() { - let contract_address = ContractAddress::from(FieldElement::from(1_u32)); - assert_eq!(contract_address, ContractAddress(FieldElement::from(1_u32))) + let contract_address = ContractAddress::from(Felt::from(1_u32)); + assert_eq!(contract_address, ContractAddress(Felt::from(1_u32))) } #[test] fn test_class_hash_from() { - let class_hash = ClassHash::from(FieldElement::from(1_u32)); - assert_eq!(class_hash, ClassHash(FieldElement::from(1_u32))) + let class_hash = ClassHash::from(Felt::from(1_u32)); + assert_eq!(class_hash, ClassHash(Felt::from(1_u32))) } #[test] fn test_eth_address_from() { - let eth_address = EthAddress::from(FieldElement::from(1_u32)); - assert_eq!(eth_address, EthAddress(FieldElement::from(1_u32))) + let eth_address = EthAddress::from(Felt::from(1_u32)); + assert_eq!(eth_address, EthAddress(Felt::from(1_u32))) } } diff --git a/crates/cairo-serde/src/types/tuple.rs b/crates/cairo-serde/src/types/tuple.rs index dd4eb9b..482a4e1 100644 --- a/crates/cairo-serde/src/types/tuple.rs +++ b/crates/cairo-serde/src/types/tuple.rs @@ -1,6 +1,6 @@ //! CairoSerde implementation for tuples. use crate::{CairoSerde, Result}; -use starknet::core::types::FieldElement; +use starknet::core::types::Felt; impl CairoSerde for () { type RustType = Self; @@ -10,11 +10,11 @@ impl CairoSerde for () { 0 } - fn cairo_serialize(_rust: &Self::RustType) -> Vec { + fn cairo_serialize(_rust: &Self::RustType) -> Vec { vec![] } - fn cairo_deserialize(_felts: &[FieldElement], _offset: usize) -> Result { + fn cairo_deserialize(_felts: &[Felt], _offset: usize) -> Result { Ok(()) } } @@ -39,15 +39,15 @@ macro_rules! impl_tuples { size } - fn cairo_serialize(rust: &Self::RustType) -> Vec { - let mut out: Vec = vec![]; + fn cairo_serialize(rust: &Self::RustType) -> Vec { + let mut out: Vec = vec![]; $( out.extend($ty::cairo_serialize(& rust.$no)); )* out } - fn cairo_deserialize(felts: &[FieldElement], offset: usize) -> Result { + fn cairo_deserialize(felts: &[Felt], offset: usize) -> Result { let mut offset = offset; $( @@ -71,40 +71,42 @@ impl_tuples!(5, A:RA:r0:0, B:RB:r1:1, C:RC:r2:2, D:RD:r3:3, E:RE:r4:4); #[cfg(test)] mod tests { + use starknet::core::types::Felt; + use super::*; #[test] fn test_serialize_tuple2() { - let v = (FieldElement::ONE, 128_u32); - let felts = <(FieldElement, u32)>::cairo_serialize(&v); + let v = (Felt::ONE, 128_u32); + let felts = <(Felt, u32)>::cairo_serialize(&v); assert_eq!(felts.len(), 2); - assert_eq!(felts[0], FieldElement::ONE); - assert_eq!(felts[1], FieldElement::from(128_u32)); + assert_eq!(felts[0], Felt::ONE); + assert_eq!(felts[1], Felt::from(128_u32)); } #[test] fn test_deserialize_tuple2() { - let felts = vec![FieldElement::THREE, 99_u32.into()]; - let vals = <(FieldElement, u32)>::cairo_deserialize(&felts, 0).unwrap(); - assert_eq!(vals.0, FieldElement::THREE); + let felts = vec![Felt::THREE, 99_u32.into()]; + let vals = <(Felt, u32)>::cairo_deserialize(&felts, 0).unwrap(); + assert_eq!(vals.0, Felt::THREE); assert_eq!(vals.1, 99_u32); } #[test] fn test_serialize_tuple2_array() { - let v = (vec![FieldElement::ONE], 128_u32); - let felts = <(Vec, u32)>::cairo_serialize(&v); + let v = (vec![Felt::ONE], 128_u32); + let felts = <(Vec, u32)>::cairo_serialize(&v); assert_eq!(felts.len(), 3); - assert_eq!(felts[0], FieldElement::ONE); - assert_eq!(felts[1], FieldElement::ONE); - assert_eq!(felts[2], FieldElement::from(128_u32)); + assert_eq!(felts[0], Felt::ONE); + assert_eq!(felts[1], Felt::ONE); + assert_eq!(felts[2], Felt::from(128_u32)); } #[test] fn test_deserialize_tuple2_array() { - let felts = vec![FieldElement::ONE, FieldElement::ONE, 99_u32.into()]; - let vals = <(Vec, u32)>::cairo_deserialize(&felts, 0).unwrap(); - assert_eq!(vals.0, vec![FieldElement::ONE]); + let felts = vec![Felt::ONE, Felt::ONE, 99_u32.into()]; + let vals = <(Vec, u32)>::cairo_deserialize(&felts, 0).unwrap(); + assert_eq!(vals.0, vec![Felt::ONE]); assert_eq!(vals.1, 99_u32); } } diff --git a/crates/cairo-serde/src/types/u256.rs b/crates/cairo-serde/src/types/u256.rs index ab2ce35..b8a560a 100644 --- a/crates/cairo-serde/src/types/u256.rs +++ b/crates/cairo-serde/src/types/u256.rs @@ -1,5 +1,5 @@ use crate::CairoSerde; -use starknet::core::types::{FieldElement, ValueOutOfRangeError}; +use starknet::core::types::{Felt, ValueOutOfRangeError}; use std::cmp::Ordering; #[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)] @@ -27,23 +27,23 @@ impl CairoSerde for U256 { fn cairo_serialized_size(this: &U256) -> usize { u128::cairo_serialized_size(&this.low) + u128::cairo_serialized_size(&this.high) } - fn cairo_serialize(this: &U256) -> Vec { + fn cairo_serialize(this: &U256) -> Vec { [ u128::cairo_serialize(&this.low), u128::cairo_serialize(&this.high), ] .concat() } - fn cairo_deserialize(felts: &[FieldElement], offset: usize) -> Result { + fn cairo_deserialize(felts: &[Felt], offset: usize) -> Result { let low = u128::cairo_deserialize(felts, offset)?; let high = u128::cairo_deserialize(felts, offset + u128::cairo_serialized_size(&low))?; Ok(U256 { low, high }) } } -/// FieldElement to U256 conversion as if the tuple was a cairo serialized U256 -impl TryFrom<(FieldElement, FieldElement)> for U256 { +/// Felt to U256 conversion as if the tuple was a cairo serialized U256 +impl TryFrom<(Felt, Felt)> for U256 { type Error = ValueOutOfRangeError; - fn try_from((a, b): (FieldElement, FieldElement)) -> Result { + fn try_from((a, b): (Felt, Felt)) -> Result { let U256 { low: a_low, high: a_high, @@ -90,7 +90,6 @@ impl U256 { #[cfg(test)] mod tests { use super::*; - use starknet::core::types::FieldElement; #[test] fn test_serialize_u256() { @@ -98,8 +97,8 @@ mod tests { let high = 8_u128; let felts = U256::cairo_serialize(&U256 { low, high }); assert_eq!(felts.len(), 2); - assert_eq!(felts[0], FieldElement::from(9_u128)); - assert_eq!(felts[1], FieldElement::from(8_u128)); + assert_eq!(felts[0], Felt::from(9_u128)); + assert_eq!(felts[1], Felt::from(8_u128)); } #[test] @@ -108,8 +107,8 @@ mod tests { let high = u128::MAX; let felts = U256::cairo_serialize(&U256 { low, high }); assert_eq!(felts.len(), 2); - assert_eq!(felts[0], FieldElement::from(u128::MAX)); - assert_eq!(felts[1], FieldElement::from(u128::MAX)); + assert_eq!(felts[0], Felt::from(u128::MAX)); + assert_eq!(felts[1], Felt::from(u128::MAX)); } #[test] @@ -118,13 +117,13 @@ mod tests { let high = u128::MIN; let felts = U256::cairo_serialize(&U256 { low, high }); assert_eq!(felts.len(), 2); - assert_eq!(felts[0], FieldElement::from(u128::MIN)); - assert_eq!(felts[1], FieldElement::from(u128::MIN)); + assert_eq!(felts[0], Felt::from(u128::MIN)); + assert_eq!(felts[1], Felt::from(u128::MIN)); } #[test] fn test_deserialize_u256() { - let felts = vec![FieldElement::from(9_u128), FieldElement::from(8_u128)]; + let felts = vec![Felt::from(9_u128), Felt::from(8_u128)]; let num_u256 = U256::cairo_deserialize(&felts, 0).unwrap(); assert_eq!(num_u256.low, 9_u128); assert_eq!(num_u256.high, 8_u128); @@ -191,7 +190,7 @@ mod tests { #[test] fn test_from_field_element() { - let felts = (FieldElement::from(9_u128), FieldElement::from(8_u128)); + let felts = (Felt::from(9_u128), Felt::from(8_u128)); let u256 = U256::try_from(felts).unwrap(); assert_eq!(u256.low, 9_u128); assert_eq!(u256.high, 8_u128);