Skip to content

Commit

Permalink
signatures debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
TymurKhr committed Sep 19, 2019
1 parent 152691a commit 2c15cc0
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 97 deletions.
36 changes: 18 additions & 18 deletions contracts/contracts/VerificationKey.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ contract VerificationKey {
function getVk() internal pure returns (uint256[14] memory vk, uint256[] memory gammaABC) {


vk[0] = 0x2998fa4d1bd5e9cb49d9f091ecb5ecab72d368d76884a73ef97f669b35bdb26e;
vk[1] = 0x1e1876d47f6f4db822fac3256bdb2e3811a4ae5c6ccb7b9b7d4c40cc1e6f4e6b;
vk[2] = 0x2f2acd0fb28e5577936a3496ed3f6c696882423e4d066aee3ad798eab9ac1451;
vk[3] = 0x1ac42b5de1468de17a3a084c206cb786571040ba73959bd456d7f0c955533bdd;
vk[4] = 0x216bae0522856918314619d4f1e65406db436bc240189468af6880b93158c3c8;
vk[5] = 0x105fa1139765624aa191ba70d7cdb66601a36cda60531d1382907d4e914d54db;
vk[6] = 0x01d70a74a77d3af3cfba02b9ac8404b5f0ea87677fef59a3102ea436dbb0f6ae;
vk[7] = 0x1c850cc41756d47de60a61a740fa21d7f9de8163703cc87b31af08dea560fd47;
vk[8] = 0x0bbc7846178891791e0605c9d4d1382141d56dbfc5f5bd2c4c49c69a4b6c23f4;
vk[9] = 0x27097b911173d44df16a1121f3fc3c4c37c8ffbdc8bc9063333154fe068dc016;
vk[10] = 0x10e0e22e81bba8c3265c072fdab234165ab6846a8aa1eff65a6d00cb59ceab8f;
vk[11] = 0x2b979c83d4af5afdabaf368438308f3c1ce9dcf75ddaba046445bf3a46d2a797;
vk[12] = 0x2bafcf23d866ecd436540e98c9f82253d285454390db60adfa82e852739d6016;
vk[13] = 0x0645c9ed18e4883a7e431e798bc8d5a63a8ce4e7214da39b228f34b49c6b69e4;
vk[0] = 0x170481d3651c74de4f3b078c6687dc3003a57dafdac32d380b900a1104906b6c;
vk[1] = 0x126ee40b13d92c8348a566cc59b8e726d2c474c5914cddda4e37e98091ca7a72;
vk[2] = 0x15a412e9132923000cb247c176bae3701cb102b3cb7b082713021e79af2b1ced;
vk[3] = 0x1330ab9412cf44ac4242dae1e3d67a0262a83148d1c0fcef53e42912382fb314;
vk[4] = 0x13b2fe66b3e1e56195f0252d1cec63412afd202ce03bd728948fe2c32e4fa13c;
vk[5] = 0x19ac86c9511f30d8d587c901371fec8e16327ea1eda10df7c0ad8e7b63a29f75;
vk[6] = 0x1b4a27677797315b23b8ff49cf391f21713e97076c9b2da57f420807d6a48c60;
vk[7] = 0x008aac71550f75a5014e0179fdca0d632dd7ceedf965bd7212af8053d70387b1;
vk[8] = 0x1f43b93b0098a45f3f8e40776d7ad550f82c6a67bb132d960863bd3ff2461c2c;
vk[9] = 0x04448a2299acd083910d6721709b552eea0915f66045ef1644bac0334ded5d8b;
vk[10] = 0x1b362c16c4d95049d5c77d657814733b8ecd98679c00206d3436ab48ed2084a3;
vk[11] = 0x049c0d56345e711c5601f3b18165fadd20a77188a934dd149fbced7912057c07;
vk[12] = 0x0fe223cee8b319c3bbc6dc4cb2cfdee628b1d671f70818a2b3acfa2809ac731f;
vk[13] = 0x272db7862d66075ad074f7726659df52147c3188730606d0347fe191af71e075;

gammaABC = new uint256[](4);
gammaABC[0] = 0x1f3b15afc251e82d266395efb736d45843d844e31242f609f976c3d320e1b0b8;
gammaABC[1] = 0x29c4b33d85f273b9686026fa460382aa3db7203d6ba28e0763d3a913e17bfbf1;
gammaABC[2] = 0x2332f617f0ad5a7685d6342bdb1a47cb1edb1705fc77fa2451cd37c269c8e565;
gammaABC[3] = 0x065f0aee16fe0a8614ce15357c504bf5f0b58d48ab58745057a92f65284b2dc2;
gammaABC[0] = 0x0201f22aa4a8f6c96c3fe1e141f6d4f7e76ab3cfb95f0f3de5df23d7a648bdcf;
gammaABC[1] = 0x00e00015642a5ebd59565eeebb61b5e3e237296e3216689f6851115c3bddd02b;
gammaABC[2] = 0x0c53fb3218a5b798d0107ae90ef08218ad6e8341da7888c209f8cedc13e7a5a0;
gammaABC[3] = 0x15374c027d2c072032742a5ed47e01e9a390c86ee3b5816646446f7cc7cfd2a3;


}
Expand Down
50 changes: 6 additions & 44 deletions core/circuit/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,11 +801,12 @@ impl<'a, E: JubjubEngine> FranklinCircuit<'a, E> {
)?;
base_valid_flags.push(is_sig_correct);

let _is_signer_valid = CircuitElement::equals(
let is_signer_valid = CircuitElement::equals(
cs.namespace(|| "signer_key_correect"),
&op_data.signer_pubkey.get_hash(),
&op_data.new_pubkey_hash, //earlier we ensured that this new_pubkey_hash is equal to current if existed
)?;
base_valid_flags.push(is_signer_valid);

// base_valid_flags.push(_is_signer_valid);
let is_base_valid = multi_and(cs.namespace(|| "valid base withdraw"), &base_valid_flags)?;
Expand Down Expand Up @@ -1046,15 +1047,6 @@ impl<'a, E: JubjubEngine> FranklinCircuit<'a, E> {
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(op_data.new_pubkey_hash.get_bits_be());
serialized_tx_bits.extend(cur.token.get_bits_be());
serialized_tx_bits.extend(op_data.full_amount.get_bits_be());
serialized_tx_bits.extend(cur.account.nonce.get_bits_be());

//useful below
let is_first_chunk = Boolean::from(Expression::equals(
cs.namespace(|| "is_first_chunk"),
Expand Down Expand Up @@ -1111,26 +1103,6 @@ impl<'a, E: JubjubEngine> FranklinCircuit<'a, E> {

is_valid_flags.push(is_a_correct);

let is_serialized_tx_correct = verify_signature_message_construction(
cs.namespace(|| "is_serialized_tx_correct"),
serialized_tx_bits,
&op_data,
)?;

let is_sig_correct = multi_or(
cs.namespace(|| "sig is valid or not first chunk"),
&[is_serialized_tx_correct, is_first_chunk.clone().not()],
)?;
is_valid_flags.push(is_sig_correct);

let _is_signer_valid = CircuitElement::equals(
cs.namespace(|| "signer_key_correect"),
&op_data.signer_pubkey.get_hash(),
&op_data.new_pubkey_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)?;

let is_valid_first = Boolean::and(
Expand All @@ -1156,17 +1128,6 @@ impl<'a, E: JubjubEngine> FranklinCircuit<'a, E> {
&cur.account.pub_key_hash,
&is_valid_first,
)?;

// update nonce
let updated_nonce =
Expression::from(&cur.account.nonce.get_number()) + Expression::u64::<CS>(1); //TODO: we can provide Expression++ syntax

cur.account.nonce = CircuitElement::conditionally_select_with_number_strict(
cs.namespace(|| "update cur nonce"),
updated_nonce,
&cur.account.nonce,
&is_valid_first,
)?;
Ok(tx_valid)
}

Expand Down Expand Up @@ -1588,14 +1549,15 @@ impl<'a, E: JubjubEngine> FranklinCircuit<'a, E> {
serialized_tx_bits,
&op_data,
)?;

lhs_valid_flags.push(is_serialized_tx_correct);

let _is_signer_valid = CircuitElement::equals(
cs.namespace(|| "signer_key_correect"),
let is_signer_valid = CircuitElement::equals(
cs.namespace(|| "signer_key_correct"),
&op_data.signer_pubkey.get_hash(),
&lhs.account.pub_key_hash,
)?;
lhs_valid_flags.push(is_signer_valid);

// lhs_valid_flags.push(_is_signer_valid);

let lhs_valid = multi_and(cs.namespace(|| "lhs_valid"), &lhs_valid_flags)?;
Expand Down
25 changes: 3 additions & 22 deletions core/circuit/src/witness/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub fn apply_deposit_tx(
) -> DepositWitness<Bn256> {
let alt_new_pubkey_hash = Fr::from_hex(&deposit.priority_op.account.to_hex()).unwrap();
let deposit_data = DepositData {
amount: deposit.priority_op.amount.to_u128().unwrap(),
amount: deposit.priority_op.amount.to_string().parse().unwrap(),
token: u32::from(deposit.priority_op.token),
account_address: deposit.account_id,
new_pub_key_hash: alt_new_pubkey_hash,
Expand All @@ -119,25 +119,7 @@ pub fn apply_deposit(
let account_address_fe = Fr::from_str(&deposit.account_address.to_string()).unwrap();
let token_fe = Fr::from_str(&deposit.token.to_string()).unwrap();
let amount_as_field_element = Fr::from_str(&deposit.amount.to_string()).unwrap();

let amount_bits = convert_to_float(
deposit.amount,
franklin_constants::AMOUNT_EXPONENT_BIT_WIDTH,
franklin_constants::AMOUNT_MANTISSA_BIT_WIDTH,
10,
)
.unwrap();
let reparsed_amount = parse_float_to_u128(
amount_bits.clone(),
franklin_constants::AMOUNT_EXPONENT_BIT_WIDTH,
franklin_constants::AMOUNT_MANTISSA_BIT_WIDTH,
10,
)
.unwrap();
assert_eq!(reparsed_amount, deposit.amount);

let amount_encoded: Fr = le_bit_vector_into_field_element(&amount_bits);

println!("amount_as_field_element is: {}", amount_as_field_element);
//calculate a and b
let a = amount_as_field_element;
let b = Fr::zero();
Expand All @@ -154,7 +136,6 @@ pub fn apply_deposit(
|| (acc.pub_key_hash == Fr::zero())
);
acc.pub_key_hash = deposit.new_pub_key_hash;
acc.nonce.add_assign(&Fr::from_str("1").unwrap());
},
|bal| bal.value.add_assign(&amount_as_field_element),
);
Expand Down Expand Up @@ -187,7 +168,7 @@ pub fn apply_deposit(
},
args: OperationArguments {
ethereum_key: Some(Fr::zero()),
amount_packed: Some(amount_encoded),
amount_packed: Some(Fr::zero()),
full_amount: Some(amount_as_field_element),
fee: Some(Fr::zero()),
a: Some(a),
Expand Down
8 changes: 6 additions & 2 deletions core/prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,10 @@ impl BabyProver {
self.rewind_state(&storage, expected_current_block)?;
}
let initial_root = self.accounts_tree.root_hash();

info!(
"initial root when block processing started: {}",
initial_root
);
for (index, item) in &self.accounts_tree.items {
info!("index: {}, item: {}", index, item.pub_key_hash);
}
Expand All @@ -309,6 +312,7 @@ impl BabyProver {
let ops = storage.get_block_operations(block.block_number).unwrap();

drop(storage);
let (root, acc_witness) = apply_fee(&mut self.accounts_tree, block.fee_account, 0, 0);
let mut operations = vec![];
let mut pub_data = vec![];
let mut fees = vec![];
Expand Down Expand Up @@ -555,7 +559,7 @@ impl BabyProver {
&mut self.accounts_tree,
block.fee_account,
u32::from(token),
fee.to_u128().unwrap(),
fee.to_string().parse().unwrap(),
);
root_after_fee = root;
validator_account_witness = acc_witness;
Expand Down
8 changes: 0 additions & 8 deletions js/franklin_lib/abi/Franklin.json
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,6 @@
"name": "_verifierAddress",
"type": "address"
},
{
"name": "_vkAddress",
"type": "address"
},
{
"name": "_genesisRoot",
"type": "bytes32"
Expand Down Expand Up @@ -961,10 +957,6 @@
"name": "_verifierAddress",
"type": "address"
},
{
"name": "_vkAddress",
"type": "address"
},
{
"name": "_genesisRoot",
"type": "bytes32"
Expand Down
4 changes: 2 additions & 2 deletions js/franklin_lib/scripts/loadtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ async function main() {
let wallet = await Wallet.fromEthWallet(ethWallet);
// let full_exit_tx = await wallet.emergencyWithdraw({id: 0, address: ethers.constants.AddressZero});
// console.log(full_exit_tx);
// let dep_tx = await wallet.deposit({id: 0, address: ''}, parseEther("0.2"));
// console.log(dep_tx);
let dep_tx = await wallet.deposit({id: 0, address: ''}, parseEther("0.2"));
console.log(dep_tx);

await wallet.updateState();
console.log(wallet.supportedTokens);
Expand Down
2 changes: 1 addition & 1 deletion js/franklin_lib/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export class Wallet {
async deposit(token: Token, amount: BigNumberish) {
const franklinDeployedContract = new Contract(this.provider.contractAddress, franklinContractCode.interface, this.ethWallet);
if (token.id == 0) {
const tx = await franklinDeployedContract.depositETH(this.address, {value: amount});
const tx = await franklinDeployedContract.depositETH(this.address, {value: amount, gasLimit: bigNumberify("300000")});
return tx.hash;
} else {
const erc20DeployedToken = new Contract(token.address, IERC20Conract.abi, this.ethWallet);
Expand Down

0 comments on commit 2c15cc0

Please sign in to comment.