-
Notifications
You must be signed in to change notification settings - Fork 59
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
Hybrid method for random access/conditional selection from 2^k values #119
base: master
Are you sure you want to change the base?
Conversation
at the cost of introducing two extra methods on the implementers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leaving an initial review for the time being, but if the selection methods are not generic, maybe we can add the methods to FpVar
as inherent methods for the time being?
src/fields/fp/mod.rs
Outdated
fn allocate_to_lc( | ||
var: Variable, | ||
val: &Self, | ||
cs: &ConstraintSystemRef<F>, | ||
) -> Result<Self, SynthesisError> { | ||
let v = val.value()?; | ||
Ok(AllocatedFp::new(Some(v), var, cs.clone()).into()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm not sure what this method is doing; is it just a clone of the variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a chance I'm doing something wrong here. The way I understand it is that I have an LC that corresponds to some Variable
in the constraint system, and I need to fix it to some value. But I need to retrieve something of type Self
(CondSelectGadget
) that is equal to that LC, since the repeated_selection
sub-routine (which used to be the main routine before this PR) operates on CondSelectGadget
elements.
Perhaps the method name isn't great... what I thought of doing was, as mentioned in the PR description, adding a trait bound for AllocVar
so that I could call AllocVar>::new_variable
, but I'm not really sure of how to do this.
src/fields/fp/mod.rs
Outdated
fn add_lc( | ||
val: &Self, | ||
lc: &LinearCombination<F>, | ||
) -> Result<LinearCombination<F>, SynthesisError> { | ||
let root: LinearCombination<F> = LinearCombination::zero(); | ||
let v = val.value()?; | ||
Ok(root + (v, lc)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why doesn't it suffice to just use addition on FpVar
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or, sum
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I simplified this slightly.
src/fields/fp/mod.rs
Outdated
lc: LinearCombination<F>, | ||
) -> Result<LinearCombination<F>, SynthesisError> { | ||
let v = val.value()?; | ||
Ok(lc * v) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what this method will look like for ECVar points.
The selection methods should be generic. I don't know yet how the implementation would look for EC points, where I can't just get a single underlying value |
call it `hybrid_selection`
fmt update docs
@Pratyush I couldn't find an intuitive interface for abstracting away the repeated part of the logic, so in the end I've followed your suggestion and have default implementation use the repeated-selection method. My best attempt is here, but then even I quickly get lost in what the method is supposed to be doing 🤣 I've implemented the improved version for:
I've benched some of the new ones too:
after:
|
@mmagician Do you have recommendations on how I should go about reviewing the PR? |
@Pratyush I can share my circom reference impl with you and maybe we can go through this PR during the maintainers' call next week? |
Sure! |
Description
Create
O(sqrt(n))
instead ofO(n)
constraints as per https://github.com/mir-protocol/r1cs-workshop/blob/master/workshop.pdf.By trying to make this as generic as possible, I had to add two extra methods on the trait, which for now are only implemented for
FpVar
for testing.@arkworks-rs/maintainers do you see a way to NOT introduce these extra methods? I tried adding a trait bound for
AllocVar
toCondSelectGadget
, but going down that rabbit hole hasn't worked so far.Benches:
for
k=7
,2^7=128
values2^(k-1) - 1
constraints2^m + 2^l - l - 2
constraintsm = k/2 = 3
;l = 4
;2^3 + 2^4 - 4 - 2 = 18
as expected.
closes: #118
Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.
Pending
section inCHANGELOG.md
Files changed
in the Github PR explorer