Skip to content

Commit ec3fe2f

Browse files
committed
internal mod
1 parent 87aa0cd commit ec3fe2f

File tree

2 files changed

+84
-82
lines changed

2 files changed

+84
-82
lines changed

src/bignum.nr

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -124,100 +124,100 @@ pub comptime fn derive_bignum(
124124

125125
fn derive_from_seed<let SeedBytes: u32>(seed: [u8; SeedBytes]) -> Self {
126126
let params = Self::params();
127-
$typ::from_limbs($crate::derive_from_seed::<$N, $MOD_BITS, SeedBytes>(params, seed))
127+
$typ::from_limbs($crate::internal::derive_from_seed::<$N, $MOD_BITS, SeedBytes>(params, seed))
128128
}
129129

130130
unconstrained fn __derive_from_seed<let SeedBytes: u32>(seed: [u8; SeedBytes]) -> Self {
131131
let params = Self::params();
132-
Self { limbs: $crate::__derive_from_seed::<$N, $MOD_BITS, SeedBytes>(params, seed) }
132+
Self { limbs: $crate::internal::__derive_from_seed::<$N, $MOD_BITS, SeedBytes>(params, seed) }
133133
}
134134

135135
fn from_be_bytes(x: [u8; ($MOD_BITS + 7) / 8]) -> Self {
136-
Self { limbs: $crate::from_be_bytes::<$N, $MOD_BITS>(x) }
136+
Self { limbs: $crate::internal::from_be_bytes::<$N, $MOD_BITS>(x) }
137137
}
138-
138+
139139
fn to_be_bytes(self) -> [u8; ($MOD_BITS + 7) / 8] {
140-
$crate::to_be_bytes::<$N, $MOD_BITS>(self.limbs)
140+
$crate::internal::to_be_bytes::<$N, $MOD_BITS>(self.limbs)
141141
}
142142

143143
fn from_le_bytes(x: [u8; ($MOD_BITS + 7) / 8]) -> Self {
144-
Self { limbs: $crate::from_le_bytes::<$N, $MOD_BITS>(x) }
144+
Self { limbs: $crate::internal::from_le_bytes::<$N, $MOD_BITS>(x) }
145145
}
146146

147147
fn to_le_bytes(self) -> [u8; ($MOD_BITS + 7) / 8] {
148-
$crate::to_le_bytes::<$N, $MOD_BITS>(self.limbs)
148+
$crate::internal::to_le_bytes::<$N, $MOD_BITS>(self.limbs)
149149
}
150150

151151
unconstrained fn __eq(self: Self, other: Self) -> bool {
152-
$crate::__eq(self.get_limbs(), other.get_limbs())
152+
$crate::internal::__eq(self.get_limbs(), other.get_limbs())
153153
}
154154

155155
unconstrained fn __is_zero(self: Self) -> bool {
156-
$crate::__is_zero(self.get_limbs())
156+
$crate::internal::__is_zero(self.get_limbs())
157157
}
158158

159159
unconstrained fn __neg(self: Self) -> Self {
160160
let params = Self::params();
161-
Self {limbs: $crate::__neg(params.modulus, self.get_limbs())}
161+
Self {limbs: $crate::internal::__neg(params.modulus, self.get_limbs())}
162162
}
163163

164164
unconstrained fn __add(self: Self, other: Self) -> Self {
165165
let params = Self::params();
166-
Self {limbs: $crate::__add(params.modulus, self.get_limbs(), other.get_limbs())}
166+
Self {limbs: $crate::internal::__add(params.modulus, self.get_limbs(), other.get_limbs())}
167167
}
168168

169169
unconstrained fn __sub(self: Self, other: Self) -> Self {
170170
let params = Self::params();
171-
Self {limbs: $crate::__sub(params.modulus, self.get_limbs(), other.get_limbs())}
171+
Self {limbs: $crate::internal::__sub(params.modulus, self.get_limbs(), other.get_limbs())}
172172
}
173173

174174
unconstrained fn __mul(self: Self, other: Self) -> Self {
175175
let params = Self::params();
176-
Self {limbs: $crate::__mul(params, self.get_limbs(), other.get_limbs())}
176+
Self {limbs: $crate::internal::__mul(params, self.get_limbs(), other.get_limbs())}
177177
}
178178

179179
unconstrained fn __sqr(self: Self) -> Self {
180180
let params = Self::params();
181-
Self {limbs: $crate::__sqr(params, self.get_limbs()) }
181+
Self {limbs: $crate::internal::__sqr(params, self.get_limbs()) }
182182
}
183183

184184
unconstrained fn __div(self: Self, divisor: Self) -> Self {
185185
let params = Self::params();
186-
Self {limbs: $crate::__div(params, self.get_limbs(), divisor.get_limbs())}
186+
Self {limbs: $crate::internal::__div(params, self.get_limbs(), divisor.get_limbs())}
187187
}
188188

189189
unconstrained fn __udiv_mod(self: Self, divisor: Self) -> (Self, Self) {
190-
let (q, r) = $crate::__udiv_mod(self.get_limbs(), divisor.get_limbs());
190+
let (q, r) = $crate::internal::__udiv_mod(self.get_limbs(), divisor.get_limbs());
191191
(Self{limbs: q}, Self{limbs: r})
192192
}
193193

194194
unconstrained fn __invmod(self: Self) -> Self {
195195
let params = Self::params();
196196
assert(params.has_multiplicative_inverse);
197-
Self {limbs: $crate::__invmod(params, self.get_limbs())}
197+
Self {limbs: $crate::internal::__invmod(params, self.get_limbs())}
198198
}
199199

200200
unconstrained fn __pow(self: Self, exponent: Self) -> Self {
201201
let params = Self::params();
202-
Self {limbs: $crate::__pow(params, self.get_limbs(), exponent.get_limbs())}
202+
Self {limbs: $crate::internal::__pow(params, self.get_limbs(), exponent.get_limbs())}
203203
}
204204

205205
#[deprecated("use __sqrt")]
206206
unconstrained fn __tonelli_shanks_sqrt(self: Self) -> std::option::Option<Self> {
207207
let params = Self::params();
208-
let maybe_limbs: Option<[u128; $N]> = $crate::__sqrt(params, self.get_limbs());
208+
let maybe_limbs: Option<[u128; $N]> = $crate::internal::__sqrt(params, self.get_limbs());
209209
maybe_limbs.map(|limbs| Self {limbs: limbs})
210210
}
211211

212212
unconstrained fn __sqrt(self: Self) -> std::option::Option<Self> {
213213
let params = Self::params();
214-
let maybe_limbs: Option<[u128; $N]> = $crate::__sqrt(params, self.get_limbs());
214+
let maybe_limbs: Option<[u128; $N]> = $crate::internal::__sqrt(params, self.get_limbs());
215215
maybe_limbs.map(|limbs| Self {limbs: limbs })
216216
}
217217

218218
fn assert_is_not_equal(self: Self, other: Self) {
219219
let params = Self::params();
220-
$crate::assert_is_not_equal(
220+
$crate::internal::assert_is_not_equal(
221221
params,
222222
self.get_limbs(),
223223
other.get_limbs(),
@@ -226,47 +226,47 @@ pub comptime fn derive_bignum(
226226

227227
fn validate_in_field(self: Self) {
228228
let params = Self::params();
229-
$crate::validate_in_field::<$N, $MOD_BITS>(params, self.get_limbs());
229+
$crate::internal::validate_in_field::<$N, $MOD_BITS>(params, self.get_limbs());
230230
}
231231

232232
fn validate_in_range(self: Self) {
233-
$crate::validate_in_range::<u128, $N, $MOD_BITS>(self.get_limbs());
233+
$crate::internal::validate_in_range::<u128, $N, $MOD_BITS>(self.get_limbs());
234234
}
235235

236236
fn sqr(self: Self) -> Self {
237237
let params = Self::params();
238-
Self { limbs: $crate::sqr::<$N, $MOD_BITS>(params, self.get_limbs()) }
238+
Self { limbs: $crate::internal::sqr::<$N, $MOD_BITS>(params, self.get_limbs()) }
239239
}
240240

241241
fn udiv_mod(self: Self, divisor: Self) -> (Self, Self) {
242-
let (q, r) = $crate::udiv_mod::<$N, $MOD_BITS>(self.get_limbs(), divisor.get_limbs());
242+
let (q, r) = $crate::internal::udiv_mod::<$N, $MOD_BITS>(self.get_limbs(), divisor.get_limbs());
243243
(Self {limbs: q}, Self {limbs: r})
244244
}
245245

246246
fn udiv(self: Self, divisor: Self) -> Self {
247-
Self {limbs: $crate::udiv::<$N, $MOD_BITS>(self.get_limbs(), divisor.get_limbs())}
247+
Self {limbs: $crate::internal::udiv::<$N, $MOD_BITS>(self.get_limbs(), divisor.get_limbs())}
248248
}
249249

250250
fn umod(self: Self, divisor: Self) -> Self {
251-
Self {limbs: $crate::umod::<$N, $MOD_BITS>(self.get_limbs(), divisor.get_limbs())}
251+
Self {limbs: $crate::internal::umod::<$N, $MOD_BITS>(self.get_limbs(), divisor.get_limbs())}
252252
}
253253

254254
fn is_zero(self: Self) -> bool {
255255
let params = Self::params();
256-
$crate::is_zero::<$N, $MOD_BITS>(params, self.get_limbs())
256+
$crate::internal::is_zero::<$N, $MOD_BITS>(params, self.get_limbs())
257257
}
258258

259259
fn is_zero_integer(self: Self) -> bool {
260-
$crate::is_zero_integer(self.get_limbs())
260+
$crate::internal::is_zero_integer(self.get_limbs())
261261
}
262262

263263
fn assert_is_not_zero(self: Self) {
264264
let params = Self::params();
265-
$crate::assert_is_not_zero::<$N, $MOD_BITS>(params, self.get_limbs());
265+
$crate::internal::assert_is_not_zero::<$N, $MOD_BITS>(params, self.get_limbs());
266266
}
267267

268268
fn assert_is_not_zero_integer(self: Self) {
269-
$crate::assert_is_not_zero_integer(self.get_limbs());
269+
$crate::internal::assert_is_not_zero_integer(self.get_limbs());
270270
}
271271
}
272272

@@ -279,49 +279,49 @@ pub comptime fn derive_bignum(
279279

280280
impl std::convert::From<Field> for $typ {
281281
fn from(input: Field) -> Self {
282-
$typ { limbs: $crate::from_field::<$N, $MOD_BITS>($params, input) }
282+
$typ { limbs: $crate::internal::from_field::<$N, $MOD_BITS>($params, input) }
283283
}
284284
}
285285

286286
impl std::ops::Neg for $typ {
287287
fn neg(self) -> Self {
288-
$typ { limbs: $crate::neg::<$N, $MOD_BITS>($params, self.limbs) }
288+
$typ { limbs: $crate::internal::neg::<$N, $MOD_BITS>($params, self.limbs) }
289289
}
290290
}
291291

292292
impl std::ops::Add for $typ {
293293
fn add(self, other: Self) -> Self {
294-
$typ { limbs: $crate::add::<$N, $MOD_BITS>($params, self.limbs, other.limbs) }
294+
$typ { limbs: $crate::internal::add::<$N, $MOD_BITS>($params, self.limbs, other.limbs) }
295295
}
296296
}
297297

298298
impl std::ops::Sub for $typ {
299299
fn sub(self, other: Self) -> Self {
300-
$typ { limbs: $crate::sub::<$N, $MOD_BITS>($params, self.limbs, other.limbs) }
300+
$typ { limbs: $crate::internal::sub::<$N, $MOD_BITS>($params, self.limbs, other.limbs) }
301301
}
302302
}
303303

304304
impl std::ops::Mul for $typ {
305305
fn mul(self, other: Self) -> Self {
306-
$typ { limbs: $crate::mul::<$N, $MOD_BITS>($params, self.limbs, other.limbs) }
306+
$typ { limbs: $crate::internal::mul::<$N, $MOD_BITS>($params, self.limbs, other.limbs) }
307307
}
308308
}
309309

310310
impl std::ops::Div for $typ {
311311
fn div(self, other: Self) -> Self {
312-
$typ { limbs: $crate::div::<$N, $MOD_BITS>($params, self.limbs, other.limbs) }
312+
$typ { limbs: $crate::internal::div::<$N, $MOD_BITS>($params, self.limbs, other.limbs) }
313313
}
314314
}
315315

316316
impl std::cmp::Eq for $typ {
317317
fn eq(self, other: Self) -> bool {
318-
$crate::eq::<$N, $MOD_BITS>($params, self.limbs, other.limbs)
318+
$crate::internal::eq::<$N, $MOD_BITS>($params, self.limbs, other.limbs)
319319
}
320320
}
321321

322322
impl std::cmp::Ord for $typ {
323323
fn cmp(self, other: Self) -> std::cmp::Ordering {
324-
$crate::cmp::<$N, $MOD_BITS>(self.limbs, other.limbs)
324+
$crate::internal::cmp::<$N, $MOD_BITS>(self.limbs, other.limbs)
325325
}
326326
}
327327

src/lib.nr

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,51 +16,53 @@ pub(crate) mod utils;
1616
mod benchmarks;
1717
mod tests;
1818

19-
// Re-export functions required by derive macros for external crates
20-
// They are not intended as public API, but must be accessible to macro-generated code
19+
// Internal functions required by derive macros for external crates
20+
// Exported through the `internal` submodule to make it clear they are not
21+
// intended for direct use by end users
22+
pub mod internal {
23+
// Constrained operations
24+
pub use crate::fns::constrained_ops::add;
25+
pub use crate::fns::constrained_ops::assert_is_not_equal;
26+
pub use crate::fns::constrained_ops::assert_is_not_zero;
27+
pub use crate::fns::constrained_ops::assert_is_not_zero_integer;
28+
pub use crate::fns::constrained_ops::cmp;
29+
pub use crate::fns::constrained_ops::derive_from_seed;
30+
pub use crate::fns::constrained_ops::div;
31+
pub use crate::fns::constrained_ops::eq;
32+
pub use crate::fns::constrained_ops::from_field;
33+
pub use crate::fns::constrained_ops::is_zero;
34+
pub use crate::fns::constrained_ops::is_zero_integer;
35+
pub use crate::fns::constrained_ops::mul;
36+
pub use crate::fns::constrained_ops::neg;
37+
pub use crate::fns::constrained_ops::sqr;
38+
pub use crate::fns::constrained_ops::sub;
39+
pub use crate::fns::constrained_ops::udiv;
40+
pub use crate::fns::constrained_ops::udiv_mod;
41+
pub use crate::fns::constrained_ops::umod;
42+
pub use crate::fns::constrained_ops::validate_in_field;
43+
pub use crate::fns::constrained_ops::validate_in_range;
2144

22-
// Constrained operations
23-
pub use fns::constrained_ops::add;
24-
pub use fns::constrained_ops::assert_is_not_equal;
25-
pub use fns::constrained_ops::assert_is_not_zero;
26-
pub use fns::constrained_ops::assert_is_not_zero_integer;
27-
pub use fns::constrained_ops::cmp;
28-
pub use fns::constrained_ops::derive_from_seed;
29-
pub use fns::constrained_ops::div;
30-
pub use fns::constrained_ops::eq;
31-
pub use fns::constrained_ops::from_field;
32-
pub use fns::constrained_ops::is_zero;
33-
pub use fns::constrained_ops::is_zero_integer;
34-
pub use fns::constrained_ops::mul;
35-
pub use fns::constrained_ops::neg;
36-
pub use fns::constrained_ops::sqr;
37-
pub use fns::constrained_ops::sub;
38-
pub use fns::constrained_ops::udiv;
39-
pub use fns::constrained_ops::udiv_mod;
40-
pub use fns::constrained_ops::umod;
41-
pub use fns::constrained_ops::validate_in_field;
42-
pub use fns::constrained_ops::validate_in_range;
45+
// Unconstrained operations
46+
pub use crate::fns::unconstrained_ops::__add;
47+
pub use crate::fns::unconstrained_ops::__derive_from_seed;
48+
pub use crate::fns::unconstrained_ops::__div;
49+
pub use crate::fns::unconstrained_ops::__eq;
50+
pub use crate::fns::unconstrained_ops::__invmod;
51+
pub use crate::fns::unconstrained_ops::__is_zero;
52+
pub use crate::fns::unconstrained_ops::__mul;
53+
pub use crate::fns::unconstrained_ops::__neg;
54+
pub use crate::fns::unconstrained_ops::__pow;
55+
pub use crate::fns::unconstrained_ops::__sqr;
56+
pub use crate::fns::unconstrained_ops::__sqrt;
57+
pub use crate::fns::unconstrained_ops::__sub;
58+
pub use crate::fns::unconstrained_ops::__udiv_mod;
4359

44-
// Unconstrained operations
45-
pub use fns::unconstrained_ops::__add;
46-
pub use fns::unconstrained_ops::__derive_from_seed;
47-
pub use fns::unconstrained_ops::__div;
48-
pub use fns::unconstrained_ops::__eq;
49-
pub use fns::unconstrained_ops::__invmod;
50-
pub use fns::unconstrained_ops::__is_zero;
51-
pub use fns::unconstrained_ops::__mul;
52-
pub use fns::unconstrained_ops::__neg;
53-
pub use fns::unconstrained_ops::__pow;
54-
pub use fns::unconstrained_ops::__sqr;
55-
pub use fns::unconstrained_ops::__sqrt;
56-
pub use fns::unconstrained_ops::__sub;
57-
pub use fns::unconstrained_ops::__udiv_mod;
58-
59-
// Serialization functions
60-
pub use fns::serialization::from_be_bytes;
61-
pub use fns::serialization::from_le_bytes;
62-
pub use fns::serialization::to_be_bytes;
63-
pub use fns::serialization::to_le_bytes;
60+
// Serialization functions
61+
pub use crate::fns::serialization::from_be_bytes;
62+
pub use crate::fns::serialization::from_le_bytes;
63+
pub use crate::fns::serialization::to_be_bytes;
64+
pub use crate::fns::serialization::to_le_bytes;
65+
}
6466

6567
// Re-export the main structs so that users don't have to specify the paths
6668
pub use bignum::BigNum;

0 commit comments

Comments
 (0)