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

Update bitwise chiplet constraints #180

Merged
merged 1 commit into from
Mar 9, 2023
Merged

Conversation

Fumuran
Copy link
Contributor

@Fumuran Fumuran commented Feb 27, 2023

Bitwise chiplet constraints have been updated, now they use variables.
Currently this is the only improvement that the syntax allows to make with these constraints.

@tohrnii
Copy link
Contributor

tohrnii commented Feb 27, 2023

@Overcastan I think there are a few more things we can do here. For e.g. since now we have list comprehension, we could redefine the shape of the trace in terms of groups, i.e.,

main: [s, a0, b0, a[4], b[4], zp, z, dummy]

And then we could use list comprehension and list folding to redefine a_aggr, b_aggr, a_and_b and a_xor_b. For e.g.:

let a_aggr = sum([2^i * a for (i, a) in (0..4, a)])
let b_aggr = sum([2^i * b for (i, b) in (0..4, b)])

It would be interesting to explore if there are other ways to make the constraints cleaner using other syntax we have planned for v0.3 as well.

@Fumuran
Copy link
Contributor Author

Fumuran commented Feb 28, 2023

Ah, indeed! I remembered that we have list comprehension, but I forgot that we also have list folding. Thank you!

@Fumuran Fumuran force-pushed the andrew-bitwise-constraints branch from 26e933f to 694b0ea Compare February 28, 2023 13:38
Copy link
Collaborator

@Al-Kindi-0 Al-Kindi-0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great @Overcastan !
Left just a few minor nits.

@@ -22,26 +22,28 @@ integrity_constraints:
enf k1 * (s' - s) = 0

# Enforce that input is decomposed into valid bits
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: that the input

enf b_limb[0]^2 - b_limb[0] = 0
enf b_limb[1]^2 - b_limb[1] = 0
enf b_limb[2]^2 - b_limb[2] = 0
enf b_limb[3]^2 - b_limb[3] = 0

# Enforce that the values in the column a in the first row should be the aggregation of the
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: in the first row of column a.
I think it should be the first row of the current 8 row cycle, but I think this is probably too verbose.


# Enforce that the values in the column a in the first row should be the aggregation of the
# decomposed bit columns a0..a3.
enf k0 * (a - (2^0 * a0 + 2^1 * a1 + 2^2 * a2 + 2^3 * a3)) = 0
let a_aggr = sum([2^i * a for (i, a) in (0..4, a_limb)])
enf k0 * (a - a_aggr) = 0
# Enforce that the values in the column b in the first row should be the aggregation of the
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment to the one on a


# Enforce that the values in the column a in the first row should be the aggregation of the
# decomposed bit columns a0..a3.
enf k0 * (a - (2^0 * a0 + 2^1 * a1 + 2^2 * a2 + 2^3 * a3)) = 0
let a_aggr = sum([2^i * a for (i, a) in (0..4, a_limb)])
enf k0 * (a - a_aggr) = 0
# Enforce that the values in the column b in the first row should be the aggregation of the
# decomposed bit columns b0..b3.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe " columns b" instead of "b0..b3"?

air-script/tests/bitwise/bitwise.air Outdated Show resolved Hide resolved
air-script/tests/bitwise/bitwise.air Show resolved Hide resolved
Comment on lines 25 to 28
enf a_limb[0]^2 - a_limb[0] = 0
enf a_limb[1]^2 - a_limb[1] = 0
enf a_limb[2]^2 - a_limb[2] = 0
enf a_limb[3]^2 - a_limb[3] = 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we not yet support something like this?:

enf a = a^2 for a in a_limb
enf b = b^2 for b in b_limb

Copy link
Contributor

@tohrnii tohrnii Mar 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bobbinth We don't. I forgot to open an issue for it. We were supposed to add support for this after list comprehension. Do you have a name suggestion for statements like these (maybe something along the lines of list comprehension)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be "ConstraintComprehension" - but looks rather long.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't think of any better name than ConstraintComprehension either so went with it in the issue description (#187) tentatively.

@Fumuran Fumuran requested review from bobbinth and Al-Kindi-0 March 2, 2023 10:44
Copy link
Contributor

@grjte grjte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Overcastan this looks good! I left one minor nit inline.

Also, I think we should keep the old bitwise constraints file for testing purposes. Let's start collecting the vm constraints somewhere new.

Can you please make new top-level directories constraints/miden-vm and put this new bitwise.air file in there? We can collect all Miden VM constraints there for now. That way we can define the VM constraints using all of the syntax that will be implemented in v0.3 instead of being restricted to only what's implemented right now.

# the aggregation of the decomposed bit columns `a_limb`.
let a_aggr = sum([2^i * a for (i, a) in (0..4, a_limb)])
enf k0 * (a - a_aggr) = 0
# Enforce that the value in the first row of column `b` of the current 8-row cycle should be
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: let's put a blank line above this comment so it's easier to read

@Fumuran Fumuran force-pushed the andrew-bitwise-constraints branch 2 times, most recently from 636fdc9 to 4aa1e2b Compare March 6, 2023 18:11
# indices.
let a_and_b = sum([2^i * a * b for (i, a, b) in (0..4, a_limb, b_limb)])
let a_xor_b = sum([2^i * (a + b - 2 * a * b) for (i, a, b) in (0..4, a_limb, b_limb)])
enf (1 - s) * (z - (zp * 16 + a_and_b)) + s * (z - (zp * 16 + a_xor_b)) = 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on @bobbinth's suggestion in another PR, we could rewrite this as:

match enf:
    z = zp * 16 + a_and_b when !s
    z = zp * 16 + a_xor_b when s

Does this seem correct or did I make some mistake here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks correct to me too. I'll update it right away, thank you!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing I'd maybe change here is the order to be more like:

match enf:
    z = zp * 16 + a_xor_b when s
    z = zp * 16 + a_and_b when !s

@Fumuran Fumuran force-pushed the andrew-bitwise-constraints branch 2 times, most recently from c860d93 to cf22093 Compare March 7, 2023 15:17
@Fumuran Fumuran requested review from grjte and tohrnii March 7, 2023 15:17
Copy link
Contributor

@tohrnii tohrnii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @Overcastan, looks good to me. I left a few minor nits.

# Returns value aggregated from limbs
fn aggregate(limb: vector[4]) -> scalar:
let result = sum([2^i * a for (i, a) in (0..4, limb)])
result
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Return statements would require return keyword. And maybe we can remove the result variable as well.

Comment on lines 35 to 36
enf check_binary([a_limb[i]]) for i in 0..4
enf check_binary([b_limb[i]]) for i in 0..4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Maybe we can rewrite this as:

enf check_binary([a]) for a in a_limb
enf check_binary([b]) for b in b_limb

Comment on lines 40 to 41
let a_aggr = aggregate([a_limb])
enf a = a_aggr when k0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Maybe we can remove a_aggr variable. aggregate([a_limb]) looks concise enough to me. Not a strong opinion though.

Comment on lines 45 to 46
let b_aggr = aggregate([b_limb])
enf b = b_aggr when k0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Maybe we can remove b_aggr variable. aggregate([b_limb]) looks concise enough to me. Not a strong opinion though.

@Fumuran Fumuran force-pushed the andrew-bitwise-constraints branch from cf22093 to be073ac Compare March 7, 2023 19:57
@Fumuran Fumuran requested a review from tohrnii March 7, 2023 21:22
Copy link
Contributor

@grjte grjte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Overcastan! I left one very minor nit inline, but I think things look good now with the use of evaluators and selectors.

One comment - we will probably want to rework this a bit according to the comments in #122 for supporting modularity, but I think we should do that separately in a future PR.

Comment on lines +54 to +56
# Enforce that for each row except the last, the aggregated output value must equal the
# previous aggregated output value in the next row.
enf z = zp' when k1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very minor nit: since constraints on z are written below and this constraint is really about the zp column, I would reverse this to enf zp' = z when k1

Copy link
Contributor

@tohrnii tohrnii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @Overcastan, looks good to me. I left a couple of minor nits.


# Enforce that the value in the first row of column `a` of the current 8-row cycle should be
# the aggregation of the decomposed bit columns `a_limb`.
enf a = aggregate([a_limb]) when k0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nit: I think it should be aggregate(a_limb) for functions. Sorry, I missed this last time.


# Enforce that the value in the first row of column `b` of the current 8-row cycle should be
# the aggregation of the decomposed bit columns `b_limb`.
enf b = aggregate([b_limb]) when k0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nit: I think it should be aggregate(b_limb) for functions. Sorry, I missed this last time.

@grjte grjte merged commit 3b033c4 into next Mar 9, 2023
@grjte grjte deleted the andrew-bitwise-constraints branch March 9, 2023 09:01
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

Successfully merging this pull request may close these issues.

update MidenVM constraints for the bitwise chiplet (excludes multiset check constraints)
5 participants