Skip to content

Commit

Permalink
Rename constant and remove informal description
Browse files Browse the repository at this point in the history
  • Loading branch information
Oppen committed Nov 21, 2023
1 parent 4b10fa6 commit adc5424
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions crates/starknet-types-core/src/felt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ impl Felt {
/// from them, but the conversion is performed in constant space on the stack.
pub fn from_bytes_be_slice(bytes: &[u8]) -> Self {
// NB: lambdaworks ignores the remaining bytes when len > 32, so we loop
// multiplying by OVERFLOW. This is equivalent to factorizing as we did
// in primary school, so `xyz` is `x * 100 + y * 10 + z * 1`, just using
// base `2^256` to build from `[u8; 32]` digits.
const OVERFLOW: Felt = Self(FieldElement::<Stark252PrimeField>::const_from_raw(
// multiplying by BASE, effectively decomposing in base 2^256 to build
// digits with a length of 32 bytes.
const BASE: Felt = Self(FieldElement::<Stark252PrimeField>::const_from_raw(
UnsignedInteger::from_limbs([
576413109808302096,
18446744073700081664,
Expand All @@ -97,7 +96,7 @@ impl Felt {
]),
));
// Sanity check; gets removed in release builds.
debug_assert_eq!(OVERFLOW, Felt::TWO.pow(256u32));
debug_assert_eq!(BASE, Felt::TWO.pow(256u32));

let mut factor = Self::ONE;
let mut res = Self::ZERO;
Expand All @@ -108,7 +107,7 @@ impl Felt {
let digit =
Self::from_bytes_be(&chunk.try_into().expect("conversion to same-sized array"));
res += digit * factor;
factor *= OVERFLOW;
factor *= BASE;
}

if remainder.is_empty() {
Expand All @@ -129,10 +128,9 @@ impl Felt {
/// from them, but the conversion is performed in constant space on the stack.
pub fn from_bytes_le_slice(bytes: &[u8]) -> Self {
// NB: lambdaworks ignores the remaining bytes when len > 32, so we loop
// multiplying by OVERFLOW. This is equivalent to factorizing as we did
// in primary school, so `xyz` is `x * 100 + y * 10 + z * 1`, just using
// base `2^256` to build from `[u8; 32]` digits.
const OVERFLOW: Felt = Self(FieldElement::<Stark252PrimeField>::const_from_raw(
// multiplying by BASE, effectively decomposing in base 2^256 to build
// digits with a length of 32 bytes.
const BASE: Felt = Self(FieldElement::<Stark252PrimeField>::const_from_raw(
UnsignedInteger::from_limbs([
576413109808302096,
18446744073700081664,
Expand All @@ -141,7 +139,7 @@ impl Felt {
]),
));
// Sanity check; gets removed in release builds.
debug_assert_eq!(OVERFLOW, Felt::TWO.pow(256u32));
debug_assert_eq!(BASE, Felt::TWO.pow(256u32));

let mut factor = Self::ONE;
let mut res = Self::ZERO;
Expand All @@ -152,7 +150,7 @@ impl Felt {
let digit =
Self::from_bytes_le(&chunk.try_into().expect("conversion to same-sized array"));
res += digit * factor;
factor *= OVERFLOW;
factor *= BASE;
}

if remainder.is_empty() {
Expand Down

0 comments on commit adc5424

Please sign in to comment.