Skip to content

Commit

Permalink
feat: add flexible nullifier to update circuit (#1315)
Browse files Browse the repository at this point in the history
* feat: add flexible nullifier to update circuit

* regenerate update keys

* fix: derive nullifier in circuit from leaf and tx hash

* regenerate uodate keys

---------

Co-authored-by: Sergey Timoshin <[email protected]>
  • Loading branch information
ananas-block and sergeytimoshin authored Oct 27, 2024
1 parent 03b17ab commit 9a50aeb
Show file tree
Hide file tree
Showing 14 changed files with 364 additions and 287 deletions.
13 changes: 11 additions & 2 deletions circuit-lib/light-prover-client/src/batch_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct BatchUpdateCircuitInputs {
pub public_input_hash: BigInt,
pub old_root: BigInt,
pub new_root: BigInt,
pub tx_hashes: Vec<BigInt>,
pub leaves_hashchain_hash: BigInt,
pub leaves: Vec<BigInt>,
pub merkle_proofs: Vec<Vec<BigInt>>,
Expand Down Expand Up @@ -38,6 +39,8 @@ pub fn get_batch_update_inputs<const HEIGHT: usize>(
// get from photon
current_root: [u8; 32],
// get from photon
tx_hashes: Vec<[u8; 32]>,
// get from photon
leaves: Vec<[u8; 32]>,
// get from account
leaves_hashchain: [u8; 32],
Expand All @@ -54,6 +57,7 @@ pub fn get_batch_update_inputs<const HEIGHT: usize>(
// Hence we patch the proofs with the changelog.
let mut changelog: Vec<ChangelogEntry<HEIGHT>> = Vec::new();
let mut circuit_merkle_proofs = vec![];
let mut nullifiers = vec![];
for (i, (_leaf, (merkle_proof, index))) in leaves
.iter()
.zip(merkle_proofs.iter().zip(path_indices.iter()))
Expand All @@ -70,9 +74,10 @@ pub fn get_batch_update_inputs<const HEIGHT: usize>(
}

let merkle_proof = bounded_vec_merkle_proof.to_array().unwrap();

let nullifier = Poseidon::hashv(&[&leaves[i], &tx_hashes[i]]).unwrap();
nullifiers.push(nullifier);
let (root, changelog_entry) =
comput_root_from_merkle_proof([0u8; 32], &merkle_proof, *index);
comput_root_from_merkle_proof(nullifier, &merkle_proof, *index);
new_root = root;

changelog.push(changelog_entry);
Expand All @@ -86,6 +91,10 @@ pub fn get_batch_update_inputs<const HEIGHT: usize>(
public_input_hash: BigInt::from_be_bytes(&public_input_hash),
old_root: BigInt::from_be_bytes(&old_root),
new_root: BigInt::from_be_bytes(&new_root),
tx_hashes: tx_hashes
.iter()
.map(|tx_hash| BigInt::from_bytes_be(Sign::Plus, tx_hash))
.collect(),
leaves_hashchain_hash: BigInt::from_be_bytes(&leaves_hashchain),
leaves: leaves
.iter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct BatchUpdateProofInputsJson {
pub height: u32,
#[serde(rename(serialize = "batchSize"))]
pub batch_size: u32,
#[serde(rename(serialize = "txHashes"))]
pub tx_hashes: Vec<String>,
}

#[derive(Serialize, Debug)]
Expand All @@ -38,7 +40,6 @@ impl BatchUpdateProofInputsJson {
let old_root = big_int_to_string(&inputs.old_root);
let new_root = big_int_to_string(&inputs.new_root);
let leaves_hashchain_hash = big_int_to_string(&inputs.leaves_hashchain_hash);

let leaves = inputs
.leaves
.iter()
Expand All @@ -54,6 +55,11 @@ impl BatchUpdateProofInputsJson {
let path_indices = inputs.path_indices.clone();
let height = inputs.height;
let batch_size = inputs.batch_size;
let tx_hashes = inputs
.tx_hashes
.iter()
.map(big_int_to_string)
.collect::<Vec<String>>();

Self {
public_input_hash,
Expand All @@ -65,6 +71,7 @@ impl BatchUpdateProofInputsJson {
path_indices,
height,
batch_size,
tx_hashes,
}
}

Expand Down
80 changes: 43 additions & 37 deletions circuit-lib/light-prover-client/tests/gnark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,51 +78,57 @@ async fn prove_inclusion() {
const HEIGHT: usize = 26;
const CANOPY: usize = 0;
let num_insertions = 10;
let tx_hash = [0u8; 32];

info!("initializing merkle tree");
info!("initializing merkle tree for update.");
let mut merkle_tree = MerkleTree::<Poseidon>::new(HEIGHT, CANOPY);
for _ in 0..2 {
let mut leaves = vec![];
let mut nullifiers = vec![];
for i in 0..num_insertions {
let mut bn: [u8; 32] = [0; 32];
bn[31] = i as u8;
let leaf: [u8; 32] = Poseidon::hash(&bn).unwrap();
leaves.push(leaf);
merkle_tree.append(&leaf).unwrap();
let nullifier = Poseidon::hashv(&[&leaf, &tx_hash]).unwrap();
nullifiers.push(nullifier);
}

let mut leaves = vec![];
for i in 0..num_insertions {
let mut bn: [u8; 32] = [0; 32];
bn[31] = i as u8;
let leaf: [u8; 32] = Poseidon::hash(&bn).unwrap();
leaves.push(leaf);
merkle_tree.append(&leaf).unwrap();
}
let mut merkle_proofs = vec![];
let mut path_indices = vec![];
for index in 0..leaves.len() {
let proof = merkle_tree.get_proof_of_leaf(index, true).unwrap();
merkle_proofs.push(proof.to_vec());
path_indices.push(index as u32);
}
let root = merkle_tree.root();
let leaves_hashchain = calculate_hash_chain(&nullifiers);
let inputs = get_batch_update_inputs::<HEIGHT>(
root,
vec![tx_hash; num_insertions],
leaves,
leaves_hashchain,
merkle_proofs,
path_indices,
num_insertions as u32,
);
let client = Client::new();
let inputs = update_inputs_string(&inputs);

let mut merkle_proofs = vec![];
let mut path_indices = vec![];
for index in 0..leaves.len() {
let proof = merkle_tree.get_proof_of_leaf(index, true).unwrap();
merkle_proofs.push(proof.to_vec());
path_indices.push(index as u32);
let response_result = client
.post(&format!("{}{}", SERVER_ADDRESS, PROVE_PATH))
.header("Content-Type", "text/plain; charset=utf-8")
.body(inputs)
.send()
.await
.expect("Failed to execute request.");
assert!(response_result.status().is_success());
}
let root = merkle_tree.root();
let leaves_hashchain = calculate_hash_chain(&leaves);
let inputs = get_batch_update_inputs::<HEIGHT>(
root,
leaves,
leaves_hashchain,
merkle_proofs,
path_indices,
num_insertions,
);
let client = Client::new();
let inputs = update_inputs_string(&inputs);
let response_result = client
.post(&format!("{}{}", SERVER_ADDRESS, PROVE_PATH))
.header("Content-Type", "text/plain; charset=utf-8")
.body(inputs)
.send()
.await
.expect("Failed to execute request.");
let status = response_result.status();
assert!(status.is_success());

let num_insertions = 10;

info!("initializing merkle tree");
info!("initializing merkle tree for append.");
let merkle_tree = MerkleTree::<Poseidon>::new(HEIGHT, CANOPY);

let old_subtrees = merkle_tree.get_subtrees();
Expand Down
90 changes: 45 additions & 45 deletions circuit-lib/verifier/src/verifying_keys/update_26_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,62 @@ use groth16_solana::groth16::Groth16Verifyingkey;
pub const VERIFYINGKEY: Groth16Verifyingkey = Groth16Verifyingkey {
nr_pubinputs: 1usize,
vk_alpha_g1: [
9u8, 143u8, 125u8, 152u8, 67u8, 157u8, 160u8, 211u8, 179u8, 117u8, 64u8, 31u8, 121u8,
130u8, 54u8, 34u8, 52u8, 211u8, 94u8, 220u8, 203u8, 29u8, 171u8, 209u8, 131u8, 44u8, 212u8,
115u8, 61u8, 188u8, 164u8, 12u8, 20u8, 48u8, 61u8, 101u8, 162u8, 205u8, 197u8, 123u8, 64u8,
255u8, 223u8, 210u8, 9u8, 1u8, 156u8, 67u8, 93u8, 183u8, 103u8, 124u8, 172u8, 32u8, 120u8,
143u8, 111u8, 20u8, 244u8, 118u8, 206u8, 166u8, 61u8, 214u8,
18u8, 242u8, 15u8, 176u8, 13u8, 65u8, 99u8, 219u8, 222u8, 77u8, 59u8, 192u8, 147u8, 96u8,
169u8, 206u8, 57u8, 25u8, 132u8, 202u8, 191u8, 245u8, 30u8, 62u8, 224u8, 234u8, 6u8, 225u8,
39u8, 53u8, 67u8, 251u8, 40u8, 163u8, 190u8, 114u8, 60u8, 43u8, 210u8, 54u8, 48u8, 46u8,
180u8, 239u8, 212u8, 232u8, 249u8, 80u8, 50u8, 153u8, 215u8, 69u8, 221u8, 47u8, 49u8, 55u8,
30u8, 162u8, 187u8, 229u8, 188u8, 161u8, 240u8, 132u8,
],
vk_beta_g2: [
10u8, 39u8, 82u8, 65u8, 235u8, 40u8, 7u8, 253u8, 127u8, 238u8, 111u8, 111u8, 28u8, 69u8,
23u8, 206u8, 190u8, 36u8, 59u8, 18u8, 215u8, 172u8, 174u8, 221u8, 146u8, 213u8, 92u8,
216u8, 96u8, 57u8, 108u8, 154u8, 8u8, 34u8, 244u8, 65u8, 149u8, 207u8, 121u8, 200u8, 179u8,
172u8, 1u8, 158u8, 254u8, 47u8, 160u8, 12u8, 193u8, 10u8, 255u8, 17u8, 80u8, 87u8, 171u8,
92u8, 90u8, 217u8, 119u8, 197u8, 54u8, 14u8, 166u8, 41u8, 39u8, 139u8, 77u8, 200u8, 244u8,
248u8, 190u8, 98u8, 122u8, 74u8, 13u8, 246u8, 166u8, 131u8, 104u8, 247u8, 236u8, 211u8,
207u8, 216u8, 207u8, 112u8, 183u8, 222u8, 27u8, 88u8, 216u8, 198u8, 251u8, 5u8, 78u8,
251u8, 40u8, 41u8, 21u8, 34u8, 16u8, 187u8, 130u8, 134u8, 22u8, 166u8, 169u8, 179u8, 193u8,
183u8, 123u8, 173u8, 146u8, 22u8, 22u8, 103u8, 218u8, 249u8, 131u8, 24u8, 49u8, 141u8,
60u8, 1u8, 103u8, 179u8, 230u8, 160u8,
44u8, 44u8, 62u8, 149u8, 146u8, 105u8, 72u8, 46u8, 27u8, 24u8, 232u8, 24u8, 109u8, 253u8,
184u8, 1u8, 203u8, 159u8, 186u8, 113u8, 55u8, 58u8, 84u8, 82u8, 241u8, 225u8, 115u8, 80u8,
249u8, 89u8, 212u8, 58u8, 43u8, 189u8, 109u8, 149u8, 201u8, 77u8, 92u8, 122u8, 42u8, 193u8,
172u8, 108u8, 156u8, 96u8, 135u8, 51u8, 151u8, 69u8, 50u8, 96u8, 67u8, 239u8, 88u8, 99u8,
239u8, 30u8, 249u8, 100u8, 109u8, 141u8, 200u8, 37u8, 7u8, 253u8, 159u8, 93u8, 35u8, 242u8,
66u8, 9u8, 108u8, 167u8, 73u8, 152u8, 151u8, 86u8, 43u8, 155u8, 8u8, 246u8, 3u8, 61u8,
84u8, 152u8, 38u8, 119u8, 199u8, 25u8, 132u8, 26u8, 248u8, 12u8, 61u8, 233u8, 45u8, 152u8,
185u8, 117u8, 249u8, 240u8, 41u8, 208u8, 156u8, 32u8, 37u8, 17u8, 96u8, 211u8, 37u8, 21u8,
247u8, 141u8, 220u8, 167u8, 130u8, 185u8, 61u8, 193u8, 208u8, 190u8, 184u8, 255u8, 10u8,
92u8, 27u8, 209u8,
],
vk_gamme_g2: [
39u8, 111u8, 183u8, 210u8, 58u8, 72u8, 54u8, 223u8, 74u8, 240u8, 100u8, 227u8, 203u8,
240u8, 117u8, 63u8, 59u8, 141u8, 49u8, 242u8, 239u8, 248u8, 202u8, 251u8, 226u8, 51u8,
195u8, 114u8, 157u8, 129u8, 3u8, 19u8, 0u8, 160u8, 157u8, 147u8, 217u8, 222u8, 163u8,
163u8, 229u8, 184u8, 93u8, 135u8, 53u8, 34u8, 177u8, 56u8, 124u8, 96u8, 135u8, 161u8,
126u8, 109u8, 200u8, 240u8, 156u8, 26u8, 34u8, 89u8, 14u8, 173u8, 201u8, 131u8, 26u8, 17u8,
107u8, 164u8, 111u8, 82u8, 81u8, 170u8, 3u8, 95u8, 175u8, 85u8, 41u8, 112u8, 126u8, 139u8,
183u8, 214u8, 63u8, 83u8, 241u8, 223u8, 117u8, 124u8, 104u8, 1u8, 76u8, 42u8, 172u8, 93u8,
89u8, 98u8, 16u8, 164u8, 65u8, 217u8, 126u8, 17u8, 4u8, 25u8, 147u8, 29u8, 145u8, 25u8,
178u8, 205u8, 187u8, 245u8, 24u8, 187u8, 106u8, 129u8, 92u8, 19u8, 175u8, 157u8, 35u8,
209u8, 19u8, 13u8, 34u8, 163u8, 82u8, 25u8,
40u8, 150u8, 246u8, 24u8, 45u8, 65u8, 115u8, 247u8, 236u8, 172u8, 212u8, 166u8, 156u8,
162u8, 177u8, 45u8, 101u8, 131u8, 68u8, 40u8, 243u8, 37u8, 192u8, 144u8, 228u8, 127u8,
197u8, 158u8, 129u8, 197u8, 168u8, 139u8, 13u8, 60u8, 131u8, 176u8, 128u8, 144u8, 254u8,
64u8, 152u8, 11u8, 101u8, 122u8, 50u8, 30u8, 43u8, 8u8, 169u8, 246u8, 45u8, 61u8, 202u8,
98u8, 14u8, 167u8, 116u8, 248u8, 184u8, 53u8, 166u8, 174u8, 50u8, 209u8, 7u8, 140u8, 86u8,
75u8, 213u8, 217u8, 31u8, 228u8, 157u8, 41u8, 58u8, 239u8, 65u8, 51u8, 5u8, 23u8, 121u8,
1u8, 26u8, 207u8, 217u8, 68u8, 105u8, 137u8, 108u8, 107u8, 205u8, 61u8, 138u8, 43u8, 26u8,
144u8, 15u8, 239u8, 138u8, 134u8, 129u8, 8u8, 178u8, 132u8, 163u8, 89u8, 130u8, 126u8,
70u8, 218u8, 212u8, 209u8, 64u8, 109u8, 136u8, 149u8, 56u8, 156u8, 223u8, 53u8, 101u8,
186u8, 14u8, 56u8, 49u8, 195u8, 146u8, 155u8,
],
vk_delta_g2: [
30u8, 38u8, 169u8, 252u8, 228u8, 121u8, 115u8, 47u8, 9u8, 222u8, 132u8, 156u8, 150u8, 78u8,
155u8, 242u8, 109u8, 137u8, 142u8, 125u8, 42u8, 244u8, 105u8, 33u8, 246u8, 122u8, 206u8,
88u8, 76u8, 254u8, 71u8, 94u8, 25u8, 243u8, 217u8, 74u8, 16u8, 132u8, 43u8, 28u8, 187u8,
70u8, 180u8, 195u8, 25u8, 132u8, 249u8, 147u8, 6u8, 138u8, 218u8, 3u8, 177u8, 119u8, 98u8,
124u8, 219u8, 34u8, 122u8, 115u8, 59u8, 39u8, 246u8, 164u8, 33u8, 254u8, 212u8, 37u8,
118u8, 103u8, 177u8, 174u8, 223u8, 93u8, 178u8, 22u8, 232u8, 130u8, 161u8, 83u8, 38u8,
146u8, 158u8, 154u8, 50u8, 148u8, 184u8, 67u8, 231u8, 221u8, 64u8, 53u8, 143u8, 96u8,
252u8, 176u8, 2u8, 211u8, 35u8, 201u8, 254u8, 108u8, 254u8, 37u8, 154u8, 225u8, 225u8,
126u8, 197u8, 185u8, 112u8, 89u8, 226u8, 47u8, 107u8, 187u8, 152u8, 186u8, 86u8, 132u8,
23u8, 116u8, 58u8, 66u8, 118u8, 126u8, 171u8, 75u8,
45u8, 48u8, 201u8, 55u8, 219u8, 25u8, 208u8, 203u8, 24u8, 103u8, 220u8, 187u8, 215u8,
210u8, 3u8, 73u8, 46u8, 76u8, 185u8, 95u8, 123u8, 188u8, 45u8, 183u8, 21u8, 107u8, 240u8,
186u8, 210u8, 205u8, 106u8, 119u8, 36u8, 4u8, 64u8, 236u8, 225u8, 183u8, 42u8, 253u8, 37u8,
210u8, 163u8, 239u8, 203u8, 236u8, 115u8, 39u8, 187u8, 252u8, 245u8, 68u8, 58u8, 92u8,
35u8, 64u8, 173u8, 179u8, 197u8, 190u8, 60u8, 6u8, 233u8, 80u8, 41u8, 59u8, 118u8, 142u8,
52u8, 177u8, 24u8, 207u8, 110u8, 207u8, 203u8, 117u8, 41u8, 38u8, 71u8, 201u8, 103u8, 55u8,
122u8, 39u8, 101u8, 100u8, 240u8, 127u8, 44u8, 117u8, 27u8, 76u8, 207u8, 77u8, 219u8,
178u8, 45u8, 178u8, 143u8, 148u8, 192u8, 125u8, 116u8, 33u8, 241u8, 186u8, 199u8, 6u8, 7u8,
197u8, 31u8, 83u8, 12u8, 120u8, 208u8, 17u8, 36u8, 67u8, 127u8, 222u8, 166u8, 217u8, 18u8,
200u8, 145u8, 14u8, 157u8, 165u8,
],
vk_ic: &[
[
43u8, 168u8, 24u8, 117u8, 123u8, 177u8, 38u8, 37u8, 254u8, 246u8, 65u8, 54u8, 232u8,
78u8, 249u8, 170u8, 62u8, 211u8, 7u8, 33u8, 44u8, 73u8, 132u8, 176u8, 76u8, 1u8, 16u8,
138u8, 38u8, 107u8, 133u8, 146u8, 46u8, 98u8, 187u8, 34u8, 44u8, 105u8, 136u8, 2u8,
15u8, 114u8, 14u8, 213u8, 101u8, 35u8, 233u8, 149u8, 66u8, 152u8, 98u8, 13u8, 215u8,
217u8, 99u8, 168u8, 137u8, 47u8, 64u8, 220u8, 131u8, 50u8, 112u8, 135u8,
41u8, 38u8, 162u8, 77u8, 186u8, 179u8, 89u8, 12u8, 28u8, 36u8, 59u8, 108u8, 242u8,
54u8, 61u8, 129u8, 19u8, 233u8, 100u8, 227u8, 96u8, 120u8, 172u8, 95u8, 7u8, 113u8,
12u8, 131u8, 228u8, 102u8, 226u8, 31u8, 27u8, 24u8, 63u8, 10u8, 13u8, 109u8, 138u8,
188u8, 199u8, 235u8, 92u8, 247u8, 250u8, 127u8, 113u8, 129u8, 242u8, 80u8, 129u8,
141u8, 208u8, 223u8, 80u8, 83u8, 134u8, 166u8, 77u8, 51u8, 177u8, 12u8, 142u8, 47u8,
],
[
9u8, 35u8, 223u8, 24u8, 86u8, 206u8, 147u8, 206u8, 121u8, 18u8, 216u8, 34u8, 124u8,
252u8, 42u8, 179u8, 27u8, 129u8, 46u8, 143u8, 178u8, 165u8, 90u8, 197u8, 162u8, 232u8,
155u8, 18u8, 81u8, 158u8, 193u8, 217u8, 32u8, 224u8, 84u8, 254u8, 98u8, 217u8, 16u8,
72u8, 24u8, 176u8, 169u8, 112u8, 75u8, 138u8, 159u8, 123u8, 112u8, 32u8, 18u8, 214u8,
16u8, 51u8, 227u8, 244u8, 178u8, 166u8, 205u8, 126u8, 89u8, 43u8, 26u8, 172u8,
35u8, 99u8, 217u8, 53u8, 54u8, 184u8, 68u8, 227u8, 251u8, 100u8, 90u8, 29u8, 112u8,
165u8, 129u8, 148u8, 15u8, 25u8, 150u8, 172u8, 202u8, 235u8, 9u8, 210u8, 108u8, 134u8,
85u8, 124u8, 194u8, 18u8, 253u8, 191u8, 17u8, 9u8, 156u8, 101u8, 10u8, 88u8, 169u8,
43u8, 199u8, 36u8, 148u8, 48u8, 144u8, 40u8, 249u8, 217u8, 130u8, 59u8, 58u8, 109u8,
111u8, 183u8, 141u8, 118u8, 14u8, 18u8, 164u8, 246u8, 194u8, 24u8, 14u8, 101u8,
],
],
};
Loading

0 comments on commit 9a50aeb

Please sign in to comment.