diff --git a/with-foundry/foundry.toml b/with-foundry/foundry.toml index 8d34fc9..6d22dfe 100644 --- a/with-foundry/foundry.toml +++ b/with-foundry/foundry.toml @@ -2,7 +2,7 @@ src = "src" out = "out" libs = ["lib"] -fs_permissions = [{ access = "read", path = "./"}] - +fs_permissions = [{ access = "read-write", path = "./"},{ access = "read-write", path = "/tmp/"}] +ffi = true # See more config options https://github.com/foundry-rs/foundry/tree/master/config diff --git a/with-foundry/script/createFile.sh b/with-foundry/script/createFile.sh new file mode 100755 index 0000000..c2bd7bc --- /dev/null +++ b/with-foundry/script/createFile.sh @@ -0,0 +1,14 @@ +#!/bin/bash +if [ "$#" -ne 1 ] +then + echo "Usage: ./createFile.sh [TESTNAME_STRING]" + exit 1 +fi +if [ ! -d "/tmp/$1" ]; then + mkdir /tmp/$1 +fi + +cp ./circuits/Nargo.toml /tmp/$1/Nargo.toml +cp ./circuits/Verifier.toml /tmp/$1/Verifier.toml +cp -r ./circuits/src /tmp/$1/src +echo "" > /tmp/$1/Prover.toml && echo "File created" diff --git a/with-foundry/script/prove.sh b/with-foundry/script/prove.sh new file mode 100755 index 0000000..bce01a9 --- /dev/null +++ b/with-foundry/script/prove.sh @@ -0,0 +1,7 @@ +#!/bin/bash +if [ "$#" -ne 1 ] +then + echo "Usage: ./prove.sh [TESTNAME_STRING]" + exit 1 +fi +cd /tmp/$1 && nargo prove d && echo "Proof Generated" diff --git a/with-foundry/test/Starter.t.sol b/with-foundry/test/Starter.t.sol index 015897c..57b60f5 100644 --- a/with-foundry/test/Starter.t.sol +++ b/with-foundry/test/Starter.t.sol @@ -8,8 +8,10 @@ contract StarterTest is Test { Starter public starter; UltraVerifier public verifier; + bytes32[] public dynamicCorrect = new bytes32[](1); bytes32[] public correct = new bytes32[](1); bytes32[] public wrong = new bytes32[](1); + function setUp() public { verifier = new UltraVerifier(); @@ -31,4 +33,82 @@ contract StarterTest is Test { bytes memory proofBytes = vm.parseBytes(proof); starter.verifyEqual(proofBytes, wrong); } + + function test_dynamicProof() public { + string[] memory _fieldNames = new string[](2); + string[] memory _fieldValues = new string[](2); + + _fieldNames[0] = "x"; + _fieldNames[1] = "y"; + _fieldValues[0] = "5"; + _fieldValues[1] = "5"; + + // Set expected dynamic proof outcome + dynamicCorrect[0] = bytes32(0x0000000000000000000000000000000000000000000000000000000000000005); + bytes memory proofBytes = generateDynamicProof("test1",_fieldNames,_fieldValues); + starter.verifyEqual(proofBytes, dynamicCorrect); + } + + function test_dynamicProofSecondTest() public { + string[] memory _fieldNames = new string[](2); + string[] memory _fieldValues = new string[](2); + + _fieldNames[0] = "x"; + _fieldNames[1] = "y"; + _fieldValues[0] = "8"; + _fieldValues[1] = "8"; + + // Set expected dynamic proof outcome + dynamicCorrect[0] = bytes32(0x0000000000000000000000000000000000000000000000000000000000000008); + bytes memory proofBytes = generateDynamicProof("test2",_fieldNames,_fieldValues); + starter.verifyEqual(proofBytes, dynamicCorrect); + } + + function test_dynamicProofThirdTest() public { + string[] memory _fieldNames = new string[](2); + string[] memory _fieldValues = new string[](2); + + _fieldNames[0] = "x"; + _fieldNames[1] = "y"; + _fieldValues[0] = "7"; + _fieldValues[1] = "7"; + + // Set expected dynamic proof outcome + dynamicCorrect[0] = bytes32(0x0000000000000000000000000000000000000000000000000000000000000007); + bytes memory proofBytes = generateDynamicProof("test3",_fieldNames,_fieldValues); + starter.verifyEqual(proofBytes, dynamicCorrect); + } + + /// @dev This function generates dynamic proofs using 2 scripts in the /script directory + /// + /// @param _testName a random string to identify the test by, this is used to create a unique folder name in the /tmp directory + /// @param _fields The field names within the Prover.toml file + /// @param _fieldValues The field values associated with fields names within the Prover.toml file + function generateDynamicProof(string memory _testName,string[] memory _fields, string[] memory _fieldValues) public returns (bytes memory) { + require(_fields.length == _fieldValues.length,"generateProof: Input arrays not the same length"); + + // Copy files and create Prover.toml in /tmp directory + string[] memory filecreateCommand = new string[] (2); + filecreateCommand[0] = "./script/createFile.sh"; + filecreateCommand[1] = _testName; + bytes memory fileCreateResponse = vm.ffi(filecreateCommand); + console.log(string(fileCreateResponse)); + + string memory _file = string.concat("/tmp/",_testName,"/Prover.toml"); + vm.writeFile(_file,""); + for(uint256 i; i < _fields.length; i++) + { + vm.writeLine(_file, string.concat( _fields[i] , " = " , _fieldValues[i])); + } + + // now generate the proof by calling the script using ffi + string[] memory ffi_command = new string[] (2); + ffi_command[0] = "./script/prove.sh"; + ffi_command[1] = _testName; + bytes memory commandResponse = vm.ffi(ffi_command); + console.log(string(commandResponse)); + string memory _newProof = vm.readLine(string.concat("/tmp/",_testName,"/proofs/d.proof")); + return vm.parseBytes(_newProof); + + } }