Skip to content

Commit

Permalink
add finished groth16 + plonk files, mv test, begin writing new Functi…
Browse files Browse the repository at this point in the history
…onVerifier autogen code
  • Loading branch information
mattstam committed Feb 9, 2024
1 parent 8df0aca commit ec30e7c
Show file tree
Hide file tree
Showing 13 changed files with 241 additions and 643 deletions.
51 changes: 28 additions & 23 deletions circomx/src/circuit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,7 @@ export abstract class Circuit {
solidityVerifier = solidityVerifier.replaceAll("calldataload", "mload");
solidityVerifier = solidityVerifier.replaceAll("calldata", "memory");
solidityVerifier = solidityVerifier.replaceAll(
"_pB, _pC",
// for some reason, uint256[2][2] memory _pB has two words (two lengths?) prepended to it
// and calldata doesn't have it
"add(_pB, 64), _pC"
);
solidityVerifier = solidityVerifier.replaceAll(
"pragma solidity >=0.7.0 <0.9.0;",
"pragma solidity ^0.8.0;",
"pragma solidity ^0.8.16;"
);
solidityVerifier += `
Expand All @@ -99,27 +93,38 @@ interface IFunctionVerifier {
contract FunctionVerifier is IFunctionVerifier, Groth16Verifier {
function verify(bytes32 _inputHash, bytes32 _outputHash, bytes memory _proof) external view returns (bool) {
(uint256[2] memory a, uint256[2][2] memory b, uint256[2] memory c) =
abi.decode(_proof, (uint256[2], uint256[2][2], uint256[2]));
(uint256[8] memory proof) = abi.decode(_proof, (uint256[8]));
uint256[2] memory input = [uint256(_outputHash), uint256(_inputHash)];
input[0] = input[0] & ((1 << 253) - 1);
input[1] = input[1] & ((1 << 253) - 1);
uint256[4] memory input;
input[0] = uint256(CIRCUIT_DIGEST);
input[1] = uint256(_inputHash) & ((1 << 253) - 1);
input[2] = uint256(_outputHash) & ((1 << 253) - 1);
return verifyProof(a, b, c, input);
this.verifyProof(proof, input);
return true;
}
function verificationKeyHash() external pure returns (bytes32) {
bytes memory left;
bytes memory right;
{
left = abi.encode(alphax, alphay, betax1, betax2, betay1, betay2);
}
{
right = abi.encode(gammax1, gammax2, gammay1, gammay2, deltax1, deltax2, deltay1, deltay2);
}
return keccak256(abi.encode(left, right));
}
bytes memory left;
bytes memory right;
{
left = abi.encode(ALPHA_X, ALPHA_Y, BETA_NEG_X_1, BETA_NEG_X_0, BETA_NEG_Y_1, BETA_NEG_Y_0);
}
{
right = abi.encode(
GAMMA_NEG_X_1,
GAMMA_NEG_X_0,
GAMMA_NEG_Y_1,
GAMMA_NEG_Y_0,
DELTA_NEG_X_1,
DELTA_NEG_X_0,
DELTA_NEG_Y_1,
DELTA_NEG_Y_0
);
}
return keccak256(abi.encode(left, right));
}
}
`;
fs.writeFileSync("build/FunctionVerifier.sol", solidityVerifier);
Expand Down
3 changes: 2 additions & 1 deletion plonky2x/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
verifier-build/
core/wrapped/
*tar.gz
verifier-build-groth16/
verifier-build-groth16/
proof_with_witness.json
15 changes: 9 additions & 6 deletions plonky2x/core/src/backend/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,7 @@ impl<C: Circuit> Plonky2xFunction for C {
let wrapper_verifier_contract = fs::read_to_string(wrapper_verifier_path)
.expect("Failed to read wrapper_verifier_path");
let generated_contract = wrapper_verifier_contract
.replace("pragma solidity ^0.8.19;", "pragma solidity ^0.8.16;")
.replace("function Verify", "function verifyProof");
.replace("pragma solidity ^0.8.0;", "pragma solidity ^0.8.16;");

let verifier_contract = "
Expand All @@ -300,17 +299,21 @@ interface IFunctionVerifier {
function verificationKeyHash() external pure returns (bytes32);
}
contract FunctionVerifier is IFunctionVerifier, PlonkVerifier {
contract FunctionVerifier is IFunctionVerifier, Verifier {
bytes32 public constant CIRCUIT_DIGEST = {CIRCUIT_DIGEST};
function verify(bytes32 _inputHash, bytes32 _outputHash, bytes memory _proof) external view returns (bool) {
uint256[] memory input = new uint256[](3);
(uint256[8] memory proof) = abi.decode(_proof, (uint256[8]));
uint256[4] memory input;
input[0] = uint256(CIRCUIT_DIGEST);
input[1] = uint256(_inputHash) & ((1 << 253) - 1);
input[2] = uint256(_outputHash) & ((1 << 253) - 1);
input[2] = uint256(_outputHash) & ((1 << 253) - 1);
this.verifyProof(proof, input);
return this.verifyProof(_proof, input);
return true;
}
function verificationKeyHash() external pure returns (bytes32) {
Expand Down
28 changes: 12 additions & 16 deletions plonky2x/verifier/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"flag"
"os"

"github.com/consensys/gnark/logger"
"github.com/succinctlabs/succinctx/plonky2x/verifier/system"
"go.uber.org/zap"
)

func main() {
Expand All @@ -21,61 +21,57 @@ func main() {
systemFlag := flag.String("system", "groth16", "proving system to use (groth16, plonk)")
flag.Parse()

logger, err := zap.NewDevelopment()
if err != nil {
panic(err)
}
defer logger.Sync()

logger := logger.Logger()
if *circuitPath == "" {
logger.Info("no circuitPath flag found, so user must input circuitPath via stdin")
logger.Info().Msg("no circuitPath flag found, so user must input circuitPath via stdin")
os.Exit(1)
}

if *dataPath == "" {
logger.Error("please specify a path to data dir (where the compiled gnark circuit data will be)")
logger.Error().Msg("please specify a path to data dir (where the compiled gnark circuit data will be)")
os.Exit(1)
}
logger.Info("Circuit path: " + *circuitPath)
logger.Info("Data path: " + *dataPath)
logger.Info().Msg("Circuit path: " + *circuitPath)
logger.Info().Msg("Data path: " + *dataPath)

var s system.ProvingSystem
if *systemFlag == "groth16" {
s = system.NewGroth16System(logger, "./data/dummy", *dataPath)
} else if *systemFlag == "plonk" {
s = system.NewPlonkSystem(logger, "./data/dummy", *dataPath)
} else {
logger.Error("invalid proving system")
logger.Error().Msg("invalid proving system")
os.Exit(1)
}

if *compileFlag {
err := s.Compile()
if err != nil {
logger.Error("failed to compile verifier circuit:" + err.Error())
logger.Error().Msg("failed to compile verifier circuit:" + err.Error())
os.Exit(1)
}
}

if *proofFlag {
err := s.Prove()
if err != nil {
logger.Error("failed to create proof:" + err.Error())
logger.Error().Msg("failed to create proof:" + err.Error())
os.Exit(1)
}
}

if *verifyFlag {
err := s.Verify()
if err != nil {
logger.Error("failed to verify proof:" + err.Error())
logger.Error().Msg("failed to verify proof:" + err.Error())
os.Exit(1)
}
}

if *exportFlag {
err := s.Export()
if err != nil {
logger.Error("failed to export verifier circuit:" + err.Error())
logger.Error().Msg("failed to export verifier circuit:" + err.Error())
os.Exit(1)
}
}
Expand Down
17 changes: 0 additions & 17 deletions plonky2x/verifier/groth16_proof_data.json

This file was deleted.

8 changes: 0 additions & 8 deletions plonky2x/verifier/plonk_proof_data_range_check.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package system

import (
"testing"
Expand Down
Loading

0 comments on commit ec30e7c

Please sign in to comment.