Skip to content

Commit

Permalink
Merge pull request #29 from Yoii-Inc/feat/#25_connect_preprocessing_o…
Browse files Browse the repository at this point in the history
…nline

Feat/#25 connect preprocessing online
  • Loading branch information
taskooh authored Nov 10, 2023
2 parents e505e67 + a926b65 commit 2dae388
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 39 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target/
Cargo.lock
/outputs
/outputs
inputs/inputs.json
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ and build:
cargo build
```

setup input file
```
cp ./inputs/inputs-template.json ./inputs/inputs.json
```

### Preprocessing phase
run(by groth16):
```
cargo run --bin main groth16 ./inputs/inputs.json
Expand All @@ -35,6 +42,20 @@ or run(by marlin):
cargo run --bin main marlin ./inputs/inputs.json
```

### Online phase
setup output folder
```
mkdir ./outputs
mkdir ./outputs/0
mkdir ./outputs/1
mkdir ./outputs/2
```

run online phase
```
./run_online.zsh
```

## Tests

```
Expand Down Expand Up @@ -133,6 +154,9 @@ impl MySecretInputCircuit {

See [this](https://github.com/arkworks-rs/r1cs-tutorial/) to learn more about how to specify constraints.

### how to specify mpc calculation
online mpc calculations are specified in circuits/circuit.rs. Defaultly, MySimpleCircuit is used. Constraints is specified in same way as input_circuit.rs.

## Technical Details
### Generating secret sharing of inputs and ZKP verification

Expand Down
4 changes: 2 additions & 2 deletions arkworks/crypto-primitives/src/crh/pedersen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ impl<C: ProjectiveCurve, W: Window> CRH<C, W> {
generators_powers
}

pub fn generator_powers<R: Rng>(num_powers: usize, rng: &mut R) -> Vec<C> {
pub fn generator_powers<R: Rng>(num_powers: usize, _rng: &mut R) -> Vec<C> {
let mut cur_gen_powers = Vec::with_capacity(num_powers);
let mut base = C::pub_rand(rng);
let mut base = C::prime_subgroup_generator();
for _ in 0..num_powers {
cur_gen_powers.push(base);
base.double_in_place();
Expand Down
File renamed without changes.
15 changes: 10 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::io::Write as Otherwrite;
use structopt::StructOpt;

use crate::circuits::*;
use crate::serialize::write_to_file;
use crate::serialize::{write_r, write_to_file};

#[derive(Debug, StructOpt)]
#[structopt(name = "example", about = "An example of StructOpt usage.")]
Expand Down Expand Up @@ -104,10 +104,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// // initialize phase
let zkpopk_parameters = preprocessing::zkpopk::Parameters::new(
1,
2,
3,
std::convert::Into::<num_bigint::BigUint>::into(FrParameters::MODULUS) / 2_u32,
1,
6,
9,
2,
);

Expand All @@ -127,7 +127,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

let e_alpha = she::Ciphertext::rand(&pk, &mut rng, &she_parameters);

let (_r_bracket, _r_angle) =
let (r_bracket, r_angle) =
preprocessing::pair(&e_alpha, &pk, &sk, &zkpopk_parameters, &she_parameters);

// // triple phase
Expand Down Expand Up @@ -195,7 +195,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

let output_file_path = "./outputs/outputs.json";

write_to_file(h_x, output_file_path, "hex_commitment")?;
write_to_file(vec![("hex_commitment".to_string(), h_x)], output_file_path)?;

// deserialize
let mut output_file = File::open(output_file_path).expect("Failed to open file");
Expand All @@ -220,5 +220,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
ark_ec::models::twisted_edwards_extended::GroupAffine::deserialize(reader).unwrap();

assert_eq!(h_x, deserialized_h_x);

// save to file
// <r>, [r] for input share
write_r(3, r_angle, r_bracket).unwrap();

Ok(())
}
120 changes: 109 additions & 11 deletions src/online.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use ark_bls12_377::Fr;
use ark_crypto_primitives::CommitmentScheme;
use ark_ff::{BigInteger, PrimeField};
use ark_ff::{BigInteger, FpParameters, PrimeField};
use ark_marlin::IndexProverKey;
use ark_serialize::Read;
use ark_serialize::{CanonicalDeserialize, Read};
use ark_std::test_rng;

use mpc_algebra::Reveal;
Expand Down Expand Up @@ -40,6 +40,34 @@ struct ArgInput {
z: u128,
}

#[derive(Debug, Deserialize)]
struct PairPhase {
r0_angle_mac: String,
r0_angle_public_modifier: String,
r0_angle_share: String,
r0_bracket_mac: String,
r0_bracket_mac_0: String,
r0_bracket_mac_1: String,
r0_bracket_mac_2: String,
r0_bracket_share: String,
r1_angle_mac: String,
r1_angle_public_modifier: String,
r1_angle_share: String,
r1_bracket_mac: String,
r1_bracket_mac_0: String,
r1_bracket_mac_1: String,
r1_bracket_mac_2: String,
r1_bracket_share: String,
r2_angle_mac: String,
r2_angle_public_modifier: String,
r2_angle_share: String,
r2_bracket_mac: String,
r2_bracket_mac_0: String,
r2_bracket_mac_1: String,
r2_bracket_mac_2: String,
r2_bracket_share: String,
}

enum ZkSnark {
Groth16,
Marlin,
Expand Down Expand Up @@ -89,26 +117,96 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// online calculation

// TODO: Separate the following part in preprocessing.

// load pair phase data
// deserialize
let online_setup_file_path = format!("./outputs/{}/online_setup.json", opt.id);
let mut online_setup_file = File::open(online_setup_file_path).expect("Failed to open file");

let mut output_string = String::new();
online_setup_file
.read_to_string(&mut output_string)
.expect("Failed to read file");

let output_data: PairPhase = serde_json::from_str(&output_string).unwrap();

let (r0, r1, r2) = {
let remove_prefix_string =
if let Some(stripped) = output_data.r0_angle_share.strip_prefix("0x") {
stripped.to_string()
} else {
output_data.r0_angle_share.clone()
};

let remove_prefix_string1 =
if let Some(stripped) = output_data.r0_angle_share.strip_prefix("0x") {
stripped.to_string()
} else {
output_data.r1_angle_share.clone()
};

let remove_prefix_string2 =
if let Some(stripped) = output_data.r0_angle_share.strip_prefix("0x") {
stripped.to_string()
} else {
output_data.r2_angle_share.clone()
};

let reader: &[u8] = &hex::decode(remove_prefix_string).unwrap();

let deserialized_r0_angle_share: Fr = Fr::deserialize(reader).unwrap();

let reader: &[u8] = &hex::decode(remove_prefix_string1).unwrap();

let deserialized_r1_angle_share: Fr = Fr::deserialize(reader).unwrap();

let reader: &[u8] = &hex::decode(remove_prefix_string2).unwrap();

let deserialized_r2_angle_share: Fr = Fr::deserialize(reader).unwrap();

(
deserialized_r0_angle_share,
deserialized_r1_angle_share,
deserialized_r2_angle_share,
)
};

let sum_r0 = MFr::from_add_shared(r0).reveal();
let sum_r1 = MFr::from_add_shared(r1).reveal();
let sum_r2 = MFr::from_add_shared(r2).reveal();

let shared_input = match Net::party_id() {
0 => {
vec![
MFr::from_add_shared(Fr::from(data.x)),
MFr::from_add_shared(Fr::from(0)),
MFr::from_add_shared(Fr::from(0)),
MFr::from_add_shared(
Fr::from(data.x) - sum_r0
+ r0
+ Fr::from(ark_ed_on_bls12_377::FrParameters::MODULUS),
),
MFr::from_add_shared(r1),
MFr::from_add_shared(r2),
]
}
1 => {
vec![
MFr::from_add_shared(Fr::from(0)),
MFr::from_add_shared(Fr::from(data.y)),
MFr::from_add_shared(Fr::from(0)),
MFr::from_add_shared(r0),
MFr::from_add_shared(
Fr::from(data.y) - sum_r1
+ r1
+ Fr::from(ark_ed_on_bls12_377::FrParameters::MODULUS),
),
MFr::from_add_shared(r2),
]
}
2 => {
vec![
MFr::from_add_shared(Fr::from(0)),
MFr::from_add_shared(Fr::from(0)),
MFr::from_add_shared(Fr::from(data.z)),
MFr::from_add_shared(r0),
MFr::from_add_shared(r1),
MFr::from_add_shared(
Fr::from(data.z) - sum_r2
+ r2
+ Fr::from(ark_ed_on_bls12_377::FrParameters::MODULUS),
),
]
}
_ => panic!("invalid party id"),
Expand Down
41 changes: 40 additions & 1 deletion src/preprocessing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,22 @@ pub struct AngleShares {
mac: Vec<Plaintexts>,
}

impl AngleShares {
pub fn separetion(&self) -> Vec<(Vec<Plaintext>, Vec<Plaintext>, Vec<Plaintext>)> {
let peer_num = self.share.len();

let mut result = Vec::new();
for peer in 0..peer_num {
result.push((
self.clone().public_modifier.vals,
self.share[peer].clone().vals,
self.mac[peer].clone().vals,
));
}
result
}
}

impl Add<Plaintexts> for AngleShares {
type Output = AngleShares;
fn add(self, rhs: Plaintexts) -> Self::Output {
Expand Down Expand Up @@ -599,6 +615,29 @@ pub struct BracketShares {
mac: Vec<(Plaintexts, Vec<Plaintexts>)>,
}

impl BracketShares {
pub fn separetion(&self) -> Vec<(Vec<Plaintext>, (Vec<Plaintext>, Vec<Vec<Plaintext>>))> {
let peer_num = self.share.len();

let mut result = Vec::new();
for peer in 0..peer_num {
result.push((
self.share[peer].clone().vals,
(
self.mac[peer].clone().0.vals,
self.mac[peer]
.clone()
.1
.iter()
.map(|x| x.clone().vals)
.collect(),
),
));
}
result
}
}

fn bracket(
m_vec: Vec<Plaintexts>,
e_m: Ciphertext,
Expand Down Expand Up @@ -795,7 +834,7 @@ pub fn pair(

// step 1
let r_vec: Vec<Plaintexts> = (0..n)
.map(|_| Plaintexts::rand(she_params, &mut rng))
.map(|_| Plaintexts::restricted_rand(she_params, &mut rng))
.collect();

// step 2
Expand Down
Loading

0 comments on commit 2dae388

Please sign in to comment.