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

Remove felt new trait #734

Merged
merged 8 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
528 changes: 305 additions & 223 deletions felt/src/bigint_felt.rs

Large diffs are not rendered by default.

22 changes: 9 additions & 13 deletions felt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,22 @@ use std::{
},
};

pub type Felt = FeltBigInt;

pub const PRIME_STR: &str = "0x800000000000011000000000000000000000000000000000000000000000001";
pub const FIELD: (u128, u128) = ((1 << 123) + (17 << 64), 1);
pub const FIELD_HIGH: u128 = (1 << 123) + (17 << 64);
pub const FIELD_LOW: u128 = 1;

pub type Felt = FeltBigInt<FIELD_HIGH, FIELD_LOW>;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ParseFeltError;

pub trait NewFelt {
fn new<T: Into<Felt>>(value: T) -> Self;
}

pub trait FeltOps {
fn modpow(&self, exponent: &Felt, modulus: &Felt) -> Self;
pub trait FeltOps<const PH: u128, const PL: u128> {
fn new<T: Into<FeltBigInt<PH, PL>>>(value: T) -> Self;
fn modpow(&self, exponent: &FeltBigInt<PH, PL>, modulus: &FeltBigInt<PH, PL>) -> Self;
fn iter_u64_digits(&self) -> U64Digits;
fn to_signed_bytes_le(&self) -> Vec<u8>;
fn to_bytes_be(&self) -> Vec<u8>;
fn parse_bytes(buf: &[u8], radix: u32) -> Option<Felt>;
fn parse_bytes(buf: &[u8], radix: u32) -> Option<FeltBigInt<PH, PL>>;
fn from_bytes_be(bytes: &[u8]) -> Self;
fn to_str_radix(&self, radix: u32) -> String;
fn to_bigint(&self) -> BigInt;
Expand All @@ -43,8 +41,7 @@ pub trait FeltOps {
macro_rules! assert_felt_impl {
($type:ty) => {
const _: () = {
fn assert_new_felt<T: NewFelt>() {}
fn assert_felt_ops<T: FeltOps>() {}
fn assert_felt_ops<T: FeltOps<FIELD_HIGH, FIELD_LOW>>() {}
fn assert_add<T: Add>() {}
fn assert_add_ref<'a, T: Add<&'a $type>>() {}
fn assert_add_u32<T: Add<u32>>() {}
Expand Down Expand Up @@ -91,7 +88,6 @@ macro_rules! assert_felt_impl {
// RFC 2056
#[allow(dead_code)]
fn assert_all() {
assert_new_felt::<$type>();
assert_felt_ops::<$type>();
assert_add::<$type>();
assert_add::<&$type>();
Expand Down
2 changes: 1 addition & 1 deletion src/cairo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ mod tests {
},
utils::test_utils::*,
};
use felt::NewFelt;
use felt::FeltOps;
use std::io::Read;

fn run_test_program(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
vm_core::VirtualMachine,
},
};
use felt::{Felt, NewFelt};
use felt::{Felt, FeltOps};
use num_traits::ToPrimitive;
use std::{borrow::Cow, collections::HashMap};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ mod tests {
vm_memory::memory::Memory,
},
};
use felt::NewFelt;
use felt::FeltOps;
use num_traits::{One, Zero};
use std::any::Any;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
vm_core::VirtualMachine,
},
};
use felt::{Felt, NewFelt};
use felt::{Felt, FeltOps};
use num_traits::{ToPrimitive, Zero};
use std::{borrow::Cow, collections::HashMap, ops::Add};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
vm_core::VirtualMachine,
},
};
use felt::{Felt, NewFelt};
use felt::{Felt, FeltOps};
use num_traits::{Signed, ToPrimitive};
use std::collections::HashMap;

Expand Down
2 changes: 1 addition & 1 deletion src/hint_processor/builtin_hint_processor/hint_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn get_reference_from_var_name<'a>(

#[cfg(test)]
mod tests {
use felt::NewFelt;
use felt::FeltOps;

use super::*;
use crate::{
Expand Down
2 changes: 1 addition & 1 deletion src/hint_processor/builtin_hint_processor/math_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
vm_core::VirtualMachine,
},
};
use felt::{Felt, FeltOps, NewFelt, PRIME_STR};
use felt::{Felt, FeltOps, PRIME_STR};
use num_bigint::BigUint;
use num_integer::Integer;
use num_traits::One;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ mod tests {
vm_memory::memory::Memory,
},
};
use felt::NewFelt;
use felt::FeltOps;

#[test]
fn get_integer_from_var_name_valid() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
types::exec_scope::ExecutionScopes,
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
};
use felt::{Felt, NewFelt};
use felt::{Felt, FeltOps};
use num_traits::Signed;
use std::{any::Any, collections::HashMap};

Expand Down
2 changes: 1 addition & 1 deletion src/hint_processor/builtin_hint_processor/pow_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
serde::deserialize_program::ApTracking,
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
};
use felt::{Felt, NewFelt};
use felt::{Felt, FeltOps};
use num_integer::Integer;
use std::collections::HashMap;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
vm_core::VirtualMachine,
},
};
use felt::{Felt, NewFelt};
use felt::{Felt, FeltOps};
use std::collections::HashMap;
/*
Implements hint:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
types::exec_scope::ExecutionScopes,
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
};
use felt::{Felt, FeltOps, NewFelt};
use felt::{Felt, FeltOps};
use num_bigint::BigInt;
use num_integer::Integer;
use num_traits::{One, Zero};
Expand Down Expand Up @@ -175,7 +175,7 @@ mod tests {
vm_memory::memory::Memory,
},
};
use felt::NewFelt;
use felt::FeltOps;
use std::any::Any;

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub fn pack_from_relocatable(rel: Relocatable, vm: &VirtualMachine) -> Result<Bi
mod tests {
use super::*;
use crate::utils::test_utils::*;
use felt::{felt_str, NewFelt};
use felt::{felt_str, FeltOps};
use num_bigint::BigUint;
use num_traits::One;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ mod tests {
vm_memory::memory::Memory,
},
};
use felt::NewFelt;
use felt::FeltOps;
use num_traits::Zero;
use std::{any::Any, ops::Shl};

Expand Down
2 changes: 1 addition & 1 deletion src/hint_processor/builtin_hint_processor/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
vm_core::VirtualMachine,
},
};
use felt::{Felt, NewFelt};
use felt::{Felt, FeltOps};
use num_traits::{One, ToPrimitive, Zero};
use std::collections::HashMap;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
vm::errors::{hint_errors::HintError, vm_errors::VirtualMachineError},
vm::vm_core::VirtualMachine,
};
use felt::{Felt, NewFelt};
use felt::{Felt, FeltOps};
use generic_array::GenericArray;
use num_traits::{One, Zero};
use sha2::compress256;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
vm_core::VirtualMachine,
},
};
use felt::{Felt, NewFelt};
use felt::{Felt, FeltOps};
use num_integer::Integer;
use num_traits::{One, ToPrimitive, Zero};
use std::collections::HashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
serde::deserialize_program::ApTracking,
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
};
use felt::{Felt, FeltOps, NewFelt};
use felt::{Felt, FeltOps};
use num_integer::div_rem;
use num_traits::{One, Signed, Zero};
use std::{
Expand Down
2 changes: 1 addition & 1 deletion src/hint_processor/builtin_hint_processor/usort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
types::exec_scope::ExecutionScopes,
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
};
use felt::{Felt, NewFelt};
use felt::{Felt, FeltOps};
use num_traits::{ToPrimitive, Zero};
use std::{any::Any, collections::HashMap};

Expand Down
2 changes: 1 addition & 1 deletion src/hint_processor/hint_processor_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ mod tests {
errors::memory_errors::MemoryError, vm_core::VirtualMachine, vm_memory::memory::Memory,
},
};
use felt::NewFelt;
use felt::FeltOps;

#[test]
fn get_integer_from_reference_with_immediate_value() {
Expand Down
2 changes: 1 addition & 1 deletion src/math_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ pub fn ec_double_slope(point: &(BigInt, BigInt), alpha: &BigInt, prime: &BigInt)
mod tests {
use super::*;
use crate::utils::test_utils::*;
use felt::NewFelt;
use felt::FeltOps;
use num_traits::Num;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/serde/deserialize_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ pub fn deserialize_program(
#[cfg(test)]
mod tests {
use super::*;
use felt::{felt_str, NewFelt};
use felt::{felt_str, FeltOps};
use num_traits::Zero;
use std::{fs::File, io::BufReader};

Expand Down
2 changes: 1 addition & 1 deletion src/serde/deserialize_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
serde::deserialize_program::{OffsetValue, ValueAddress},
types::instruction::Register,
};
use felt::{Felt, NewFelt, ParseFeltError};
use felt::{Felt, FeltOps, ParseFeltError};
use nom::{
branch::alt,
bytes::{
Expand Down
2 changes: 1 addition & 1 deletion src/types/exec_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl Default for ExecutionScopes {
#[cfg(test)]
mod tests {
use super::*;
use felt::{Felt, NewFelt};
use felt::{Felt, FeltOps};
use num_traits::One;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/types/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub(crate) fn is_call_instruction(encoded_instruction: &Felt, imm: Option<&Felt>
#[cfg(test)]
mod tests {
use super::*;
use felt::NewFelt;
use felt::FeltOps;

#[test]
fn is_call_instruction_true() {
Expand Down
2 changes: 1 addition & 1 deletion src/types/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ mod tests {
use super::*;
use crate::serde::deserialize_program::{ApTracking, FlowTrackingData};
use crate::utils::test_utils::mayberelocatable;
use felt::{felt_str, NewFelt};
use felt::{felt_str, FeltOps};
use num_traits::Zero;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/types/relocatable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
relocatable,
vm::errors::{memory_errors::MemoryError, vm_errors::VirtualMachineError},
};
use felt::{Felt, NewFelt};
use felt::{Felt, FeltOps};
use num_traits::{FromPrimitive, ToPrimitive, Zero};
use serde::{Deserialize, Serialize};
use std::{
Expand Down
7 changes: 5 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ pub mod test_utils {
MaybeRelocatable::from(($val1, $val2))
};
($val1 : expr) => {
MaybeRelocatable::from(<felt::Felt as felt::NewFelt>::new($val1 as i128))
MaybeRelocatable::from(<felt::Felt as felt::FeltOps<
{ felt::FIELD_HIGH },
{ felt::FIELD_LOW },
>>::new($val1 as i128))
};
}
pub(crate) use mayberelocatable;
Expand Down Expand Up @@ -528,7 +531,7 @@ mod test {
vm_core::VirtualMachine, vm_memory::memory::Memory,
},
};
use felt::{Felt, NewFelt};
use felt::{Felt, FeltOps};
use num_traits::One;
use std::{any::Any, cell::RefCell, collections::HashMap, rc::Rc};

Expand Down
2 changes: 1 addition & 1 deletion src/vm/context/run_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ mod tests {
use crate::types::instruction::{ApUpdate, FpUpdate, Opcode, PcUpdate, Res};
use crate::utils::test_utils::mayberelocatable;
use crate::vm::errors::memory_errors::MemoryError;
use felt::{Felt, NewFelt};
use felt::{Felt, FeltOps};

#[test]
fn compute_dst_addr_for_ap_register() {
Expand Down
2 changes: 1 addition & 1 deletion src/vm/decoding/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ fn decode_offset(offset: i64) -> isize {
#[cfg(test)]
mod decoder_test {
use super::*;
use felt::NewFelt;
use felt::FeltOps;

#[test]
fn invalid_op1_reg() {
Expand Down
2 changes: 1 addition & 1 deletion src/vm/runners/builtin_runner/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ mod tests {
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor,
types::program::Program, utils::test_utils::*, vm::runners::cairo_runner::CairoRunner,
};
use felt::{Felt, NewFelt};
use felt::{Felt, FeltOps};

#[test]
fn get_used_instances() {
Expand Down
2 changes: 1 addition & 1 deletion src/vm/runners/builtin_runner/ec_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::vm::errors::runner_errors::RunnerError;
use crate::vm::vm_core::VirtualMachine;
use crate::vm::vm_memory::memory::Memory;
use crate::vm::vm_memory::memory_segments::MemorySegmentManager;
use felt::{Felt, FeltOps, NewFelt};
use felt::{Felt, FeltOps};
use num_bigint::{BigInt, ToBigInt};
use num_integer::{div_ceil, Integer};
use num_traits::{Num, One, Pow, Zero};
Expand Down
2 changes: 1 addition & 1 deletion src/vm/runners/builtin_runner/keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ mod tests {
runners::builtin_runner::BuiltinRunner,
vm_core::VirtualMachine,
};
use felt::NewFelt;
use felt::FeltOps;
use std::path::Path;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/vm/runners/builtin_runner/range_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
},
},
};
use felt::{Felt, NewFelt};
use felt::{Felt, FeltOps};
use num_integer::Integer;
use num_traits::{One, ToPrimitive, Zero};
use std::{
Expand Down
2 changes: 1 addition & 1 deletion src/vm/runners/cairo_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,7 @@ mod tests {
utils::test_utils::*,
vm::{trace::trace_entry::TraceEntry, vm_memory::memory::Memory},
};
use felt::{felt_str, NewFelt};
use felt::{felt_str, FeltOps};
use num_traits::One;
use std::{
collections::{HashMap, HashSet},
Expand Down
2 changes: 1 addition & 1 deletion src/vm/vm_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ mod tests {
},
};

use felt::{felt_str, NewFelt};
use felt::{felt_str, FeltOps};
use std::{collections::HashSet, path::Path};

#[test]
Expand Down
Loading