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: Complete, auditors: [Luke], commit: a48c205d6dcd4338f5b83b4fda18bff6015be07b}
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
9
10namespace acir_format {
11
12using namespace bb;
13using namespace bb::stdlib;
14
32template <typename Builder>
35 const bb::stdlib::bool_t<Builder>& predicate,
37{
39
40 bool constant_coordinates = input_x.is_constant && input_y.is_constant;
41
42 auto point_x = to_field_ct(input_x, builder);
43 auto point_y = to_field_ct(input_y, builder);
44
45 // If a witness is not provided (we are in a write_vk scenario) we ensure the coordinates correspond to a valid
46 // point to avoid erroneous failures during circuit construction. We set coordinates to the generator (a finite
47 // point).
48 if (builder.is_write_vk_mode() && !constant_coordinates) {
49 builder.set_variable(input_x.index, bb::grumpkin::g1::affine_one.x);
50 builder.set_variable(input_y.index, bb::grumpkin::g1::affine_one.y);
51 }
52
53 // If the predicate is a non-constant witness, conditionally replace coordinates with a valid point.
54 if (!predicate.is_constant()) {
57 } else {
58 BB_ASSERT(predicate.get_value(), "Creating Grumpkin point with a constant predicate equal to false.");
59 }
60
61 // Use public constructor which auto-detects infinity from (0,0) coordinates.
62 return cycle_group<Builder>(point_x, point_y, /*assert_on_curve=*/true);
63}
64
70
76
92template <typename Builder>
96 const bb::stdlib::bool_t<Builder>& predicate,
98{
100 using cycle_scalar_ct = typename bb::stdlib::cycle_group<Builder>::cycle_scalar;
101
102 auto lo_as_field = to_field_ct(scalar_lo, builder);
103 auto hi_as_field = to_field_ct(scalar_hi, builder);
104
105 // We assert that scalar_hi is not a witness when scalar_lo is constant as this might indicate unintended behavior.
106 BB_ASSERT(!(scalar_lo.is_constant && !scalar_hi.is_constant),
107 "to_grumpkin_scalar: scalar_lo is constant while scalar_hi is not.");
108
109 // If a witness is not provided (we are in a write_vk scenario) we ensure the scalar is valid.
110 // We only do this if the limbs are non-constant since otherwise no variable indices exist.
111 // Note: the two limbs may have different constancy, e.g. if the scalar is a witness known to be <= 128 bits.
112 if (builder.is_write_vk_mode()) {
113 if (!scalar_lo.is_constant) {
114 builder.set_variable(scalar_lo.index, bb::fr(1));
115 }
116 if (!scalar_hi.is_constant) {
117 builder.set_variable(scalar_hi.index, bb::fr(0));
118 }
119 }
120
121 // If the predicate is a non-constant witness, conditionally replace the scalar with 1.
122 if (!predicate.is_constant()) {
123 lo_as_field = field_ct::conditional_assign(predicate, lo_as_field, field_ct(1));
124 hi_as_field = field_ct::conditional_assign(predicate, hi_as_field, field_ct(0));
125 } else {
126 BB_ASSERT(predicate.get_value(), "Creating Grumpkin scalar with a constant predicate equal to false.");
127 }
128
129 cycle_scalar_ct scalar(lo_as_field, hi_as_field);
130 return scalar;
131}
132
138
144
145} // namespace acir_format
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
static constexpr affine_element affine_one
Definition group.hpp:50
Implements boolean logic in-circuit.
Definition bool.hpp:60
bool get_value() const
Definition bool.hpp:125
bool is_constant() const
Definition bool.hpp:127
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:385
AluTraceBuilder builder
Definition alu.test.cpp:124
stdlib::field_t< Builder > field_ct
bb::stdlib::cycle_group< Builder > to_grumpkin_point(const WitnessOrConstant< typename Builder::FF > &input_x, const WitnessOrConstant< typename Builder::FF > &input_y, const bb::stdlib::bool_t< Builder > &predicate, Builder &builder)
Convert inputs representing a Grumpkin point into a cycle_group element.
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