From fba67d202cd488cdab915e0fbd7a78e0a5370b61 Mon Sep 17 00:00:00 2001 From: finiteprods Date: Fri, 5 Feb 2021 21:04:56 +0100 Subject: [PATCH] review comment: add scalar settings check regenerate C header file update command for compiling FFI tests --- rust/xaynet-core/src/mask/model.rs | 6 +++--- rust/xaynet-mobile/README.md | 2 +- rust/xaynet-mobile/src/ffi/mod.rs | 22 +++++++++++--------- rust/xaynet-mobile/src/ffi/settings.rs | 3 +++ rust/xaynet-mobile/src/settings.rs | 17 ++++++++-------- rust/xaynet-mobile/xaynet_ffi.h | 28 ++++++++++++++++---------- 6 files changed, 45 insertions(+), 33 deletions(-) diff --git a/rust/xaynet-core/src/mask/model.rs b/rust/xaynet-core/src/mask/model.rs index 931cf816e..208d4c16e 100644 --- a/rust/xaynet-core/src/mask/model.rs +++ b/rust/xaynet-core/src/mask/model.rs @@ -81,9 +81,9 @@ pub struct ModelCastError { target: PrimitiveType, } -#[derive(Error, Debug)] -#[error("Could not convert primitive type {0:?} to model weight")] -/// Errors related to model conversion from primitives. +#[derive(Clone, Error, Debug)] +#[error("Could not convert primitive type {0:?} to weight")] +/// Errors related to weight conversion from primitives. pub struct PrimitiveCastError(pub(crate) P); /// An interface to convert a collection of numerical values into an iterator of primitive values. diff --git a/rust/xaynet-mobile/README.md b/rust/xaynet-mobile/README.md index 457c55a7f..bc7809f6c 100644 --- a/rust/xaynet-mobile/README.md +++ b/rust/xaynet-mobile/README.md @@ -33,7 +33,7 @@ gcc \ tests/ffi_test.c -Wall \ -I. \ - -lpthread -lm -ldl \ + -pthread -Wl,--no-as-needed -lm -ldl \ ../target/debug/libxaynet_mobile.a \ -o tests/ffi_test.o ./tests/ffi_test.o diff --git a/rust/xaynet-mobile/src/ffi/mod.rs b/rust/xaynet-mobile/src/ffi/mod.rs index 637ec7232..335a66dfc 100644 --- a/rust/xaynet-mobile/src/ffi/mod.rs +++ b/rust/xaynet-mobile/src/ffi/mod.rs @@ -85,23 +85,25 @@ pub const ERR_INVALID_URL: c_int = 2; pub const ERR_SETTINGS_URL: c_int = 3; /// Invalid settings: signing keys are not set pub const ERR_SETTINGS_KEYS: c_int = 4; +/// Invalid settings: scalar is out of bounds +pub const ERR_SETTINGS_SCALAR: c_int = 5; /// Failed to set the local model: invalid model -pub const ERR_SETMODEL_MODEL: c_int = 5; +pub const ERR_SETMODEL_MODEL: c_int = 6; /// Failed to set the local model: invalid data type -pub const ERR_SETMODEL_DATATYPE: c_int = 6; +pub const ERR_SETMODEL_DATATYPE: c_int = 7; /// Failed to initialized the crypto library -pub const ERR_CRYPTO_INIT: c_int = 7; +pub const ERR_CRYPTO_INIT: c_int = 8; /// Invalid secret signing key -pub const ERR_CRYPTO_SECRET_KEY: c_int = 8; +pub const ERR_CRYPTO_SECRET_KEY: c_int = 9; /// Invalid public signing key -pub const ERR_CRYPTO_PUBLIC_KEY: c_int = 9; +pub const ERR_CRYPTO_PUBLIC_KEY: c_int = 10; /// No global model is currently available -pub const GLOBALMODEL_NONE: c_int = 10; +pub const GLOBALMODEL_NONE: c_int = 11; /// Failed to get the global model: communication with the coordinator failed -pub const ERR_GLOBALMODEL_IO: c_int = 11; +pub const ERR_GLOBALMODEL_IO: c_int = 12; /// Failed to get the global model: invalid data type -pub const ERR_GLOBALMODEL_DATATYPE: c_int = 12; +pub const ERR_GLOBALMODEL_DATATYPE: c_int = 13; /// Failed to get the global model: invalid buffer length -pub const ERR_GLOBALMODEL_LEN: c_int = 13; +pub const ERR_GLOBALMODEL_LEN: c_int = 14; /// Failed to get the global model: invalid model -pub const ERR_GLOBALMODEL_CONVERT: c_int = 14; +pub const ERR_GLOBALMODEL_CONVERT: c_int = 15; diff --git a/rust/xaynet-mobile/src/ffi/settings.rs b/rust/xaynet-mobile/src/ffi/settings.rs index 98ee990cb..38c9388ec 100644 --- a/rust/xaynet-mobile/src/ffi/settings.rs +++ b/rust/xaynet-mobile/src/ffi/settings.rs @@ -10,6 +10,7 @@ use super::{ ERR_INVALID_URL, ERR_NULLPTR, ERR_SETTINGS_KEYS, + ERR_SETTINGS_SCALAR, ERR_SETTINGS_URL, OK, }; @@ -255,6 +256,7 @@ pub unsafe extern "C" fn xaynet_ffi_settings_set_keys( /// - [`OK`] on success /// - [`ERR_SETTINGS_URL`] if the URL has not been set /// - [`ERR_SETTINGS_KEYS`] if the signing keys have not been set +/// - [`ERR_SETTINGS_SCALAR`] if the scalar is out of bounds /// /// # Safety /// @@ -275,6 +277,7 @@ pub unsafe extern "C" fn xaynet_ffi_check_settings(settings: *const Settings) -> Ok(()) => OK, Err(SettingsError::MissingUrl) => ERR_SETTINGS_URL, Err(SettingsError::MissingKeys) => ERR_SETTINGS_KEYS, + Err(SettingsError::OutOfScalarRange(_)) => ERR_SETTINGS_SCALAR, }, None => ERR_NULLPTR, } diff --git a/rust/xaynet-mobile/src/settings.rs b/rust/xaynet-mobile/src/settings.rs index f5baa9e22..8e47320bf 100644 --- a/rust/xaynet-mobile/src/settings.rs +++ b/rust/xaynet-mobile/src/settings.rs @@ -6,7 +6,7 @@ use std::convert::TryInto; use thiserror::Error; use xaynet_core::{ crypto::SigningKeyPair, - mask::{FromPrimitive, Scalar}, + mask::{FromPrimitive, PrimitiveCastError, Scalar}, }; use xaynet_sdk::settings::{MaxMessageSize, PetSettings}; @@ -18,7 +18,7 @@ pub struct Settings { /// The participant signing keys. keys: Option, /// The scalar used for masking. - scalar: Scalar, + scalar: Result>, /// The maximum possible size of a message. max_message_size: MaxMessageSize, } @@ -35,7 +35,7 @@ impl Settings { Self { url: None, keys: None, - scalar: Scalar::unit(), + scalar: Ok(Scalar::unit()), max_message_size: MaxMessageSize::default(), } } @@ -46,11 +46,8 @@ impl Settings { } /// Set the scalar to use for masking - /// - /// # Panics - /// Panics if a `Scalar` cannot be constructed from the given `scalar`. pub fn set_scalar(&mut self, scalar: f64) { - self.scalar = Scalar::from_primitive(scalar).unwrap() + self.scalar = Scalar::from_primitive(scalar) } /// Set the Xaynet coordinator address @@ -69,6 +66,8 @@ impl Settings { Err(SettingsError::MissingUrl) } else if self.keys.is_none() { Err(SettingsError::MissingKeys) + } else if let Err(e) = &self.scalar { + Err(e.clone().into()) } else { Ok(()) } @@ -82,6 +81,8 @@ pub enum SettingsError { MissingUrl, #[error("the participant signing key pair must be specified")] MissingKeys, + #[error("float not within range of scalar: {0}")] + OutOfScalarRange(#[from] PrimitiveCastError), } impl TryInto<(String, PetSettings)> for Settings { @@ -96,8 +97,8 @@ impl TryInto<(String, PetSettings)> for Settings { } = self; let url = url.ok_or(SettingsError::MissingUrl)?; - let keys = keys.ok_or(SettingsError::MissingKeys)?; + let scalar = scalar.map_err(SettingsError::OutOfScalarRange)?; let pet_settings = PetSettings { keys, diff --git a/rust/xaynet-mobile/xaynet_ffi.h b/rust/xaynet-mobile/xaynet_ffi.h index 1413892cc..40a0d00a3 100644 --- a/rust/xaynet-mobile/xaynet_ffi.h +++ b/rust/xaynet-mobile/xaynet_ffi.h @@ -1,4 +1,4 @@ -/* Generated with cbindgen:0.16.0 */ +/* Generated with cbindgen:0.17.0 */ /* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */ @@ -32,55 +32,60 @@ */ #define ERR_SETTINGS_KEYS 4 +/** + * Invalid settings: scalar is out of bounds + */ +#define ERR_SETTINGS_SCALAR 5 + /** * Failed to set the local model: invalid model */ -#define ERR_SETMODEL_MODEL 5 +#define ERR_SETMODEL_MODEL 6 /** * Failed to set the local model: invalid data type */ -#define ERR_SETMODEL_DATATYPE 6 +#define ERR_SETMODEL_DATATYPE 7 /** * Failed to initialized the crypto library */ -#define ERR_CRYPTO_INIT 7 +#define ERR_CRYPTO_INIT 8 /** * Invalid secret signing key */ -#define ERR_CRYPTO_SECRET_KEY 8 +#define ERR_CRYPTO_SECRET_KEY 9 /** * Invalid public signing key */ -#define ERR_CRYPTO_PUBLIC_KEY 9 +#define ERR_CRYPTO_PUBLIC_KEY 10 /** * No global model is currently available */ -#define GLOBALMODEL_NONE 10 +#define GLOBALMODEL_NONE 11 /** * Failed to get the global model: communication with the coordinator failed */ -#define ERR_GLOBALMODEL_IO 11 +#define ERR_GLOBALMODEL_IO 12 /** * Failed to get the global model: invalid data type */ -#define ERR_GLOBALMODEL_DATATYPE 12 +#define ERR_GLOBALMODEL_DATATYPE 13 /** * Failed to get the global model: invalid buffer length */ -#define ERR_GLOBALMODEL_LEN 13 +#define ERR_GLOBALMODEL_LEN 14 /** * Failed to get the global model: invalid model */ -#define ERR_GLOBALMODEL_CONVERT 14 +#define ERR_GLOBALMODEL_CONVERT 15 /** * The participant is not taking part in the sum or update task @@ -771,6 +776,7 @@ int xaynet_ffi_settings_set_keys(struct Settings *settings, const struct KeyPair * - [`OK`] on success * - [`ERR_SETTINGS_URL`] if the URL has not been set * - [`ERR_SETTINGS_KEYS`] if the signing keys have not been set + * - [`ERR_SETTINGS_SCALAR`] if the scalar is out of bounds * * # Safety *