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

feat: Allow array of fields on public output #131

Merged
merged 6 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions examples/fixture/asm/kimchi/public_output_array.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@ noname.0.7.0

DoubleGeneric<1>
DoubleGeneric<1>
DoubleGeneric<1>
DoubleGeneric<1,1,-1>
DoubleGeneric<1,0,0,0,-2>
DoubleGeneric<1,0,-1,0,6>
DoubleGeneric<0,0,-1,1>
DoubleGeneric<1,-1>
DoubleGeneric<1,-1>
(0,0) -> (7,0)
(1,0) -> (8,0)
(2,0) -> (3,1) -> (6,1)
(3,2) -> (4,0) -> (5,0) -> (6,0)
(5,2) -> (7,1)
(6,2) -> (8,1)
6 changes: 6 additions & 0 deletions examples/fixture/asm/r1cs/public_output_array.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@ noname.0.7.0

2 == (v_3 + v_4) * (1)
v_5 == (v_3 + v_4) * (v_3)
v_3 + v_4 + 6 == (v_1) * (1)
v_5 == (v_2) * (1)
Copy link
Contributor

Choose a reason for hiding this comment

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

^ this last constraint seems really useless, we should optimize that, I'll create an issue :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you mean the second constraint should be instead
v_2 = (v_3 + v_4) * (v_3)?

v_1 and v_2 are the outputs and they have to be constrained

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah! v_2 == (v_3 + v_4) * v_3 would have been better. But I don't think we should look to create such optimization, because an optimization pass that would do inlining would easily inline the last constraint

6 changes: 6 additions & 0 deletions examples/public_output_array.no
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main(pub public_input: Field, private_input: Field) -> [Field; 2] {
let xx = private_input + public_input;
assert_eq(xx, 2);
let yy = xx + 6;
return [yy, xx * public_input];
}
13 changes: 8 additions & 5 deletions src/circuit_writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,15 @@ impl<B: Backend> CircuitWriter<B> {

// create public output
if let Some(typ) = &function.sig.return_type {
if typ.kind != TyKind::Field {
unimplemented!();
match typ.kind {
TyKind::Field => {
circuit_writer.add_public_outputs(1, typ.span);
}
TyKind::Array(_, len) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is correct as this won't work if you have [T; len] for T a different type than Field.

I think a quick fix here could be to match for TyKind::Array(TyKind::Field, len) or to actually recurse and figure out the real size (akin to the size_of function), but this would be out of the scope of this issue and perhaps a tad more complicated :o

circuit_writer.add_public_outputs(len as usize, typ.span);
}
_ => unimplemented!(),
}

// create it
circuit_writer.add_public_outputs(1, typ.span);
}

// public inputs should be handled first
Expand Down
18 changes: 18 additions & 0 deletions src/tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,21 @@ fn test_literals(#[case] backend: BackendKind) -> miette::Result<()> {

Ok(())
}

#[rstest]
#[case::kimchi_vesta(BackendKind::KimchiVesta(KimchiVesta::new(false)))]
#[case::r1cs(BackendKind::R1csBls12_381(R1CS::new()))]
fn test_public_output_array(#[case] backend: BackendKind) -> miette::Result<()> {
let public_inputs = r#"{"public_input": "1"}"#;
let private_inputs = r#"{"private_input": "1"}"#;

test_file(
"public_output_array",
public_inputs,
private_inputs,
vec!["8", "2"],
backend,
)?;

Ok(())
}
21 changes: 14 additions & 7 deletions src/type_checker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,21 @@ impl<B: Backend> TypeChecker<B> {
// the output value returned by the main function is also a main_args with a special name (public_output)
if let Some(typ) = &function.sig.return_type {
if is_main {
if !matches!(typ.kind, TyKind::Field) {
unimplemented!();
match typ.kind {
TyKind::Field => {
typed_fn_env.store_type(
"public_output".to_string(),
TypeInfo::new_mut(typ.kind.clone(), typ.span),
)?;
}
TyKind::Array(_, _) => {
typed_fn_env.store_type(
"public_output".to_string(),
TypeInfo::new_mut(typ.kind.clone(), typ.span),
)?;
}
_ => unimplemented!(),
}

typed_fn_env.store_type(
"public_output".to_string(),
TypeInfo::new_mut(typ.kind.clone(), typ.span),
)?;
}
}

Expand Down