Skip to content

Commit

Permalink
refactor(core): move Pallas methods from attr_fn to locals
Browse files Browse the repository at this point in the history
  • Loading branch information
krnak committed Sep 12, 2022
1 parent c42708d commit 5fb1ebb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 124 deletions.
51 changes: 12 additions & 39 deletions core/embed/rust/src/zcash_primitives/pallas/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ use core::ops::Deref;
use ff::PrimeField;
use pasta_curves::{arithmetic::FieldExt, Fp};

use crate::{
error::Error,
micropython::{
ffi,
gc::Gc,
obj::Obj,
qstr::Qstr,
typ::Type,
util,
wrap::{Wrapable, Wrapped},
},
use crate::micropython::{
ffi,
gc::Gc,
map::Map,
obj::Obj,
qstr::Qstr,
typ::Type,
util,
wrap::{Wrapable, Wrapped},
};

pub static FP_TYPE: Type = obj_type! {
name: Qstr::MP_QSTR_Fp,
attr_fn: fp_attr,
locals: &obj_dict!(obj_map! {
Qstr::MP_QSTR_to_bytes => obj_fn_1!(fp_to_bytes).as_obj(),
}),
make_new_fn: super::common::ff_from_bytes::<Fp>,
binary_op_fn: fp_binary_op,
};
Expand All @@ -28,31 +28,6 @@ impl Wrapable for Fp {
}
}

unsafe extern "C" fn fp_attr(self_in: Obj, attr: ffi::qstr, dest: *mut Obj) {
let block = || {
//let this = Gc::<Fp>::try_from(self_in)?;
let attr = Qstr::from_u16(attr as _);

let arg = unsafe { dest.read() };
if !arg.is_null() {
// Null destination would mean a `setattr`.
return Err(Error::TypeError);
}

match attr {
Qstr::MP_QSTR_to_bytes => unsafe {
dest.write(FP_TO_BYTES_OBJ.as_obj());
dest.offset(1).write(self_in);
},
_ => {
return Err(Error::AttributeError(attr));
}
}
Ok(())
};
unsafe { util::try_or_raise(block) }
}

unsafe extern "C" fn fp_to_bytes(self_in: Obj) -> Obj {
let block = || {
let this: Gc<Wrapped<Fp>> = self_in.try_into()?;
Expand All @@ -62,8 +37,6 @@ unsafe extern "C" fn fp_to_bytes(self_in: Obj) -> Obj {
unsafe { util::try_or_raise(block) }
}

static FP_TO_BYTES_OBJ: ffi::mp_obj_fun_builtin_fixed_t = obj_fn_1!(fp_to_bytes);

unsafe extern "C" fn fp_binary_op(op: ffi::mp_binary_op_t, this: Obj, other: Obj) -> Obj {
let block = || {
let this = Gc::<Wrapped<Fp>>::try_from(this)?;
Expand Down
45 changes: 5 additions & 40 deletions core/embed/rust/src/zcash_primitives/pallas/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ use crate::{

pub static POINT_TYPE: Type = obj_type! {
name: Qstr::MP_QSTR_Point,
attr_fn: point_attr,
locals: &obj_dict!(obj_map! {
Qstr::MP_QSTR_extract => obj_fn_1!(point_extract).as_obj(),
Qstr::MP_QSTR_to_bytes => obj_fn_1!(point_to_bytes).as_obj(),
Qstr::MP_QSTR_is_identity => obj_fn_1!(point_is_identity).as_obj(),
}),
make_new_fn: point_from_bytes,
unary_op_fn: point_unary_op,
binary_op_fn: point_binary_op,
Expand Down Expand Up @@ -57,39 +61,6 @@ unsafe extern "C" fn point_from_bytes(
unsafe { util::try_with_args_and_kwargs_inline(n_args, n_kw, args, block) }
}

unsafe extern "C" fn point_attr(self_in: Obj, attr: ffi::qstr, dest: *mut Obj) {
let block = || {
//let this = Gc::<Wrapped<Point>>::try_from(self_in)?;
let attr = Qstr::from_u16(attr as _);

let arg = unsafe { dest.read() };
if !arg.is_null() {
// Null destination would mean a `setattr`.
return Err(Error::TypeError);
}

match attr {
Qstr::MP_QSTR_extract => unsafe {
dest.write(POINT_EXTRACT_OBJ.as_obj());
dest.offset(1).write(self_in);
},
Qstr::MP_QSTR_to_bytes => unsafe {
dest.write(POINT_TO_BYTES_OBJ.as_obj());
dest.offset(1).write(self_in);
},
Qstr::MP_QSTR_is_identity => unsafe {
dest.write(POINT_IS_IDENTITY_OBJ.as_obj());
dest.offset(1).write(self_in);
},
_ => {
return Err(Error::AttributeError(attr));
}
}
Ok(())
};
unsafe { util::try_or_raise(block) }
}

unsafe extern "C" fn point_unary_op(op: ffi::mp_binary_op_t, this: Obj) -> Obj {
let block = || {
let this = Gc::<Wrapped<Point>>::try_from(this)?;
Expand Down Expand Up @@ -143,8 +114,6 @@ unsafe extern "C" fn point_extract(self_in: Obj) -> Obj {
unsafe { util::try_or_raise(block) }
}

pub static POINT_EXTRACT_OBJ: ffi::mp_obj_fun_builtin_fixed_t = obj_fn_1!(point_extract);

unsafe extern "C" fn point_is_identity(self_in: Obj) -> Obj {
let block = || {
let this = Gc::<Wrapped<Point>>::try_from(self_in)?;
Expand All @@ -154,8 +123,6 @@ unsafe extern "C" fn point_is_identity(self_in: Obj) -> Obj {
unsafe { util::try_or_raise(block) }
}

pub static POINT_IS_IDENTITY_OBJ: ffi::mp_obj_fun_builtin_fixed_t = obj_fn_1!(point_is_identity);

unsafe extern "C" fn point_to_bytes(self_in: Obj) -> Obj {
let block = || {
let this = Gc::<Wrapped<Point>>::try_from(self_in)?;
Expand All @@ -165,8 +132,6 @@ unsafe extern "C" fn point_to_bytes(self_in: Obj) -> Obj {
unsafe { util::try_or_raise(block) }
}

pub static POINT_TO_BYTES_OBJ: ffi::mp_obj_fun_builtin_fixed_t = obj_fn_1!(point_to_bytes);

#[no_mangle]
pub unsafe extern "C" fn group_hash(domain: Obj, message: Obj) -> Obj {
let block = || {
Expand Down
58 changes: 13 additions & 45 deletions core/embed/rust/src/zcash_primitives/pallas/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ use pasta_curves::{
pallas::{Point, Scalar},
};

use crate::{
error::Error,
micropython::{
ffi,
gc::Gc,
obj::Obj,
qstr::Qstr,
typ::Type,
util,
wrap::{Wrapable, Wrapped},
},
use crate::micropython::{
ffi,
gc::Gc,
map::Map,
obj::Obj,
qstr::Qstr,
typ::Type,
util,
wrap::{Wrapable, Wrapped},
};

pub static SCALAR_TYPE: Type = obj_type! {
name: Qstr::MP_QSTR_Scalar,
attr_fn: scalar_attr,
locals: &obj_dict!(obj_map! {
Qstr::MP_QSTR_to_bytes => obj_fn_1!(scalar_to_bytes).as_obj(),
Qstr::MP_QSTR_is_not_zero => obj_fn_1!(scalar_is_not_zero).as_obj(),
}),
make_new_fn: super::common::ff_from_bytes::<Scalar>,
unary_op_fn: scalar_unary_op,
binary_op_fn: scalar_binary_op,
Expand All @@ -33,35 +34,6 @@ impl Wrapable for Scalar {
}
}

unsafe extern "C" fn scalar_attr(self_in: Obj, attr: ffi::qstr, dest: *mut Obj) {
let block = || {
//let this = Gc::<Wrapped<Scalar>>::try_from(self_in)?;
let attr = Qstr::from_u16(attr as _);

let arg = unsafe { dest.read() };
if !arg.is_null() {
// Null destination would mean a `setattr`.
return Err(Error::TypeError);
}

match attr {
Qstr::MP_QSTR_to_bytes => unsafe {
dest.write(SCALAR_TO_BYTES_OBJ.as_obj());
dest.offset(1).write(self_in);
},
Qstr::MP_QSTR_is_not_zero => unsafe {
dest.write(SCALAR_IS_NOT_ZERO_OBJ.as_obj());
dest.offset(1).write(self_in);
},
_ => {
return Err(Error::AttributeError(attr));
}
}
Ok(())
};
unsafe { util::try_or_raise(block) }
}

unsafe extern "C" fn scalar_binary_op(op: ffi::mp_binary_op_t, this: Obj, other: Obj) -> Obj {
let block = || {
let this = Gc::<Wrapped<Scalar>>::try_from(this)?;
Expand Down Expand Up @@ -104,8 +76,6 @@ unsafe extern "C" fn scalar_to_bytes(self_in: Obj) -> Obj {
unsafe { util::try_or_raise(block) }
}

static SCALAR_TO_BYTES_OBJ: ffi::mp_obj_fun_builtin_fixed_t = obj_fn_1!(scalar_to_bytes);

unsafe extern "C" fn scalar_is_not_zero(self_in: Obj) -> Obj {
let block = || {
let this = Gc::<Wrapped<Scalar>>::try_from(self_in)?;
Expand All @@ -114,8 +84,6 @@ unsafe extern "C" fn scalar_is_not_zero(self_in: Obj) -> Obj {
unsafe { util::try_or_raise(block) }
}

static SCALAR_IS_NOT_ZERO_OBJ: ffi::mp_obj_fun_builtin_fixed_t = obj_fn_1!(scalar_is_not_zero);

#[no_mangle]
pub unsafe extern "C" fn to_scalar(bytes: Obj) -> Obj {
let block = || {
Expand Down

0 comments on commit 5fb1ebb

Please sign in to comment.