Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merklesumtreechip sum constraint #16

Open
teddav opened this issue Feb 28, 2024 · 2 comments
Open

merklesumtreechip sum constraint #16

teddav opened this issue Feb 28, 2024 · 2 comments

Comments

@teddav
Copy link
Collaborator

teddav commented Feb 28, 2024

I might be wrong, but it seems like the sum constraint in the MerkleSumTreeChip has a useless loop that just ends up creating the same constraint N_CURRENCIES times.

        meta.create_gate("sum constraint", |meta| {
            (0..N_CURRENCIES)
                .map(|_| {
                    let left_balance = meta.query_advice(col_a, Rotation::cur());
                    let right_balance = meta.query_advice(col_b, Rotation::cur());
                    let computed_sum = meta.query_advice(col_c, Rotation::cur());
                    let s = meta.query_selector(sum_selector);
                    s * (left_balance + right_balance - computed_sum)
                })
                .collect::<Vec<_>>()
        });

It loops from 0 to N_CURRENCIES and it seems like it's getting the exact same values at Rotation::cur. Or maybe my understanding of Halo2 is wrong, and each loop would increase the Rotation index?
If we looped, the correct code would be:

meta.create_gate("sum constraint", |meta| {
            (0..N_CURRENCIES)
                .map(|i| {
                    let left_balance = meta.query_advice(col_a, Rotation(i as i32));
                    let right_balance = meta.query_advice(col_b, Rotation(i as i32));
                    let computed_sum = meta.query_advice(col_c, Rotation(i as i32));
                    let s = meta.query_selector(sum_selector);
                    s * (left_balance + right_balance - computed_sum)
                })
                .collect::<Vec<_>>()
        });

But the sum_balances_per_level is supposed to take only the left and right currency and add them. The loop is actually performed in the MstInclusionCircuit, so we should get rid of the loop:

meta.create_gate("sum constraint", |meta| {
  let left_balance = meta.query_advice(col_a, Rotation::cur());
  let right_balance = meta.query_advice(col_b, Rotation::cur());
  let computed_sum = meta.query_advice(col_c, Rotation::cur());
  let s = meta.query_selector(sum_selector);
  vec![s * (left_balance + right_balance - computed_sum)]
});
@SleepingShell
Copy link
Collaborator

SleepingShell commented Feb 28, 2024

I agree that this seems like it is looping N^2 times instead of the expected N times (where N is number of currencies).

I don't think we should be taking the index and using it in the rotation since, as you pointed out, the caller is also looping over N.

I'll take a deeper look at this later when I have access to a computer.

@0xalizk
Copy link

0xalizk commented Jun 4, 2024

Cc @alxkzmn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants