-
Notifications
You must be signed in to change notification settings - Fork 305
/
Copy pathbn254.nr
216 lines (186 loc) · 6.04 KB
/
bn254.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
use crate::field::field_less_than;
use crate::runtime::is_unconstrained;
// The low and high decomposition of the field modulus
global PLO: Field = 53438638232309528389504892708671455233;
global PHI: Field = 64323764613183177041862057485226039389;
pub(crate) global TWO_POW_128: Field = 0x100000000000000000000000000000000;
global TWO_POW_64: Field = 0x10000000000000000;
// Decomposes a single field into two 16 byte fields.
fn compute_decomposition(mut x: Field) -> (Field, Field) {
// Here's we're taking advantage of truncating 64 bit limbs from the input field
// and then subtracting them from the input such the field division is equivalent to integer division.
let low_lower_64 = (x as u64) as Field;
x = (x - low_lower_64) / TWO_POW_64;
let low_upper_64 = (x as u64) as Field;
let high = (x - low_upper_64) / TWO_POW_64;
let low = low_upper_64 * TWO_POW_64 + low_lower_64;
(low, high)
}
pub(crate) unconstrained fn decompose_hint(x: Field) -> (Field, Field) {
compute_decomposition(x)
}
unconstrained fn lte_hint(x: Field, y: Field) -> bool {
if x == y {
true
} else {
field_less_than(x, y)
}
}
// Assert that (alo > blo && ahi >= bhi) || (alo <= blo && ahi > bhi)
fn assert_gt_limbs(a: (Field, Field), b: (Field, Field)) {
let (alo, ahi) = a;
let (blo, bhi) = b;
unsafe {
/*@safety: borrow is enforced to be boolean due to its type.
if borrow is 0, it asserts that (alo > blo && ahi >= bhi)
if borrow is 1, it asserts that (alo <= blo && ahi > bhi)
*/
let borrow = lte_hint(alo, blo);
let rlo = alo - blo - 1 + (borrow as Field) * TWO_POW_128;
let rhi = ahi - bhi - (borrow as Field);
rlo.assert_max_bit_size::<128>();
rhi.assert_max_bit_size::<128>();
}
}
/// Decompose a single field into two 16 byte fields.
pub fn decompose(x: Field) -> (Field, Field) {
if is_unconstrained() {
compute_decomposition(x)
} else {
unsafe {
/*@safety: decomposition is properly checked below*/
// Take hints of the decomposition
let (xlo, xhi) = decompose_hint(x);
// Range check the limbs
xlo.assert_max_bit_size::<128>();
xhi.assert_max_bit_size::<128>();
// Check that the decomposition is correct
assert_eq(x, xlo + TWO_POW_128 * xhi);
// Assert that the decomposition of P is greater than the decomposition of x
assert_gt_limbs((PLO, PHI), (xlo, xhi));
(xlo, xhi)
}
}
}
pub fn assert_gt(a: Field, b: Field) {
if is_unconstrained() {
assert(
unsafe {
//@safety: already unconstrained
field_less_than(b, a)
},
);
} else {
// Decompose a and b
let a_limbs = decompose(a);
let b_limbs = decompose(b);
// Assert that a_limbs is greater than b_limbs
assert_gt_limbs(a_limbs, b_limbs)
}
}
pub fn assert_lt(a: Field, b: Field) {
assert_gt(b, a);
}
pub fn gt(a: Field, b: Field) -> bool {
if is_unconstrained() {
unsafe {
//@safety: unsafe in unconstrained
field_less_than(b, a)
}
} else if a == b {
false
} else {
unsafe {
//@safety: Take a hint of the comparison and verify it
if field_less_than(a, b) {
assert_gt(b, a);
false
} else {
assert_gt(a, b);
true
}
}
}
}
pub fn lt(a: Field, b: Field) -> bool {
gt(b, a)
}
mod tests {
// TODO: Allow imports from "super"
use crate::field::bn254::{assert_gt, decompose, gt, lte_hint, PHI, PLO, TWO_POW_128};
#[test]
fn check_decompose() {
assert_eq(decompose(TWO_POW_128), (0, 1));
assert_eq(decompose(TWO_POW_128 + 0x1234567890), (0x1234567890, 1));
assert_eq(decompose(0x1234567890), (0x1234567890, 0));
}
#[test]
unconstrained fn check_decompose_unconstrained() {
assert_eq(decompose(TWO_POW_128), (0, 1));
assert_eq(decompose(TWO_POW_128 + 0x1234567890), (0x1234567890, 1));
assert_eq(decompose(0x1234567890), (0x1234567890, 0));
}
#[test]
unconstrained fn check_lte_hint() {
assert(lte_hint(0, 1));
assert(lte_hint(0, 0x100));
assert(lte_hint(0x100, TWO_POW_128 - 1));
assert(!lte_hint(0 - 1, 0));
assert(lte_hint(0, 0));
assert(lte_hint(0x100, 0x100));
assert(lte_hint(0 - 1, 0 - 1));
}
#[test]
fn check_assert_gt() {
assert_gt(1, 0);
assert_gt(0x100, 0);
assert_gt((0 - 1), (0 - 2));
assert_gt(TWO_POW_128, 0);
assert_gt(0 - 1, 0);
}
#[test]
unconstrained fn check_assert_gt_unconstrained() {
assert_gt(1, 0);
assert_gt(0x100, 0);
assert_gt((0 - 1), (0 - 2));
assert_gt(TWO_POW_128, 0);
assert_gt(0 - 1, 0);
}
#[test]
fn check_gt() {
assert(gt(1, 0));
assert(gt(0x100, 0));
assert(gt((0 - 1), (0 - 2)));
assert(gt(TWO_POW_128, 0));
assert(!gt(0, 0));
assert(!gt(0, 0x100));
assert(gt(0 - 1, 0 - 2));
assert(!gt(0 - 2, 0 - 1));
}
#[test]
unconstrained fn check_gt_unconstrained() {
assert(gt(1, 0));
assert(gt(0x100, 0));
assert(gt((0 - 1), (0 - 2)));
assert(gt(TWO_POW_128, 0));
assert(!gt(0, 0));
assert(!gt(0, 0x100));
assert(gt(0 - 1, 0 - 2));
assert(!gt(0 - 2, 0 - 1));
}
#[test]
fn check_plo_phi() {
assert_eq(PLO + PHI * TWO_POW_128, 0);
let p_bytes = crate::field::modulus_le_bytes();
let mut p_low: Field = 0;
let mut p_high: Field = 0;
let mut offset = 1;
for i in 0..16 {
p_low += (p_bytes[i] as Field) * offset;
p_high += (p_bytes[i + 16] as Field) * offset;
offset *= 256;
}
assert_eq(p_low, PLO);
assert_eq(p_high, PHI);
}
}