-
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
Support hint function #220
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
af422ea
add IR translator
katat 7636d1a
add Value::HintIR for hint calculations
katat 34af420
enable parsing hint function body
katat f004a33
add cargo deps
katat d64281d
circ require nightly rust toolchain
katat a38bba6
add hint test
katat e30e563
fmt
katat 34162a7
support complex inputs and outputs
katat a11e5c2
Merge remote-tracking branch 'origin/main' into feat/ir2
katat 5d05284
update asm
katat 427475b
map backend fields to circ fields
katat 5b91bd1
better error for calling builtins in hint function
katat 190df51
refine error
katat ed9dd4d
more arithmetic operations
katat 99498fa
support boolean operations
katat 3dc4c71
update tests
katat 2cbc07e
update test
katat 881ff1d
only allow calling hint functions in a hint function
katat 255b1e3
format
katat f2c0e3a
comment
katat 3ab7525
Merge remote-tracking branch 'origin/main' into feat/ir2
katat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
@ noname.0.7.0 | ||
@ public inputs: 2 | ||
|
||
DoubleGeneric<1> | ||
DoubleGeneric<1> | ||
DoubleGeneric<1,0,0,0,-2> | ||
DoubleGeneric<1,0,0,0,-2> | ||
DoubleGeneric<2,0,-1> | ||
DoubleGeneric<1,-1> | ||
DoubleGeneric<1,-1> | ||
DoubleGeneric<1,-1> | ||
DoubleGeneric<1,0,0,0,-16> | ||
DoubleGeneric<1,-1> | ||
DoubleGeneric<1,0,0,0,-3> | ||
DoubleGeneric<1,0,-1,0,1> | ||
DoubleGeneric<1,0,0,0,-1> | ||
DoubleGeneric<1,0,0,0,-1> | ||
DoubleGeneric<1,1> | ||
DoubleGeneric<1,0,-1,0,1> | ||
DoubleGeneric<1,0,0,0,-1> | ||
DoubleGeneric<1,0,0,0,-1> | ||
DoubleGeneric<1,-1> | ||
(0,0) -> (18,0) | ||
(1,0) -> (2,0) -> (9,1) | ||
(4,0) -> (6,1) | ||
(4,2) -> (5,1) | ||
(5,0) -> (7,1) -> (18,1) | ||
(9,0) -> (11,0) | ||
(14,1) -> (15,0) | ||
(15,2) -> (16,0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@ noname.0.7.0 | ||
@ public inputs: 2 | ||
|
||
2 == (v_2) * (1) | ||
2 == (v_3) * (1) | ||
2 * v_5 == (v_4) * (1) | ||
v_5 == (v_6) * (1) | ||
v_4 == (v_7) * (1) | ||
16 == (v_8) * (1) | ||
v_2 == (v_9) * (1) | ||
3 == (v_10) * (1) | ||
1 == (v_11) * (1) | ||
1 == (v_12) * (1) | ||
1 == (-1 * v_13 + 1) * (1) | ||
1 == (v_14) * (1) | ||
v_4 == (v_1) * (1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
struct Thing { | ||
xx: Field, | ||
yy: Field, | ||
} | ||
|
||
hint fn mul(lhs: Field, rhs: Field) -> Field { | ||
return lhs * rhs; | ||
} | ||
|
||
hint fn add_mul_2(lhs: Field, rhs: Field) -> Field { | ||
let sum = lhs + rhs; | ||
return unsafe mul(sum, 2); | ||
} | ||
|
||
hint fn div(lhs: Field, rhs: Field) -> Field { | ||
return lhs / rhs; | ||
} | ||
|
||
hint fn ite(lhs: Field, rhs: Field) -> Field { | ||
return if lhs != rhs { lhs } else { rhs }; | ||
} | ||
|
||
hint fn exp(const EXP: Field, val: Field) -> Field { | ||
let mut res = val; | ||
|
||
for num in 1..EXP { | ||
res = res * val; | ||
} | ||
|
||
return res; | ||
} | ||
|
||
hint fn sub(lhs: Field, rhs: Field) -> Field { | ||
return lhs - rhs; | ||
} | ||
|
||
hint fn boolean_ops(lhs: Field, rhs: Field) -> [Bool; 3] { | ||
let aa = lhs == rhs; | ||
|
||
let bb = aa && false; | ||
let cc = bb || true; | ||
|
||
return [aa, bb, cc]; | ||
} | ||
|
||
hint fn multiple_inputs_outputs(aa: [Field; 2]) -> Thing { | ||
return Thing { | ||
xx: aa[0], | ||
yy: aa[1], | ||
}; | ||
} | ||
|
||
fn main(pub public_input: Field, private_input: Field) -> Field { | ||
// have to assert these inputs, otherwise it throws vars not in circuit error | ||
assert_eq(public_input, 2); | ||
assert_eq(private_input, 2); | ||
|
||
let xx = unsafe add_mul_2(public_input, private_input); | ||
let yy = unsafe mul(public_input, private_input); | ||
assert_eq(xx, yy * 2); | ||
|
||
let zz = unsafe div(xx, public_input); | ||
assert_eq(zz, yy); | ||
|
||
let ww = unsafe ite(xx, yy); | ||
assert_eq(ww, xx); | ||
|
||
let kk = unsafe exp(4, public_input); | ||
assert_eq(kk, 16); | ||
|
||
let thing = unsafe multiple_inputs_outputs([public_input, 3]); | ||
// have to include all the outputs from hint function, otherwise it throws vars not in circuit error. | ||
// this is because each individual element in the hint output maps to a separate cell var in noname. | ||
assert_eq(thing.xx, public_input); | ||
assert_eq(thing.yy, 3); | ||
|
||
let jj = unsafe sub(thing.xx + 1, public_input); | ||
assert_eq(jj, 1); | ||
|
||
let oo = unsafe boolean_ops(2, 2); | ||
assert(oo[0]); | ||
assert(!oo[1]); | ||
assert(oo[2]); | ||
|
||
return xx; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[toolchain] | ||
channel = "nightly" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This lgtm, but I would suggest adding some unit tests here to check corner cases (0, p, p-1, p+1).