Skip to content

Commit

Permalink
feat: sxg contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVikasRushi committed Oct 16, 2024
1 parent cfbdc77 commit 9e3c697
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 84 deletions.
41 changes: 23 additions & 18 deletions contracts/src/Fibonacci.sol → contracts/src/Sxg.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,46 @@ pragma solidity ^0.8.20;
import {ISP1Verifier} from "@sp1-contracts/ISP1Verifier.sol";

struct PublicValuesStruct {
uint32 n;
uint32 a;
uint32 b;
uint32 result;
}

/// @title Fibonacci.
/// @title Sxg.
/// @author Succinct Labs
/// @notice This contract implements a simple example of verifying the proof of a computing a
/// fibonacci number.
contract Fibonacci {
contract SXG {
/// @notice The address of the SP1 verifier contract.
/// @dev This can either be a specific SP1Verifier for a specific version, or the
/// SP1VerifierGateway which can be used to verify proofs for any version of SP1.
/// For the list of supported verifiers on each chain, see:
/// https://github.com/succinctlabs/sp1-contracts/tree/main/contracts/deployments
address public verifier;

/// @notice The verification key for the fibonacci program.
bytes32 public fibonacciProgramVKey;
/// @notice The verification key for the sxg program.
bytes32 public sxgProgramVKey;

constructor(address _verifier, bytes32 _fibonacciProgramVKey) {
constructor(address _verifier, bytes32 _sxgProgramVKey) {
verifier = _verifier;
fibonacciProgramVKey = _fibonacciProgramVKey;
sxgProgramVKey = _sxgProgramVKey;
}

/// @notice The entrypoint for verifying the proof of a fibonacci number.
/// @notice The entrypoint for verifying the proof of a sxg number.
/// @param _proofBytes The encoded proof.
/// @param _publicValues The encoded public values.
function verifyFibonacciProof(bytes calldata _publicValues, bytes calldata _proofBytes)
public
view
returns (uint32, uint32, uint32)
{
ISP1Verifier(verifier).verifyProof(fibonacciProgramVKey, _publicValues, _proofBytes);
PublicValuesStruct memory publicValues = abi.decode(_publicValues, (PublicValuesStruct));
return (publicValues.n, publicValues.a, publicValues.b);
function verifySXGProof(
bytes calldata _publicValues,
bytes calldata _proofBytes
) public view returns (uint32) {
ISP1Verifier(verifier).verifyProof(
sxgProgramVKey,
_publicValues,
_proofBytes
);
PublicValuesStruct memory publicValues = abi.decode(
_publicValues,
(PublicValuesStruct)
);

return publicValues.result;
}
}
4 changes: 2 additions & 2 deletions contracts/src/fixtures/groth16-fixture.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"result": 1,
"vkey": "0x00d5975e3e6e42e90de71f036dc4cf7027b79e3d96ee5302a2d9debfb584f36c",
"vkey": "0x00cacd7a6bd9bf05403e6025a3f07687589f397c117db0387631fc2acb105eff",
"publicValues": "0x0000000000000000000000000000000000000000000000000000000000000001",
"proof": "0x6a2906ac0bdd22aa619ff4bfb1ff2edcd29e5c5cc8a7258002a29e7cc33e245aef436a28119de9acb1bb1beb65c769adb55cfd4e6d7d034a9ac6c29251c36536e326b53027ff2a4283ad71c2f299c095dfd4326fce553915357cf285c36f16c55d8281232ca3e0b612f14c58be500a18213d96c31d4e502bffec7e11719f09e0f9c6bac42dd64ce2cf4387e646f1c47c7264d13437cb07bb27f561efefa0adc66d0829bb0d53ff0ef529b170303ecfc7945d68fe9b7a1dca48f8c78f2d79b177f762f11f27cc4c335cf5201c7cff9b5e3f5f5ca8bb683c69f3def89b6e5f82bc7cb2fda7196ff6fe1babcf28d62ef919a5d7d4cc0fb9b583f050c33c213b9306fa1ae5f6"
"proof": "0x6a2906ac21d04f16a0b65a4614269ffaa702b89e5f4af256bf057d7210ea14d2e4eb003718416563bd7c3558910f61a515ec26b3aaba426931da7e0196e1c007827c4bda2947aa3e01c50b0d09205a076be5f96f3093d081e8ddfe0ed20d5bdbeeae3080240a293565baa15ef6787722810f6d70411f920f054e22a3a94a92e5f3cfc90d1bec7415f91ecf1ee6493c6e8d2736059afeda7c4e1b5a6f11c61cd5936db86e0d27d53150eb1a1137044994767c44dc5d61a65c76f95a9fd416e35de1b6982b2a407fc15fa38cf2df4f64fc6f3eb84a7f6e73a6cc01b3916db1734f3e8698b90b62224a46f50fae2d637b0dd7e7d70854bb87e99d6426cc58132c8b98563f42"
}
10 changes: 4 additions & 6 deletions contracts/src/fixtures/plonk-fixture.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{
"a": 6765,
"b": 10946,
"n": 20,
"vkey": "0x00a22fd3af2b4ec77de39ec50023cf6c2b64984d0156a3df1984262984ef71bf",
"publicValues": "0x00000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000001a6d0000000000000000000000000000000000000000000000000000000000002ac2",
"proof": "0x4aca240a10e48d547d454301bd5384e07852d60e6ba0f115f65a60f4443c4cee1348b695086b30d01c43bbd73a4026a1e2085b206e16f2e996ea101e35e116501fbe8c70003cef43831a390fc7d551058c6b186bb29597eb5d4981d72453a1c48c9ef3b90f1571cc98e55ebac36e5fecb14f7840419de70cd9a4c5c9692ab35b7222203c0f146200c674ffde1bb21feded6250d54d1bbbae633d1c4d070ce21384187ee60e87d43ed7a580181615ca48a5dfa0704ed429f690e0cb8f4382a16fc911d24623dc90465ed642716ffcb0cd53ff149273f5c511129f029f4072371572cf3bcc1e53ace42dbd6890319597f31401c96287eac3300c5930cfc150e9d81bf1ee740e5b7e8ed8e8e592d9f779f35e2be0208e209ff9b728ff3825b1a9b7aef826a525f9d755f96ec2e68a88b4da246b425ebd4af4c3041ea12c5e4c44ed9e83a4ad069f674517a08ec7bcda9c7672978a2fd056956c5f48e9b8f348028335aee6db1c105a306b0d03f309f406ceb824c72133f186a615849b950ab9baec61bff812034bb448565d0b789adeaaead1e5c07b9224d081a9c42dbe3bf0d66889dda3d11fc11c3465d952f797cf9a26341edbfb868de65bb51cc3e4445ca084e120c4b406f30b78f720ff785e6592ad218f71b4382cd6cfbb4d653a64efea019e5c705e1e15f9034b85b02b5c65d5c4a79143982b596bb1dc49d2799c581906686042780ca366da2fed051649bfa7fd0a350bd5953db403e2b638d992288cf2df85adba29442fd8f5fe7db31bd22002ba1e9bf59d5fe848dc66242191c070ddc1ba6e9d16affcb406d1b765024293ee018e48a79b81dc26c3219a7c76a91071fc1abb67096561ba7cb68c5c7f0eb82c3aed59c413abb7cad01a2ad2c6a3cbc87700f01716ecb7c7b42f6d5cb2dd20c5ec528236fa0220500e07daf5138a8ede04ee206f02912f9571ef693a699811a9bcbe544a5ab6e97e61dcff677e631f50126868460715fda230220afce3ca030a8b561a0863d2a1e926f8b07c4846e858690f593a1713eb06cf21f1a3962ab2a9086c4d8761dcf0d8c2c7dcbc35ade5b7be2848ff1861e824799ace0e4ea39031ff336e2005373d35a9549e5d9cc4191f337f407f0fc4ce65a7484686747f6de7914eef3bdca97cf4ca0013c3af4097e7d768146a1bfa12db65d818ecae20612f5e14d71f608bd19da830794b7ca09ff5e00b0722"
"result": 1,
"vkey": "0x004cd1fd1ad03c770da6b9d99d8a4741e47d63176acde3a09974c10231a63560",
"publicValues": "0x0000000000000000000000000000000000000000000000000000000000000001",
"proof": "0x4aca240a2ae327f196925bd625217b02714bb66468d3dc3464bd05384713ee2129521f8a20fb5f8269097d0e1472da51c2b4c49a720344d7eb097f7ba8ef4879c516f56a046ea026bdfcf0287e0ac7789068cacfb67748004807da5a20c5d657a2ccad5616b63355ef787f403fc2c68eb3ee0574d87697e9e9f257a151a8cc2b87fd1e442f367aefefc1968cc970ef041553f724a8a015e5e29ce330eec7b135a9b0e8f0014ea5a7aa304051aeaab7218c3d964badbd8fbee1dbfaa92b1bea781c7178c81ae55b85aa0115b7791e2600d1c835f2ba17dd453d33294c9cf42afb38e97a241817c2f5977d18812c99eed05f29f803c74621a4436abad4480cd8bd3b9ffd6c24d076786ff9fb006a284156881dd07cdf89b1c578d2d6fca949ef8f6a86fc0d0112cf757238c4e081d2dacbc131f47fc0f1ea3a0ebb07941f7b524635fb2cb10572b3ae43e927106fdbde40af27321d6aa45fb6a6529eaea59d0fdd154b40552264b133ea1a2a2a46613858f49371840c3fb81fa6c1578096f48c21c90eacaa18097cbfb6273809328a8588bb866f499477db058326797ee414a7e391f6459e22083160c5c195cb70bf36b4fdd542ac81fc6cf45db022b3c6b6df3054ec3c3f06d3dd3688b06812fca838654ad8980f5ec738fb88fc6fe8a3a223ace9a4122b2f86c45f670b038217b979ee0a904c9dbe5c7e7a67838556c6f1723d79a61c36222957305cd6557b2409d333dbcd910d34b24aaee9487fcb7395167f30c58bfa2f63b8f5d7d43e898117844763050ebd9f4720a451809b3e36ffb9fa6bb0283f079b2c76bdabab52d8858148744c40907ebfb32803cf6ef0c05a011d0908879a22e7f4a4353efe08b7b7b4ea1d54e4f564ea0d2838278254543aa753b0e5107205eff2f273c29a89e49575fdd2a46960de8a2fe4c6b919c43997944ce92e554e26e6cc4a4dd2145202fda01eb84cbacf08f4581499b0d695ef86ac4336347dcc29ed785a90db5db5bad1efd34fce123b7aa8a681f6839a990b0cdcf4936b27812fa7d847152ef256eff9281e14ab9880917d47d8e597e64d5f3559e6079166c224c055b2c2d6540626331a076a73773ecbd3d61fc962caf0d0b82c5cf64a84a1205cb364f5e176cefecc08fe105a3f2d87d211d212e6863861ca57e53388066b22a413236f266aebcb5e25b376a1851c6f7eb31da475e50faee47b201e8c0841"
}
58 changes: 0 additions & 58 deletions contracts/test/Fibonacci.t.sol

This file was deleted.

56 changes: 56 additions & 0 deletions contracts/test/Sxg.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {Test, console} from "forge-std/Test.sol";
import {stdJson} from "forge-std/StdJson.sol";
import {SXG} from "../src/Sxg.sol";
import {SP1VerifierGateway} from "@sp1-contracts/SP1VerifierGateway.sol";

struct SP1ProofFixtureJson {
uint32 result;
bytes32 vkey;
bytes publicValues;
bytes proof;
}

contract SXGTest is Test {
address verifier;
SXG public sxg;

function loadSample() public view returns (SP1ProofFixtureJson memory) {
return
SP1ProofFixtureJson({
result: 1,
vkey: 0x00cacd7a6bd9bf05403e6025a3f07687589f397c117db0387631fc2acb105eff,
publicValues: hex"0000000000000000000000000000000000000000000000000000000000000001",
proof: hex"6a2906ac21d04f16a0b65a4614269ffaa702b89e5f4af256bf057d7210ea14d2e4eb003718416563bd7c3558910f61a515ec26b3aaba426931da7e0196e1c007827c4bda2947aa3e01c50b0d09205a076be5f96f3093d081e8ddfe0ed20d5bdbeeae3080240a293565baa15ef6787722810f6d70411f920f054e22a3a94a92e5f3cfc90d1bec7415f91ecf1ee6493c6e8d2736059afeda7c4e1b5a6f11c61cd5936db86e0d27d53150eb1a1137044994767c44dc5d61a65c76f95a9fd416e35de1b6982b2a407fc15fa38cf2df4f64fc6f3eb84a7f6e73a6cc01b3916db1734f3e8698b90b62224a46f50fae2d637b0dd7e7d70854bb87e99d6426cc58132c8b98563f42"
});
}

function setUp() public {
SP1ProofFixtureJson memory fixture = loadSample();
verifier = address(new SP1VerifierGateway(address(1)));

sxg = new SXG(verifier, fixture.vkey);
}

function test_ValidSXGProof() public {
SP1ProofFixtureJson memory fixture = loadSample();
vm.mockCall(
verifier,
abi.encodeWithSelector(SP1VerifierGateway.verifyProof.selector),
abi.encode(true)
);
uint32 result = sxg.verifySXGProof(fixture.publicValues, fixture.proof);
assert(result == 1);
}

function testFail_InvalidSxgProof() public view {
SP1ProofFixtureJson memory fixture = loadSample();

bytes memory fakeProof = new bytes(fixture.proof.length);

uint32 result = sxg.verifySXGProof(fixture.publicValues, fakeProof);
assert(result == 0);
}
}

0 comments on commit 9e3c697

Please sign in to comment.