Skip to content

Commit

Permalink
WIP: scaffolding for testing vk manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
vicsn committed Jan 22, 2024
1 parent 5d0c2da commit 01b95d2
Showing 1 changed file with 125 additions and 1 deletion.
126 changes: 125 additions & 1 deletion synthesizer/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ pub(crate) mod test_helpers {
use indexmap::IndexMap;
use once_cell::sync::OnceCell;
use std::borrow::Borrow;
use synthesizer_snark::VerifyingKey;

pub(crate) type CurrentNetwork = Testnet3;

Expand Down Expand Up @@ -1100,7 +1101,7 @@ function check:
// Deploy the base program.
let program = Program::from_str(
r"
program program_layer_0.aleo;
program synthesis_overload.aleo;
function do:
input r0 as [[u128; 32u32]; 2u32].private;
Expand All @@ -1116,6 +1117,129 @@ function do:
assert!(vm.check_transaction(&deployment, None, rng).is_err());
}

#[test]
fn test_deployment_synthesis_overreport() {
let rng = &mut TestRng::default();

// Initialize a private key.
let private_key = sample_genesis_private_key(rng);

// Initialize the genesis block.
let genesis = sample_genesis_block(rng);

// Initialize the VM.
let vm = sample_vm();
// Update the VM.
vm.add_next_block(&genesis).unwrap();

// Deploy the base program.
let program = Program::from_str(
r"
program synthesis_overreport.aleo;
function do:
input r0 as u32.private;
add r0 r0 into r1;
output r1 as u32.public;",
)
.unwrap();

// Create the deployment transaction.
let transaction = vm.deploy(&private_key, &program, None, 0, None, rng).unwrap();

// Destructure the deployment transaction.
let Transaction::Deploy(txid, program_owner, deployment, fee) = transaction else {
panic!("Expected a deployment transaction");
};

// Increase the number of constraints in the verifying keys.
let mut vks_with_overreport = Vec::with_capacity(deployment.verifying_keys().len());
for (id, (vk, cert)) in deployment.verifying_keys() {
let mut vk = vk.deref().clone();
vk.circuit_info.num_constraints += 1;
let vk = VerifyingKey::new(Arc::new(vk));
vks_with_overreport.push((*id, (vk, cert.clone())));
}

// Compute the required fee.
let required_fee = *fee.base_amount().unwrap() + 1000; // TODO: calculate exact
// Authorize a new fee.
let fee_authorization = vm
.authorize_fee_public(&private_key, required_fee, 0, deployment.as_ref().to_deployment_id().unwrap(), rng)
.unwrap();
// Compute the fee.
let fee = vm.execute_fee_authorization(fee_authorization, None, rng).unwrap();

// Create a new deployment tranasction with the overreported verifying keys.
let adjusted_deployment =
Deployment::new(deployment.edition(), deployment.program().clone(), vks_with_overreport).unwrap();
let adjusted_transaction = Transaction::Deploy(txid, program_owner, Box::new(adjusted_deployment), fee);

// Verify the deployment transaction. It should fail because the num_constraints in the vk are not correct.
let res = vm.check_transaction(&adjusted_transaction, None, rng);
println!("Result of check transaction: {:?}", res);
assert!(vm.check_transaction(&adjusted_transaction, None, rng).is_err());

println!("Test if we reach this code");
}

#[test]
fn test_deployment_synthesis_underreport() {
let rng = &mut TestRng::default();

// Initialize a private key.
let private_key = sample_genesis_private_key(rng);

// Initialize the genesis block.
let genesis = sample_genesis_block(rng);

// Initialize the VM.
let vm = sample_vm();
// Update the VM.
vm.add_next_block(&genesis).unwrap();

// Deploy the base program.
let program = Program::from_str(
r"
program synthesis_overreport.aleo;
function do:
input r0 as u32.private;
add r0 r0 into r1;
output r1 as u32.public;",
)
.unwrap();

// Create the deployment transaction.
let transaction = vm.deploy(&private_key, &program, None, 0, None, rng).unwrap();

// Destructure the deployment transaction.
let Transaction::Deploy(txid, program_owner, deployment, fee) = transaction else {
panic!("Expected a deployment transaction");
};

// Decrease the number of constraints in the verifying keys.
let mut vks_with_underreport = Vec::with_capacity(deployment.verifying_keys().len());
for (id, (vk, cert)) in deployment.verifying_keys() {
let mut vk = vk.deref().clone();
vk.circuit_info.num_constraints -= 1;
let vk = VerifyingKey::new(Arc::new(vk));
vks_with_underreport.push((*id, (vk, cert.clone())));
}

// Create a new deployment tranasction with the underreported verifying keys.
let adjusted_deployment =
Deployment::new(deployment.edition(), deployment.program().clone(), vks_with_underreport).unwrap();
let adjusted_transaction = Transaction::Deploy(txid, program_owner, Box::new(adjusted_deployment), fee);

// Verify the deployment transaction. It should fail because the num_constraints in the vk are not correct.
let res = vm.check_transaction(&adjusted_transaction, None, rng);
println!("Result of check transaction: {:?}", res);
assert!(vm.check_transaction(&adjusted_transaction, None, rng).is_err());

println!("Test if we reach this code");
}

#[test]
#[ignore]
fn test_deployment_memory_overload() {
Expand Down

0 comments on commit 01b95d2

Please sign in to comment.