Skip to content

Commit

Permalink
Cosmetic changes to the circuit
Browse files Browse the repository at this point in the history
  • Loading branch information
ly0va committed May 7, 2021
1 parent b61b0cb commit 54cdfbe
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 90 deletions.
112 changes: 29 additions & 83 deletions core/lib/circuit/src/allocated_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,25 +292,41 @@ impl<E: RescueEngine> AllocatedOperationData<E> {
mut cs: CS,
op: &Operation<E>,
) -> Result<AllocatedOperationData<E>, SynthesisError> {
macro_rules! parse_circuit_elements {
($element:ident, $len:expr) => {
let $element = op
.args
.$element
.iter()
.enumerate()
.map(|(idx, item)| {
CircuitElement::from_fe_with_known_length(
cs.namespace(|| {
format!("{} item with index {}", stringify!($element), idx)
}),
|| item.grab(),
$len,
)
})
.collect::<Result<Vec<_>, SynthesisError>>()?;
};
}

let eth_address = CircuitElement::from_fe_with_known_length(
cs.namespace(|| "eth_address"),
|| op.args.eth_address.grab(),
franklin_constants::ETH_ADDRESS_BIT_WIDTH,
)?;

let special_content_hash = op
.args
.special_content_hash
.iter()
.enumerate()
.map(|(idx, special_content_hash_bit)| {
CircuitElement::from_fe_with_known_length(
cs.namespace(|| format!("special_content_hash bit with index {}", idx)),
|| special_content_hash_bit.grab(),
1,
)
})
.collect::<Result<Vec<_>, SynthesisError>>()?;
parse_circuit_elements!(special_content_hash, 1);
parse_circuit_elements!(special_tokens, franklin_constants::TOKEN_BIT_WIDTH);
parse_circuit_elements!(special_accounts, franklin_constants::ACCOUNT_ID_BIT_WIDTH);
parse_circuit_elements!(special_nonces, franklin_constants::NONCE_BIT_WIDTH);
parse_circuit_elements!(special_prices, franklin_constants::PRICE_BIT_WIDTH);
parse_circuit_elements!(
special_eth_addresses,
franklin_constants::ETH_ADDRESS_BIT_WIDTH
);

let special_serial_id = CircuitElement::from_fe_with_known_length(
cs.namespace(|| "special_serial_id"),
Expand All @@ -332,76 +348,6 @@ impl<E: RescueEngine> AllocatedOperationData<E> {
op.args.second_amount_packed,
)?;

let special_tokens = op
.args
.special_tokens
.iter()
.enumerate()
.map(|(idx, special_token)| {
CircuitElement::from_fe_with_known_length(
cs.namespace(|| format!("special_token with index {}", idx)),
|| special_token.grab(),
franklin_constants::TOKEN_BIT_WIDTH,
)
})
.collect::<Result<Vec<_>, _>>()?;

let special_eth_addresses = op
.args
.special_eth_addresses
.iter()
.enumerate()
.map(|(idx, special_token)| {
CircuitElement::from_fe_with_known_length(
cs.namespace(|| format!("special_eth_address with index {}", idx)),
|| special_token.grab(),
franklin_constants::ETH_ADDRESS_BIT_WIDTH,
)
})
.collect::<Result<Vec<_>, _>>()?;

let special_accounts = op
.args
.special_accounts
.iter()
.enumerate()
.map(|(idx, special_account_id)| {
CircuitElement::from_fe_with_known_length(
cs.namespace(|| format!("special_account_id with index {}", idx)),
|| special_account_id.grab(),
franklin_constants::ACCOUNT_ID_BIT_WIDTH,
)
})
.collect::<Result<Vec<_>, _>>()?;

let special_nonces = op
.args
.special_nonces
.iter()
.enumerate()
.map(|(idx, special_nonce)| {
CircuitElement::from_fe_with_known_length(
cs.namespace(|| format!("special_nonce with index {}", idx)),
|| special_nonce.grab(),
franklin_constants::NONCE_BIT_WIDTH,
)
})
.collect::<Result<Vec<_>, _>>()?;

let special_prices = op
.args
.special_prices
.iter()
.enumerate()
.map(|(idx, special_price)| {
CircuitElement::from_fe_with_known_length(
cs.namespace(|| format!("special_price with index {}", idx)),
|| special_price.grab(),
franklin_constants::PRICE_BIT_WIDTH,
)
})
.collect::<Result<Vec<_>, _>>()?;

let (special_amounts_packed, special_amounts_unpacked) = op
.args
.special_amounts
Expand Down
19 changes: 12 additions & 7 deletions core/lib/circuit/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1179,8 +1179,6 @@ impl<'a, E: RescueEngine + JubjubEngine> ZkSyncCircuit<'a, E> {
self.swap(
cs.namespace(|| "swap"),
&mut cur,
&lhs,
&rhs,
global_variables,
&is_a_geq_b,
&is_account_empty,
Expand Down Expand Up @@ -2996,8 +2994,6 @@ impl<'a, E: RescueEngine + JubjubEngine> ZkSyncCircuit<'a, E> {
&self,
mut cs: CS,
cur: &mut AllocatedOperationBranch<E>,
lhs: &AllocatedOperationBranch<E>,
_rhs: &AllocatedOperationBranch<E>,
global_variables: &CircuitGlobalVariables<E>,
is_a_geq_b: &Boolean,
is_account_empty: &Boolean,
Expand All @@ -3014,6 +3010,15 @@ impl<'a, E: RescueEngine + JubjubEngine> ZkSyncCircuit<'a, E> {
!pubdata_holder.is_empty(),
"pubdata holder has to be preallocated"
);
/*
fields specification:
special_eth_addresses = [recipient_0_address, recipient_1_address]
special_tokens = [order_0_sell_token, order_1_sell_token, fee_token]
special_accounts = [order_0_sell_amount, order_1_sell_amount]
special_prices = [order_0_sell_price, order_0_buy_price, order_1_sell_price, order_1_buy_price]
special_nonces = [account_0_nonce, account_1_nonce, submitter_nonce]
*/

// construct pubdata
let mut pubdata_bits = vec![];
Expand Down Expand Up @@ -3191,7 +3196,7 @@ impl<'a, E: RescueEngine + JubjubEngine> ZkSyncCircuit<'a, E> {
.map(|num| {
let nonce_correct = CircuitElement::equals(
cs.namespace(|| format!("is_nonce_correct_in_slot {}", num)),
&lhs.account.nonce,
&cur.account.nonce,
&op_data.special_nonces[num],
)?;
Boolean::and(
Expand Down Expand Up @@ -3519,7 +3524,7 @@ impl<'a, E: RescueEngine + JubjubEngine> ZkSyncCircuit<'a, E> {
let is_signer_valid = CircuitElement::equals(
cs.namespace(|| "signer_key_correct"),
&signer_key.pubkey.get_hash(),
&lhs.account.pub_key_hash,
&cur.account.pub_key_hash,
)?;

let lhs_valid_flags = vec![
Expand Down Expand Up @@ -3559,7 +3564,7 @@ impl<'a, E: RescueEngine + JubjubEngine> ZkSyncCircuit<'a, E> {

let sender_is_submitter = CircuitElement::equals(
cs.namespace(|| "is account sender == submitter"),
&lhs.account_id,
&cur.account_id,
&op_data.special_accounts[4],
)?;

Expand Down

0 comments on commit 54cdfbe

Please sign in to comment.