Skip to content

perf: normalize the random linear combination in logderivarg#1333

Merged
ivokub merged 3 commits intoConsensys:masterfrom
reilabs:logderiv-opt
Dec 2, 2024
Merged

perf: normalize the random linear combination in logderivarg#1333
ivokub merged 3 commits intoConsensys:masterfrom
reilabs:logderiv-opt

Conversation

@kustosz
Copy link
Copy Markdown
Contributor

@kustosz kustosz commented Nov 28, 2024

Description

Sets the first coefficient for random linear combinations in the logderiv argument to 1. This should not affect soundness  (the attacker still cannot abuse any relationship between the coefficients), but it shaves off a constraint from table lookups – in the most basic case we go from 3 constraints down to 2.

Idea by @ivokub, soundness arguments may need to be refined.

Type of change

  • Perf improvement

How has this been tested?

  • run go test ./...

How has this been benchmarked?

Run this before and after:

type TestLookup struct {
	In frontend.Variable
}

func (c *TestLookup) Define(api frontend.API) error {
	table := logderivlookup.New(api)
	for i := range 256 {
		table.Insert(bits.RotateLeft8(uint8(i), 3))
	}
	c0 := c.In
	for range 256 {
		c0 = table.Lookup(c0)[0]
	}
	api.AssertIsEqual(c0, c.In)
	return nil
}

func main() {
	ccs, _ := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &TestLookup{})
	fmt.Printf("constraints: %d\n", ccs.GetNbConstraints())
}

Output goes from 1687 to 1100

Checklist:

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • I did not modify files generated from templates
  • [?] golangci-lint does not output errors locally - it output a bunch of errors, but none near my changes
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@ivokub ivokub self-requested a review December 2, 2024 12:56
@ivokub ivokub added the type: consolidate strengthen an existing feature label Dec 2, 2024
@ivokub ivokub added this to the v0.11.N milestone Dec 2, 2024
Copy link
Copy Markdown
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.

LGTM! Thanks for the contribution!

Seems sound and @ThomasPiellard has also confirmed.

@ivokub ivokub merged commit 92b9823 into Consensys:master Dec 2, 2024
@kustosz kustosz deleted the logderiv-opt branch December 2, 2024 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: consolidate strengthen an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants