-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented representations of Cairo
u256
and u512
commit-id:fad6001c
- Loading branch information
1 parent
b3eee6e
commit 67f3995
Showing
8 changed files
with
184 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use crate as conversions; // Must be imported because of derive macros | ||
use cairo_serde_macros::{CairoDeserialize, CairoSerialize}; | ||
use starknet::core::types::Felt; | ||
use starknet_types_core::felt::FromStrError; | ||
use std::str::FromStr; | ||
|
||
#[derive(CairoDeserialize, CairoSerialize, Debug, Clone, Copy, PartialEq, Eq)] | ||
pub struct CairoBytes31 { | ||
inner: Felt, | ||
} | ||
|
||
impl CairoBytes31 { | ||
pub const MAX: CairoBytes31 = CairoBytes31 { | ||
inner: Felt::from_hex_unchecked( | ||
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
), | ||
}; | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] | ||
pub enum ParseBytes31Error { | ||
#[error("Failed to parse as Cairo type")] | ||
CairoFromStrError, // Error thrown by Cairo is useless. We cannot write anything beyond that | ||
#[error("Number is too large to fit in 31 bytes")] | ||
Overflow, | ||
} | ||
|
||
impl From<FromStrError> for ParseBytes31Error { | ||
fn from(_value: FromStrError) -> Self { | ||
ParseBytes31Error::CairoFromStrError | ||
} | ||
} | ||
|
||
impl FromStr for CairoBytes31 { | ||
type Err = ParseBytes31Error; | ||
|
||
fn from_str(input: &str) -> Result<Self, Self::Err> { | ||
let inner = input.parse::<Felt>()?; | ||
|
||
if inner > CairoBytes31::MAX.inner { | ||
return Err(ParseBytes31Error::Overflow); | ||
} | ||
|
||
Ok(CairoBytes31 { inner }) | ||
} | ||
} | ||
|
||
impl From<CairoBytes31> for Felt { | ||
fn from(value: CairoBytes31) -> Self { | ||
value.inner | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use crate as conversions; // Must be imported because of derive macros | ||
use cairo_serde_macros::{CairoDeserialize, CairoSerialize}; | ||
use num_bigint::{BigUint, ParseBigIntError}; | ||
use std::str::FromStr; | ||
|
||
#[derive(CairoDeserialize, CairoSerialize, Clone, Copy, Debug, PartialEq, Eq)] | ||
pub struct CairoU256 { | ||
low: u128, | ||
high: u128, | ||
} | ||
|
||
impl CairoU256 { | ||
#[must_use] | ||
pub fn from_bytes(bytes: &[u8]) -> Self { | ||
// Takes slice without explicit size because of cheatnet's specific usages | ||
Self { | ||
low: u128::from_be_bytes(bytes[16..32].try_into().unwrap()), | ||
high: u128::from_be_bytes(bytes[0..16].try_into().unwrap()), | ||
} | ||
} | ||
|
||
#[must_use] | ||
pub fn to_be_bytes(&self) -> [u8; 32] { | ||
let mut result = [0; 32]; | ||
|
||
result[16..].copy_from_slice(&self.low.to_be_bytes()); | ||
result[..16].copy_from_slice(&self.high.to_be_bytes()); | ||
|
||
result | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] | ||
pub enum ParseCairoU256Error { | ||
#[error(transparent)] | ||
InvalidString(#[from] ParseBigIntError), | ||
#[error("Number is too large to fit in 32 bytes")] | ||
Overflow, | ||
} | ||
|
||
impl FromStr for CairoU256 { | ||
type Err = ParseCairoU256Error; | ||
|
||
fn from_str(input: &str) -> Result<Self, Self::Err> { | ||
let bytes = input.parse::<BigUint>()?.to_bytes_be(); | ||
|
||
if bytes.len() > 32 { | ||
return Err(ParseCairoU256Error::Overflow); | ||
} | ||
|
||
let mut result = [0u8; 32]; | ||
let start = 32 - bytes.len(); | ||
result[start..].copy_from_slice(&bytes); | ||
|
||
Ok(CairoU256::from_bytes(&result)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use crate as conversions; // Must be imported because of derive macros | ||
use cairo_serde_macros::{CairoDeserialize, CairoSerialize}; | ||
use num_bigint::{BigUint, ParseBigIntError}; | ||
use std::str::FromStr; | ||
|
||
#[derive(CairoDeserialize, CairoSerialize, Clone, Copy, Debug, PartialEq, Eq)] | ||
pub struct CairoU512 { | ||
limb_0: u128, | ||
limb_1: u128, | ||
limb_2: u128, | ||
limb_3: u128, | ||
} | ||
|
||
impl CairoU512 { | ||
#[must_use] | ||
pub fn from_bytes(bytes: &[u8; 64]) -> Self { | ||
Self { | ||
limb_0: u128::from_be_bytes(bytes[48..64].try_into().unwrap()), | ||
limb_1: u128::from_be_bytes(bytes[32..48].try_into().unwrap()), | ||
limb_2: u128::from_be_bytes(bytes[16..32].try_into().unwrap()), | ||
limb_3: u128::from_be_bytes(bytes[00..16].try_into().unwrap()), | ||
} | ||
} | ||
|
||
#[must_use] | ||
pub fn to_be_bytes(&self) -> [u8; 64] { | ||
let mut result = [0; 64]; | ||
|
||
result[48..64].copy_from_slice(&self.limb_0.to_be_bytes()); | ||
result[32..48].copy_from_slice(&self.limb_1.to_be_bytes()); | ||
result[16..32].copy_from_slice(&self.limb_2.to_be_bytes()); | ||
result[00..16].copy_from_slice(&self.limb_3.to_be_bytes()); | ||
|
||
result | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] | ||
pub enum ParseCairoU512Error { | ||
#[error(transparent)] | ||
InvalidString(#[from] ParseBigIntError), | ||
#[error("Number is too large to fit in 64 bytes")] | ||
Overflow, | ||
} | ||
|
||
impl FromStr for CairoU512 { | ||
type Err = ParseCairoU512Error; | ||
|
||
fn from_str(input: &str) -> Result<Self, Self::Err> { | ||
let bytes = input.parse::<BigUint>()?.to_bytes_be(); | ||
|
||
if bytes.len() > 64 { | ||
return Err(ParseCairoU512Error::Overflow); | ||
} | ||
|
||
let mut result = [0u8; 64]; | ||
let start = 64 - bytes.len(); | ||
result[start..].copy_from_slice(&bytes); | ||
|
||
Ok(CairoU512::from_bytes(&result)) | ||
} | ||
} |