-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathmod.nr
332 lines (294 loc) · 11 KB
/
mod.nr
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
pub mod bn254;
use crate::runtime::is_unconstrained;
use bn254::lt as bn254_lt;
impl Field {
/// Asserts that `self` can be represented in `bit_size` bits.
///
/// # Failures
/// Causes a constraint failure for `Field` values exceeding `2^{bit_size}`.
// docs:start:assert_max_bit_size
pub fn assert_max_bit_size<let BIT_SIZE: u32>(self) {
// docs:end:assert_max_bit_size
assert(BIT_SIZE < modulus_num_bits() as u32);
self.__assert_max_bit_size(BIT_SIZE);
}
#[builtin(apply_range_constraint)]
fn __assert_max_bit_size(self, bit_size: u32) {}
/// Decomposes `self` into its little endian bit decomposition as a `[u1; N]` array.
/// This slice will be zero padded should not all bits be necessary to represent `self`.
///
/// # Failures
/// Causes a constraint failure for `Field` values exceeding `2^N` as the resulting slice will not
/// be able to represent the original `Field`.
///
/// # Safety
/// Values of `N` equal to or greater than the number of bits necessary to represent the `Field` modulus
/// (e.g. 254 for the BN254 field) allow for multiple bit decompositions. This is due to how the `Field` will
/// wrap around due to overflow when verifying the decomposition.
#[builtin(to_le_bits)]
// docs:start:to_le_bits
pub fn to_le_bits<let N: u32>(self: Self) -> [u1; N] {}
// docs:end:to_le_bits
/// Decomposes `self` into its big endian bit decomposition as a `[u1; N]` array.
/// This array will be zero padded should not all bits be necessary to represent `self`.
///
/// # Failures
/// Causes a constraint failure for `Field` values exceeding `2^N` as the resulting slice will not
/// be able to represent the original `Field`.
///
/// # Safety
/// Values of `N` equal to or greater than the number of bits necessary to represent the `Field` modulus
/// (e.g. 254 for the BN254 field) allow for multiple bit decompositions. This is due to how the `Field` will
/// wrap around due to overflow when verifying the decomposition.
#[builtin(to_be_bits)]
// docs:start:to_be_bits
pub fn to_be_bits<let N: u32>(self: Self) -> [u1; N] {}
// docs:end:to_be_bits
/// Decomposes `self` into its little endian byte decomposition as a `[u8;N]` array
/// This array will be zero padded should not all bytes be necessary to represent `self`.
///
/// # Failures
/// The length N of the array must be big enough to contain all the bytes of the 'self',
/// and no more than the number of bytes required to represent the field modulus
///
/// # Safety
/// The result is ensured to be the canonical decomposition of the field element
// docs:start:to_le_bytes
pub fn to_le_bytes<let N: u32>(self: Self) -> [u8; N] {
// docs:end:to_le_bytes
// Compute the byte decomposition
let bytes = self.to_le_radix(256);
if !is_unconstrained() {
// Ensure that the byte decomposition does not overflow the modulus
let p = modulus_le_bytes();
assert(bytes.len() <= p.len());
let mut ok = bytes.len() != p.len();
for i in 0..N {
if !ok {
if (bytes[N - 1 - i] != p[N - 1 - i]) {
assert(bytes[N - 1 - i] < p[N - 1 - i]);
ok = true;
}
}
}
assert(ok);
}
bytes
}
/// Decomposes `self` into its big endian byte decomposition as a `[u8;N]` array of length required to represent the field modulus
/// This array will be zero padded should not all bytes be necessary to represent `self`.
///
/// # Failures
/// The length N of the array must be big enough to contain all the bytes of the 'self',
/// and no more than the number of bytes required to represent the field modulus
///
/// # Safety
/// The result is ensured to be the canonical decomposition of the field element
// docs:start:to_be_bytes
pub fn to_be_bytes<let N: u32>(self: Self) -> [u8; N] {
// docs:end:to_be_bytes
// Compute the byte decomposition
let bytes = self.to_be_radix(256);
if !is_unconstrained() {
// Ensure that the byte decomposition does not overflow the modulus
let p = modulus_be_bytes();
assert(bytes.len() <= p.len());
let mut ok = bytes.len() != p.len();
for i in 0..N {
if !ok {
if (bytes[i] != p[i]) {
assert(bytes[i] < p[i]);
ok = true;
}
}
}
assert(ok);
}
bytes
}
// docs:start:to_le_radix
pub fn to_le_radix<let N: u32>(self: Self, radix: u32) -> [u8; N] {
// Brillig does not need an immediate radix
if !crate::runtime::is_unconstrained() {
crate::assert_constant(radix);
}
self.__to_le_radix(radix)
}
// docs:end:to_le_radix
// docs:start:to_be_radix
pub fn to_be_radix<let N: u32>(self: Self, radix: u32) -> [u8; N] {
// Brillig does not need an immediate radix
if !crate::runtime::is_unconstrained() {
crate::assert_constant(radix);
}
self.__to_be_radix(radix)
}
// docs:end:to_be_radix
// `_radix` must be less than 256
#[builtin(to_le_radix)]
fn __to_le_radix<let N: u32>(self, radix: u32) -> [u8; N] {}
#[builtin(to_be_radix)]
fn __to_be_radix<let N: u32>(self, radix: u32) -> [u8; N] {}
// Returns self to the power of the given exponent value.
// Caution: we assume the exponent fits into 32 bits
// using a bigger bit size impacts negatively the performance and should be done only if the exponent does not fit in 32 bits
pub fn pow_32(self, exponent: Field) -> Field {
let mut r: Field = 1;
let b: [u1; 32] = exponent.to_le_bits();
for i in 1..33 {
r *= r;
r = (b[32 - i] as Field) * (r * self) + (1 - b[32 - i] as Field) * r;
}
r
}
// Parity of (prime) Field element, i.e. sgn0(x mod p) = 0 if x `elem` {0, ..., p-1} is even, otherwise sgn0(x mod p) = 1.
pub fn sgn0(self) -> u1 {
self as u1
}
pub fn lt(self, another: Field) -> bool {
if crate::compat::is_bn254() {
bn254_lt(self, another)
} else {
lt_fallback(self, another)
}
}
/// Convert a little endian byte array to a field element.
/// If the provided byte array overflows the field modulus then the Field will silently wrap around.
pub fn from_le_bytes<let N: u32>(bytes: [u8; N]) -> Field {
let mut v = 1;
let mut result = 0;
for i in 0..N {
result += (bytes[i] as Field) * v;
v = v * 256;
}
result
}
/// Convert a big endian byte array to a field element.
/// If the provided byte array overflows the field modulus then the Field will silently wrap around.
pub fn from_be_bytes<let N: u32>(bytes: [u8; N]) -> Field {
let mut v = 1;
let mut result = 0;
for i in 0..N {
result += (bytes[N - 1 - i] as Field) * v;
v = v * 256;
}
result
}
}
#[builtin(modulus_num_bits)]
pub comptime fn modulus_num_bits() -> u64 {}
#[builtin(modulus_be_bits)]
pub comptime fn modulus_be_bits() -> [u1] {}
#[builtin(modulus_le_bits)]
pub comptime fn modulus_le_bits() -> [u1] {}
#[builtin(modulus_be_bytes)]
pub comptime fn modulus_be_bytes() -> [u8] {}
#[builtin(modulus_le_bytes)]
pub comptime fn modulus_le_bytes() -> [u8] {}
/// An unconstrained only built in to efficiently compare fields.
#[builtin(field_less_than)]
unconstrained fn __field_less_than(x: Field, y: Field) -> bool {}
pub(crate) unconstrained fn field_less_than(x: Field, y: Field) -> bool {
__field_less_than(x, y)
}
// Convert a 32 byte array to a field element by modding
pub fn bytes32_to_field(bytes32: [u8; 32]) -> Field {
// Convert it to a field element
let mut v = 1;
let mut high = 0 as Field;
let mut low = 0 as Field;
for i in 0..16 {
high = high + (bytes32[15 - i] as Field) * v;
low = low + (bytes32[16 + 15 - i] as Field) * v;
v = v * 256;
}
// Abuse that a % p + b % p = (a + b) % p and that low < p
low + high * v
}
fn lt_fallback(x: Field, y: Field) -> bool {
if is_unconstrained() {
/// Safety: unconstrained context
unsafe {
field_less_than(x, y)
}
} else {
let x_bytes: [u8; 32] = x.to_le_bytes();
let y_bytes: [u8; 32] = y.to_le_bytes();
let mut x_is_lt = false;
let mut done = false;
for i in 0..32 {
if (!done) {
let x_byte = x_bytes[32 - 1 - i] as u8;
let y_byte = y_bytes[32 - 1 - i] as u8;
let bytes_match = x_byte == y_byte;
if !bytes_match {
x_is_lt = x_byte < y_byte;
done = true;
}
}
}
x_is_lt
}
}
mod tests {
use super::field_less_than;
#[test]
// docs:start:to_be_bits_example
fn test_to_be_bits() {
let field = 2;
let bits: [u1; 8] = field.to_be_bits();
assert_eq(bits, [0, 0, 0, 0, 0, 0, 1, 0]);
}
// docs:end:to_be_bits_example
#[test]
// docs:start:to_le_bits_example
fn test_to_le_bits() {
let field = 2;
let bits: [u1; 8] = field.to_le_bits();
assert_eq(bits, [0, 1, 0, 0, 0, 0, 0, 0]);
}
// docs:end:to_le_bits_example
#[test]
// docs:start:to_be_bytes_example
fn test_to_be_bytes() {
let field = 2;
let bytes: [u8; 8] = field.to_be_bytes();
assert_eq(bytes, [0, 0, 0, 0, 0, 0, 0, 2]);
assert_eq(Field::from_be_bytes::<8>(bytes), field);
}
// docs:end:to_be_bytes_example
#[test]
// docs:start:to_le_bytes_example
fn test_to_le_bytes() {
let field = 2;
let bytes: [u8; 8] = field.to_le_bytes();
assert_eq(bytes, [2, 0, 0, 0, 0, 0, 0, 0]);
assert_eq(Field::from_le_bytes::<8>(bytes), field);
}
// docs:end:to_le_bytes_example
#[test]
// docs:start:to_be_radix_example
fn test_to_be_radix() {
let field = 2;
let bytes: [u8; 8] = field.to_be_radix(256);
assert_eq(bytes, [0, 0, 0, 0, 0, 0, 0, 2]);
assert_eq(Field::from_be_bytes::<8>(bytes), field);
}
// docs:end:to_be_radix_example
#[test]
// docs:start:to_le_radix_example
fn test_to_le_radix() {
let field = 2;
let bytes: [u8; 8] = field.to_le_radix(256);
assert_eq(bytes, [2, 0, 0, 0, 0, 0, 0, 0]);
assert_eq(Field::from_le_bytes::<8>(bytes), field);
}
// docs:end:to_le_radix_example
#[test]
unconstrained fn test_field_less_than() {
assert(field_less_than(0, 1));
assert(field_less_than(0, 0x100));
assert(field_less_than(0x100, 0 - 1));
assert(!field_less_than(0 - 1, 0));
}
}