Skip to content

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 type: cleanup type: 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

type: cleanup type: consolidate strengthen an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants