-
Notifications
You must be signed in to change notification settings - Fork 69
/
vkey.go
62 lines (51 loc) · 1.99 KB
/
vkey.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package succinct
import (
"bytes"
"io"
"strings"
"github.com/consensys/gnark/backend/groth16"
)
type SuccinctVerifyingKey struct {
groth16.VerifyingKey
}
func (svk *SuccinctVerifyingKey) ExportIFunctionVerifierSolidity(w io.Writer) error {
// Create a new buffer and export the VerifyingKey into it as a Solidity contract and
// convert the buffer content to a string for further manipulation.
buf := new(bytes.Buffer)
err := svk.VerifyingKey.ExportSolidity(buf)
if err != nil {
return err
}
content := buf.String()
// Custom replacements to make compatible with IFunctionVerifier.
content = strings.ReplaceAll(content, "uint256[2] calldata input", "uint256[2] memory input")
content = strings.ReplaceAll(content, "pragma solidity ^0.8.0;", "pragma solidity ^0.8.16;")
// write the new content to the writer
_, err = w.Write([]byte(content))
if err != nil {
return err
}
// Generate the IFunctionVerifier interface and FunctionVerifier contract.
solidityIFunctionVerifier := `
interface IFunctionVerifier {
function verify(bytes32 _inputHash, bytes32 _outputHash, bytes memory _proof) external view returns (bool);
function verificationKeyHash() external pure returns (bytes32);
}
contract FunctionVerifier is IFunctionVerifier, Verifier {
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[2] memory input = [uint256(_inputHash), uint256(_outputHash)];
input[0] = input[0] & ((1 << 253) - 1);
input[1] = input[1] & ((1 << 253) - 1);
return verifyProof(a, b, c, input);
}
function verificationKeyHash() external pure returns (bytes32) {
return keccak256(abi.encode(verifyingKey()));
}
}
`
// write the IFunctionVerifier and FunctionVerifier to the writer
_, err = w.Write([]byte(solidityIFunctionVerifier))
return err
}