Skip to content

Commit

Permalink
bitwise
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Aug 26, 2024
1 parent e054744 commit f3ecce8
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 5 deletions.
23 changes: 22 additions & 1 deletion src/vm/uint128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn eval(
Uint128Concrete::FromFelt252(info) => eval_from_felt(registry, info, args),
Uint128Concrete::IsZero(info) => eval_is_zero(registry, info, args),
Uint128Concrete::Divmod(_) => todo!(),
Uint128Concrete::Bitwise(_) => todo!(),
Uint128Concrete::Bitwise(info) => eval_bitwise(registry, info, args),
Uint128Concrete::GuaranteeMul(_) => todo!(),
Uint128Concrete::MulGuaranteeVerify(_) => todo!(),
Uint128Concrete::ByteReverse(_) => todo!(),
Expand Down Expand Up @@ -85,6 +85,27 @@ pub fn eval_is_zero(
}
}

pub fn eval_bitwise(
_registry: &ProgramRegistry<CoreType, CoreLibfunc>,
_info: &SignatureOnlyConcreteLibfunc,
args: Vec<Value>,
) -> EvalAction {
let [bitwise @ Value::Unit, Value::U128(lhs), Value::U128(rhs)]: [Value; 3] =
args.try_into().unwrap()
else {
panic!()
};

let and = lhs & rhs;
let or = lhs | rhs;
let xor = lhs ^ rhs;

EvalAction::NormalBranch(
0,
smallvec![bitwise, Value::U128(and), Value::U128(or), Value::U128(xor)],
)
}

pub fn eval_const(
_registry: &ProgramRegistry<CoreType, CoreLibfunc>,
info: &IntConstConcreteLibfunc<Uint128Traits>,
Expand Down
23 changes: 22 additions & 1 deletion src/vm/uint32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn eval(
Uint32Concrete::IsZero(info) => eval_is_zero(registry, info, args),
Uint32Concrete::Divmod(info) => eval_divmod(registry, info, args),
Uint32Concrete::WideMul(info) => eval_widemul(registry, info, args),
Uint32Concrete::Bitwise(_) => todo!(),
Uint32Concrete::Bitwise(info) => eval_bitwise(registry, info, args),
}
}

Expand Down Expand Up @@ -104,6 +104,27 @@ pub fn eval_equal(
EvalAction::NormalBranch((lhs == rhs) as usize, smallvec![])
}

pub fn eval_bitwise(
_registry: &ProgramRegistry<CoreType, CoreLibfunc>,
_info: &SignatureOnlyConcreteLibfunc,
args: Vec<Value>,
) -> EvalAction {
let [bitwise @ Value::Unit, Value::U32(lhs), Value::U32(rhs)]: [Value; 3] =
args.try_into().unwrap()
else {
panic!()
};

let and = lhs & rhs;
let or = lhs | rhs;
let xor = lhs ^ rhs;

EvalAction::NormalBranch(
0,
smallvec![bitwise, Value::U32(and), Value::U32(or), Value::U32(xor)],
)
}

pub fn eval_is_zero(
_registry: &ProgramRegistry<CoreType, CoreLibfunc>,
_info: &SignatureOnlyConcreteLibfunc,
Expand Down
23 changes: 22 additions & 1 deletion src/vm/uint64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn eval(
Uint64Concrete::IsZero(info) => eval_is_zero(registry, info, args),
Uint64Concrete::Divmod(info) => eval_divmod(registry, info, args),
Uint64Concrete::WideMul(info) => eval_widemul(registry, info, args),
Uint64Concrete::Bitwise(_) => todo!(),
Uint64Concrete::Bitwise(info) => eval_bitwise(registry, info, args),
}
}

Expand Down Expand Up @@ -104,6 +104,27 @@ pub fn eval_equal(
EvalAction::NormalBranch((lhs == rhs) as usize, smallvec![])
}

pub fn eval_bitwise(
_registry: &ProgramRegistry<CoreType, CoreLibfunc>,
_info: &SignatureOnlyConcreteLibfunc,
args: Vec<Value>,
) -> EvalAction {
let [bitwise @ Value::Unit, Value::U64(lhs), Value::U64(rhs)]: [Value; 3] =
args.try_into().unwrap()
else {
panic!()
};

let and = lhs & rhs;
let or = lhs | rhs;
let xor = lhs ^ rhs;

EvalAction::NormalBranch(
0,
smallvec![bitwise, Value::U64(and), Value::U64(or), Value::U64(xor)],
)
}

pub fn eval_is_zero(
_registry: &ProgramRegistry<CoreType, CoreLibfunc>,
_info: &SignatureOnlyConcreteLibfunc,
Expand Down
42 changes: 40 additions & 2 deletions src/vm/uint8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,29 @@ pub fn eval(
Uint8Concrete::ToFelt252(info) => eval_to_felt252(registry, info, args),
Uint8Concrete::FromFelt252(info) => eval_from_felt(registry, info, args),
Uint8Concrete::IsZero(info) => eval_is_zero(registry, info, args),
Uint8Concrete::Divmod(_) => todo!(),
Uint8Concrete::Divmod(info) => eval_divmod(registry, info, args),
Uint8Concrete::WideMul(info) => eval_widemul(registry, info, args),
Uint8Concrete::Bitwise(_) => todo!(),
Uint8Concrete::Bitwise(info) => eval_bitwise(registry, info, args),
}
}

pub fn eval_divmod(
_registry: &ProgramRegistry<CoreType, CoreLibfunc>,
_info: &SignatureOnlyConcreteLibfunc,
args: Vec<Value>,
) -> EvalAction {
let [range_check @ Value::Unit, Value::U8(x), Value::U8(y)]: [Value; 3] =
args.try_into().unwrap()
else {
panic!()
};

let val = Value::U8(x / y);
let rem = Value::U8(x % y);

EvalAction::NormalBranch(0, smallvec![range_check, val, rem])
}

pub fn eval_to_felt252(
_registry: &ProgramRegistry<CoreType, CoreLibfunc>,
_info: &SignatureOnlyConcreteLibfunc,
Expand Down Expand Up @@ -99,6 +116,27 @@ pub fn eval_equal(
EvalAction::NormalBranch((lhs == rhs) as usize, smallvec![])
}

pub fn eval_bitwise(
_registry: &ProgramRegistry<CoreType, CoreLibfunc>,
_info: &SignatureOnlyConcreteLibfunc,
args: Vec<Value>,
) -> EvalAction {
let [bitwise @ Value::Unit, Value::U8(lhs), Value::U8(rhs)]: [Value; 3] =
args.try_into().unwrap()
else {
panic!()
};

let and = lhs & rhs;
let or = lhs | rhs;
let xor = lhs ^ rhs;

EvalAction::NormalBranch(
0,
smallvec![bitwise, Value::U8(and), Value::U8(or), Value::U8(xor)],
)
}

pub fn eval_is_zero(
_registry: &ProgramRegistry<CoreType, CoreLibfunc>,
_info: &SignatureOnlyConcreteLibfunc,
Expand Down

0 comments on commit f3ecce8

Please sign in to comment.