1

I need your help in structuring data in zokrates.

I have two data let's say Attr(Public) and Res(Private).

Res data is something like this (Not fixed, keys can vary but will be same as Attr).

{
    age: "21",
    income: "1000",
    gender: "male"
    ...
}

Attr also has similar keys and also some data with comparisons like Smaller Than, Greater Than or Equals. For example, age is greater than 18 but smaller than 25 (18<age<25), income is higher than 500 etc (income>500).

What would be the best way to structure Attr so that I can compare them with Res. These two data will be passed in private and public input in zokrates script.

Brief information about the Project.
Basically, I am trying to generate ZKP proof of two data public(Attr) and private(Res). Where public data will be compared with private data and if private data satisfies the requirement of public data a ZKP proof should be generated. Thank you!

EDIT:

Please check the following structs in zokrates.

struct PrivateData {
    u8 age;
    field monthlySpends;
    field userGender;

}
//Contains values which will be compared with private data using the equals operator
struct Eq {
    u8 age;
    field monthlySpends;
    field userGender;
}
//Contains values which will be compared with private data using less than operator
struct Lt{
    u8 age;
    field monthlySpends;
}
//Contains values which will be compared with private data using greater than operator
struct Gt{
    u8 age;
    field monthlySpends;
}


struct PublicData {
    Lt lessThan;
    Eq equalsTo;
    Gt greaterThan;
}

Now I want to compare its value accordingly, a static code would look something like this (Its not correct but it might help to clear what I want to achieve)

def main(PrivateData x, PublicData y) {
    log("Checkin if privateData age is {}, is greater than  required template data age is {}", x.age, y.greaterThan.age);
    assert(x.age > y.greaterThan.age);
    log("Checkin if privateData monthlySpends is {}, is greater than  required template data monthlySpends is {}", x.monthlySpends, y.greaterThan.monthlySpends);
    assert(x.monthlySpends > y.greaterThan.monthlySpends);


    log("Checkin if privateData age is {}, is less than  required template data age is {}", x.age, y.lessThan.age);
    assert(x.age < y.lessThan.age);
    log("Checkin if privateData monthlySpends is {}, is less than  required template data monthlySpends is {}", x.monthlySpends, y.lessThan.monthlySpends);
    assert(x.monthlySpends < y.lessThan.monthlySpends);

    log("Checkin if privateData age is {}, is equals to  required template data age is {}", x.age, y.equalsTo.age);
    assert(x.age == y.equalsTo.age);
    log("Checkin if privateData monthlySpends is {}, is equals to  required template data monthlySpends is {}", x.monthlySpends, y.equalsTo.monthlySpends);
    assert(x.monthlySpends == y.equalsTo.monthlySpends);
    log("Checking if privateData gender {}, is equals to required template data gender {}", x.userGender, y.equalsTo.userGender);
    assert(x.userGender == y.equalsTo.userGender);
    return;
}

Can you guide the correct and optimal way to structure and compare this type of code? Struct attributes might have up to 25 values so need a dynamic solution. If you could even provide a hint like which data structure should I use for this like some kind of tree? Struct data is fixed but some attributes might be null so we need to check that too, currently using "0" to denote null values as couldn't find a way to use null in zokrates.

Here is the input JSON

[
    {
       "age":"26",
       "monthlySpends":"0",
       "userGender":"0"
    },
    {
        "lessThan":{
            "age": "25",
            "monthlySpends": "0"
        },
        "equalsTo":{
            "age": "0",
            "monthlySpends": "0",
            "userGender": "0"
        },
        "greaterThan":{
            "age":"0",
            "monthlySpends":"0"
        }
    }
 ]

1 Answer 1

1

The best way to structure them is subjective - either way, you're going to have to iterate through at least one of your data files completely to ascertain which parameters they share.

If it's for the sake of simplicity, structure your comparable params at slots 0,1,2...etc and use the shorter of the two as as your comparison bases.

For ZKP, this defies the entire premise in that knowledge (contained data) must be shared for comparison.

If you wanted to use identical structures, the params could be hashed (without their values) to ensure that the structures themselves are the same, but without continuity in data the premise of ZKP is unobtainable in this example.

EDIT:

2
  • Is there any way to parse JSON data and store them in key value pair? Do you have any example code to help? Commented Dec 6, 2022 at 10:21
  • I've added some code to demonstrate what I am trying to achieve. Commented Dec 6, 2022 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.