-
Notifications
You must be signed in to change notification settings - Fork 157
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
9d9f2ab
commit 07ad778
Showing
7 changed files
with
130 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
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,56 @@ | ||
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, Debug)] | ||
pub struct CairoU256 { | ||
low: u128, | ||
high: u128, | ||
} | ||
|
||
impl CairoU256 { | ||
#[must_use] | ||
pub fn from_bytes(bytes: &[u8]) -> Self { | ||
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, Debug)] | ||
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]) -> 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)) | ||
} | ||
} |