-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathintegration.cpp
70 lines (57 loc) · 3.22 KB
/
integration.cpp
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
63
64
65
66
67
68
69
70
/** @file
*****************************************************************************
* @author This file is part of libsnark, developed by SCIPR Lab
* and contributors (see AUTHORS).
* @copyright MIT license (see LICENSE file)
*****************************************************************************/
#include <libsnark/gadgetlib2/adapters.hpp>
#include <libsnark/gadgetlib2/integration.hpp>
namespace libsnark {
linear_combination<libff::Fr<libff::default_ec_pp> > convert_gadgetlib2_linear_combination(const gadgetlib2::GadgetLibAdapter::linear_combination_t &lc)
{
typedef libff::Fr<libff::default_ec_pp> FieldT;
typedef gadgetlib2::GadgetLibAdapter GLA;
linear_combination<FieldT> result = lc.second * variable<FieldT>(0);
for (const GLA::linear_term_t < : lc.first)
{
result = result + lt.second * variable<FieldT>(lt.first+1);
}
return result;
}
r1cs_constraint_system<libff::Fr<libff::default_ec_pp> > get_constraint_system_from_gadgetlib2(const gadgetlib2::Protoboard &pb)
{
typedef libff::Fr<libff::default_ec_pp> FieldT;
typedef gadgetlib2::GadgetLibAdapter GLA;
r1cs_constraint_system<FieldT> result;
const GLA adapter;
GLA::protoboard_t converted_pb = adapter.convert(pb);
for (const GLA::constraint_t &constr : converted_pb.first)
{
result.constraints.emplace_back(r1cs_constraint<FieldT>(convert_gadgetlib2_linear_combination(std::get<0>(constr)),
convert_gadgetlib2_linear_combination(std::get<1>(constr)),
convert_gadgetlib2_linear_combination(std::get<2>(constr))));
}
//The numbers of variables is the highest index created.
//TODO: If there are multiple protoboard, or variables not assigned to a protoboard, then getNextFreeIndex() is *not* the number of variables! See also in get_variable_assignment_from_gadgetlib2.
const size_t num_variables = GLA::getNextFreeIndex();
result.primary_input_size = pb.numInputs();
result.auxiliary_input_size = num_variables - pb.numInputs();
return result;
}
r1cs_variable_assignment<libff::Fr<libff::default_ec_pp> > get_variable_assignment_from_gadgetlib2(const gadgetlib2::Protoboard &pb)
{
typedef libff::Fr<libff::default_ec_pp> FieldT;
typedef gadgetlib2::GadgetLibAdapter GLA;
//The numbers of variables is the highest index created. This is also the required size for the assignment vector.
//TODO: If there are multiple protoboard, or variables not assigned to a protoboard, then getNextFreeIndex() is *not* the number of variables! See also in get_constraint_system_from_gadgetlib2.
const size_t num_vars = GLA::getNextFreeIndex();
const GLA adapter;
r1cs_variable_assignment<FieldT> result(num_vars, FieldT::zero());
VariableAssignment assignment = pb.assignment();
//Go over all assigned values of the protoboard, from every variable-value pair, put the value in the variable.index place of the new assignment.
for(VariableAssignment::iterator iter = assignment.begin(); iter != assignment.end(); ++iter){
result[GLA::getVariableIndex(iter->first)] = adapter.convert(iter->second);
}
return result;
}
}