Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Felt to Felt252 #899

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
370 changes: 185 additions & 185 deletions felt/src/lib.rs

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/cairo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
};

use bincode::enc::write::Writer;
use felt::Felt;
use felt::Felt252;

#[cfg(feature = "std")]
use thiserror::Error;
Expand Down Expand Up @@ -115,7 +115,7 @@ pub fn write_encoded_trace(
/// * address -> 8-byte encoded
/// * value -> 32-byte encoded
pub fn write_encoded_memory(
relocated_memory: &[Option<Felt>],
relocated_memory: &[Option<Felt252>],
dest: &mut impl Writer,
) -> Result<(), EncodeTraceError> {
// initialize bytes vector that will be dumped to file
Expand Down Expand Up @@ -161,7 +161,7 @@ mod tests {
utils::test_utils::*,
};
use bincode::enc::write::SliceWriter;
use felt::Felt;
use felt::Felt252;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;
Expand Down Expand Up @@ -203,7 +203,7 @@ mod tests {
assert!(cairo_runner.relocate(&mut vm).is_ok());
// `main` returns without doing nothing, but `not_main` sets `[ap]` to `1`
// Memory location was found empirically and simply hardcoded
assert_eq!(cairo_runner.relocated_memory[2], Some(Felt::new(123)));
assert_eq!(cairo_runner.relocated_memory[2], Some(Felt252::new(123)));
}

#[test]
Expand Down
22 changes: 11 additions & 11 deletions src/hint_processor/builtin_hint_processor/blake2s_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ use crate::{
types::relocatable::{MaybeRelocatable, Relocatable},
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
};
use felt::Felt;
use felt::Felt252;
use num_traits::ToPrimitive;

fn get_fixed_size_u32_array<const T: usize>(
h_range: &Vec<Cow<Felt>>,
h_range: &Vec<Cow<Felt252>>,
) -> Result<[u32; T], HintError> {
let mut u32_vec = Vec::<u32>::with_capacity(h_range.len());
for num in h_range {
Expand All @@ -31,12 +31,12 @@ fn get_fixed_size_u32_array<const T: usize>(
fn get_maybe_relocatable_array_from_u32(array: &Vec<u32>) -> Vec<MaybeRelocatable> {
let mut new_array = Vec::<MaybeRelocatable>::with_capacity(array.len());
for element in array {
new_array.push(MaybeRelocatable::from(Felt::new(*element)));
new_array.push(MaybeRelocatable::from(Felt252::new(*element)));
}
new_array
}

fn get_maybe_relocatable_array_from_felt(array: &[Felt]) -> Vec<MaybeRelocatable> {
fn get_maybe_relocatable_array_from_felt(array: &[Felt252]) -> Vec<MaybeRelocatable> {
array.iter().map(MaybeRelocatable::from).collect()
}
/*Helper function for the Cairo blake2s() implementation.
Expand Down Expand Up @@ -139,17 +139,17 @@ pub fn blake2s_add_uint256(
const MASK: u32 = u32::MAX;
const B: u32 = 32;
//Convert MASK to felt
let mask = Felt::new(MASK);
let mask = Felt252::new(MASK);
//Build first batch of data
let mut inner_data = Vec::<Felt>::new();
let mut inner_data = Vec::<Felt252>::new();
for i in 0..4 {
inner_data.push((&low >> (B * i)) & &mask);
}
//Insert first batch of data
let data = get_maybe_relocatable_array_from_felt(&inner_data);
vm.load_data(data_ptr, &data).map_err(HintError::Memory)?;
//Build second batch of data
let mut inner_data = Vec::<Felt>::new();
let mut inner_data = Vec::<Felt252>::new();
for i in 0..4 {
inner_data.push((&high >> (B * i)) & &mask);
}
Expand Down Expand Up @@ -182,17 +182,17 @@ pub fn blake2s_add_uint256_bigend(
const MASK: u32 = u32::MAX;
const B: u32 = 32;
//Convert MASK to felt
let mask = Felt::new(MASK);
let mask = Felt252::new(MASK);
//Build first batch of data
let mut inner_data = Vec::<Felt>::new();
let mut inner_data = Vec::<Felt252>::new();
for i in 0..4 {
inner_data.push((&high >> (B * (3 - i))) & &mask);
}
//Insert first batch of data
let data = get_maybe_relocatable_array_from_felt(&inner_data);
vm.load_data(data_ptr, &data).map_err(HintError::Memory)?;
//Build second batch of data
let mut inner_data = Vec::<Felt>::new();
let mut inner_data = Vec::<Felt252>::new();
for i in 0..4 {
inner_data.push((&low >> (B * (3 - i))) & &mask);
}
Expand Down Expand Up @@ -409,7 +409,7 @@ mod tests {
)
)) if x == MaybeRelocatable::from((2, 0)) &&
y == MaybeRelocatable::from((2, 0)) &&
z == MaybeRelocatable::from(Felt::new(1795745351))
z == MaybeRelocatable::from(Felt252::new(1795745351))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ use crate::{
types::exec_scope::ExecutionScopes,
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
};
use felt::Felt;
use felt::Felt252;

#[cfg(feature = "skip_next_instruction_hint")]
use crate::hint_processor::builtin_hint_processor::skip_next_instruction::skip_next_instruction;
Expand Down Expand Up @@ -90,7 +90,7 @@ pub struct HintFunc(
&mut ExecutionScopes,
&HashMap<String, HintReference>,
&ApTracking,
&HashMap<String, Felt>,
&HashMap<String, Felt252>,
) -> Result<(), HintError>
+ Sync,
>,
Expand Down Expand Up @@ -120,7 +120,7 @@ impl HintProcessor for BuiltinHintProcessor {
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
hint_data: &Box<dyn Any>,
constants: &HashMap<String, Felt>,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let hint_data = hint_data
.downcast_ref::<HintProcessorData>()
Expand Down Expand Up @@ -579,7 +579,7 @@ mod tests {
// initialize fp
vm.run_context.fp = 2;
// initialize vm scope with variable `n`
let mut exec_scopes = scope![("n", Felt::one())];
let mut exec_scopes = scope![("n", Felt252::one())];
// initialize ids.continue_copying
// we create a memory gap so that there is None in (1, 0), the actual addr of continue_copying
vm.segments = segments![((1, 2), 5)];
Expand Down Expand Up @@ -616,7 +616,7 @@ mod tests {
// initialize fp
vm.run_context.fp = 2;
// initialize with variable `n`
let mut exec_scopes = scope![("n", Felt::one())];
let mut exec_scopes = scope![("n", Felt252::one())];
// initialize ids.continue_copying
// a value is written in the address so the hint cant insert value there
vm.segments = segments![((1, 1), 5)];
Expand All @@ -631,8 +631,8 @@ mod tests {
z
)
)) if x == MaybeRelocatable::from((1, 1)) &&
y == MaybeRelocatable::from(Felt::new(5)) &&
z == MaybeRelocatable::from(Felt::zero())
y == MaybeRelocatable::from(Felt252::new(5)) &&
z == MaybeRelocatable::from(Felt252::zero())
);
}

Expand All @@ -643,7 +643,7 @@ mod tests {
let mut vm = vm!();
// Create new vm scope with dummy variable
let mut exec_scopes = ExecutionScopes::new();
let a_value: Box<dyn Any> = Box::new(Felt::one());
let a_value: Box<dyn Any> = Box::new(Felt252::one());
exec_scopes.enter_scope(HashMap::from([(String::from("a"), a_value)]));
// Initialize memory segments
add_segments!(vm, 1);
Expand Down Expand Up @@ -703,7 +703,7 @@ mod tests {
((1, 5), 0)
];
let ids_data = ids_data!["length", "data", "high", "low"];
let mut exec_scopes = scope![("__keccak_max_size", Felt::new(500))];
let mut exec_scopes = scope![("__keccak_max_size", Felt252::new(500))];
assert!(run_hint!(vm, ids_data, hint_code, &mut exec_scopes).is_ok());
}

Expand All @@ -725,10 +725,10 @@ mod tests {
((1, 2), (2, 0))
];
let ids_data = ids_data!["length", "data", "high", "low"];
let mut exec_scopes = scope![("__keccak_max_size", Felt::new(2))];
let mut exec_scopes = scope![("__keccak_max_size", Felt252::new(2))];
assert_matches!(
run_hint!(vm, ids_data, hint_code, &mut exec_scopes),
Err(HintError::KeccakMaxSize(x, y)) if x == Felt::new(5) && y == Felt::new(2)
Err(HintError::KeccakMaxSize(x, y)) if x == Felt252::new(5) && y == Felt252::new(2)
);
}

Expand Down Expand Up @@ -773,10 +773,10 @@ mod tests {
((1, 2), (2, 0))
];
let ids_data = ids_data!["length", "data", "high", "low"];
let mut exec_scopes = scope![("__keccak_max_size", Felt::new(10))];
let mut exec_scopes = scope![("__keccak_max_size", Felt252::new(10))];
assert_matches!(
run_hint!(vm, ids_data, hint_code, &mut exec_scopes),
Err(HintError::InvalidWordSize(x)) if x == Felt::new(-1)
Err(HintError::InvalidWordSize(x)) if x == Felt252::new(-1)
);
}

Expand Down Expand Up @@ -850,7 +850,7 @@ mod tests {
exec_scopes: &mut ExecutionScopes,
_ids_data: &HashMap<String, HintReference>,
_ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt>,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
exec_scopes.enter_scope(HashMap::new());
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
vm_core::VirtualMachine,
},
};
use felt::Felt;
use felt::Felt252;
use num_traits::{ToPrimitive, Zero};

// Constants in package "starkware.cairo.common.cairo_keccak.keccak".
Expand Down Expand Up @@ -49,8 +49,8 @@ pub fn keccak_write_args(
let low = low.as_ref();
let high = high.as_ref();

let low_args = [low & Felt::new(u64::MAX), low >> 64];
let high_args = [high & Felt::new(u64::MAX), high >> 64];
let low_args = [low & Felt252::new(u64::MAX), low >> 64];
let high_args = [high & Felt252::new(u64::MAX), high >> 64];

let low_args: Vec<_> = low_args.into_iter().map(MaybeRelocatable::from).collect();
vm.write_arg(inputs_ptr, &low_args)
Expand All @@ -75,7 +75,7 @@ pub fn compare_bytes_in_word_nondet(
vm: &mut VirtualMachine,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
constants: &HashMap<String, Felt>,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let n_bytes = get_integer_from_var_name("n_bytes", vm, ids_data, ap_tracking)?;
let n_bytes = n_bytes.as_ref();
Expand All @@ -84,11 +84,11 @@ pub fn compare_bytes_in_word_nondet(
// One option is to try to convert n_bytes into usize, with failure to do so simply
// making value be 0 (if it can't convert then it's either negative, which can't be in Cairo memory
// or too big, which also means n_bytes > BYTES_IN_WORD). The other option is to exctract
// Felt::new(BYTES_INTO_WORD) into a lazy_static!
// Felt252::new(BYTES_INTO_WORD) into a lazy_static!
let bytes_in_word = constants
.get(BYTES_IN_WORD)
.ok_or(HintError::MissingConstant(BYTES_IN_WORD))?;
let value = Felt::new((n_bytes < bytes_in_word) as usize);
let value = Felt252::new((n_bytes < bytes_in_word) as usize);
insert_value_into_ap(vm, value)
}

Expand All @@ -104,15 +104,15 @@ pub fn compare_keccak_full_rate_in_bytes_nondet(
vm: &mut VirtualMachine,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
constants: &HashMap<String, Felt>,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let n_bytes = get_integer_from_var_name("n_bytes", vm, ids_data, ap_tracking)?;
let n_bytes = n_bytes.as_ref();

let keccak_full_rate_in_bytes = constants
.get(KECCAK_FULL_RATE_IN_BYTES)
.ok_or(HintError::MissingConstant(KECCAK_FULL_RATE_IN_BYTES))?;
let value = Felt::new((n_bytes >= keccak_full_rate_in_bytes) as usize);
let value = Felt252::new((n_bytes >= keccak_full_rate_in_bytes) as usize);
insert_value_into_ap(vm, value)
}

Expand All @@ -132,14 +132,14 @@ pub fn block_permutation(
vm: &mut VirtualMachine,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
constants: &HashMap<String, Felt>,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let keccak_state_size_felts = constants
.get(KECCAK_STATE_SIZE_FELTS)
.ok_or(HintError::MissingConstant(KECCAK_STATE_SIZE_FELTS))?;

if keccak_state_size_felts >= &Felt::new(100_i32) {
return Err(HintError::InvalidKeccakStateSizeFelts(
if keccak_state_size_felts >= &Felt252::new(100_i32) {
return Err(HintError::InvalidKeccakStateSizeFelt252s(
keccak_state_size_felts.clone(),
));
}
Expand Down Expand Up @@ -184,7 +184,7 @@ pub fn cairo_keccak_finalize(
vm: &mut VirtualMachine,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
constants: &HashMap<String, Felt>,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let keccak_state_size_felts = constants
.get(KECCAK_STATE_SIZE_FELTS)
Expand All @@ -193,13 +193,13 @@ pub fn cairo_keccak_finalize(
.get(BLOCK_SIZE)
.ok_or(HintError::MissingConstant(BLOCK_SIZE))?;

if keccak_state_size_felts >= &Felt::new(100_i32) {
return Err(HintError::InvalidKeccakStateSizeFelts(
if keccak_state_size_felts >= &Felt252::new(100_i32) {
return Err(HintError::InvalidKeccakStateSizeFelt252s(
keccak_state_size_felts.clone(),
));
}

if block_size >= &Felt::new(10_i32) {
if block_size >= &Felt252::new(10_i32) {
return Err(HintError::InvalidBlockSize(block_size.clone()));
}

Expand All @@ -211,7 +211,7 @@ pub fn cairo_keccak_finalize(
.map_err(|_| VirtualMachineError::SliceToArrayError)?;
keccak::f1600(&mut inp);

let mut padding = vec![Felt::zero().into(); keccak_state_size_felts];
let mut padding = vec![Felt252::zero().into(); keccak_state_size_felts];
padding.extend(u64_array_to_mayberelocatable_vec(&inp));

let base_padding = padding.clone();
Expand Down Expand Up @@ -239,7 +239,7 @@ pub(crate) fn maybe_reloc_vec_to_u64_array(
Some(Cow::Owned(MaybeRelocatable::Int(ref num)))
| Some(Cow::Borrowed(MaybeRelocatable::Int(ref num))) => num
.to_u64()
.ok_or_else(|| MathError::FeltToU64Conversion(num.clone()).into()),
.ok_or_else(|| MathError::Felt252ToU64Conversion(num.clone()).into()),
_ => Err(VirtualMachineError::ExpectedIntAtRange(
n.as_ref().map(|x| x.as_ref().to_owned()),
)),
Expand All @@ -250,7 +250,7 @@ pub(crate) fn maybe_reloc_vec_to_u64_array(
}

pub fn u64_array_to_mayberelocatable_vec(array: &[u64]) -> Vec<MaybeRelocatable> {
array.iter().map(|n| Felt::new(*n).into()).collect()
array.iter().map(|n| Felt252::new(*n).into()).collect()
}

#[cfg(test)]
Expand Down Expand Up @@ -328,7 +328,7 @@ mod tests {
ids_data,
hint_code,
exec_scopes_ref!(),
&[(KECCAK_FULL_RATE_IN_BYTES, Felt::new(136))]
&[(KECCAK_FULL_RATE_IN_BYTES, Felt252::new(136))]
.into_iter()
.map(|(k, v)| (k.to_string(), v))
.collect()
Expand Down Expand Up @@ -357,7 +357,7 @@ mod tests {
ids_data,
hint_code,
exec_scopes_ref!(),
&[(KECCAK_FULL_RATE_IN_BYTES, Felt::new(136))]
&[(KECCAK_FULL_RATE_IN_BYTES, Felt252::new(136))]
.into_iter()
.map(|(k, v)| (k.to_string(), v))
.collect()
Expand Down Expand Up @@ -385,7 +385,7 @@ mod tests {
ids_data,
hint_code,
exec_scopes_ref!(),
&[(KECCAK_FULL_RATE_IN_BYTES, Felt::new(136))]
&[(KECCAK_FULL_RATE_IN_BYTES, Felt252::new(136))]
.into_iter()
.map(|(k, v)| (k.to_string(), v))
.collect()
Expand Down
Loading