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: add cs.GetConstraint with examples, and pretty printer helpers #452

Merged
merged 14 commits into from
Feb 1, 2023

Conversation

gbotrel
Copy link
Collaborator

@gbotrel gbotrel commented Jan 30, 2023

Closes #445 . May also be helpful for #397 , and https://github.com/lambdaclass/noir_backend_using_gnark . Should be enough for play.gnark.io update.

func ExampleR1CS_GetConstraints() {
	// build a constraint system; this is (usually) done by the frontend package
	// for this Example we want to manipulate the constraints and output a string representation
	// and build the linear expressions "manually".
	r1cs := cs.NewR1CS(0)

	ONE := r1cs.AddPublicVariable("1") // the "ONE" wire
	Y := r1cs.AddPublicVariable("Y")
	X := r1cs.AddSecretVariable("X")

	v0 := r1cs.AddInternalVariable() // X²
	v1 := r1cs.AddInternalVariable() // X³

	// coefficients
	cOne := r1cs.FromInterface(1)
	cFive := r1cs.FromInterface(5)

	// X² == X * X
	r1cs.AddConstraint(constraint.R1C{
		L: constraint.LinearExpression{r1cs.MakeTerm(&cOne, X)},
		R: constraint.LinearExpression{r1cs.MakeTerm(&cOne, X)},
		O: constraint.LinearExpression{r1cs.MakeTerm(&cOne, v0)},
	})

	// X³ == X² * X
	r1cs.AddConstraint(constraint.R1C{
		L: constraint.LinearExpression{r1cs.MakeTerm(&cOne, v0)},
		R: constraint.LinearExpression{r1cs.MakeTerm(&cOne, X)},
		O: constraint.LinearExpression{r1cs.MakeTerm(&cOne, v1)},
	})

	// Y == X³ + X + 5
	r1cs.AddConstraint(constraint.R1C{
		R: constraint.LinearExpression{r1cs.MakeTerm(&cOne, ONE)},
		L: constraint.LinearExpression{r1cs.MakeTerm(&cOne, Y)},
		O: constraint.LinearExpression{
			r1cs.MakeTerm(&cFive, ONE),
			r1cs.MakeTerm(&cOne, X),
			r1cs.MakeTerm(&cOne, v1),
		},
	})

	// get the constraints
	constraints, r := r1cs.GetConstraints()

	for _, r1c := range constraints {
		fmt.Println(r1c.String(r))
		// for more granularity use constraint.NewStringBuilder(r) that embeds a string.Builder
		// and has WriteLinearExpression and WriteTerm methods.
	}

	// Output:
	// X ⋅ X == v0
	// v0 ⋅ X == v1
	// Y ⋅ 1 == 5 + X + v1
}

@gbotrel gbotrel added cleanup consolidate strengthen an existing feature labels Jan 30, 2023
@gbotrel gbotrel added this to the v0.8.0 milestone Jan 30, 2023
@gbotrel gbotrel requested a review from ivokub January 30, 2023 20:30
@gbotrel
Copy link
Collaborator Author

gbotrel commented Jan 30, 2023

(to merge after #450 )

Base automatically changed from refactor/witness to develop February 1, 2023 03:34
Copy link
Collaborator

@ivokub ivokub left a comment

Choose a reason for hiding this comment

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

I like the new API for pretty printing. Very compact and imo a lot easier to use.

@gbotrel gbotrel merged commit f08ef4d into develop Feb 1, 2023
@gbotrel gbotrel deleted the feat/getconstraints branch February 1, 2023 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleanup consolidate strengthen an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants