Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

Commit

Permalink
review comment: add scalar settings check
Browse files Browse the repository at this point in the history
regenerate C header file

update command for compiling FFI tests
  • Loading branch information
finiteprods committed Feb 8, 2021
1 parent d1ffaf5 commit fba67d2
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 33 deletions.
6 changes: 3 additions & 3 deletions rust/xaynet-core/src/mask/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: Debug>(pub(crate) P);

/// An interface to convert a collection of numerical values into an iterator of primitive values.
Expand Down
2 changes: 1 addition & 1 deletion rust/xaynet-mobile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 12 additions & 10 deletions rust/xaynet-mobile/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
3 changes: 3 additions & 0 deletions rust/xaynet-mobile/src/ffi/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use super::{
ERR_INVALID_URL,
ERR_NULLPTR,
ERR_SETTINGS_KEYS,
ERR_SETTINGS_SCALAR,
ERR_SETTINGS_URL,
OK,
};
Expand Down Expand Up @@ -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
///
Expand All @@ -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,
}
Expand Down
17 changes: 9 additions & 8 deletions rust/xaynet-mobile/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -18,7 +18,7 @@ pub struct Settings {
/// The participant signing keys.
keys: Option<SigningKeyPair>,
/// The scalar used for masking.
scalar: Scalar,
scalar: Result<Scalar, PrimitiveCastError<f64>>,
/// The maximum possible size of a message.
max_message_size: MaxMessageSize,
}
Expand All @@ -35,7 +35,7 @@ impl Settings {
Self {
url: None,
keys: None,
scalar: Scalar::unit(),
scalar: Ok(Scalar::unit()),
max_message_size: MaxMessageSize::default(),
}
}
Expand All @@ -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
Expand All @@ -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(())
}
Expand All @@ -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<f64>),
}

impl TryInto<(String, PetSettings)> for Settings {
Expand All @@ -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,
Expand Down
28 changes: 17 additions & 11 deletions rust/xaynet-mobile/xaynet_ffi.h
Original file line number Diff line number Diff line change
@@ -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. */

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit fba67d2

Please sign in to comment.