Skip to content

Commit

Permalink
Rahul/move split secret (#4557)
Browse files Browse the repository at this point in the history
* moving split secret to frost_utils

* removing unused import

* removing double &

* source code heading
  • Loading branch information
patnir authored Jan 18, 2024
1 parent 96bf135 commit df29d10
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 89 deletions.
1 change: 1 addition & 0 deletions ironfish-rust/src/frost_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

pub mod round_one;
pub mod split_secret;
3 changes: 1 addition & 2 deletions ironfish-rust/src/frost_utils/round_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ pub fn round_one(key_package: &KeyPackage, seed: u64) -> (SigningNonces, Signing

#[cfg(test)]
mod test {

use ff::Field;
use ironfish_frost::frost::keys::IdentifierList;
use jubjub::Fr;
use rand::rngs::ThreadRng;

use crate::transaction::{split_secret, SecretShareConfig};
use crate::frost_utils::split_secret::{split_secret, SecretShareConfig};

#[test]
pub fn test_seed_provides_same_result() {
Expand Down
90 changes: 90 additions & 0 deletions ironfish-rust/src/frost_utils/split_secret.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use ironfish_frost::frost;
use ironfish_frost::frost::Error;
use ironfish_frost::frost::{
keys::{IdentifierList, KeyPackage, PublicKeyPackage},
Identifier, SigningKey,
};
use rand::rngs::ThreadRng;
use std::collections::HashMap;

pub struct SecretShareConfig {
pub min_signers: u16,
pub max_signers: u16,
pub secret: Vec<u8>,
}

pub fn split_secret(
config: &SecretShareConfig,
identifiers: IdentifierList,
rng: &mut ThreadRng,
) -> Result<(HashMap<Identifier, KeyPackage>, PublicKeyPackage), Error> {
let secret_key = SigningKey::deserialize(
config
.secret
.clone()
.try_into()
.map_err(|_| Error::MalformedSigningKey)?,
)?;

let (shares, pubkeys) = frost::keys::split(
&secret_key,
config.max_signers,
config.min_signers,
identifiers,
rng,
)?;

for (_k, v) in shares.clone() {
frost::keys::KeyPackage::try_from(v)?;
}

let mut key_packages: HashMap<_, _> = HashMap::new();

for (identifier, secret_share) in shares {
let key_package = frost::keys::KeyPackage::try_from(secret_share.clone()).unwrap();
key_packages.insert(identifier, key_package);
}

Ok((key_packages, pubkeys))
}

#[cfg(test)]
mod test {
use super::*;
use crate::keys::SaplingKey;
use ironfish_frost::frost::{frost::keys::reconstruct, JubjubBlake2b512};

#[test]
fn test_split_secret() {
let mut rng = rand::thread_rng();

let key = SaplingKey::generate_key().spend_authorizing_key.to_bytes();

let config = SecretShareConfig {
min_signers: 2,
max_signers: 3,
secret: key.to_vec(),
};

let (key_packages, _) = split_secret(
&config,
ironfish_frost::frost::keys::IdentifierList::Default,
&mut rng,
)
.unwrap();
assert_eq!(key_packages.len(), 3);

let key_parts: Vec<_> = key_packages.values().cloned().collect();

let signing_key =
reconstruct::<JubjubBlake2b512>(&key_parts).expect("key reconstruction failed");

let scalar = signing_key.to_scalar();

assert_eq!(scalar.to_bytes(), key);
}
}
54 changes: 1 addition & 53 deletions ironfish-rust/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,7 @@ use crate::{
OutgoingViewKey, OutputDescription, SpendDescription, ViewKey,
};

use ironfish_frost::frost;
use ironfish_frost::frost::Error;
use rand::{
rngs::{OsRng, ThreadRng},
thread_rng,
};

use ironfish_frost::frost::{
keys::{IdentifierList, KeyPackage, PublicKeyPackage},
Identifier, SigningKey,
};
use rand::{rngs::OsRng, thread_rng};

use bellperson::groth16::{verify_proofs_batch, PreparedVerifyingKey};
use blake2b_simd::Params as Blake2b;
Expand All @@ -49,7 +39,6 @@ use ironfish_zkp::{
};

use std::{
collections::HashMap,
io::{self, Write},
iter,
slice::Iter,
Expand Down Expand Up @@ -947,44 +936,3 @@ pub fn batch_verify_transactions<'a>(
&SAPLING.mint_verifying_key,
)
}

pub struct SecretShareConfig {
pub min_signers: u16,
pub max_signers: u16,
pub secret: Vec<u8>,
}

pub fn split_secret(
config: &SecretShareConfig,
identifiers: IdentifierList,
rng: &mut ThreadRng,
) -> Result<(HashMap<Identifier, KeyPackage>, PublicKeyPackage), Error> {
let secret_key = SigningKey::deserialize(
config
.secret
.clone()
.try_into()
.map_err(|_| Error::MalformedSigningKey)?,
)?;

let (shares, pubkeys) = frost::keys::split(
&secret_key,
config.max_signers,
config.min_signers,
identifiers,
rng,
)?;

for (_k, v) in shares.clone() {
frost::keys::KeyPackage::try_from(v)?;
}

let mut key_packages: HashMap<_, _> = HashMap::new();

for (identifier, secret_share) in shares {
let key_package = frost::keys::KeyPackage::try_from(secret_share.clone()).unwrap();
key_packages.insert(identifier, key_package);
}

Ok((key_packages, pubkeys))
}
36 changes: 2 additions & 34 deletions ironfish-rust/src/transaction/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ use crate::{
sapling_bls12::SAPLING,
test_util::make_fake_witness,
transaction::{
batch_verify_transactions, split_secret, verify_transaction, SecretShareConfig,
TransactionVersion, TRANSACTION_EXPIRATION_SIZE, TRANSACTION_FEE_SIZE,
TRANSACTION_SIGNATURE_SIZE,
batch_verify_transactions, verify_transaction, TransactionVersion,
TRANSACTION_EXPIRATION_SIZE, TRANSACTION_FEE_SIZE, TRANSACTION_SIGNATURE_SIZE,
},
};

use ff::Field;
use ironfish_frost::frost::{frost::keys::reconstruct, JubjubBlake2b512};
use ironfish_zkp::{
constants::{ASSET_ID_LENGTH, SPENDING_KEY_GENERATOR, TREE_DEPTH},
proofs::{MintAsset, Output, Spend},
Expand Down Expand Up @@ -645,33 +643,3 @@ fn test_batch_verify() {
Err(e) if matches!(e.kind, IronfishErrorKind::InvalidSpendSignature)
));
}

#[test]
fn test_split_secret() {
let mut rng = rand::thread_rng();

let key = SaplingKey::generate_key().spend_authorizing_key.to_bytes();

let config = SecretShareConfig {
min_signers: 2,
max_signers: 3,
secret: key.to_vec(),
};

let (key_packages, _) = split_secret(
&config,
ironfish_frost::frost::keys::IdentifierList::Default,
&mut rng,
)
.unwrap();
assert_eq!(key_packages.len(), 3);

let key_parts: Vec<_> = key_packages.values().cloned().collect();

let signing_key =
reconstruct::<JubjubBlake2b512>(&key_parts).expect("key reconstruction failed");

let scalar = signing_key.to_scalar();

assert_eq!(scalar.to_bytes(), key);
}

0 comments on commit df29d10

Please sign in to comment.