Skip to content

Commit

Permalink
Auxiliary data for program evaluation (#475)
Browse files Browse the repository at this point in the history
* feat: auxiliary data for program evaluation

* fmt

* clippy

* use constraints commit from master

* taplo

* removed unnecessary local variables, renaming

* Update crypto/server/src/user/api.rs

Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>

* fmt

* rename program structs

* fmt

* renamed and explained testing variables

* refactored extra to auxilary

* use ec-runtime under latest tagged release

* Added PR updates to changelog

* Update CHANGELOG.md

Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>

* Update CHANGELOG.md

Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>

---------

Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>
  • Loading branch information
jakehemmerle and HCastano authored Nov 9, 2023
1 parent 1696407 commit b0bf381
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 86 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ At the moment this project **does not** adhere to

## [Unreleased]

### Breaking Changes

- In the JSON body of the `/sign_tx` endpoint, the `preimage` field has been renamed to `message`. It remains a hex-encoded `string`. [#475](https://github.com/entropyxyz/entropy-core/pull/475/)

### Added
- Auxiliary data for program evaluation ([#475](https://github.com/entropyxyz/entropy-core/pull/475/))

## [0.0.8](https://github.com/entropyxyz/entropy-core/compare/v0.0.7..v0.0.8) - 2023-11-06

### Breaking Changes
Expand Down
17 changes: 14 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crypto/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ kvdb ={ path="../kvdb", default-features=false }
entropy-protocol={ path="../protocol", features=["server"] }

# Constraints
ec-runtime={ git="ssh://git@github.com/entropyxyz/constraints.git", tag="v0.2.0" }
ec-runtime={ git="ssh://git@github.com/entropyxyz/constraints.git", tag="v0.3.0" }

# Logging
log ="0.4.17" # todo: remove, overlaps tracing
Expand Down
4 changes: 2 additions & 2 deletions crypto/server/src/helpers/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ use crate::{
protocol_transport::open_protocol_connections,
Listener, ProtocolErr,
},
user::api::UserTransactionRequest,
user::api::UserSignatureRequest,
validation::derive_static_secret,
AppState,
};

/// Start the signing protocol for a given message
pub async fn do_signing(
message: UserTransactionRequest,
message: UserSignatureRequest,
sig_hash: String,
app_state: &AppState,
tx_id: String,
Expand Down
7 changes: 3 additions & 4 deletions crypto/server/src/helpers/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub async fn spawn_testing_validators(
extra_private_keys: bool,
) -> (Vec<String>, Vec<PartyId>, Option<KeyShare<KeyParams>>) {
// spawn threshold servers
let ports = vec![3001i64, 3002];
let ports = [3001i64, 3002];

let (alice_axum, alice_kv) =
create_clients("validator1".to_string(), vec![], vec![], true, false).await;
Expand Down Expand Up @@ -183,11 +183,10 @@ pub async fn check_if_confirmation(
let registering_query = entropy::storage().relayer().registering(signer.account_id());
let registered_query = entropy::storage().relayer().registered(signer.account_id());
let block_hash = rpc.chain_get_block_hash(None).await.unwrap().unwrap();
let is_registering = api.storage().at(block_hash.clone()).fetch(&registering_query).await;
let is_registering = api.storage().at(block_hash).fetch(&registering_query).await;
// cleared from is_registering state
assert!(is_registering.unwrap().is_none());
let is_registered =
api.storage().at(block_hash.clone()).fetch(&registered_query).await.unwrap();
let is_registered = api.storage().at(block_hash).fetch(&registered_query).await.unwrap();
assert_eq!(is_registered.as_ref().unwrap().verifying_key.0.len(), 33usize);
assert_eq!(is_registered.unwrap().key_visibility, Static(KeyVisibility::Public));
}
Expand Down
4 changes: 2 additions & 2 deletions crypto/server/src/sign_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use subxt::utils::AccountId32;
use synedrion::sessions::PrehashedMessage;

use crate::{signing_client::ProtocolErr, user::api::UserTransactionRequest};
use crate::{signing_client::ProtocolErr, user::api::UserSignatureRequest};

/// Information passed to the Signing Client, to initiate the signing process.
/// Most of this information comes from a `Message` struct which gets propagated when a user's
Expand All @@ -25,7 +25,7 @@ impl SignInit {
/// Creates new signing object based on passed in data
#[allow(dead_code)]
pub fn new(
message: UserTransactionRequest,
message: UserSignatureRequest,
sig_hash: String,
tx_id: String,
user: AccountId32,
Expand Down
2 changes: 1 addition & 1 deletion crypto/server/src/signing_client/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async fn test_proactive_refresh() {
let one = AccountKeyring::Eve;
let _cxt = test_node_process_testing_state().await;

let signing_address = one.clone().to_account_id().to_ss58check();
let signing_address = one.to_account_id().to_ss58check();
let (validator_ips, _validator_ids, _users_keyshare_option) =
spawn_testing_validators(Some(signing_address.clone()), true).await;

Expand Down
30 changes: 17 additions & 13 deletions crypto/server/src/user/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use axum::{
};
use bip39::{Language, Mnemonic};
use blake2::{Blake2s256, Digest};
use ec_runtime::{InitialState, Runtime};
use ec_runtime::{Runtime, SignatureRequest};
use entropy_protocol::ValidatorInfo;
use entropy_shared::{types::KeyVisibility, OcwMessageDkg, X25519PublicKey, SIGNING_PARTY_SIZE};
use futures::{
Expand Down Expand Up @@ -63,9 +63,11 @@ use crate::{
/// Represents an unparsed, transaction request coming from the client.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct UserTransactionRequest {
/// Hex-encoded raw data to be signed (eg. RLP-serialized Ethereum transaction)
pub transaction_request: String,
pub struct UserSignatureRequest {
/// Hex-encoded raw data to be signed (eg. hex-encoded RLP-serialized Ethereum transaction)
pub message: String,
/// Hex-encoded auxilary data for program evaluation, will not be signed (eg. zero-knowledge proof, serialized struct, etc)
pub auxilary_data: Option<String>,
/// Information from the validators in signing party
pub validators_info: Vec<ValidatorInfo>,
/// When the message was created and signed
Expand Down Expand Up @@ -115,16 +117,18 @@ pub async fn sign_tx(
let decrypted_message =
signed_msg.decrypt(signer.signer()).map_err(|e| UserErr::Decryption(e.to_string()))?;

let mut user_tx_req: UserTransactionRequest = serde_json::from_slice(&decrypted_message)?;
check_stale(user_tx_req.timestamp)?;
let raw_message = hex::decode(user_tx_req.transaction_request.clone())?;
let sig_hash = hex::encode(Hasher::keccak(&raw_message));
let mut user_sig_req: UserSignatureRequest = serde_json::from_slice(&decrypted_message)?;
check_stale(user_sig_req.timestamp)?;

let message = hex::decode(&user_sig_req.message)?;
let auxilary_data = user_sig_req.auxilary_data.as_ref().map(hex::decode).transpose()?;
let sig_hash = hex::encode(Hasher::keccak(&message));
let subgroup_signers = get_current_subgroup_signers(&api, &rpc, &sig_hash).await?;
check_signing_group(&subgroup_signers, &user_tx_req.validators_info, signer.account_id())?;
check_signing_group(&subgroup_signers, &user_sig_req.validators_info, signer.account_id())?;

// Use the validator info from chain as we can be sure it is in the correct order and the
// details are correct
user_tx_req.validators_info = subgroup_signers;
user_sig_req.validators_info = subgroup_signers;

let tx_id = create_unique_tx_id(&signing_address, &sig_hash);

Expand All @@ -136,16 +140,16 @@ pub async fn sign_tx(
let program = get_program(&api, &rpc, &second_signing_address_conversion).await?;

let mut runtime = Runtime::new();
let initial_state = InitialState { data: raw_message };
let signature_request = SignatureRequest { message, auxilary_data };

runtime.evaluate(&program, &initial_state)?;
runtime.evaluate(&program, &signature_request)?;

let (mut response_tx, response_rx) = mpsc::channel(1);

// Do the signing protocol in another task, so we can already respond
tokio::spawn(async move {
let signing_protocol_output = do_signing(
user_tx_req,
user_sig_req,
sig_hash,
&app_state,
tx_id,
Expand Down
Loading

0 comments on commit b0bf381

Please sign in to comment.