forked from Consensys/gnark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/ci: make CI run solidity verification with integartion test. Add…
…ed test.WithSolidity option. (Consensys#732) * feat: plonk + sol rebased on latest develop branch * feat: kept only verifier template * refactor: plonk solidity generate match expected gnark interface * tmp: change Library to Contract and add MarshalSolidity on proof * feat: plonk sodliity change visibility of some funcs * feat: add SPDIX apache 2 identifier * fix: move new solidity code into tmpl generation * fix: use big int string in solidity generation for plonk to avoid negative numbers * feat: add WithSolidity option in assert helper * ci: attempt to make gnark-solidity-checker run on CI * fix: address PR comments --------- Co-authored-by: Thomas Piellard <[email protected]>
- Loading branch information
1 parent
a75adf0
commit e1332fa
Showing
10 changed files
with
181 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,10 +70,16 @@ jobs: | |
restore-keys: | | ||
${{ runner.os }}-go- | ||
- name: install deps | ||
run: go install golang.org/x/tools/cmd/goimports@latest && go install github.com/klauspost/asmfmt/cmd/asmfmt@latest | ||
run: | | ||
go install golang.org/x/tools/cmd/goimports@latest && go install github.com/klauspost/asmfmt/cmd/asmfmt@latest | ||
go install github.com/ethereum/go-ethereum/cmd/[email protected] | ||
go install github.com/consensys/gnark-solidity-checker@latest | ||
sudo add-apt-repository ppa:ethereum/ethereum | ||
sudo apt-get update | ||
sudo apt-get install solc | ||
- name: Test | ||
run: | | ||
go test -v -short -timeout=30m ./... | ||
go test -v -short -tags=solccheck -timeout=30m ./... | ||
- name: Test race | ||
run: | | ||
go test -v -short -race -timeout=30m | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package test | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strconv" | ||
|
||
"github.com/consensys/gnark/backend" | ||
groth16_bn254 "github.com/consensys/gnark/backend/groth16/bn254" | ||
plonk_bn254 "github.com/consensys/gnark/backend/plonk/bn254" | ||
"github.com/consensys/gnark/backend/witness" | ||
) | ||
|
||
// solidityVerification checks that the exported solidity contract can verify the proof | ||
// and that the proof is valid. | ||
// It uses gnark-solidity-checker see test.WithSolidity option. | ||
func (assert *Assert) solidityVerification(b backend.ID, vk interface { | ||
NbPublicWitness() int | ||
ExportSolidity(io.Writer) error | ||
}, | ||
proof any, | ||
validPublicWitness witness.Witness) { | ||
if !solcCheck { | ||
return // nothing to check, will make solc fail. | ||
} | ||
assert.t.Helper() | ||
|
||
// make temp dir | ||
tmpDir, err := os.MkdirTemp("", "gnark-solidity-check*") | ||
assert.NoError(err) | ||
defer os.RemoveAll(tmpDir) | ||
|
||
// export solidity contract | ||
fSolidity, err := os.Create(filepath.Join(tmpDir, "gnark_verifier.sol")) | ||
assert.NoError(err) | ||
|
||
err = vk.ExportSolidity(fSolidity) | ||
assert.NoError(err) | ||
|
||
err = fSolidity.Close() | ||
assert.NoError(err) | ||
|
||
// generate assets | ||
// gnark-solidity-checker generate --dir tmpdir --solidity contract_g16.sol | ||
cmd := exec.Command("gnark-solidity-checker", "generate", "--dir", tmpDir, "--solidity", "gnark_verifier.sol") | ||
assert.t.Log("running ", cmd.String()) | ||
out, err := cmd.CombinedOutput() | ||
assert.NoError(err, string(out)) | ||
|
||
// proof to hex | ||
var proofStr string | ||
var optBackend string | ||
|
||
if b == backend.GROTH16 { | ||
optBackend = "--groth16" | ||
var buf bytes.Buffer | ||
_proof := proof.(*groth16_bn254.Proof) | ||
_, err = _proof.WriteRawTo(&buf) | ||
assert.NoError(err) | ||
proofStr = hex.EncodeToString(buf.Bytes()) | ||
} else if b == backend.PLONK { | ||
optBackend = "--plonk" | ||
_proof := proof.(*plonk_bn254.Proof) | ||
// TODO @gbotrel make a single Marshal function for PlonK proof. | ||
proofStr = hex.EncodeToString(_proof.MarshalSolidity()) | ||
} else { | ||
panic("not implemented") | ||
} | ||
|
||
// public witness to hex | ||
bPublicWitness, err := validPublicWitness.MarshalBinary() | ||
assert.NoError(err) | ||
// that's quite dirty... | ||
// first 4 bytes -> nbPublic | ||
// next 4 bytes -> nbSecret | ||
// next 4 bytes -> nb elements in the vector (== nbPublic + nbSecret) | ||
bPublicWitness = bPublicWitness[12:] | ||
publicWitnessStr := hex.EncodeToString(bPublicWitness) | ||
|
||
// verify proof | ||
// gnark-solidity-checker verify --dir tmdir --groth16 --nb-public-inputs 1 --proof 1234 --public-inputs dead | ||
cmd = exec.Command("gnark-solidity-checker", "verify", | ||
"--dir", tmpDir, | ||
optBackend, | ||
"--nb-public-inputs", strconv.Itoa(vk.NbPublicWitness()), | ||
"--proof", proofStr, | ||
"--public-inputs", publicWitnessStr) | ||
assert.t.Log("running ", cmd.String()) | ||
out, err = cmd.CombinedOutput() | ||
assert.NoError(err, string(out)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
//go:build solccheck | ||
|
||
package test | ||
|
||
const solcCheck = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
//go:build !solccheck | ||
|
||
package test | ||
|
||
const solcCheck = false |