Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
witness_constant.cpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
9
10namespace acir_format {
11
12using namespace bb;
13using namespace bb::stdlib;
14
33template <typename Builder>
36 const WitnessOrConstant<typename Builder::FF>& input_infinite,
37 const bb::stdlib::bool_t<Builder>& predicate,
39{
40 using bool_ct = bb::stdlib::bool_t<Builder>;
42
43 bool constant_coordinates = input_x.is_constant && input_y.is_constant;
44
45 auto point_x = to_field_ct(input_x, builder);
46 auto point_y = to_field_ct(input_y, builder);
47 auto infinite = bool_ct(to_field_ct(input_infinite, builder));
48
49 // If a witness is not provided (we are in a write_vk scenario) we ensure the coordinates correspond to a valid
50 // point to avoid erroneous failures during circuit construction. We only do this if the coordinates are
51 // non-constant since otherwise no variable indices exist. Note that there is no need to assign the infinite flag
52 // because native on-curve checks will always pass as long x and y coordinates correspond to a valid point on
53 // Grumpkin.
54 if (builder.is_write_vk_mode() && !constant_coordinates) {
55 builder.set_variable(input_x.index, bb::grumpkin::g1::affine_one.x);
56 builder.set_variable(input_y.index, bb::grumpkin::g1::affine_one.y);
57 }
58
59 // If the predicate is a non-constant witness, conditionally replace coordinates with a valid point.
60 if (!predicate.is_constant()) {
63 infinite = bool_ct::conditional_assign(predicate, infinite, bool_ct(false));
64 } else {
65 BB_ASSERT(predicate.get_value(), "Creating Grumpkin point with a constant predicate equal to false.");
66 }
67
68 cycle_group<Builder> input_point(point_x, point_y, infinite, /*assert_on_curve=*/true);
69 return input_point;
70}
71
78
85
101template <typename Builder>
105 const bb::stdlib::bool_t<Builder>& predicate,
107{
109 using cycle_scalar_ct = typename bb::stdlib::cycle_group<Builder>::cycle_scalar;
110
111 auto lo_as_field = to_field_ct(scalar_lo, builder);
112 auto hi_as_field = to_field_ct(scalar_hi, builder);
113
114 // We assert that scalar_hi is not a witness when scalar_lo is constant as this might indicate unintended behavior.
115 BB_ASSERT(!(scalar_lo.is_constant && !scalar_hi.is_constant),
116 "to_grumpkin_scalar: scalar_lo is constant while scalar_hi is not.");
117
118 // If a witness is not provided (we are in a write_vk scenario) we ensure the scalar is valid.
119 // We only do this if the limbs are non-constant since otherwise no variable indices exist.
120 // Note: the two limbs may have different constancy, e.g. if the scalar is a witness known to be <= 128 bits.
121 if (builder.is_write_vk_mode()) {
122 if (!scalar_lo.is_constant) {
123 builder.set_variable(scalar_lo.index, bb::fr(1));
124 }
125 if (!scalar_hi.is_constant) {
126 builder.set_variable(scalar_hi.index, bb::fr(0));
127 }
128 }
129
130 // If the predicate is a non-constant witness, conditionally replace the scalar with 1.
131 if (!predicate.is_constant()) {
132 lo_as_field = field_ct::conditional_assign(predicate, lo_as_field, field_ct(1));
133 hi_as_field = field_ct::conditional_assign(predicate, hi_as_field, field_ct(0));
134 } else {
135 BB_ASSERT(predicate.get_value(), "Creating Grumpkin scalar with a constant predicate equal to false.");
136 }
137
138 cycle_scalar_ct scalar(lo_as_field, hi_as_field);
139 return scalar;
140}
141
147
153
154} // namespace acir_format
#define BB_ASSERT(expression,...)
Definition assert.hpp:67
static constexpr affine_element affine_one
Definition group.hpp:48
Implements boolean logic in-circuit.
Definition bool.hpp:59
bool get_value() const
Definition bool.hpp:124
bool is_constant() const
Definition bool.hpp:126
cycle_group represents a group Element of the proving system's embedded curve, i.e....
Represents a member of the Grumpkin curve scalar field (i.e. BN254 base field).
static field_t conditional_assign(const bool_t< Builder > &predicate, const field_t &lhs, const field_t &rhs)
Definition field.hpp:372
AluTraceBuilder builder
Definition alu.test.cpp:124
bb::stdlib::cycle_group< Builder > to_grumpkin_point(const WitnessOrConstant< typename Builder::FF > &input_x, const WitnessOrConstant< typename Builder::FF > &input_y, const WitnessOrConstant< typename Builder::FF > &input_infinite, const bb::stdlib::bool_t< Builder > &predicate, Builder &builder)
Convert inputs representing a Grumpkin point into a cycle_group element.
stdlib::field_t< Builder > field_ct
bb::stdlib::cycle_group< Builder >::cycle_scalar to_grumpkin_scalar(const WitnessOrConstant< typename Builder::FF > &scalar_lo, const WitnessOrConstant< typename Builder::FF > &scalar_hi, const bb::stdlib::bool_t< Builder > &predicate, Builder &builder)
Convert inputs representing a Grumpkin scalar into a cycle_scalar element.
bb::stdlib::field_t< Builder > to_field_ct(const WitnessOrConstant< typename Builder::FF > &input, Builder &builder)
Entry point for Barretenberg command-line interface.
Definition api.hpp:5