Struct seec_channel::util::Statistics

source ·
pub struct Statistics { /* private fields */ }
Expand description

A utility struct which is used to track communication on a best-effort basis.

When created, it is initialized with the Counters of the main channel. Optionally, a counter pair for a helper channel can be set.

§Serialization

The Statistics struct can be serialized into a serializable form via the Statistics::into_run_result method. The result can be serialized into a variety of formats via serde (Note: .csv output is currently not supported, json is recommended).

Example json output:

{
  "meta": {
    "custom": {
      "circuit": "sha256.rs"
    }
  },
  "communication": {
    "Unaccounted": {
      "sent": 58,
      "rcvd": 120
    },
    "FunctionDependentSetup": {
      "sent": 75,
      "rcvd": 13
    },
    "Online": {
      "sent": 36776,
      "rcvd": 36776
    }
  },
  "time": {
    "Unaccounted": 0,
    "FunctionDependentSetup": 0,
    "Online": 291
  }
}

§Caveat

As the actual sending of values transmitted via channels is done in an asynchronous background task, Statistics can only record the communication on a best-effort basis. It is possible for values that are sent to a channel within a Statistics::record call to not be tracked as the specified Phase, but rather as Unaccounted. If the amount of unaccounted communication is higher than desired, adding a [tokio::time::sleep] at the end of the record call might reduce it.

Implementations§

source§

impl Statistics

source

pub fn new(send_counter: Counter, recv_counter: Counter) -> Self

Create a new Statistics with the counters for the main channel.

Examples found in repository?
crates/seec/examples/sha256.rs (line 67)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
async fn main() -> Result<()> {
    let _guard = init_tracing()?;
    let args = Args::parse();
    let circuit: ExecutableCircuit<bool, BooleanGate, u32> = ExecutableCircuit::DynLayers(
        BaseCircuit::load_bristol(args.circuit, Load::Circuit)?.into(),
    );

    let (mut sender, bytes_written, mut receiver, bytes_read) = match args.id {
        0 => seec_channel::tcp::listen(args.server).await?,
        1 => seec_channel::tcp::connect(args.server).await?,
        illegal => anyhow::bail!("Illegal party id {illegal}. Must be 0 or 1."),
    };

    // Initialize the communication statistics tracker with the counters for the main channel
    let mut comm_stats = Statistics::new(bytes_written, bytes_read).without_unaccounted(true);

    let (mut sender, mut receiver) =
        sub_channels_for!(&mut sender, &mut receiver, 8, Message<BooleanGmw>).await?;

    let mut executor: Executor<BooleanGmw, _> = if let Some(addr) = args.mt_provider {
        let (mt_sender, bytes_written, mt_receiver, bytes_read) =
            seec_channel::tcp::connect(addr).await?;
        // Set the counters for the helper channel
        comm_stats.set_helper(bytes_written, bytes_read);
        let mt_provider = TrustedMTProviderClient::new("unique-id".into(), mt_sender, mt_receiver);
        // As the MTs are generated when the Executor is created, we record the communication
        // with the `record_helper` method and a custom category
        comm_stats
            .record_helper(
                Phase::Custom("Helper-Mts".into()),
                Executor::new(&circuit, args.id, mt_provider),
            )
            .await?
    } else {
        let mt_provider = InsecureMTProvider::default();
        comm_stats
            .record(
                Phase::FunctionDependentSetup,
                Executor::new(&circuit, args.id, mt_provider),
            )
            .await?
    };
    let input = BitVec::repeat(false, 768);
    let _out = comm_stats
        .record(
            Phase::Online,
            executor.execute(Input::Scalar(input), &mut sender, &mut receiver),
        )
        .await?;

    // Depending on whether a --stats file is set, create a file writer or stdout
    let mut writer: Box<dyn Write> = match args.stats {
        Some(path) => {
            let file = File::create(path)?;
            Box::new(file)
        }
        None => Box::new(stdout()),
    };
    // serde_json is used to write the statistics in json format. `.csv` is currently not
    // supported.
    let mut res = comm_stats.into_run_result();
    res.add_metadata("circuit", "sha256.rs");
    serde_json::to_writer_pretty(&mut writer, &res)?;
    writeln!(writer)?;

    Ok(())
}
source

pub fn with_helper( self, helper_send_counter: Counter, helper_recv_counter: Counter ) -> Self

Add the helper counters. This might be used to track the communication with a trusted third party.

source

pub fn with_sleep(self, sleep: Duration) -> Self

Add a sleep duration at the end of a record or record_helper call to reduce unaccounted communication. This time is not part of the tracked statistics.

source

pub fn without_unaccounted(self, record_as_prev: bool) -> Self

If record_as_prev is set to true (default is false), at the beginning of each phase, every unaccounted communication is recorded as the previous phase. If communication occurs before the first phase, it is still recorded as unaccounted.

Examples found in repository?
crates/seec/examples/sha256.rs (line 67)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
async fn main() -> Result<()> {
    let _guard = init_tracing()?;
    let args = Args::parse();
    let circuit: ExecutableCircuit<bool, BooleanGate, u32> = ExecutableCircuit::DynLayers(
        BaseCircuit::load_bristol(args.circuit, Load::Circuit)?.into(),
    );

    let (mut sender, bytes_written, mut receiver, bytes_read) = match args.id {
        0 => seec_channel::tcp::listen(args.server).await?,
        1 => seec_channel::tcp::connect(args.server).await?,
        illegal => anyhow::bail!("Illegal party id {illegal}. Must be 0 or 1."),
    };

    // Initialize the communication statistics tracker with the counters for the main channel
    let mut comm_stats = Statistics::new(bytes_written, bytes_read).without_unaccounted(true);

    let (mut sender, mut receiver) =
        sub_channels_for!(&mut sender, &mut receiver, 8, Message<BooleanGmw>).await?;

    let mut executor: Executor<BooleanGmw, _> = if let Some(addr) = args.mt_provider {
        let (mt_sender, bytes_written, mt_receiver, bytes_read) =
            seec_channel::tcp::connect(addr).await?;
        // Set the counters for the helper channel
        comm_stats.set_helper(bytes_written, bytes_read);
        let mt_provider = TrustedMTProviderClient::new("unique-id".into(), mt_sender, mt_receiver);
        // As the MTs are generated when the Executor is created, we record the communication
        // with the `record_helper` method and a custom category
        comm_stats
            .record_helper(
                Phase::Custom("Helper-Mts".into()),
                Executor::new(&circuit, args.id, mt_provider),
            )
            .await?
    } else {
        let mt_provider = InsecureMTProvider::default();
        comm_stats
            .record(
                Phase::FunctionDependentSetup,
                Executor::new(&circuit, args.id, mt_provider),
            )
            .await?
    };
    let input = BitVec::repeat(false, 768);
    let _out = comm_stats
        .record(
            Phase::Online,
            executor.execute(Input::Scalar(input), &mut sender, &mut receiver),
        )
        .await?;

    // Depending on whether a --stats file is set, create a file writer or stdout
    let mut writer: Box<dyn Write> = match args.stats {
        Some(path) => {
            let file = File::create(path)?;
            Box::new(file)
        }
        None => Box::new(stdout()),
    };
    // serde_json is used to write the statistics in json format. `.csv` is currently not
    // supported.
    let mut res = comm_stats.into_run_result();
    res.add_metadata("circuit", "sha256.rs");
    serde_json::to_writer_pretty(&mut writer, &res)?;
    writeln!(writer)?;

    Ok(())
}
source

pub fn set_helper( &mut self, helper_send_counter: Counter, helper_recv_counter: Counter )

Set the helper counters. This might be used to track the communication with a trusted third party.

Examples found in repository?
crates/seec/examples/sha256.rs (line 76)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
async fn main() -> Result<()> {
    let _guard = init_tracing()?;
    let args = Args::parse();
    let circuit: ExecutableCircuit<bool, BooleanGate, u32> = ExecutableCircuit::DynLayers(
        BaseCircuit::load_bristol(args.circuit, Load::Circuit)?.into(),
    );

    let (mut sender, bytes_written, mut receiver, bytes_read) = match args.id {
        0 => seec_channel::tcp::listen(args.server).await?,
        1 => seec_channel::tcp::connect(args.server).await?,
        illegal => anyhow::bail!("Illegal party id {illegal}. Must be 0 or 1."),
    };

    // Initialize the communication statistics tracker with the counters for the main channel
    let mut comm_stats = Statistics::new(bytes_written, bytes_read).without_unaccounted(true);

    let (mut sender, mut receiver) =
        sub_channels_for!(&mut sender, &mut receiver, 8, Message<BooleanGmw>).await?;

    let mut executor: Executor<BooleanGmw, _> = if let Some(addr) = args.mt_provider {
        let (mt_sender, bytes_written, mt_receiver, bytes_read) =
            seec_channel::tcp::connect(addr).await?;
        // Set the counters for the helper channel
        comm_stats.set_helper(bytes_written, bytes_read);
        let mt_provider = TrustedMTProviderClient::new("unique-id".into(), mt_sender, mt_receiver);
        // As the MTs are generated when the Executor is created, we record the communication
        // with the `record_helper` method and a custom category
        comm_stats
            .record_helper(
                Phase::Custom("Helper-Mts".into()),
                Executor::new(&circuit, args.id, mt_provider),
            )
            .await?
    } else {
        let mt_provider = InsecureMTProvider::default();
        comm_stats
            .record(
                Phase::FunctionDependentSetup,
                Executor::new(&circuit, args.id, mt_provider),
            )
            .await?
    };
    let input = BitVec::repeat(false, 768);
    let _out = comm_stats
        .record(
            Phase::Online,
            executor.execute(Input::Scalar(input), &mut sender, &mut receiver),
        )
        .await?;

    // Depending on whether a --stats file is set, create a file writer or stdout
    let mut writer: Box<dyn Write> = match args.stats {
        Some(path) => {
            let file = File::create(path)?;
            Box::new(file)
        }
        None => Box::new(stdout()),
    };
    // serde_json is used to write the statistics in json format. `.csv` is currently not
    // supported.
    let mut res = comm_stats.into_run_result();
    res.add_metadata("circuit", "sha256.rs");
    serde_json::to_writer_pretty(&mut writer, &res)?;
    writeln!(writer)?;

    Ok(())
}
source

pub fn set_sleep(&mut self, sleep: Duration)

Set the sleep duration at the end of a record or record_helper call to reduce unaccounted communication. This time is not part of the tracked statistics.

source

pub fn set_without_unaccounted(&mut self, record_as_prev: bool)

source

pub async fn record<F, R>(&mut self, comm: Phase, f: F) -> R
where F: Future<Output = R>,

Record the main channel communication that happens within the future f on a best-effort basis.

Examples found in repository?
crates/seec/examples/sha256.rs (lines 89-92)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
async fn main() -> Result<()> {
    let _guard = init_tracing()?;
    let args = Args::parse();
    let circuit: ExecutableCircuit<bool, BooleanGate, u32> = ExecutableCircuit::DynLayers(
        BaseCircuit::load_bristol(args.circuit, Load::Circuit)?.into(),
    );

    let (mut sender, bytes_written, mut receiver, bytes_read) = match args.id {
        0 => seec_channel::tcp::listen(args.server).await?,
        1 => seec_channel::tcp::connect(args.server).await?,
        illegal => anyhow::bail!("Illegal party id {illegal}. Must be 0 or 1."),
    };

    // Initialize the communication statistics tracker with the counters for the main channel
    let mut comm_stats = Statistics::new(bytes_written, bytes_read).without_unaccounted(true);

    let (mut sender, mut receiver) =
        sub_channels_for!(&mut sender, &mut receiver, 8, Message<BooleanGmw>).await?;

    let mut executor: Executor<BooleanGmw, _> = if let Some(addr) = args.mt_provider {
        let (mt_sender, bytes_written, mt_receiver, bytes_read) =
            seec_channel::tcp::connect(addr).await?;
        // Set the counters for the helper channel
        comm_stats.set_helper(bytes_written, bytes_read);
        let mt_provider = TrustedMTProviderClient::new("unique-id".into(), mt_sender, mt_receiver);
        // As the MTs are generated when the Executor is created, we record the communication
        // with the `record_helper` method and a custom category
        comm_stats
            .record_helper(
                Phase::Custom("Helper-Mts".into()),
                Executor::new(&circuit, args.id, mt_provider),
            )
            .await?
    } else {
        let mt_provider = InsecureMTProvider::default();
        comm_stats
            .record(
                Phase::FunctionDependentSetup,
                Executor::new(&circuit, args.id, mt_provider),
            )
            .await?
    };
    let input = BitVec::repeat(false, 768);
    let _out = comm_stats
        .record(
            Phase::Online,
            executor.execute(Input::Scalar(input), &mut sender, &mut receiver),
        )
        .await?;

    // Depending on whether a --stats file is set, create a file writer or stdout
    let mut writer: Box<dyn Write> = match args.stats {
        Some(path) => {
            let file = File::create(path)?;
            Box::new(file)
        }
        None => Box::new(stdout()),
    };
    // serde_json is used to write the statistics in json format. `.csv` is currently not
    // supported.
    let mut res = comm_stats.into_run_result();
    res.add_metadata("circuit", "sha256.rs");
    serde_json::to_writer_pretty(&mut writer, &res)?;
    writeln!(writer)?;

    Ok(())
}
source

pub async fn record_helper<F, R>(&mut self, phase: Phase, f: F) -> R
where F: Future<Output = R>,

Record the helper channel communication that happens within the future f on a best-effort basis.

Examples found in repository?
crates/seec/examples/sha256.rs (lines 81-84)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
async fn main() -> Result<()> {
    let _guard = init_tracing()?;
    let args = Args::parse();
    let circuit: ExecutableCircuit<bool, BooleanGate, u32> = ExecutableCircuit::DynLayers(
        BaseCircuit::load_bristol(args.circuit, Load::Circuit)?.into(),
    );

    let (mut sender, bytes_written, mut receiver, bytes_read) = match args.id {
        0 => seec_channel::tcp::listen(args.server).await?,
        1 => seec_channel::tcp::connect(args.server).await?,
        illegal => anyhow::bail!("Illegal party id {illegal}. Must be 0 or 1."),
    };

    // Initialize the communication statistics tracker with the counters for the main channel
    let mut comm_stats = Statistics::new(bytes_written, bytes_read).without_unaccounted(true);

    let (mut sender, mut receiver) =
        sub_channels_for!(&mut sender, &mut receiver, 8, Message<BooleanGmw>).await?;

    let mut executor: Executor<BooleanGmw, _> = if let Some(addr) = args.mt_provider {
        let (mt_sender, bytes_written, mt_receiver, bytes_read) =
            seec_channel::tcp::connect(addr).await?;
        // Set the counters for the helper channel
        comm_stats.set_helper(bytes_written, bytes_read);
        let mt_provider = TrustedMTProviderClient::new("unique-id".into(), mt_sender, mt_receiver);
        // As the MTs are generated when the Executor is created, we record the communication
        // with the `record_helper` method and a custom category
        comm_stats
            .record_helper(
                Phase::Custom("Helper-Mts".into()),
                Executor::new(&circuit, args.id, mt_provider),
            )
            .await?
    } else {
        let mt_provider = InsecureMTProvider::default();
        comm_stats
            .record(
                Phase::FunctionDependentSetup,
                Executor::new(&circuit, args.id, mt_provider),
            )
            .await?
    };
    let input = BitVec::repeat(false, 768);
    let _out = comm_stats
        .record(
            Phase::Online,
            executor.execute(Input::Scalar(input), &mut sender, &mut receiver),
        )
        .await?;

    // Depending on whether a --stats file is set, create a file writer or stdout
    let mut writer: Box<dyn Write> = match args.stats {
        Some(path) => {
            let file = File::create(path)?;
            Box::new(file)
        }
        None => Box::new(stdout()),
    };
    // serde_json is used to write the statistics in json format. `.csv` is currently not
    // supported.
    let mut res = comm_stats.into_run_result();
    res.add_metadata("circuit", "sha256.rs");
    serde_json::to_writer_pretty(&mut writer, &res)?;
    writeln!(writer)?;

    Ok(())
}
source

pub fn get(self, phase: &Phase) -> Option<(CountPair, Duration)>

Get the statistics for a phase.

source

pub fn into_run_result(self) -> RunResult

Convert into a RunResult which can be serialized via serde.

Examples found in repository?
crates/seec/examples/sha256.rs (line 113)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
async fn main() -> Result<()> {
    let _guard = init_tracing()?;
    let args = Args::parse();
    let circuit: ExecutableCircuit<bool, BooleanGate, u32> = ExecutableCircuit::DynLayers(
        BaseCircuit::load_bristol(args.circuit, Load::Circuit)?.into(),
    );

    let (mut sender, bytes_written, mut receiver, bytes_read) = match args.id {
        0 => seec_channel::tcp::listen(args.server).await?,
        1 => seec_channel::tcp::connect(args.server).await?,
        illegal => anyhow::bail!("Illegal party id {illegal}. Must be 0 or 1."),
    };

    // Initialize the communication statistics tracker with the counters for the main channel
    let mut comm_stats = Statistics::new(bytes_written, bytes_read).without_unaccounted(true);

    let (mut sender, mut receiver) =
        sub_channels_for!(&mut sender, &mut receiver, 8, Message<BooleanGmw>).await?;

    let mut executor: Executor<BooleanGmw, _> = if let Some(addr) = args.mt_provider {
        let (mt_sender, bytes_written, mt_receiver, bytes_read) =
            seec_channel::tcp::connect(addr).await?;
        // Set the counters for the helper channel
        comm_stats.set_helper(bytes_written, bytes_read);
        let mt_provider = TrustedMTProviderClient::new("unique-id".into(), mt_sender, mt_receiver);
        // As the MTs are generated when the Executor is created, we record the communication
        // with the `record_helper` method and a custom category
        comm_stats
            .record_helper(
                Phase::Custom("Helper-Mts".into()),
                Executor::new(&circuit, args.id, mt_provider),
            )
            .await?
    } else {
        let mt_provider = InsecureMTProvider::default();
        comm_stats
            .record(
                Phase::FunctionDependentSetup,
                Executor::new(&circuit, args.id, mt_provider),
            )
            .await?
    };
    let input = BitVec::repeat(false, 768);
    let _out = comm_stats
        .record(
            Phase::Online,
            executor.execute(Input::Scalar(input), &mut sender, &mut receiver),
        )
        .await?;

    // Depending on whether a --stats file is set, create a file writer or stdout
    let mut writer: Box<dyn Write> = match args.stats {
        Some(path) => {
            let file = File::create(path)?;
            Box::new(file)
        }
        None => Box::new(stdout()),
    };
    // serde_json is used to write the statistics in json format. `.csv` is currently not
    // supported.
    let mut res = comm_stats.into_run_result();
    res.add_metadata("circuit", "sha256.rs");
    serde_json::to_writer_pretty(&mut writer, &res)?;
    writeln!(writer)?;

    Ok(())
}

Trait Implementations§

source§

impl Default for Statistics

source§

fn default() -> Statistics

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more