From 3b633b08bd902c4fc2319cf8ca44debaafc1dcd8 Mon Sep 17 00:00:00 2001 From: Joey Kraut Date: Tue, 10 Oct 2023 09:30:43 -0700 Subject: [PATCH] meta: Cleanup crate, comments, README, etc after curve generics refactor --- Cargo.toml | 8 +-- README.md | 21 +++--- benches/circuit_msm_throughput.rs | 6 +- benches/circuit_mul_throughput.rs | 2 +- benches/gate_throughput.rs | 8 +-- benches/gate_throughput_traced.rs | 8 +-- benches/growable_buffer.rs | 2 +- benches/native_msm.rs | 8 +-- benches/test_stats.rs | 2 +- integration/authenticated_curve.rs | 48 ++++++------- integration/authenticated_scalar.rs | 6 +- integration/circuits.rs | 4 +- integration/fabric.rs | 2 +- integration/helpers.rs | 10 +-- integration/main.rs | 10 +-- integration/mpc_curve.rs | 58 ++++++++-------- integration/mpc_scalar.rs | 4 +- src/algebra/authenticated_curve.rs | 104 ++++++++++++++-------------- src/algebra/authenticated_scalar.rs | 40 +++++------ src/algebra/curve.rs | 27 ++++---- src/algebra/mpc_curve.rs | 4 +- src/algebra/mpc_scalar.rs | 14 ++-- src/algebra/scalar.rs | 12 ++-- src/commitment.rs | 2 +- src/fabric.rs | 10 +-- src/lib.rs | 4 +- 26 files changed, 213 insertions(+), 211 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d7c5430..1a03e14 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,17 +1,17 @@ [package] -name = "mpc-stark" -version = "0.2.4" +name = "ark-mpc" +version = "0.1.0" description = "Malicious-secure SPDZ style two party secure computation" keywords = ["mpc", "crypto", "cryptography"] homepage = "https://renegade.fi" authors = ["Joey Kraut "] edition = "2021" readme = "README.md" -repository = "https://github.com/renegade-fi/mpc-ristretto" +repository = "https://github.com/renegade-fi/ark-mpc" license = "MIT OR Apache-2.0" [lib] -name = "mpc_stark" +name = "ark_mpc" path = "src/lib.rs" [features] diff --git a/README.md b/README.md index 90bda06..49790e4 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,31 @@ -# MPC-Stark +# `ark-mpc`
## Example -`mpc-stark` provides a malicious secure [SPDZ](https://eprint.iacr.org/2011/535.pdf) style framework for two party secure computation. The circuit is constructed on the fly, by overloading arithmetic operators of MPC types, see the example below in which each of the parties shares a value and together they compute the product: +`ark-mpc` provides a malicious secure [SPDZ](https://eprint.iacr.org/2011/535.pdf) style framework for two party secure computation. The circuit is constructed on the fly, by overloading arithmetic operators of MPC types, see the example below in which each of the parties shares a value and together they compute the product: ```rust -use mpc_stark::{ +use ark_mpc::{ algebra::scalar::Scalar, beaver::SharedValueSource, network::QuicTwoPartyNet, MpcFabric, PARTY0, PARTY1, }; +use ark_curve25519::EdwardsProjective as Curve25519Projective; use rand::thread_rng; +type Curve = Curve25519Projective; + #[tokio::main] async fn main() { // Beaver source should be defined outside of the crate and rely on separate infrastructure @@ -34,7 +37,7 @@ async fn main() { // MPC circuit let mut rng = thread_rng(); - let my_val = Scalar::random(&mut rng); + let my_val = Scalar::::random(&mut rng); let fabric = MpcFabric::new(network, beaver); let a = fabric.share_scalar(my_val, PARTY0 /* sender */); // party0 value @@ -50,7 +53,7 @@ async fn main() { ## Tests Unit tests for isolated parts of the library are available via ```bash -cargo test --lib +cargo test --lib --all-features ``` The bulk of this library's testing is best done with real communication; and so most of the tests are integration tests. The integration tests can be run as diff --git a/benches/circuit_msm_throughput.rs b/benches/circuit_msm_throughput.rs index 6f85116..a94db3f 100644 --- a/benches/circuit_msm_throughput.rs +++ b/benches/circuit_msm_throughput.rs @@ -2,11 +2,11 @@ use std::time::{Duration, Instant}; -use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; -use itertools::Itertools; -use mpc_stark::{ +use ark_mpc::{ algebra::authenticated_curve::AuthenticatedPointResult, test_helpers::execute_mock_mpc, }; +use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; +use itertools::Itertools; use tokio::runtime::Builder as RuntimeBuilder; /// Measure the throughput and latency of a variable-sized MSM diff --git a/benches/circuit_mul_throughput.rs b/benches/circuit_mul_throughput.rs index 10c794f..bdf2e68 100644 --- a/benches/circuit_mul_throughput.rs +++ b/benches/circuit_mul_throughput.rs @@ -2,8 +2,8 @@ use std::time::{Duration, Instant}; +use ark_mpc::{test_helpers::execute_mock_mpc, PARTY0}; use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; -use mpc_stark::{test_helpers::execute_mock_mpc, PARTY0}; use tokio::runtime::Builder as RuntimeBuilder; /// Measure the throughput and latency of a set of sequential multiplication gates diff --git a/benches/gate_throughput.rs b/benches/gate_throughput.rs index 39efd9b..fde669e 100644 --- a/benches/gate_throughput.rs +++ b/benches/gate_throughput.rs @@ -1,14 +1,14 @@ use std::{path::Path, sync::Mutex}; +use ark_mpc::{ + algebra::scalar::Scalar, beaver::PartyIDBeaverSource, network::NoRecvNetwork, + test_helpers::TestCurve, MpcFabric, PARTY0, +}; use cpuprofiler::{Profiler as CpuProfiler, PROFILER}; use criterion::{ criterion_group, criterion_main, profiler::Profiler as CriterionProfiler, BenchmarkId, Criterion, Throughput, }; -use mpc_stark::{ - algebra::scalar::Scalar, beaver::PartyIDBeaverSource, network::NoRecvNetwork, - test_helpers::TestCurve, MpcFabric, PARTY0, -}; use rand::{rngs::OsRng, thread_rng}; use tokio::runtime::Builder as RuntimeBuilder; diff --git a/benches/gate_throughput_traced.rs b/benches/gate_throughput_traced.rs index cc71280..b95d672 100644 --- a/benches/gate_throughput_traced.rs +++ b/benches/gate_throughput_traced.rs @@ -3,13 +3,13 @@ use std::time::Instant; -use clap::Parser; -use cpuprofiler::PROFILER; -use gperftools::HEAP_PROFILER; -use mpc_stark::{ +use ark_mpc::{ algebra::scalar::Scalar, beaver::PartyIDBeaverSource, network::NoRecvNetwork, test_helpers::TestCurve, MpcFabric, PARTY0, }; +use clap::Parser; +use cpuprofiler::PROFILER; +use gperftools::HEAP_PROFILER; use rand::thread_rng; // ----------- diff --git a/benches/growable_buffer.rs b/benches/growable_buffer.rs index a51415c..dde700f 100644 --- a/benches/growable_buffer.rs +++ b/benches/growable_buffer.rs @@ -1,5 +1,5 @@ +use ark_mpc::{algebra::scalar::Scalar, buffer::GrowableBuffer, test_helpers::TestCurve}; use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; -use mpc_stark::{algebra::scalar::Scalar, buffer::GrowableBuffer, test_helpers::TestCurve}; // -------------- // | Benchmarks | diff --git a/benches/native_msm.rs b/benches/native_msm.rs index a70fb10..496cf26 100644 --- a/benches/native_msm.rs +++ b/benches/native_msm.rs @@ -1,12 +1,12 @@ -//! Defines a benchmark for native multiscalar-multiplication on `Scalar` and `StarkPoint` types +//! Defines a benchmark for native multiscalar-multiplication on `Scalar` and `CurvePoint` types -use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; -use itertools::Itertools; -use mpc_stark::{ +use ark_mpc::{ algebra::{curve::CurvePoint, scalar::Scalar}, random_point, test_helpers::TestCurve, }; +use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; +use itertools::Itertools; use rand::thread_rng; /// The maximum power of two to scale the MSM benchmark to diff --git a/benches/test_stats.rs b/benches/test_stats.rs index 0962fa2..3a7738d 100644 --- a/benches/test_stats.rs +++ b/benches/test_stats.rs @@ -1,6 +1,6 @@ //! A simple benchmark for testing that stats collection is properly working -use mpc_stark::{algebra::scalar::Scalar, test_helpers::execute_mock_mpc, PARTY0, PARTY1}; +use ark_mpc::{algebra::scalar::Scalar, test_helpers::execute_mock_mpc, PARTY0, PARTY1}; use rand::{distributions::uniform::SampleRange, thread_rng}; #[tokio::main] diff --git a/integration/authenticated_curve.rs b/integration/authenticated_curve.rs index fbae544..8635c5b 100644 --- a/integration/authenticated_curve.rs +++ b/integration/authenticated_curve.rs @@ -1,7 +1,6 @@ -//! Integration tests for the `AuthenticatedStarkPoint` type +//! Integration tests for the `AuthenticatedPointResult` type -use itertools::Itertools; -use mpc_stark::{ +use ark_mpc::{ algebra::{ authenticated_curve::{ test_helpers::{modify_mac, modify_public_modifier, modify_share}, @@ -11,6 +10,7 @@ use mpc_stark::{ }, random_point, PARTY0, PARTY1, }; +use itertools::Itertools; use rand::thread_rng; use crate::{ @@ -151,7 +151,7 @@ fn test_batch_add(test_args: &IntegrationTestArgs) -> Result<(), String> { assert_point_batches_eq(res_open, expected_result) } -/// Test addition between a batch of `AuthenticatedStarkPoint`s and `StarkPoint`s +/// Test addition between a batch of `AuthenticatedPointResult`s and `CurvePoint`s fn test_batch_add_public(test_args: &IntegrationTestArgs) -> Result<(), String> { let n = 10; let fabric = &test_args.fabric; @@ -245,7 +245,7 @@ fn test_batch_sub(test_args: &IntegrationTestArgs) -> Result<(), String> { assert_point_batches_eq(res_open, expected_result) } -/// Test addition between a batch of `AuthenticatedStarkPoint`s and `StarkPoint`s +/// Test addition between a batch of `AuthenticatedPointResult`s and `CurvePoint`s fn test_batch_sub_public(test_args: &IntegrationTestArgs) -> Result<(), String> { let n = 10; let fabric = &test_args.fabric; @@ -384,7 +384,7 @@ fn test_batch_mul(test_args: &IntegrationTestArgs) -> Result<(), String> { assert_point_batches_eq(res_open, expected_result) } -/// Test addition between a batch of `AuthenticatedStarkPoint`s and `StarkPoint`s +/// Test addition between a batch of `AuthenticatedPointResult`s and `CurvePoint`s fn test_batch_mul_public(test_args: &IntegrationTestArgs) -> Result<(), String> { let n = 10; let fabric = &test_args.fabric; @@ -412,91 +412,91 @@ fn test_batch_mul_public(test_args: &IntegrationTestArgs) -> Result<(), String> } inventory::submit!(IntegrationTest { - name: "authenticated_stark_point::test_open_authenticated", + name: "authenticated_curve_point::test_open_authenticated", test_fn: test_open_authenticated }); inventory::submit!(IntegrationTest { - name: "authenticated_stark_point::test_open_authenticated__bad_mac", + name: "authenticated_curve_point::test_open_authenticated__bad_mac", test_fn: test_open_authenticated__bad_mac }); inventory::submit!(IntegrationTest { - name: "authenticated_stark_point::test_open_authenticated__bad_share", + name: "authenticated_curve_point::test_open_authenticated__bad_share", test_fn: test_open_authenticated__bad_share }); inventory::submit!(IntegrationTest { - name: "authenticated_stark_point::test_open_authenticated__bad_public_modifier", + name: "authenticated_curve_point::test_open_authenticated__bad_public_modifier", test_fn: test_open_authenticated__bad_public_modifier }); inventory::submit!(IntegrationTest { - name: "authenticated_stark_point::test_addition_public_point", + name: "authenticated_curve_point::test_addition_public_point", test_fn: test_addition_public_point }); inventory::submit!(IntegrationTest { - name: "authenticated_stark_point::test_add", + name: "authenticated_curve_point::test_add", test_fn: test_add }); inventory::submit!(IntegrationTest { - name: "authenticated_stark_point::test_batch_add", + name: "authenticated_curve_point::test_batch_add", test_fn: test_batch_add }); inventory::submit!(IntegrationTest { - name: "authenticated_stark_point::test_batch_add_public", + name: "authenticated_curve_point::test_batch_add_public", test_fn: test_batch_add_public }); inventory::submit!(IntegrationTest { - name: "authenticated_stark_point::test_sub_public_point", + name: "authenticated_curve_point::test_sub_public_point", test_fn: test_sub_public_point }); inventory::submit!(IntegrationTest { - name: "authenticated_stark_point::test_sub", + name: "authenticated_curve_point::test_sub", test_fn: test_sub }); inventory::submit!(IntegrationTest { - name: "authenticated_stark_point::test_batch_sub", + name: "authenticated_curve_point::test_batch_sub", test_fn: test_batch_sub }); inventory::submit!(IntegrationTest { - name: "authenticated_stark_point::test_batch_sub_public", + name: "authenticated_curve_point::test_batch_sub_public", test_fn: test_batch_sub_public }); inventory::submit!(IntegrationTest { - name: "authenticated_stark_point::test_negation", + name: "authenticated_curve_point::test_negation", test_fn: test_negation }); inventory::submit!(IntegrationTest { - name: "authenticated_stark_point::test_batch_negation", + name: "authenticated_curve_point::test_batch_negation", test_fn: test_batch_negation }); inventory::submit!(IntegrationTest { - name: "authenticated_stark_point::test_multiplication_public_scalar", + name: "authenticated_curve_point::test_multiplication_public_scalar", test_fn: test_multiplication_public_scalar }); inventory::submit!(IntegrationTest { - name: "authenticated_stark_point::test_multiplication", + name: "authenticated_curve_point::test_multiplication", test_fn: test_multiplication }); inventory::submit!(IntegrationTest { - name: "authenticated_stark_point::test_batch_mul", + name: "authenticated_curve_point::test_batch_mul", test_fn: test_batch_mul }); inventory::submit!(IntegrationTest { - name: "authenticated_stark_point::test_batch_mul_public", + name: "authenticated_curve_point::test_batch_mul_public", test_fn: test_batch_mul_public }); diff --git a/integration/authenticated_scalar.rs b/integration/authenticated_scalar.rs index 68c2101..939717e 100644 --- a/integration/authenticated_scalar.rs +++ b/integration/authenticated_scalar.rs @@ -1,14 +1,14 @@ -//! Integration tests for arithmetic on the `AuthenticatedScalar` type which provides +//! Integration tests for arithmetic on the `AuthenticatedScalarResult` type which provides //! a malicious-secure primitive -use itertools::Itertools; -use mpc_stark::{ +use ark_mpc::{ algebra::{ authenticated_scalar::{test_helpers::*, AuthenticatedScalarResult}, scalar::Scalar, }, ResultValue, PARTY0, PARTY1, }; +use itertools::Itertools; use rand::thread_rng; use std::ops::Neg; diff --git a/integration/circuits.rs b/integration/circuits.rs index 5cfe1a7..5a31bc6 100644 --- a/integration/circuits.rs +++ b/integration/circuits.rs @@ -1,13 +1,13 @@ //! Tests for more complicated operations (i.e. circuits) -use itertools::Itertools; -use mpc_stark::{ +use ark_mpc::{ algebra::{ authenticated_curve::AuthenticatedPointResult, authenticated_scalar::AuthenticatedScalarResult, scalar::Scalar, }, random_point, PARTY0, PARTY1, }; +use itertools::Itertools; use rand::thread_rng; use crate::{ diff --git a/integration/fabric.rs b/integration/fabric.rs index 8c58b71..ed955aa 100644 --- a/integration/fabric.rs +++ b/integration/fabric.rs @@ -1,6 +1,6 @@ //! Defines tests for the fabric directly -use mpc_stark::{algebra::scalar::Scalar, PARTY0, PARTY1}; +use ark_mpc::{algebra::scalar::Scalar, PARTY0, PARTY1}; use crate::{ helpers::{assert_scalars_eq, await_result, share_scalar}, diff --git a/integration/helpers.rs b/integration/helpers.rs index 205c20c..e579cee 100644 --- a/integration/helpers.rs +++ b/integration/helpers.rs @@ -1,10 +1,8 @@ -//! Defines testing mocks +//! Defines testing mocks and helpers for integration tests use std::fmt::Debug; -use futures::{future::join_all, Future}; -use itertools::Itertools; -use mpc_stark::{ +use ark_mpc::{ algebra::{ authenticated_curve::AuthenticatedPointResult, authenticated_scalar::AuthenticatedScalarResult, mpc_curve::MpcPointResult, @@ -14,6 +12,8 @@ use mpc_stark::{ network::{NetworkPayload, PartyId}, {MpcFabric, ResultHandle, ResultValue}, }; +use futures::{future::join_all, Future}; +use itertools::Itertools; use tokio::runtime::Handle; // ----------- @@ -147,7 +147,7 @@ pub(crate) fn share_point( sender: PartyId, test_args: &IntegrationTestArgs, ) -> MpcPointResult { - // Share the point then cast to an `MpcStarkPoint` + // Share the point then cast to an `MpcPointResult` let authenticated_point = share_authenticated_point(value, sender, test_args); authenticated_point.mpc_share() } diff --git a/integration/main.rs b/integration/main.rs index 2c2a919..e17576d 100644 --- a/integration/main.rs +++ b/integration/main.rs @@ -2,17 +2,17 @@ use std::{borrow::Borrow, io::Write, net::SocketAddr, process::exit, thread, tim use ark_curve25519::Curve25519Config; use ark_ec::twisted_edwards::Projective; +use ark_mpc::{ + algebra::{curve::CurvePoint, scalar::Scalar}, + network::{NetworkOutbound, NetworkPayload, QuicTwoPartyNet}, + MpcFabric, PARTY0, +}; use clap::Parser; use colored::Colorize; use dns_lookup::lookup_host; use env_logger::Builder; use futures::{SinkExt, StreamExt}; use helpers::PartyIDBeaverSource; -use mpc_stark::{ - algebra::{curve::CurvePoint, scalar::Scalar}, - network::{NetworkOutbound, NetworkPayload, QuicTwoPartyNet}, - MpcFabric, PARTY0, -}; use tokio::runtime::{Builder as RuntimeBuilder, Handle}; use tracing::log::{self, LevelFilter}; diff --git a/integration/mpc_curve.rs b/integration/mpc_curve.rs index 4ae0766..c5f1c6c 100644 --- a/integration/mpc_curve.rs +++ b/integration/mpc_curve.rs @@ -1,7 +1,6 @@ -//! Defines tests for the `MpcStarkPoint` type and arithmetic on this type +//! Defines tests for the `MpcPointResult` type and arithmetic on this type -use itertools::Itertools; -use mpc_stark::{ +use ark_mpc::{ algebra::{ curve::CurvePointResult, mpc_curve::MpcPointResult, @@ -9,6 +8,7 @@ use mpc_stark::{ }, random_point, PARTY0, PARTY1, }; +use itertools::Itertools; use rand::thread_rng; use crate::{ @@ -20,7 +20,7 @@ use crate::{ IntegrationTest, IntegrationTestArgs, TestCurve, }; -/// Test addition of `MpcStarkPoint` types +/// Test addition of `MpcPointResult` types fn test_add(test_args: &IntegrationTestArgs) -> Result<(), String> { let val = random_point(); let my_value = test_args.fabric.allocate_point(val); @@ -42,7 +42,7 @@ fn test_add(test_args: &IntegrationTestArgs) -> Result<(), String> { assert_points_eq(opened_res, expected_result) } -/// Test addition of `MpcStarkPoint` with `StarkPoint` +/// Test addition of `MpcPointResult` with `CurvePoint` /// /// Party 0 chooses an MPC point and party 1 chooses a plaintext point fn test_add_point_constant(test_args: &IntegrationTestArgs) -> Result<(), String> { @@ -67,7 +67,7 @@ fn test_add_point_constant(test_args: &IntegrationTestArgs) -> Result<(), String assert_points_eq(opened_res, expected_result) } -/// Test adding a batch of `MpcStarkPoint` types +/// Test adding a batch of `MpcPointResult` types fn test_batch_add(test_args: &IntegrationTestArgs) -> Result<(), String> { let n = 10; let fabric = &test_args.fabric; @@ -95,7 +95,7 @@ fn test_batch_add(test_args: &IntegrationTestArgs) -> Result<(), String> { assert_point_batches_eq(opened_res, expected_result) } -/// Tests addition between a batch of `MpcPointResult`s and `StarkPointResult`s +/// Tests addition between a batch of `MpcPointResult`s and `CurvePointResult`s fn test_batch_add_public(test_args: &IntegrationTestArgs) -> Result<(), String> { let n = 10; let fabric = &test_args.fabric; @@ -120,7 +120,7 @@ fn test_batch_add_public(test_args: &IntegrationTestArgs) -> Result<(), String> assert_point_batches_eq(res_open, expected_result) } -/// Test subtraction of `MpcStarkPoint` types +/// Test subtraction of `MpcPointResult` types fn test_sub(test_args: &IntegrationTestArgs) -> Result<(), String> { let val = random_point(); let my_value = test_args.fabric.allocate_point(val); @@ -142,7 +142,7 @@ fn test_sub(test_args: &IntegrationTestArgs) -> Result<(), String> { assert_points_eq(opened_res, expected_result) } -/// Test subtraction of `MpcStarkPoint` with `StarkPoint` +/// Test subtraction of `MpcPointResult` with `PointResult` /// /// Party 0 chooses an MPC point and party 1 chooses a plaintext point fn test_sub_point_constant(test_args: &IntegrationTestArgs) -> Result<(), String> { @@ -167,7 +167,7 @@ fn test_sub_point_constant(test_args: &IntegrationTestArgs) -> Result<(), String assert_points_eq(opened_res, expected_result) } -/// Test subtracting a batch of `MpcStarkPoint` types +/// Test subtracting a batch of `MpcPointResult` types fn test_batch_sub(test_args: &IntegrationTestArgs) -> Result<(), String> { let n = 10; let fabric = &test_args.fabric; @@ -195,7 +195,7 @@ fn test_batch_sub(test_args: &IntegrationTestArgs) -> Result<(), String> { assert_point_batches_eq(opened_res, expected_result) } -/// Test subtracting a batch of `MpcStarkPoint` types and `StarkPointResult`s +/// Test subtracting a batch of `MpcPointResult` types and `CurvePointResult`s fn test_batch_sub_public(test_args: &IntegrationTestArgs) -> Result<(), String> { let n = 10; let fabric = &test_args.fabric; @@ -220,7 +220,7 @@ fn test_batch_sub_public(test_args: &IntegrationTestArgs) -> Result<(), String> assert_point_batches_eq(res_open, expected_result) } -/// Test negation of `MpcStarkPoint` types +/// Test negation of `MpcPointResult` types fn test_neg(test_args: &IntegrationTestArgs) -> Result<(), String> { let val = random_point(); let my_value = test_args.fabric.allocate_point(val); @@ -240,7 +240,7 @@ fn test_neg(test_args: &IntegrationTestArgs) -> Result<(), String> { assert_points_eq(opened_res, expected_result) } -/// Test negating a batch of `MpcStarkPoint` types +/// Test negating a batch of `MpcPointResult` types fn test_batch_neg(test_args: &IntegrationTestArgs) -> Result<(), String> { let n = 10; let fabric = &test_args.fabric; @@ -264,7 +264,7 @@ fn test_batch_neg(test_args: &IntegrationTestArgs) -> Result<(), String> { assert_point_batches_eq(opened_res, expected_result) } -/// Test multiplication of an `MpcStarkPoint` type with an `MpcScalarResult` type +/// Test multiplication of an `MpcPointResult` type with an `MpcScalarResult` type /// /// Party 0 chooses the point, party 1 chooses the scalar fn test_mul(test_args: &IntegrationTestArgs) -> Result<(), String> { @@ -331,7 +331,7 @@ fn test_mul_scalar_constant(test_args: &IntegrationTestArgs) -> Result<(), Strin assert_points_eq(opened_res, expected_result) } -/// Test multiplying a batch of `MpcStarkPoint` types +/// Test multiplying a batch of `MpcPointResult` types /// /// Party 0 chooses the points and party 1 chooses the scalars fn test_batch_mul(test_args: &IntegrationTestArgs) -> Result<(), String> { @@ -397,71 +397,71 @@ fn test_batch_mul_public(test_args: &IntegrationTestArgs) -> Result<(), String> } inventory::submit!(IntegrationTest { - name: "mpc_stark_point::test_add", + name: "mpc_point::test_add", test_fn: test_add, }); inventory::submit!(IntegrationTest { - name: "mpc_stark_point::test_add_point_constant", + name: "mpc_point::test_add_point_constant", test_fn: test_add_point_constant, }); inventory::submit!(IntegrationTest { - name: "mpc_stark_point::test_batch_add", + name: "mpc_point::test_batch_add", test_fn: test_batch_add, }); inventory::submit!(IntegrationTest { - name: "mpc_stark_point::test_batch_add_public", + name: "mpc_point::test_batch_add_public", test_fn: test_batch_add_public, }); inventory::submit!(IntegrationTest { - name: "mpc_stark_point::test_sub", + name: "mpc_point::test_sub", test_fn: test_sub, }); inventory::submit!(IntegrationTest { - name: "mpc_stark_point::test_sub_point_constant", + name: "mpc_point::test_sub_point_constant", test_fn: test_sub_point_constant, }); inventory::submit!(IntegrationTest { - name: "mpc_stark_point::test_batch_sub", + name: "mpc_point::test_batch_sub", test_fn: test_batch_sub, }); inventory::submit!(IntegrationTest { - name: "mpc_stark_point::test_batch_sub_public", + name: "mpc_point::test_batch_sub_public", test_fn: test_batch_sub_public, }); inventory::submit!(IntegrationTest { - name: "mpc_stark_point::test_neg", + name: "mpc_point::test_neg", test_fn: test_neg, }); inventory::submit!(IntegrationTest { - name: "mpc_stark_point::test_batch_neg", + name: "mpc_point::test_batch_neg", test_fn: test_batch_neg, }); inventory::submit!(IntegrationTest { - name: "mpc_stark_point::test_mul", + name: "mpc_point::test_mul", test_fn: test_mul, }); inventory::submit!(IntegrationTest { - name: "mpc_stark_point::test_mul_scalar_constant", + name: "mpc_point::test_mul_scalar_constant", test_fn: test_mul_scalar_constant, }); inventory::submit!(IntegrationTest { - name: "mpc_stark_point::test_batch_mul", + name: "mpc_point::test_batch_mul", test_fn: test_batch_mul, }); inventory::submit!(IntegrationTest { - name: "mpc_stark_point::test_batch_mul_public", + name: "mpc_point::test_batch_mul_public", test_fn: test_batch_mul_public, }); diff --git a/integration/mpc_scalar.rs b/integration/mpc_scalar.rs index fd54527..352c5ba 100644 --- a/integration/mpc_scalar.rs +++ b/integration/mpc_scalar.rs @@ -1,12 +1,12 @@ //! Defines unit tests for `MpcScalarResult` types -use itertools::Itertools; -use mpc_stark::{ +use ark_mpc::{ algebra::{ mpc_scalar::MpcScalarResult, scalar::{Scalar, ScalarResult}, }, PARTY0, PARTY1, }; +use itertools::Itertools; use rand::thread_rng; use std::ops::Neg; diff --git a/src/algebra/authenticated_curve.rs b/src/algebra/authenticated_curve.rs index 173a234..ddef4d0 100644 --- a/src/algebra/authenticated_curve.rs +++ b/src/algebra/authenticated_curve.rs @@ -28,10 +28,10 @@ use super::{ scalar::{Scalar, ScalarResult}, }; -/// The number of underlying results in an `AuthenticatedPointResult` -pub(crate) const AUTHENTICATED_STARK_POINT_RESULT_LEN: usize = 3; +/// The number of underlying results in an `AuthenticatedPointResult` +pub(crate) const AUTHENTICATED_POINT_RESULT_LEN: usize = 3; -/// A maliciously secure wrapper around `MpcCurvePoint` that includes a MAC as per +/// A maliciously secure wrapper around `MpcPointResult` that includes a MAC as per /// the SPDZ protocol: https://eprint.iacr.org/2011/535.pdf #[derive(Clone)] pub struct AuthenticatedPointResult { @@ -39,7 +39,8 @@ pub struct AuthenticatedPointResult { pub(crate) share: MpcPointResult, /// A SPDZ style, unconditionally secure MAC of the value /// This is used to ensure computational integrity of the opened value - /// See the doc comment in `AuthenticatedScalar` for more details + /// + /// See the doc comment in `AuthenticatedScalar` for more details pub(crate) mac: MpcPointResult, /// The public modifier tracks additions and subtractions of public values to the shares /// @@ -59,9 +60,9 @@ impl Debug for AuthenticatedPointResult { } impl AuthenticatedPointResult { - /// Creates a new `AuthenticatedCurvePoint` from a given underlying point + /// Creates a new authenticated point from a given underlying point pub fn new_shared(value: CurvePointResult) -> AuthenticatedPointResult { - // Create an `MpcCurvePoint` from the value + // Create an mpc point from the value let fabric_clone = value.fabric.clone(); let mpc_value = MpcPointResult::new_shared(value); @@ -77,13 +78,13 @@ impl AuthenticatedPointResult { } } - /// Creates a batch of `AuthenticatedCurvePoint`s from a given batch of underlying points + /// Creates a batch of `AuthenticatedPointResult`s from a given batch of underlying points pub fn new_shared_batch(values: &[CurvePointResult]) -> Vec> { if values.is_empty() { return vec![]; } - // Create an `MpcCurvePoint` from the value + // Create mpc points from the value let n = values.len(); let fabric = values[0].fabric(); let mpc_values = values @@ -107,10 +108,10 @@ impl AuthenticatedPointResult { .collect_vec() } - /// Creates a batch of `AuthenticatedCurvePoint`s from a batch result + /// Creates a batch of `AuthenticatedPointResult`s from a batch result /// /// The batch result combines the batch into one result, so it must be split out - /// first before creating the `AuthenticatedPointResult`s + /// first before creating the `AuthenticatedPointResult`s pub fn new_shared_from_batch_result( values: BatchCurvePointResult, n: usize, @@ -132,7 +133,7 @@ impl AuthenticatedPointResult { self.share.id() } - /// Get the IDs of the results that make up the `AuthenticatedPointResult` representation + /// Get the IDs of the results that make up the `AuthenticatedPointResult` representation pub(crate) fn ids(&self) -> Vec { vec![self.share.id(), self.mac.id(), self.public_modifier.id] } @@ -142,7 +143,7 @@ impl AuthenticatedPointResult { self.share.fabric() } - /// Get the underlying share as an `MpcCurvePoint` + /// Get the underlying share as an `MpcPointResult` #[cfg(feature = "test_helpers")] pub fn mpc_share(&self) -> MpcPointResult { self.share.clone() @@ -158,15 +159,15 @@ impl AuthenticatedPointResult { MpcPointResult::open_batch(&values.iter().map(|v| v.share.clone()).collect_vec()) } - /// Convert a flattened iterator into a batch of `AuthenticatedPointResult`s + /// Convert a flattened iterator into a batch of `AuthenticatedPointResult`s /// /// We assume that the iterator has been flattened in the same way order that `Self::id`s returns - /// the `AuthenticatedScalar`'s values: `[share, mac, public_modifier]` + /// the `AuthenticatedScalar`'s values: `[share, mac, public_modifier]` pub fn from_flattened_iterator(iter: I) -> Vec where I: Iterator>, { - iter.chunks(AUTHENTICATED_STARK_POINT_RESULT_LEN) + iter.chunks(AUTHENTICATED_POINT_RESULT_LEN) .into_iter() .map(|mut chunk| Self { share: chunk.next().unwrap().into(), @@ -195,7 +196,7 @@ impl AuthenticatedPointResult { } // Check that the MAC check shares add up to the additive identity in - // the Starknet curve group + // the curve group if my_mac_share + peer_mac_share != CurvePoint::identity() { return false; } @@ -283,7 +284,7 @@ impl AuthenticatedPointResult { // --- MAC Check --- // // Compute the shares of the MAC check in batch - let mut mac_check_deps = Vec::with_capacity(1 + AUTHENTICATED_STARK_POINT_RESULT_LEN * n); + let mut mac_check_deps = Vec::with_capacity(1 + AUTHENTICATED_POINT_RESULT_LEN * n); mac_check_deps.push(fabric.borrow_mac_key().id()); for i in 0..n { mac_check_deps.push(opened_values[i].id()); @@ -382,7 +383,7 @@ impl AuthenticatedPointResult { } } -/// The value that results from opening an `AuthenticatedPointResult` and checking its MAC. This encapsulates +/// The value that results from opening an `AuthenticatedPointResult` and checking its MAC. This encapsulates /// both the underlying value and the result of the MAC check #[derive(Clone)] pub struct AuthenticatedPointOpenResult { @@ -504,7 +505,7 @@ impl Add<&AuthenticatedPointResult> for &AuthenticatedPointRes impl_borrow_variants!(AuthenticatedPointResult, Add, add, +, AuthenticatedPointResult, C: CurveGroup); impl AuthenticatedPointResult { - /// Add two batches of `AuthenticatedPointResult`s + /// Add two batches of `AuthenticatedPointResult`s pub fn batch_add( a: &[AuthenticatedPointResult], b: &[AuthenticatedPointResult], @@ -520,16 +521,16 @@ impl AuthenticatedPointResult { let res: Vec> = fabric.new_batch_gate_op( all_ids, - AUTHENTICATED_STARK_POINT_RESULT_LEN * n, + AUTHENTICATED_POINT_RESULT_LEN * n, move |mut args| { let len = args.len(); let a_vals = args.drain(..len / 2).collect_vec(); let b_vals = args; - let mut result = Vec::with_capacity(AUTHENTICATED_STARK_POINT_RESULT_LEN * n); + let mut result = Vec::with_capacity(AUTHENTICATED_POINT_RESULT_LEN * n); for (a_chunk, b_chunk) in a_vals - .chunks(AUTHENTICATED_STARK_POINT_RESULT_LEN) - .zip(b_vals.chunks(AUTHENTICATED_STARK_POINT_RESULT_LEN)) + .chunks(AUTHENTICATED_POINT_RESULT_LEN) + .zip(b_vals.chunks(AUTHENTICATED_POINT_RESULT_LEN)) { let a_share: CurvePoint = a_chunk[0].clone().into(); let a_mac: CurvePoint = a_chunk[1].clone().into(); @@ -551,7 +552,7 @@ impl AuthenticatedPointResult { Self::from_flattened_iterator(res.into_iter()) } - /// Add a batch of `AuthenticatedPointResult`s to a batch of `CurvePointResult`s + /// Add a batch of `AuthenticatedPointResult`s to a batch of `CurvePointResult`s pub fn batch_add_public( a: &[AuthenticatedPointResult], b: &[CurvePointResult], @@ -576,16 +577,16 @@ impl AuthenticatedPointResult { let party_id = fabric.party_id(); let res: Vec> = fabric.new_batch_gate_op( all_ids, - AUTHENTICATED_STARK_POINT_RESULT_LEN * n, + AUTHENTICATED_POINT_RESULT_LEN * n, move |mut args| { let a_vals = args - .drain(..AUTHENTICATED_STARK_POINT_RESULT_LEN * n) + .drain(..AUTHENTICATED_POINT_RESULT_LEN * n) .collect_vec(); let b_vals = args; - let mut result = Vec::with_capacity(AUTHENTICATED_STARK_POINT_RESULT_LEN * n); + let mut result = Vec::with_capacity(AUTHENTICATED_POINT_RESULT_LEN * n); for (a_chunk, b_val) in a_vals - .chunks(AUTHENTICATED_STARK_POINT_RESULT_LEN) + .chunks(AUTHENTICATED_POINT_RESULT_LEN) .zip(b_vals.into_iter()) { let a_share: CurvePoint = a_chunk[0].clone().into(); @@ -683,7 +684,7 @@ impl Sub<&AuthenticatedPointResult> for &AuthenticatedPointRes impl_borrow_variants!(AuthenticatedPointResult, Sub, sub, -, AuthenticatedPointResult, C: CurveGroup); impl AuthenticatedPointResult { - /// Add two batches of `AuthenticatedPointResult`s + /// Add two batches of `AuthenticatedPointResult`s pub fn batch_sub( a: &[AuthenticatedPointResult], b: &[AuthenticatedPointResult], @@ -699,16 +700,16 @@ impl AuthenticatedPointResult { let res: Vec> = fabric.new_batch_gate_op( all_ids, - AUTHENTICATED_STARK_POINT_RESULT_LEN * n, + AUTHENTICATED_POINT_RESULT_LEN * n, move |mut args| { let len = args.len(); let a_vals = args.drain(..len / 2).collect_vec(); let b_vals = args; - let mut result = Vec::with_capacity(AUTHENTICATED_STARK_POINT_RESULT_LEN * n); + let mut result = Vec::with_capacity(AUTHENTICATED_POINT_RESULT_LEN * n); for (a_chunk, b_chunk) in a_vals - .chunks(AUTHENTICATED_STARK_POINT_RESULT_LEN) - .zip(b_vals.chunks(AUTHENTICATED_STARK_POINT_RESULT_LEN)) + .chunks(AUTHENTICATED_POINT_RESULT_LEN) + .zip(b_vals.chunks(AUTHENTICATED_POINT_RESULT_LEN)) { let a_share: CurvePoint = a_chunk[0].clone().into(); let a_mac: CurvePoint = a_chunk[1].clone().into(); @@ -730,7 +731,7 @@ impl AuthenticatedPointResult { Self::from_flattened_iterator(res.into_iter()) } - /// Subtract a batch of `AuthenticatedPointResult`s to a batch of `CurvePointResult`s + /// Subtract a batch of `AuthenticatedPointResult`s to a batch of `CurvePointResult`s pub fn batch_sub_public( a: &[AuthenticatedPointResult], b: &[CurvePointResult], @@ -755,16 +756,16 @@ impl AuthenticatedPointResult { let party_id = fabric.party_id(); let res: Vec> = fabric.new_batch_gate_op( all_ids, - AUTHENTICATED_STARK_POINT_RESULT_LEN * n, + AUTHENTICATED_POINT_RESULT_LEN * n, move |mut args| { let a_vals = args - .drain(..AUTHENTICATED_STARK_POINT_RESULT_LEN * n) + .drain(..AUTHENTICATED_POINT_RESULT_LEN * n) .collect_vec(); let b_vals = args; - let mut result = Vec::with_capacity(AUTHENTICATED_STARK_POINT_RESULT_LEN * n); + let mut result = Vec::with_capacity(AUTHENTICATED_POINT_RESULT_LEN * n); for (a_chunk, b_val) in a_vals - .chunks(AUTHENTICATED_STARK_POINT_RESULT_LEN) + .chunks(AUTHENTICATED_POINT_RESULT_LEN) .zip(b_vals.into_iter()) { let a_share: CurvePoint = a_chunk[0].clone().into(); @@ -812,7 +813,7 @@ impl Neg for &AuthenticatedPointResult { impl_borrow_variants!(AuthenticatedPointResult, Neg, neg, -, C: CurveGroup); impl AuthenticatedPointResult { - /// Negate a batch of `AuthenticatedPointResult`s + /// Negate a batch of `AuthenticatedPointResult`s pub fn batch_neg(a: &[AuthenticatedPointResult]) -> Vec> { if a.is_empty() { return Vec::new(); @@ -822,17 +823,14 @@ impl AuthenticatedPointResult { let fabric = a[0].fabric(); let all_ids = a.iter().flat_map(|p| p.ids()).collect_vec(); - let res: Vec> = fabric.new_batch_gate_op( - all_ids, - AUTHENTICATED_STARK_POINT_RESULT_LEN * n, - move |args| { + let res: Vec> = + fabric.new_batch_gate_op(all_ids, AUTHENTICATED_POINT_RESULT_LEN * n, move |args| { args.into_iter() .map(CurvePoint::from) .map(CurvePoint::neg) .map(ResultValue::Point) .collect_vec() - }, - ); + }); Self::from_flattened_iterator(res.into_iter()) } @@ -903,7 +901,7 @@ impl_borrow_variants!(AuthenticatedPointResult, Mul, mul, *, AuthenticatedSca impl_commutative!(AuthenticatedPointResult, Mul, mul, *, AuthenticatedScalarResult, C: CurveGroup); impl AuthenticatedPointResult { - /// Multiply a batch of `AuthenticatedPointResult`s by a batch of `AuthenticatedScalarResult`s + /// Multiply a batch of `AuthenticatedPointResult`s by a batch of `AuthenticatedScalarResult`s #[allow(non_snake_case)] pub fn batch_mul( a: &[AuthenticatedScalarResult], @@ -939,7 +937,7 @@ impl AuthenticatedPointResult { AuthenticatedPointResult::batch_add(&de_db_G, &ae_c_G) } - /// Multiply a batch of `AuthenticatedPointResult`s by a batch of `ScalarResult`s + /// Multiply a batch of `AuthenticatedPointResult`s by a batch of `ScalarResult`s pub fn batch_mul_public( a: &[ScalarResult], b: &[AuthenticatedPointResult], @@ -963,16 +961,16 @@ impl AuthenticatedPointResult { let results: Vec> = fabric.new_batch_gate_op( all_ids, - AUTHENTICATED_STARK_POINT_RESULT_LEN * n, /* output_arity */ + AUTHENTICATED_POINT_RESULT_LEN * n, /* output_arity */ move |mut args| { let scalars: Vec> = args.drain(..n).map(Scalar::from).collect_vec(); let points: Vec> = args.into_iter().map(CurvePoint::from).collect_vec(); - let mut result = Vec::with_capacity(AUTHENTICATED_STARK_POINT_RESULT_LEN * n); + let mut result = Vec::with_capacity(AUTHENTICATED_POINT_RESULT_LEN * n); for (scalar, points) in scalars .into_iter() - .zip(points.chunks(AUTHENTICATED_STARK_POINT_RESULT_LEN)) + .zip(points.chunks(AUTHENTICATED_POINT_RESULT_LEN)) { let share: CurvePoint = points[0]; let mac: CurvePoint = points[1]; @@ -1005,7 +1003,7 @@ impl AuthenticatedPointResult { // Multiply the shares in a batch gate let results = fabric.new_batch_gate_op( all_ids, - AUTHENTICATED_STARK_POINT_RESULT_LEN * n, /* output_arity */ + AUTHENTICATED_POINT_RESULT_LEN * n, /* output_arity */ move |args| { let scalars = args.into_iter().map(Scalar::from).collect_vec(); let generator = CurvePoint::generator(); @@ -1050,7 +1048,7 @@ impl AuthenticatedPointResult { let results = fabric.new_batch_gate_op( all_ids, - AUTHENTICATED_STARK_POINT_RESULT_LEN, /* output_arity */ + AUTHENTICATED_POINT_RESULT_LEN, /* output_arity */ move |args| { // Accumulators let mut share = CurvePoint::identity(); @@ -1060,7 +1058,7 @@ impl AuthenticatedPointResult { for mut chunk in args .into_iter() .map(CurvePoint::from) - .chunks(AUTHENTICATED_STARK_POINT_RESULT_LEN) + .chunks(AUTHENTICATED_POINT_RESULT_LEN) .into_iter() { share += chunk.next().unwrap(); diff --git a/src/algebra/authenticated_scalar.rs b/src/algebra/authenticated_scalar.rs index 49f5b82..985c1a0 100644 --- a/src/algebra/authenticated_scalar.rs +++ b/src/algebra/authenticated_scalar.rs @@ -30,7 +30,7 @@ use super::{ /// The number of results wrapped by an `AuthenticatedScalarResult` pub const AUTHENTICATED_SCALAR_RESULT_LEN: usize = 3; -/// A maliciously secure wrapper around an `MpcScalarResult`, includes a MAC as per the +/// A maliciously secure wrapper around an `MpcScalarResult`, includes a MAC as per the /// SPDZ protocol: https://eprint.iacr.org/2011/535.pdf /// that ensures security against a malicious adversary #[derive(Clone)] @@ -42,7 +42,7 @@ pub struct AuthenticatedScalarResult { /// If the value is `x`, parties hold secret shares of the value /// \delta * x for the global MAC key `\delta`. The parties individually /// hold secret shares of this MAC key [\delta], so we can very naturally - /// extend the secret share arithmetic of the underlying `MpcScalarResult` to + /// extend the secret share arithmetic of the underlying `MpcScalarResult` to /// the MAC updates as well pub(crate) mac: MpcScalarResult, /// The public modifier tracks additions and subtractions of public values to the @@ -65,7 +65,7 @@ impl Debug for AuthenticatedScalarResult { impl AuthenticatedScalarResult { /// Create a new result from the given shared value pub fn new_shared(value: ScalarResult) -> Self { - // Create an `MpcScalarResult` to represent the fact that this is a shared value + // Create an `MpcScalarResult` to represent the fact that this is a shared value let fabric = value.fabric.clone(); let mpc_value = MpcScalarResult::new_shared(value); @@ -113,7 +113,7 @@ impl AuthenticatedScalarResult { /// Create a nwe shared batch of values from a batch network result /// /// The batch result combines the batch into one result, so it must be split out - /// first before creating the `AuthenticatedScalarResult`s + /// first before creating the `AuthenticatedScalarResult`s pub fn new_shared_from_batch_result( values: BatchScalarResult, n: usize, @@ -130,13 +130,13 @@ impl AuthenticatedScalarResult { Self::new_shared_batch(&scalar_results) } - /// Get the raw share as an `MpcScalarResult` + /// Get the raw share as an `MpcScalarResult` #[cfg(feature = "test_helpers")] pub fn mpc_share(&self) -> MpcScalarResult { self.share.clone() } - /// Get the raw share as a `ScalarResult` + /// Get the raw share as a `ScalarResult` pub fn share(&self) -> ScalarResult { self.share.to_scalar() } @@ -162,7 +162,7 @@ impl AuthenticatedScalarResult { MpcScalarResult::open_batch(&values.iter().map(|val| val.share.clone()).collect_vec()) } - /// Convert a flattened iterator into a batch of `AuthenticatedScalarResult`s + /// Convert a flattened iterator into a batch of `AuthenticatedScalarResult`s /// /// We assume that the iterator has been flattened in the same way order that `Self::id`s returns /// the `AuthenticatedScalar`'s values: `[share, mac, public_modifier]` @@ -387,7 +387,7 @@ impl AuthenticatedScalarResult { } } -/// The value that results from opening an `AuthenticatedScalarResult` and checking its +/// The value that results from opening an `AuthenticatedScalarResult` and checking its /// MAC. This encapsulates both the underlying value and the result of the MAC check #[derive(Clone)] pub struct AuthenticatedScalarOpenResult { @@ -484,7 +484,7 @@ impl Add<&AuthenticatedScalarResult> for &AuthenticatedScalarR impl_borrow_variants!(AuthenticatedScalarResult, Add, add, +, AuthenticatedScalarResult, Output=AuthenticatedScalarResult, C: CurveGroup); impl AuthenticatedScalarResult { - /// Add two batches of `AuthenticatedScalarResult`s + /// Add two batches of `AuthenticatedScalarResult`s pub fn batch_add( a: &[AuthenticatedScalarResult], b: &[AuthenticatedScalarResult], @@ -533,11 +533,11 @@ impl AuthenticatedScalarResult { }, ); - // Collect the gate results into a series of `AuthenticatedScalarResult`s + // Collect the gate results into a series of `AuthenticatedScalarResult`s AuthenticatedScalarResult::from_flattened_iterator(gate_results.into_iter()) } - /// Add a batch of `AuthenticatedScalarResult`s to a batch of `ScalarResult`s + /// Add a batch of `AuthenticatedScalarResult`s to a batch of `ScalarResult`s pub fn batch_add_public( a: &[AuthenticatedScalarResult], b: &[ScalarResult], @@ -704,7 +704,7 @@ impl Sub<&AuthenticatedScalarResult> for &AuthenticatedScalarR impl_borrow_variants!(AuthenticatedScalarResult, Sub, sub, -, AuthenticatedScalarResult, Output=AuthenticatedScalarResult, C: CurveGroup); impl AuthenticatedScalarResult { - /// Add two batches of `AuthenticatedScalarResult`s + /// Add two batches of `AuthenticatedScalarResult`s pub fn batch_sub( a: &[AuthenticatedScalarResult], b: &[AuthenticatedScalarResult], @@ -753,11 +753,11 @@ impl AuthenticatedScalarResult { }, ); - // Collect the gate results into a series of `AuthenticatedScalarResult`s + // Collect the gate results into a series of `AuthenticatedScalarResult`s AuthenticatedScalarResult::from_flattened_iterator(gate_results.into_iter()) } - /// Subtract a batch of `ScalarResult`s from a batch of `AuthenticatedScalarResult`s + /// Subtract a batch of `ScalarResult`s from a batch of `AuthenticatedScalarResult`s pub fn batch_sub_public( a: &[AuthenticatedScalarResult], b: &[ScalarResult], @@ -813,7 +813,7 @@ impl AuthenticatedScalarResult { }, ); - // Collect the gate results into a series of `AuthenticatedScalarResult`s + // Collect the gate results into a series of `AuthenticatedScalarResult`s AuthenticatedScalarResult::from_flattened_iterator(gate_results.into_iter()) } } @@ -834,7 +834,7 @@ impl Neg for &AuthenticatedScalarResult { impl_borrow_variants!(AuthenticatedScalarResult, Neg, neg, -, C: CurveGroup); impl AuthenticatedScalarResult { - /// Negate a batch of `AuthenticatedScalarResult`s + /// Negate a batch of `AuthenticatedScalarResult`s pub fn batch_neg(a: &[AuthenticatedScalarResult]) -> Vec> { if a.is_empty() { return vec![]; @@ -951,7 +951,7 @@ impl AuthenticatedScalarResult { AuthenticatedScalarResult::batch_add(&de_plus_db, &ea_plus_c) } - /// Multiply a batch of `AuthenticatedScalarResult`s by a batch of `ScalarResult`s + /// Multiply a batch of `AuthenticatedScalarResult`s by a batch of `ScalarResult`s pub fn batch_mul_public( a: &[AuthenticatedScalarResult], b: &[ScalarResult], @@ -1049,12 +1049,12 @@ pub mod test_helpers { use super::AuthenticatedScalarResult; - /// Modify the MAC of an `AuthenticatedScalarResult` + /// Modify the MAC of an `AuthenticatedScalarResult` pub fn modify_mac(val: &mut AuthenticatedScalarResult, new_value: Scalar) { val.mac = val.fabric().allocate_scalar(new_value).into() } - /// Modify the underlying secret share of an `AuthenticatedScalarResult` + /// Modify the underlying secret share of an `AuthenticatedScalarResult` pub fn modify_share( val: &mut AuthenticatedScalarResult, new_value: Scalar, @@ -1062,7 +1062,7 @@ pub mod test_helpers { val.share = val.fabric().allocate_scalar(new_value).into() } - /// Modify the public modifier of an `AuthenticatedScalarResult` by adding an offset + /// Modify the public modifier of an `AuthenticatedScalarResult` by adding an offset pub fn modify_public_modifier( val: &mut AuthenticatedScalarResult, new_value: Scalar, diff --git a/src/algebra/curve.rs b/src/algebra/curve.rs index 0e6870b..eefe29d 100644 --- a/src/algebra/curve.rs +++ b/src/algebra/curve.rs @@ -1,4 +1,5 @@ -//! Defines the `Scalar` type of the Starknet field +//! Defines the `CurvePoint` type, a wrapper around a generic curve that allows us to +//! bring curve arithmetic into the execution graph use std::{ iter::Sum, @@ -13,7 +14,7 @@ use ark_ec::{ HashToCurveError, }, short_weierstrass::Projective, - CurveGroup, + AffineRepr, CurveGroup, }; use ark_ff::PrimeField; @@ -23,7 +24,7 @@ use serde::{de::Error as DeError, Deserialize, Serialize}; use crate::{ algebra::{ - authenticated_curve::AUTHENTICATED_STARK_POINT_RESULT_LEN, + authenticated_curve::AUTHENTICATED_POINT_RESULT_LEN, authenticated_scalar::AUTHENTICATED_SCALAR_RESULT_LEN, }, fabric::{ResultHandle, ResultValue}, @@ -160,9 +161,11 @@ where let p1 = mapper.map_to_curve(f1)?; let p2 = mapper.map_to_curve(f2)?; - // The IETF spec above requires that we clear the cofactor. However, the STARK curve has cofactor - // h = 1, so no works needs to be done - Ok(CurvePoint(p1 + p2)) + // Clear the cofactor + let p1_clear = p1.clear_cofactor(); + let p2_clear = p2.clear_cofactor(); + + Ok(CurvePoint(p1_clear + p2_clear)) } /// A helper that converts an arbitrarily long byte buffer to a field element @@ -536,12 +539,12 @@ impl CurvePointResult { let results = fabric.new_batch_gate_op( all_ids, - AUTHENTICATED_STARK_POINT_RESULT_LEN * n, /* output_arity */ + AUTHENTICATED_POINT_RESULT_LEN * n, /* output_arity */ move |mut args| { let points: Vec> = args.drain(..n).map(CurvePoint::from).collect_vec(); - let mut results = Vec::with_capacity(AUTHENTICATED_STARK_POINT_RESULT_LEN * n); + let mut results = Vec::with_capacity(AUTHENTICATED_POINT_RESULT_LEN * n); for (scalars, point) in args .chunks_exact(AUTHENTICATED_SCALAR_RESULT_LEN) @@ -782,7 +785,7 @@ impl CurvePointResult { ) } - /// Compute the multiscalar multiplication of the given `AuthenticatedScalar`s and points + /// Compute the multiscalar multiplication of the given `AuthenticatedScalarResult`s and points pub fn msm_authenticated( scalars: &[AuthenticatedScalarResult], points: &[CurvePointResult], @@ -803,7 +806,7 @@ impl CurvePointResult { let res = fabric.new_batch_gate_op( all_ids, - AUTHENTICATED_STARK_POINT_RESULT_LEN, /* output_arity */ + AUTHENTICATED_POINT_RESULT_LEN, /* output_arity */ move |mut args| { let mut shares = Vec::with_capacity(n); let mut macs = Vec::with_capacity(n); @@ -840,7 +843,7 @@ impl CurvePointResult { } } - /// Compute the multiscalar multiplication of the given `AuthenticatedScalar`s and points + /// Compute the multiscalar multiplication of the given `AuthenticatedScalarResult`s and points /// represented as streaming iterators pub fn msm_authenticated_iter(scalars: I, points: J) -> AuthenticatedPointResult where @@ -858,8 +861,6 @@ impl CurvePointResult { // | Tests | // --------- -/// We test our config against a known implementation of the Stark curve: -/// https://github.com/xJonathanLEI/starknet-rs #[cfg(test)] mod test { use rand::thread_rng; diff --git a/src/algebra/mpc_curve.rs b/src/algebra/mpc_curve.rs index 2f24b55..773a80d 100644 --- a/src/algebra/mpc_curve.rs +++ b/src/algebra/mpc_curve.rs @@ -210,7 +210,7 @@ impl MpcPointResult { .collect_vec() } - /// Add a batch of `MpcPointResults` to a batch of `CurvePointResult`s + /// Add a batch of `MpcPointResults` to a batch of `CurvePointResult`s pub fn batch_add_public( a: &[MpcPointResult], b: &[CurvePointResult], @@ -479,7 +479,7 @@ impl_borrow_variants!(MpcPointResult, Mul, mul, *, MpcScalarResult, C: Cur impl_commutative!(MpcPointResult, Mul, mul, *, MpcScalarResult, C:CurveGroup); impl MpcPointResult { - /// Multiply a batch of `MpcPointResult`s with a batch of `MpcScalarResult`s + /// Multiply a batch of `MpcPointResult`s with a batch of `MpcScalarResult`s #[allow(non_snake_case)] pub fn batch_mul(a: &[MpcScalarResult], b: &[MpcPointResult]) -> Vec> { assert_eq!(a.len(), b.len(), "Batch add requires equal length inputs"); diff --git a/src/algebra/mpc_scalar.rs b/src/algebra/mpc_scalar.rs index 29aaa39..9937fce 100644 --- a/src/algebra/mpc_scalar.rs +++ b/src/algebra/mpc_scalar.rs @@ -33,7 +33,7 @@ impl From> for MpcScalarResult { } } -/// Defines the result handle type that represents a future result of an `MpcScalarResult` +/// Defines the result handle type that represents a future result of an `MpcScalar` impl MpcScalarResult { /// Creates an MPC scalar from a given underlying scalar assumed to be a secret share pub fn new_shared(value: ScalarResult) -> MpcScalarResult { @@ -199,7 +199,7 @@ impl Add<&MpcScalarResult> for &MpcScalarResult { impl_borrow_variants!(MpcScalarResult, Add, add, +, MpcScalarResult, C: CurveGroup); impl MpcScalarResult { - /// Add two batches of `MpcScalarResult`s using a single batched gate + /// Add two batches of `MpcScalarResult`s using a single batched gate pub fn batch_add( a: &[MpcScalarResult], b: &[MpcScalarResult], @@ -230,7 +230,7 @@ impl MpcScalarResult { scalars.into_iter().map(|s| s.into()).collect_vec() } - /// Add a batch of `MpcScalarResult`s to a batch of public `ScalarResult`s + /// Add a batch of `MpcScalarResult`s to a batch of public `ScalarResult`s pub fn batch_add_public( a: &[MpcScalarResult], b: &[ScalarResult], @@ -364,7 +364,7 @@ impl Sub<&MpcScalarResult> for &MpcScalarResult { impl_borrow_variants!(MpcScalarResult, Sub, sub, -, MpcScalarResult, C: CurveGroup); impl MpcScalarResult { - /// Subtract two batches of `MpcScalarResult`s using a single batched gate + /// Subtract two batches of `MpcScalarResult`s using a single batched gate pub fn batch_sub( a: &[MpcScalarResult], b: &[MpcScalarResult], @@ -400,7 +400,7 @@ impl MpcScalarResult { scalars.into_iter().map(|s| s.into()).collect_vec() } - /// Subtract a batch of `MpcScalarResult`s from a batch of public `ScalarResult`s + /// Subtract a batch of `MpcScalarResult`s from a batch of public `ScalarResult`s pub fn batch_sub_public( a: &[MpcScalarResult], b: &[ScalarResult], @@ -543,7 +543,7 @@ impl Mul<&MpcScalarResult> for &MpcScalarResult { impl_borrow_variants!(MpcScalarResult, Mul, mul, *, MpcScalarResult, C: CurveGroup); impl MpcScalarResult { - /// Multiply a batch of `MpcScalarResults` over a single network op + /// Multiply a batch of `MpcScalarResult`s over a single network op pub fn batch_mul( a: &[MpcScalarResult], b: &[MpcScalarResult], @@ -578,7 +578,7 @@ impl MpcScalarResult { MpcScalarResult::batch_add(&de_plus_db, &ea_plus_c) } - /// Multiply a batch of `MpcScalarResult`s by a batch of public `ScalarResult`s + /// Multiply a batch of `MpcScalarResult`s by a batch of public `ScalarResult`s pub fn batch_mul_public( a: &[MpcScalarResult], b: &[ScalarResult], diff --git a/src/algebra/scalar.rs b/src/algebra/scalar.rs index 07dba5d..b144f7a 100644 --- a/src/algebra/scalar.rs +++ b/src/algebra/scalar.rs @@ -1,4 +1,4 @@ -//! Defines the scalar types that form the basis of the Starknet algebra +//! Defines the scalar types that form the basis of the MPC algebra // ---------------------------- // | Scalar Field Definitions | @@ -143,9 +143,9 @@ impl<'de, C: CurveGroup> Deserialize<'de> for Scalar { // === Addition === // -/// A type alias for a result that resolves to a `Scalar` +/// A type alias for a result that resolves to a `Scalar` pub type ScalarResult = ResultHandle>; -/// A type alias for a result that resolves to a batch of `Scalar`s +/// A type alias for a result that resolves to a batch of `Scalar`s pub type BatchScalarResult = ResultHandle>>; impl ScalarResult { /// Compute the multiplicative inverse of the scalar in its field @@ -275,7 +275,7 @@ impl Sub<&ScalarResult> for &ScalarResult { impl_borrow_variants!(ScalarResult, Sub, sub, -, ScalarResult, C: CurveGroup); impl ScalarResult { - /// Subtract two batches of `ScalarResult`s + /// Subtract two batches of `ScalarResult`s pub fn batch_sub(a: &[ScalarResult], b: &[ScalarResult]) -> Vec> { assert_eq!(a.len(), b.len(), "Batch sub requires equal length inputs"); @@ -343,7 +343,7 @@ impl Mul<&ScalarResult> for &ScalarResult { impl_borrow_variants!(ScalarResult, Mul, mul, *, ScalarResult, C: CurveGroup); impl ScalarResult { - /// Multiply two batches of `ScalarResult`s + /// Multiply two batches of `ScalarResult`s pub fn batch_mul(a: &[ScalarResult], b: &[ScalarResult]) -> Vec> { assert_eq!(a.len(), b.len(), "Batch mul requires equal length inputs"); @@ -385,7 +385,7 @@ impl Neg for &ScalarResult { impl_borrow_variants!(ScalarResult, Neg, neg, -, C: CurveGroup); impl ScalarResult { - /// Negate a batch of `ScalarResult`s + /// Negate a batch of `ScalarResult`s pub fn batch_neg(a: &[ScalarResult]) -> Vec> { let n = a.len(); let fabric = &a[0].fabric; diff --git a/src/commitment.rs b/src/commitment.rs index b96f13f..f6c0aff 100644 --- a/src/commitment.rs +++ b/src/commitment.rs @@ -1,4 +1,4 @@ -//! Defines Pedersen commitments over the Stark curve used to commit to a value +//! Defines Pedersen commitments over the system curve used to commit to a value //! before opening it use ark_ec::CurveGroup; diff --git a/src/fabric.rs b/src/fabric.rs index 1ecd054..fef35d4 100644 --- a/src/fabric.rs +++ b/src/fabric.rs @@ -540,17 +540,17 @@ impl MpcFabric { (0..n).map(|_| val.clone()).collect_vec() } - /// Get the hardcoded curve identity wire as a raw `StarkPoint` + /// Get the hardcoded curve identity wire as a raw `CurvePointResult` pub fn curve_identity(&self) -> CurvePointResult { ResultHandle::new(self.inner.curve_identity(), self.clone()) } - /// Get the hardcoded shared curve identity wire as an `MpcStarkPointResult` + /// Get the hardcoded shared curve identity wire as an `MpcPointResult` fn curve_identity_shared(&self) -> MpcPointResult { MpcPointResult::new_shared(self.curve_identity()) } - /// Get the hardcoded curve identity wire as an `AuthenticatedStarkPointResult` + /// Get the hardcoded curve identity wire as an `AuthenticatedPointResult` /// /// Both parties hold the identity point directly in this case pub fn curve_identity_authenticated(&self) -> AuthenticatedPointResult { @@ -633,7 +633,7 @@ impl MpcFabric { AuthenticatedScalarResult::new_shared_from_batch_result(shares, n) } - /// Share a `StarkPoint` value with the counterparty + /// Share a `CurvePoint` value with the counterparty pub fn share_point(&self, val: CurvePoint, sender: PartyId) -> AuthenticatedPointResult { let point: CurvePointResult = if self.party_id() == sender { // As mentioned in https://eprint.iacr.org/2009/226.pdf @@ -657,7 +657,7 @@ impl MpcFabric { AuthenticatedPointResult::new_shared(point) } - /// Share a batch of `StarkPoint`s with the counterparty + /// Share a batch of `CurvePoint`s with the counterparty pub fn batch_share_point( &self, vals: Vec>, diff --git a/src/lib.rs b/src/lib.rs index 41dabe3..3be0464 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ #![allow(incomplete_features)] #![feature(inherent_associated_types)] -//! Defines an MPC implementation over the Stark curve that allows for out-of-order execution of +//! Defines an MPC implementation over the a generic Arkworks curve that allows for out-of-order execution of //! the underlying MPC circuit use std::sync::{Arc, RwLock}; @@ -39,7 +39,7 @@ pub const PARTY0: u64 = 0; pub const PARTY1: u64 = 1; /// Generate a random curve point by multiplying a random scalar with the -/// Stark curve group generator +/// curve group generator pub fn random_point() -> CurvePoint { let mut rng = thread_rng(); CurvePoint::generator() * Scalar::random(&mut rng)