Skip to content

Commit

Permalink
Merge pull request #571 from matter-labs/dvush/remove-unused-code-fro…
Browse files Browse the repository at this point in the history
…m-circuit

remove unused code from circuit for review
  • Loading branch information
dvush authored May 21, 2020
2 parents d44e009 + 689df0f commit c4255a6
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 228 deletions.
99 changes: 0 additions & 99 deletions core/circuit/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1253,105 +1253,6 @@ impl<'a, E: RescueEngine + JubjubEngine> FranklinCircuit<'a, E> {
Ok(tx_valid)
}

// Close disable
// fn close_account<CS: ConstraintSystem<E>>(
// &self,
// mut cs: CS,
// cur: &mut AllocatedOperationBranch<E>,
// chunk_data: &AllocatedChunkData<E>,
// ext_pubdata_chunk: &AllocatedNum<E>,
// op_data: &AllocatedOperationData<E>,
// signer_key: &AllocatedSignerPubkey<E>,
// subtree_root: &CircuitElement<E>,
// is_sig_verified: &Boolean,
// ) -> Result<Boolean, SynthesisError> {
// 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::<CS>(4), //close_account tx_type
// )?);
// is_valid_flags.push(is_close_account.clone());

// let tmp = CircuitAccount::<E>::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::<CS>(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::<CS>(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::<CS>(E::Fr::zero()),
// &cur.account.nonce,
// &tx_valid,
// )?;

// Ok(tx_valid)
// }

fn noop<CS: ConstraintSystem<E>>(
&self,
mut cs: CS,
Expand Down
130 changes: 1 addition & 129 deletions core/circuit/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -263,134 +263,6 @@ pub fn verify_signature_message_construction<E: JubjubEngine, CS: ConstraintSyst
Ok(is_serialized_transaction_correct)
}

pub fn is_pedersen_signature_verified<E: JubjubEngine, CS: ConstraintSystem<E>>(
mut cs: CS,
sig_data_bits: &[Boolean],
signature: &EddsaSignature<E>,
params: &E::Params,
generator: ecc::EdwardsPoint<E>,
) -> Result<Boolean, SynthesisError> {
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<Boolean> = 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<E: JubjubEngine, CS: ConstraintSystem<E>>(
mut cs: CS,
sig_data_bits: &[Boolean],
signature: &EddsaSignature<E>,
params: &E::Params,
generator: ecc::EdwardsPoint<E>,
) -> Result<Boolean, SynthesisError> {
// 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<Boolean> = 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<E: RescueEngine + JubjubEngine, CS: ConstraintSystem<E>>(
mut cs: CS,
sig_data_bits: &[Boolean],
Expand Down

0 comments on commit c4255a6

Please sign in to comment.