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: groth16 solidity use calldatacopy for commitments #1097

Merged
merged 6 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
61 changes: 49 additions & 12 deletions backend/groth16/bn254/solidity.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,17 +541,36 @@ contract Verifier {
}
{{- end}}
(uint256 Px, uint256 Py) = decompress_g1(compressedCommitmentPok);

uint256[] memory publicAndCommitmentCommitted;
{{- range $i := intRange $numCommitments }}
{{- $pcIndex := index $PublicAndCommitmentCommitted $i }}
{{- if gt (len $pcIndex) 0 }}
publicAndCommitmentCommitted = new uint256[]({{(len $pcIndex)}});
assembly ("memory-safe") {
let publicAndCommitmentCommittedOffset := add(publicAndCommitmentCommitted, 0x20)
{{- $segment_start := index $pcIndex 0 }}
{{- $segment_end := index $pcIndex 0 }}
{{- $l := 0 }}
{{- range $k := intRange (sub (len $pcIndex) 1) }}
{{- $next := index $pcIndex (sum $k 1) }}
{{- if ne $next (sum $segment_end 1) }}
calldatacopy(add(publicAndCommitmentCommittedOffset, {{mul $l 0x20}}), add(input, {{mul 0x20 (sub $segment_start 1)}}), {{mul 0x20 (sum 1 (sub $segment_end $segment_start))}})
{{- $segment_start = $next }}
{{- $l = (sum $k 1) }}
{{- end }}
{{- $segment_end = $next }}
{{- end }}
calldatacopy(add(publicAndCommitmentCommittedOffset, {{mul $l 0x20}}), add(input, {{mul 0x20 (sub $segment_start 1)}}), {{mul 0x20 (sum 1 (sub $segment_end $segment_start))}})
}
{{- end }}

publicCommitments[{{$i}}] = uint256(
sha256(
abi.encodePacked(
commitments[{{mul $i 2}}],
commitments[{{sum (mul $i 2) 1}}]
{{- $pcIndex := index $PublicAndCommitmentCommitted $i }}
{{- range $j := intRange (len $pcIndex) }}
{{- $l := index $pcIndex $j }}
,input[{{sub $l 1}}]
{{- end }}
commitments[{{sum (mul $i 2) 1}}],
publicAndCommitmentCommitted
)
)
) % R;
Expand Down Expand Up @@ -670,17 +689,35 @@ contract Verifier {
{{- else }}
// HashToField
uint256[{{$numCommitments}}] memory publicCommitments;
uint256[] memory publicAndCommitmentCommitted;
{{- range $i := intRange $numCommitments }}
{{- $pcIndex := index $PublicAndCommitmentCommitted $i }}
{{- if gt (len $pcIndex) 0 }}
publicAndCommitmentCommitted = new uint256[]({{(len $pcIndex)}});
assembly ("memory-safe") {
let publicAndCommitmentCommittedOffset := add(publicAndCommitmentCommitted, 0x20)
{{- $segment_start := index $pcIndex 0 }}
{{- $segment_end := index $pcIndex 0 }}
{{- $l := 0 }}
{{- range $k := intRange (sub (len $pcIndex) 1) }}
{{- $next := index $pcIndex (sum $k 1) }}
{{- if ne $next (sum $segment_end 1) }}
calldatacopy(add(publicAndCommitmentCommittedOffset, {{mul $l 0x20}}), add(input, {{mul 0x20 (sub $segment_start 1)}}), {{mul 0x20 (sum 1 (sub $segment_end $segment_start))}})
{{- $segment_start = $next }}
{{- $l = (sum $k 1) }}
{{- end }}
{{- $segment_end = $next }}
{{- end }}
calldatacopy(add(publicAndCommitmentCommittedOffset, {{mul $l 0x20}}), add(input, {{mul 0x20 (sub $segment_start 1)}}), {{mul 0x20 (sum 1 (sub $segment_end $segment_start))}})
}
{{- end }}

publicCommitments[{{$i}}] = uint256(
sha256(
abi.encodePacked(
commitments[{{mul $i 2}}],
commitments[{{sum (mul $i 2) 1}}]
{{- $pcIndex := index $PublicAndCommitmentCommitted $i }}
{{- range $j := intRange (len $pcIndex) }}
{{- $l := index $pcIndex $j }}
,input[{{sub $l 1}}]
{{- end }}
commitments[{{sum (mul $i 2) 1}}],
publicAndCommitmentCommitted
)
)
) % R;
Expand Down
8 changes: 4 additions & 4 deletions std/math/polynomial/polynomial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func testEvalPoly[FR emulated.FieldParams](t *testing.T, p []int64, at int64, ev
Evaluation: emulated.ValueOf[FR](evaluation),
}

assert.CheckCircuit(&evalPolyCircuit[FR]{P: make([]emulated.Element[FR], len(p))}, test.WithValidAssignment(&witness), test.NoSolidityChecks())
assert.CheckCircuit(&evalPolyCircuit[FR]{P: make([]emulated.Element[FR], len(p))}, test.WithValidAssignment(&witness))
}

func TestEvalPoly(t *testing.T) {
Expand Down Expand Up @@ -98,7 +98,7 @@ func testEvalMultiLin[FR emulated.FieldParams](t *testing.T) {
Evaluation: emulated.ValueOf[FR](17),
}

assert.CheckCircuit(&evalMultiLinCircuit[FR]{M: make([]emulated.Element[FR], 4), At: make([]emulated.Element[FR], 2)}, test.WithValidAssignment(&witness), test.NoSolidityChecks())
assert.CheckCircuit(&evalMultiLinCircuit[FR]{M: make([]emulated.Element[FR], 4), At: make([]emulated.Element[FR], 2)}, test.WithValidAssignment(&witness))
}

type evalEqCircuit[FR emulated.FieldParams] struct {
Expand Down Expand Up @@ -144,7 +144,7 @@ func testEvalEq[FR emulated.FieldParams](t *testing.T) {
Eq: emulated.ValueOf[FR](148665),
}

assert.CheckCircuit(&evalEqCircuit[FR]{X: make([]emulated.Element[FR], 4), Y: make([]emulated.Element[FR], 4)}, test.WithValidAssignment(&witness), test.NoSolidityChecks())
assert.CheckCircuit(&evalEqCircuit[FR]{X: make([]emulated.Element[FR], 4), Y: make([]emulated.Element[FR], 4)}, test.WithValidAssignment(&witness))
}

type interpolateLDECircuit[FR emulated.FieldParams] struct {
Expand Down Expand Up @@ -180,7 +180,7 @@ func testInterpolateLDE[FR emulated.FieldParams](t *testing.T, at int64, values
Expected: emulated.ValueOf[FR](expected),
}

assert.CheckCircuit(&interpolateLDECircuit[FR]{Values: make([]emulated.Element[FR], len(values))}, test.WithValidAssignment(assignment), test.NoSolidityChecks())
assert.CheckCircuit(&interpolateLDECircuit[FR]{Values: make([]emulated.Element[FR], len(values))}, test.WithValidAssignment(assignment))
}

func TestInterpolateLDEOnRange(t *testing.T) {
Expand Down
Loading