Skip to content

Commit

Permalink
Merge pull request #30 from bbresearcher/main
Browse files Browse the repository at this point in the history
Submission to suggest a fix for issue : Add on-the-fly proof generation #26
  • Loading branch information
critesjosh authored Jun 15, 2023
2 parents 1dc4e00 + a8fec9f commit 7b1d94a
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 2 deletions.
4 changes: 2 additions & 2 deletions with-foundry/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions with-foundry/script/createFile.sh
Original file line number Diff line number Diff line change
@@ -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"
7 changes: 7 additions & 0 deletions with-foundry/script/prove.sh
Original file line number Diff line number Diff line change
@@ -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"
80 changes: 80 additions & 0 deletions with-foundry/test/Starter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);

}
}

0 comments on commit 7b1d94a

Please sign in to comment.