From 689df0f8b72f8caca8d5eb976d5811a15694451c Mon Sep 17 00:00:00 2001 From: Vitalii Drohan Date: Thu, 21 May 2020 14:40:35 +0300 Subject: [PATCH] remove unused code from circuit for review --- core/circuit/src/circuit.rs | 99 -------------------------- core/circuit/src/signature.rs | 130 +--------------------------------- 2 files changed, 1 insertion(+), 228 deletions(-) diff --git a/core/circuit/src/circuit.rs b/core/circuit/src/circuit.rs index b0a36b42f4..535c804bbb 100644 --- a/core/circuit/src/circuit.rs +++ b/core/circuit/src/circuit.rs @@ -1253,105 +1253,6 @@ impl<'a, E: RescueEngine + JubjubEngine> FranklinCircuit<'a, E> { Ok(tx_valid) } - // Close disable - // fn close_account>( - // &self, - // mut cs: CS, - // cur: &mut AllocatedOperationBranch, - // chunk_data: &AllocatedChunkData, - // ext_pubdata_chunk: &AllocatedNum, - // op_data: &AllocatedOperationData, - // signer_key: &AllocatedSignerPubkey, - // subtree_root: &CircuitElement, - // is_sig_verified: &Boolean, - // ) -> Result { - // let mut is_valid_flags = vec![]; - // //construct pubdata - // let mut pubdata_bits = vec![]; - // pubdata_bits.extend(chunk_data.tx_type.get_bits_be()); //TX_TYPE_BIT_WIDTH=8 - // pubdata_bits.extend(cur.account_address.get_bits_be()); //ACCOUNT_TREE_DEPTH=24 - // pubdata_bits.resize( - // params::CHUNK_BIT_WIDTH, - // Boolean::constant(false), - // ); - - // // construct signature message preimage (serialized_tx) - // let mut serialized_tx_bits = vec![]; - // serialized_tx_bits.extend(chunk_data.tx_type.get_bits_be()); - // serialized_tx_bits.extend(cur.account.pub_key_hash.get_bits_be()); - // serialized_tx_bits.extend(cur.account.nonce.get_bits_be()); - - // let pubdata_chunk = select_pubdata_chunk( - // cs.namespace(|| "select_pubdata_chunk"), - // &pubdata_bits, - // &chunk_data.chunk_number, - // 1, - // )?; - - // let is_pubdata_chunk_correct = Boolean::from(Expression::equals( - // cs.namespace(|| "is_pubdata_equal"), - // &pubdata_chunk, - // ext_pubdata_chunk, - // )?); - // is_valid_flags.push(is_pubdata_chunk_correct); - - // let is_close_account = Boolean::from(Expression::equals( - // cs.namespace(|| "is_deposit"), - // &chunk_data.tx_type.get_number(), - // Expression::u64::(4), //close_account tx_type - // )?); - // is_valid_flags.push(is_close_account.clone()); - - // let tmp = CircuitAccount::::empty_balances_root_hash(); - // let mut r_repr = E::Fr::zero().into_repr(); - // r_repr.read_be(&tmp[..]).unwrap(); - // let empty_root = E::Fr::from_repr(r_repr).unwrap(); - - // let are_balances_empty = Boolean::from(Expression::equals( - // cs.namespace(|| "are_balances_empty"), - // &subtree_root.get_number(), - // Expression::constant::(empty_root), //This is precalculated root_hash of subtree with empty balances - // )?); - // is_valid_flags.push(are_balances_empty); - - // let is_serialized_tx_correct = verify_signature_message_construction( - // cs.namespace(|| "is_serialized_tx_correct"), - // serialized_tx_bits, - // &op_data, - // )?; - - // is_valid_flags.push(is_serialized_tx_correct); - // is_valid_flags.push(is_sig_verified.clone()); - // let is_signer_valid = CircuitElement::equals( - // cs.namespace(|| "signer_key_correct"), - // &signer_key.pubkey.get_hash(), - // &cur.account.pub_key_hash, //earlier we ensured that this new_pubkey_hash is equal to current if existed - // )?; - - // is_valid_flags.push(is_signer_valid); - - // let tx_valid = multi_and(cs.namespace(|| "is_tx_valid"), &is_valid_flags)?; - - // // below we conditionally update state if it is valid operation - - // // update pub_key - // cur.account.pub_key_hash = CircuitElement::conditionally_select_with_number_strict( - // cs.namespace(|| "mutated_pubkey"), - // Expression::constant::(E::Fr::zero()), - // &cur.account.pub_key_hash, - // &tx_valid, - // )?; - // // update nonce - // cur.account.nonce = CircuitElement::conditionally_select_with_number_strict( - // cs.namespace(|| "update cur nonce"), - // Expression::constant::(E::Fr::zero()), - // &cur.account.nonce, - // &tx_valid, - // )?; - - // Ok(tx_valid) - // } - fn noop>( &self, mut cs: CS, diff --git a/core/circuit/src/signature.rs b/core/circuit/src/signature.rs index 278bc353d7..f57dcaaaab 100644 --- a/core/circuit/src/signature.rs +++ b/core/circuit/src/signature.rs @@ -6,7 +6,7 @@ use crypto_exports::franklin_crypto::{ boolean::{le_bits_into_le_bytes, AllocatedBit, Boolean}, ecc, expression::Expression, - multipack, pedersen_hash, rescue, sha256, + multipack, rescue, }, jubjub::JubjubEngine, rescue::RescueEngine, @@ -263,134 +263,6 @@ pub fn verify_signature_message_construction>( - mut cs: CS, - sig_data_bits: &[Boolean], - signature: &EddsaSignature, - params: &E::Params, - generator: ecc::EdwardsPoint, -) -> Result { - let mut sig_data_bits = sig_data_bits.to_vec(); - assert!( - sig_data_bits.len() <= MAX_SIGN_MESSAGE_BIT_WIDTH, - "Signature message len is too big {}/{}", - sig_data_bits.len(), - MAX_SIGN_MESSAGE_BIT_WIDTH - ); - sig_data_bits.resize(MAX_SIGN_MESSAGE_BIT_WIDTH, Boolean::constant(false)); - - let mut first_round_bits: Vec = vec![]; - - let mut pk_x_serialized = signature - .pk - .get_x() - .clone() - .into_bits_le(cs.namespace(|| "pk_x_bits"))?; - pk_x_serialized.resize(FR_BIT_WIDTH_PADDED, Boolean::constant(false)); - - let mut r_x_serialized = signature - .r - .get_x() - .clone() - .into_bits_le(cs.namespace(|| "r_x_bits"))?; - r_x_serialized.resize(FR_BIT_WIDTH_PADDED, Boolean::constant(false)); - - first_round_bits.extend(pk_x_serialized); - first_round_bits.extend(r_x_serialized); - - let first_round_hash = pedersen_hash::pedersen_hash( - cs.namespace(|| "first_round_hash"), - pedersen_hash::Personalization::NoteCommitment, - &first_round_bits, - params, - )?; - let mut first_round_hash_bits = first_round_hash - .get_x() - .into_bits_le(cs.namespace(|| "first_round_hash_bits"))?; - first_round_hash_bits.resize(FR_BIT_WIDTH_PADDED, Boolean::constant(false)); - - let mut second_round_bits = vec![]; - second_round_bits.extend(first_round_hash_bits); - second_round_bits.extend(sig_data_bits); - let second_round_hash = pedersen_hash::pedersen_hash( - cs.namespace(|| "second_hash"), - pedersen_hash::Personalization::NoteCommitment, - &second_round_bits, - params, - )? - .get_x() - .clone(); - - let h_bits = second_round_hash.into_bits_le(cs.namespace(|| "h_bits"))?; - - let max_message_len = 32 as usize; //since it is the result of pedersen hash - - let is_sig_verified = verify_schnorr_relationship( - signature, - cs.namespace(|| "verify transaction signature"), - params, - &h_bits, - generator, - max_message_len, - )?; - Ok(is_sig_verified) -} - -pub fn is_sha256_signature_verified>( - mut cs: CS, - sig_data_bits: &[Boolean], - signature: &EddsaSignature, - params: &E::Params, - generator: ecc::EdwardsPoint, -) -> Result { - // this contant is also used inside franklin_crypto verify sha256(enforce version of this check) - const INPUT_PAD_LEN_FOR_SHA256: usize = 768; - - let mut sig_data_bits = sig_data_bits.to_vec(); - assert!( - sig_data_bits.len() <= MAX_SIGN_MESSAGE_BIT_WIDTH, - "Signature message len is too big {}/{}", - sig_data_bits.len(), - MAX_SIGN_MESSAGE_BIT_WIDTH - ); - sig_data_bits.resize(MAX_SIGN_MESSAGE_BIT_WIDTH, Boolean::constant(false)); - sig_data_bits = le_bits_into_le_bytes(sig_data_bits); - - let mut hash_input: Vec = vec![]; - { - let mut pk_x_serialized = signature - .pk - .get_x() - .into_bits_le_fixed(cs.namespace(|| "pk_x_bits"), FR_BIT_WIDTH)?; - pk_x_serialized.resize(FR_BIT_WIDTH_PADDED, Boolean::constant(false)); - hash_input.extend(le_bits_into_le_bytes(pk_x_serialized)); - } - { - let mut r_x_serialized = signature - .r - .get_x() - .into_bits_le_fixed(cs.namespace(|| "r_x_bits"), FR_BIT_WIDTH)?; - r_x_serialized.resize(FR_BIT_WIDTH_PADDED, Boolean::constant(false)); - hash_input.extend(le_bits_into_le_bytes(r_x_serialized)); - } - hash_input.extend(sig_data_bits); - - hash_input.resize(INPUT_PAD_LEN_FOR_SHA256, Boolean::constant(false)); - let h_bits = sha256::sha256(cs.namespace(|| "sha256"), &hash_input)?; - - let max_message_len = 32 as usize; //since it is the result of sha256 hash - - let is_sig_verified = verify_schnorr_relationship( - signature, - cs.namespace(|| "verify transaction signature"), - params, - &le_bits_into_le_bytes(h_bits), - generator, - max_message_len, - )?; - Ok(is_sig_verified) -} - pub fn is_rescue_signature_verified>( mut cs: CS, sig_data_bits: &[Boolean],