pepper-project / pequin

A system for verifying outsourced computations, and applying SNARKs. Simplified release of the main Pepper codebase.
Other
122 stars 46 forks source link

Pass arguments to input generation function #31

Closed cnasikas closed 5 years ago

cnasikas commented 5 years ago

This PR pass the argv variable to the corresponding input generation function.

A useful feature where the input generation function is parameterized by external factors

Real case scenario:

The input generation function takes as input a hash value in hex and transform it to a bytearray which is passed to input_q

Command:

bin/pepper_verifier_prover_knows_hash_preimage gen_input prover_knows_hash_preimage.inputs 88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589

Sample Code

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <limits.h>
#include <unistd.h>

void hex_to_bytearray(std::string str, std::vector<int> &array)
{
    int length = str.length();
    // make sure the input string has an even digit numbers
    if(length%2 == 1)
    {
        str = "0" + str;
        length++;
    }

    // allocate memory for the output array
    array.reserve(length/2);

    std::stringstream sstr(str);
    for(int i=0; i < length/2; i++)
    {
        char ch1, ch2;
        sstr >> ch1 >> ch2;
        int dig1, dig2;
        if(isdigit(ch1)) dig1 = ch1 - '0';
        else if(ch1>='A' && ch1<='F') dig1 = ch1 - 'A' + 10;
        else if(ch1>='a' && ch1<='f') dig1 = ch1 - 'a' + 10;
        if(isdigit(ch2)) dig2 = ch2 - '0';
        else if(ch2>='A' && ch2<='F') dig2 = ch2 - 'A' + 10;
        else if(ch2>='a' && ch2<='f') dig2 = ch2 - 'a' + 10;
        array.push_back(dig1*16 + dig2);
    }
}

void prover_knows_hash_preimage_input_gen (mpq_t * input_q, int num_inputs, char *argv[]) {

    std::vector<int> bytearray;
    std::string hash = argv[3];

    hex_to_bytearray(hash, bytearray);

    for (int i = 0; i < num_inputs; i++) {
        mpq_set_ui(input_q[i],  bytearray[i], 1);
    }
}
maxhowald commented 5 years ago

Thanks for the PR. Cool feature!

cnasikas commented 5 years ago

Thanks!