Skip to content

Commit

Permalink
prove works
Browse files Browse the repository at this point in the history
  • Loading branch information
mattstam committed Feb 12, 2024
1 parent 65ab050 commit d74f44f
Show file tree
Hide file tree
Showing 9 changed files with 1,842 additions and 76 deletions.
2 changes: 1 addition & 1 deletion circomx/src/circuit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ contract FunctionVerifier is IFunctionVerifier, Groth16Verifier {
function verify(bytes32 _inputHash, bytes32 _outputHash, bytes memory _proof) external view returns (bool) {
(uint256[8] memory proof) = abi.decode(_proof, (uint256[8]));
uint256[4] memory input;
uint256[3] memory input;
input[0] = uint256(CIRCUIT_DIGEST);
input[1] = uint256(_inputHash) & ((1 << 253) - 1);
input[2] = uint256(_outputHash) & ((1 << 253) - 1);
Expand Down
2 changes: 1 addition & 1 deletion plonky2x/core/src/backend/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ contract FunctionVerifier is IFunctionVerifier, Verifier {
function verify(bytes32 _inputHash, bytes32 _outputHash, bytes memory _proof) external view returns (bool) {
(uint256[8] memory proof) = abi.decode(_proof, (uint256[8]));
uint256[4] memory input;
uint256[3] memory input;
input[0] = uint256(CIRCUIT_DIGEST);
input[1] = uint256(_inputHash) & ((1 << 253) - 1);
input[2] = uint256(_outputHash) & ((1 << 253) - 1);
Expand Down
4 changes: 2 additions & 2 deletions plonky2x/verifier/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func main() {

var s system.ProvingSystem
if *systemFlag == "groth16" {
s = system.NewGroth16System(logger, "./data/dummy", *dataPath)
s = system.NewGroth16System(logger, "../data/dummy", *dataPath)
} else if *systemFlag == "plonk" {
s = system.NewPlonkSystem(logger, "./data/dummy", *dataPath)
s = system.NewPlonkSystem(logger, "../data/dummy", *dataPath)
} else {
logger.Error().Msg("invalid proving system")
os.Exit(1)
Expand Down
555 changes: 555 additions & 0 deletions plonky2x/verifier/system/VerifierGroth16.sol

Large diffs are not rendered by default.

1,193 changes: 1,193 additions & 0 deletions plonky2x/verifier/system/VerifierPlonkRangeCheck.sol

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions plonky2x/verifier/system/circuit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func TestPlonky2xVerifierCircuit(t *testing.T) {
assert := test.NewAssert(t)

testCase := func(option int64) error {
dummyCircuitPath := "./data/dummy"
circuitPath := "./data/test_circuit"
dummyCircuitPath := "../data/dummy"
circuitPath := "../data/test_circuit"

verifierOnlyCircuitDataDummy := variables.DeserializeVerifierOnlyCircuitData(
types.ReadVerifierOnlyCircuitData(dummyCircuitPath + "/verifier_only_circuit_data.json"),
Expand Down
133 changes: 63 additions & 70 deletions plonky2x/verifier/system/groth16.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,42 +476,42 @@ func (s *Groth16System) LoadPublicWitness() (witness.Witness, error) {
// }

type VerifyingKeyJSON struct {
VkAlpha1X uint64 `json:"vk_alpha1_x"`
VkAlpha1Y uint64 `json:"vk_alpha1_y"`
VkBeta2X1 uint64 `json:"vk_beta2_x1"`
VkBeta2X0 uint64 `json:"vk_beta2_x0"`
VkBeta2Y1 uint64 `json:"vk_beta2_y1"`
VkBeta2Y0 uint64 `json:"vk_beta2_y0"`
VkGamma2X1 uint64 `json:"vk_gamma2_x1"`
VkGamma2X0 uint64 `json:"vk_gamma2_x0"`
VkGamma2Y1 uint64 `json:"vk_gamma2_y1"`
VkGamma2Y0 uint64 `json:"vk_gamma2_y0"`
VkDelta2X1 uint64 `json:"vk_delta2_x1"`
VkDelta2X0 uint64 `json:"vk_delta2_x0"`
VkDelta2Y1 uint64 `json:"vk_delta2_y1"`
VkDelta2Y0 uint64 `json:"vk_delta2_y0"`
Ax uint64 `json:"ax"`
Ay uint64 `json:"ay"`
Bx1 uint64 `json:"bx1"`
Bx0 uint64 `json:"bx0"`
By1 uint64 `json:"by1"`
By0 uint64 `json:"by0"`
Cx uint64 `json:"cx"`
Cy uint64 `json:"cy"`
VkIc0X uint64 `json:"vk_ic0_x"`
VkIc0Y uint64 `json:"vk_ic0_y"`
VkIc1X uint64 `json:"vk_ic1_x"`
VkIc1Y uint64 `json:"vk_ic1_y"`
VkIc2X uint64 `json:"vk_ic2_x"`
VkIc2Y uint64 `json:"vk_ic2_y"`
VkIc3X uint64 `json:"vk_ic3_x"`
VkIc3Y uint64 `json:"vk_ic3_y"`
VkIc4X uint64 `json:"vk_ic4_x"`
VkIc4Y uint64 `json:"vk_ic4_y"`
Input0 uint64 `json:"input_0"`
Input1 uint64 `json:"input_1"`
Input2 uint64 `json:"input_2"`
Input3 uint64 `json:"input_3"`
VkAlpha1X string `json:"vk_alpha1_x"`
VkAlpha1Y string `json:"vk_alpha1_y"`
VkBeta2X1 string `json:"vk_beta2_x1"`
VkBeta2X0 string `json:"vk_beta2_x0"`
VkBeta2Y1 string `json:"vk_beta2_y1"`
VkBeta2Y0 string `json:"vk_beta2_y0"`
VkGamma2X1 string `json:"vk_gamma2_x1"`
VkGamma2X0 string `json:"vk_gamma2_x0"`
VkGamma2Y1 string `json:"vk_gamma2_y1"`
VkGamma2Y0 string `json:"vk_gamma2_y0"`
VkDelta2X1 string `json:"vk_delta2_x1"`
VkDelta2X0 string `json:"vk_delta2_x0"`
VkDelta2Y1 string `json:"vk_delta2_y1"`
VkDelta2Y0 string `json:"vk_delta2_y0"`
Ax string `json:"ax"`
Ay string `json:"ay"`
Bx1 string `json:"bx1"`
Bx0 string `json:"bx0"`
By1 string `json:"by1"`
By0 string `json:"by0"`
Cx string `json:"cx"`
Cy string `json:"cy"`
VkIc0X string `json:"vk_ic0_x"`
VkIc0Y string `json:"vk_ic0_y"`
VkIc1X string `json:"vk_ic1_x"`
VkIc1Y string `json:"vk_ic1_y"`
VkIc2X string `json:"vk_ic2_x"`
VkIc2Y string `json:"vk_ic2_y"`
VkIc3X string `json:"vk_ic3_x"`
VkIc3Y string `json:"vk_ic3_y"`
VkIc4X string `json:"vk_ic4_x"`
VkIc4Y string `json:"vk_ic4_y"`
Input0 string `json:"input_0"`
Input1 string `json:"input_1"`
Input2 string `json:"input_2"`
Input3 string `json:"input_3"`
}

// VerifyingKeyWrapper wraps groth16.VerifyingKey to allow adding methods.
Expand All @@ -523,42 +523,25 @@ func (vk *VerifyingKeyWrapper) WriteJSONTo(w io.Writer) error {
vkJSON := VerifyingKeyJSON{}

// Fill in the scalar fields
vkJSON.VkAlpha1X = vk.G1.Alpha.X[0]
vkJSON.VkAlpha1Y = vk.G1.Alpha.Y[0]
vkJSON.VkAlpha1X = elementToStr(vk.G1.Alpha.X)
vkJSON.VkAlpha1Y = elementToStr(vk.G1.Alpha.Y)
vkJSON.Ax = elementToStr(vk.G1.Beta.X)

// Fill in the complex fields like vk.G2.Beta, vk.G2.Gamma, vk.G2.Delta
vkJSON.VkBeta2X1 = vk.G2.Beta.X.A1[0]
vkJSON.VkBeta2X0 = vk.G2.Beta.X.A0[0]
vkJSON.VkBeta2Y1 = vk.G2.Beta.Y.A1[0]
vkJSON.VkBeta2Y0 = vk.G2.Beta.Y.A0[0]
vkJSON.VkGamma2X1 = vk.G2.Gamma.X.A1[0]
vkJSON.VkGamma2X0 = vk.G2.Gamma.X.A0[0]
vkJSON.VkGamma2Y1 = vk.G2.Gamma.Y.A1[0]
vkJSON.VkGamma2Y0 = vk.G2.Gamma.Y.A0[0]
vkJSON.VkDelta2X1 = vk.G2.Delta.X.A1[0]
vkJSON.VkDelta2X0 = vk.G2.Delta.X.A0[0]
vkJSON.VkDelta2Y1 = vk.G2.Delta.Y.A1[0]
vkJSON.VkDelta2Y0 = vk.G2.Delta.Y.A0[0]

// Fill in the scalar fields
vkJSON.Ax = vk.G1.Alpha.X[0]
vkJSON.Ay = vk.G1.Alpha.Y[0]
vkJSON.Bx1 = vk.G2.Beta.X.A1[0]
vkJSON.Bx0 = vk.G2.Beta.X.A0[0]
vkJSON.By1 = vk.G2.Beta.Y.A1[0]
vkJSON.By0 = vk.G2.Beta.Y.A0[0]
vkJSON.Cx = vk.G1.Alpha.X[0]
vkJSON.Cy = vk.G1.Alpha.Y[0]

// Fill in the scalar fields
vkJSON.VkIc0X = vk.G1.Delta.X[0]
vkJSON.VkIc0Y = vk.G1.Delta.Y[0]
vkJSON.VkIc1X = vk.G1.Delta.X[1]
vkJSON.VkIc1Y = vk.G1.Delta.Y[1]
vkJSON.VkIc2X = vk.G1.Delta.X[2]
vkJSON.VkIc2Y = vk.G1.Delta.Y[2]
vkJSON.VkIc3X = vk.G1.Delta.X[3]
vkJSON.VkIc3Y = vk.G1.Delta.Y[3]
vkJSON.VkBeta2X1 = elementToStr(vk.G2.Beta.X.A1)
vkJSON.VkBeta2X0 = elementToStr(vk.G2.Beta.X.A0)
vkJSON.VkBeta2Y1 = elementToStr(vk.G2.Beta.Y.A1)
vkJSON.VkBeta2Y0 = elementToStr(vk.G2.Beta.Y.A0)
vkJSON.VkGamma2X1 = elementToStr(vk.G2.Gamma.X.A1)
vkJSON.VkGamma2X0 = elementToStr(vk.G2.Gamma.X.A0)
vkJSON.VkGamma2Y1 = elementToStr(vk.G2.Gamma.Y.A1)
vkJSON.VkGamma2Y0 = elementToStr(vk.G2.Gamma.Y.A0)
vkJSON.VkDelta2X1 = elementToStr(vk.G2.Delta.X.A1)
vkJSON.VkDelta2X0 = elementToStr(vk.G2.Delta.X.A0)
vkJSON.VkDelta2Y1 = elementToStr(vk.G2.Delta.Y.A1)
vkJSON.VkDelta2Y0 = elementToStr(vk.G2.Delta.Y.A0)

vk.NbG1()

// Marshal the struct to JSON
jsonData, err := json.MarshalIndent(vkJSON, "", " ")
Expand All @@ -570,3 +553,13 @@ func (vk *VerifyingKeyWrapper) WriteJSONTo(w io.Writer) error {
_, err = w.Write(jsonData)
return err
}

func elementToStr(e [4]uint64) string {
// assumes little endian, shifts each limb by 64 bits and adds to bigInt
bigInt := new(big.Int)
for i := len(e) - 1; i >= 0; i-- {
bigInt.Lsh(bigInt, 64)
bigInt.Add(bigInt, new(big.Int).SetUint64(e[i]))
}
return bigInt.String()
}
17 changes: 17 additions & 0 deletions plonky2x/verifier/system/groth16_proof_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"proof": [
"0x25c185337eb32b4a14249b4182920cfbec865d135ec217c42c80999b9e7c5e9e",
"0x199b5341edb422fc632b222fa212a791ce4df97289211191200f40fa29ddcb77",
"0x14a50ece1b4e8f6872934171f784c240af601b5bbeee8a96f9b09fed3174506e",
"0x04bbe25498f9b1ff653ba75a9e2a56f03a76f159392280db281b0926ca2a59a3",
"0x0a6fa941541dfc1663f21ddec16685f2fe2b7655d6b4a6a7a9eedd0c45772dc1",
"0x16c243a088083dedc7b40e82587e0a8e380e4feb110f45ddaf2cc9497a5d48ed",
"0x13b9c8d36dfe56c7e8ffe548499df91264a668b3b8f456a471cd9660b1b84da8",
"0x20cb4c88165681acc79270d4274410576abfcd524281025fec30d85f3a171f24"
],
"inputs": [
"0x0000000000000000000000000000000000000000000000000000000000000001",
"0x0000000000000000000000000000000000000000000000000000000000000002",
"0x0000000000000000000000000000000000000000000000000000000000000003"
]
}
8 changes: 8 additions & 0 deletions plonky2x/verifier/system/plonk_proof_data_range_check.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"proof": "0x19c0034a18cac97126f76d2bd8ecd911661bfd54997800b1b17417a11ef8f6630037b884c6fbc4fcb22c00625173d2d33ef3ca3b3e639600c4e13c665913d09a1a7f8e84b0af818e97f4870096342b4c56f02dd119a85c09b9ef2b98cbb209f202cf5af5608addf3edaaeef295bad371b8ef43961b23d90623642fccdb1e1c30304388f725967a4e856ff325224e10234e989a15642531e43c494acde3738e550ad2a2864c2be31e0640ba5ef7515a85e3affb001e0f53bb3f036649d1e222de23e93152bc7f429595f79aa2ea8324dd598197e61223e134bb8d71c03ef3019520f646f0ce0f908b553c0b7790518ba705e3a6372aa7aefbb6ba92d18bd73c701638cf540bc25ebe12a85fd014a498b723a073f9c864c81ab5d4f0c963eeb76d246d6c7e9a9c12e4a09a25d5bb70adffd4f43b866fe5f022380322652011076b2593b67b3de2ee1d498581630006411f88c00f36ef2e567745995fa7a707f82227733585978311358dc9be48a84089b3d0abca8905dbd2ce6ac4a03e3bbde0b001507003b005a0c028031cd6fd6dcc9371c4f7abec12e9d4b386f89b71d9b78f158d16111fe9159adcded18d4ded33bc7484a5cd1b1734140f7fca5b7ee51c23198a581af8c56f4dbe674c354dc68c171e6f4ee48a5836b2cafe8e135df028f92c57ba61d9469ab96a3c537282b28278a17b3591ac439db41fe547323cf89c0b1c1dc647c257610c176fe4331cbdf37d9352b9ce6a22b3b799ebdec8625e1eac30345ed74e6b33acdb78b2170638306ce8078b34f29ce42bf26ee083427a93b80710d51f7ca0e8458879f8cb9d64a1680ce100586d47e5db75ea43b1a5ceaa0d2f66502ef8d1bfd4a35fd71ecf11388d32a29fcf29695e063d7297b6f24f641a233d01ff862f2ce4a69628c412365488f3311ffe08c3f076e62847e26e4c782e154ddb8a630faa18a0168651e125a998d734a59a4821237ab0f0c42f24c350ef297b798dd0abd4dc15f4fcb7edf3ed05c59b219143c4e2165676bdf57a79ba0106f8469b9b8bc4e88f96611aed1f2961344db818c6c967c091e67eab0d46bad52aaabc3bf63f905118370917ba1da00603e1e829a00e7d8d94a923d2c3d03e3d1902a0e320762ebbe638d8f75e674f091729b98a9cf0584a8ded385f2821f8e205879e35cd9ee368a0aed16703a69a5df5ec3afc5d09aacc3c8aa7d782d23f962f37233358c430e6a90e548d71577aae72851978dbb6fd3f7a03a83bf4cda1500fccc649e0bd283fbd61e46442c6b1d17d9085a939485870687a004a6bf4c638",
"inputs": [
"0x0000000000000000000000000000000000000000000000000000000000000001",
"0x0000000000000000000000000000000000000000000000000000000000000002",
"0x0000000000000000000000000000000000000000000000000000000000000003"
]
}

0 comments on commit d74f44f

Please sign in to comment.