Skip to content

Commit a8a3e4e

Browse files
authored
Merge pull request #1 from 0xjei/main
add `Enclave` parameter sets
2 parents 56a096e + 860c7c1 commit a8a3e4e

File tree

10 files changed

+95
-3
lines changed

10 files changed

+95
-3
lines changed

src/fields/enclave_bfv_512.nr

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use crate::bignum::BigNum;
2+
use crate::bignum::derive_bignum;
3+
use crate::params::BigNumParams;
4+
5+
/// BFV parameters set corresponding to INSECURE_SET_512_10_1.
6+
pub global Enclave_BFV_512_PARAMS: BigNumParams<1, 51> = BigNumParams {
7+
has_multiplicative_inverse: true,
8+
modulus: [0x07fffffffe0001],
9+
double_modulus: [0x0100000000000000000ffffffffc0001],
10+
redc_param: [0x800000001ffff0],
11+
};
12+
13+
#[derive_bignum(1, 51, quote { Enclave_BFV_512_PARAMS })]
14+
pub struct Enclave_BFV_512 {
15+
limbs: [u128; 1],
16+
}

src/fields/enclave_bfv_8192.nr

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use crate::bignum::BigNum;
2+
use crate::bignum::derive_bignum;
3+
use crate::params::BigNumParams;
4+
5+
/// BFV parameters set corresponding to SET_8192_100_4.
6+
pub global Enclave_BFV_8192_PARAMS: BigNumParams<1, 113> = BigNumParams {
7+
has_multiplicative_inverse: false,
8+
modulus: [0x010000000418000203d8ac04180001],
9+
double_modulus: [0x01020000000830000407b15808300001],
10+
redc_param: [0x3ffffffef9ffff833a650308b8b343],
11+
};
12+
13+
#[derive_bignum(1, 113, quote { Enclave_BFV_8192_PARAMS })]
14+
pub struct Enclave_BFV_8192 {
15+
limbs: [u128; 1],
16+
}

src/fields/enclave_trbfv_512.nr

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use crate::bignum::BigNum;
2+
use crate::bignum::derive_bignum;
3+
use crate::params::BigNumParams;
4+
5+
/// TRBFV parameters set corresponding to INSECURE_SET_512_10_1.
6+
pub global Enclave_TRBFV_512_PARAMS: BigNumParams<1, 72> = BigNumParams {
7+
has_multiplicative_inverse: false,
8+
modulus: [0xffffb2002437fb2001],
9+
double_modulus: [0x01000000000001ffff6400486ff64001],
10+
redc_param: [0x100004dfff38bf60b973],
11+
};
12+
13+
#[derive_bignum(1, 72, quote { Enclave_TRBFV_512_PARAMS })]
14+
pub struct Enclave_TRBFV_512 {
15+
limbs: [u128; 1],
16+
}

src/fields/enclave_trbfv_8192.nr

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use crate::bignum::BigNum;
2+
use crate::bignum::derive_bignum;
3+
use crate::params::BigNumParams;
4+
5+
/// TRBFV parameters set corresponding to SET_8192_100_4.
6+
pub global Enclave_TRBFV_8192_PARAMS: BigNumParams<2, 208> = BigNumParams {
7+
has_multiplicative_inverse: false,
8+
modulus: [0xfe38ce2cf15ff03368791001340001, 0x8000000db0002869ba02ea],
9+
double_modulus: [0x01fc719c59e2bfe066d0f22002680002, 0x010000001b600050d37405d4],
10+
redc_param: [0x447d587f4015acebd23b19ddd6f6c5, 0x1ffffffc93fff6433e016463],
11+
};
12+
13+
#[derive_bignum(2, 208, quote { Enclave_TRBFV_8192_PARAMS })]
14+
pub struct Enclave_TRBFV_8192 {
15+
limbs: [u128; 2],
16+
}

src/fields/mod.nr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ pub mod U1024;
2727
pub mod U2048;
2828
pub mod U4096;
2929
pub mod U8192;
30+
pub mod enclave_bfv_512;
31+
pub mod enclave_bfv_8192;
32+
pub mod enclave_trbfv_512;
33+
pub mod enclave_trbfv_8192;

src/fns/constrained_ops.nr

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub(crate) fn limbs_to_field<let N: u32, let MOD_BITS: u32>(
5151
limbs[0] as Field
5252
} else if N == 2 {
5353
validate_in_range::<_, N, MOD_BITS>(limbs);
54-
(limbs[0] + limbs[1] * TWO_POW_120) as Field
54+
limbs[0] as Field + limbs[1] as Field * TWO_POW_120 as Field
5555
} else {
5656
// validate_in_range::<N, 254>(limbs);
5757
(
@@ -332,6 +332,7 @@ pub(crate) fn validate_gt<let N: u32, let MOD_BITS: u32>(lhs: [u128; N], rhs: [u
332332
// so we do... p - x - r = 0 and there might be borrow flags
333333
// a - b = r
334334
// p + a - b - r = 0
335+
// Safety: fix
335336
let (underflow, result, borrow_flags) = unsafe { __validate_gt_with_flags(lhs, rhs) };
336337
validate_in_range::<_, _, MOD_BITS>(result);
337338
assert(!underflow, "BigNum::validate_gt check fails");
@@ -376,6 +377,7 @@ pub(crate) fn validate_in_field<let N: u32, let MOD_BITS: u32>(
376377
for i in 0..N {
377378
p_minus_self[i] = (modulus[i] as Field - val[i] as Field);
378379
}
380+
// Safety: fix
379381
let borrow_flags = unsafe { __validate_in_field_compute_borrow_flags(params, val) };
380382
p_minus_self[0] += (borrow_flags[0] as Field * TWO_POW_120 as Field);
381383
for i in 1..N - 1 {
@@ -388,6 +390,7 @@ pub(crate) fn validate_in_field<let N: u32, let MOD_BITS: u32>(
388390

389391
// a comparison function. returns true if lhs > rhs and false otherwise
390392
pub(crate) fn cmp<let N: u32, let MOD_BITS: u32>(lhs: [u128; N], rhs: [u128; N]) -> Ordering {
393+
// Safety: fix
391394
let (underflow, result, borrow_flags) = unsafe { __validate_gt_with_flags(lhs, rhs) };
392395
// if underflow is true, swap lhs and rhs
393396
let (lhs, rhs) = if underflow { (rhs, lhs) } else { (lhs, rhs) };
@@ -412,7 +415,7 @@ pub(crate) fn neg<let N: u32, let MOD_BITS: u32>(
412415
__neg(params.modulus, val)
413416
}
414417
} else {
415-
// so we do... p - x - r = 0 and there might be borrow flags
418+
// Safety: fix
416419
let (result, borrow_flags) = unsafe { __neg_with_flags(params.modulus, val) };
417420
validate_in_range::<_, _, MOD_BITS>(result);
418421
let modulus = params.modulus;
@@ -443,7 +446,7 @@ pub(crate) fn add<let N: u32, let MOD_BITS: u32>(
443446
__add(params.modulus, lhs, rhs)
444447
}
445448
} else {
446-
// so we do... p - x - r = 0 and there might be borrow flags
449+
// Safety: fix
447450
let (result, carry_flags, borrow_flags, overflow_modulus) =
448451
unsafe { __add_with_flags(params.modulus, lhs, rhs) };
449452
validate_in_range::<_, _, MOD_BITS>(result);
@@ -493,6 +496,7 @@ pub(crate) fn sub<let N: u32, let MOD_BITS: u32>(
493496
// so we do... p - x - r = 0 and there might be borrow flags
494497
// a - b = r
495498
// p + a - b - r = 0
499+
// Safety: fix
496500
let (result, carry_flags, borrow_flags, underflow) =
497501
unsafe { __sub_with_flags(params.modulus, lhs, rhs) };
498502

@@ -541,6 +545,7 @@ pub(crate) fn mul<let N: u32, let MOD_BITS: u32>(
541545
lhs: [u128; N],
542546
rhs: [u128; N],
543547
) -> [u128; N] {
548+
// Safety: fix
544549
let result = unsafe { __mul::<_, MOD_BITS>(params, lhs, rhs) };
545550
if !std::runtime::is_unconstrained() {
546551
evaluate_quadratic_expression(
@@ -566,6 +571,7 @@ pub(crate) fn div<let N: u32, let MOD_BITS: u32>(
566571
params.has_multiplicative_inverse,
567572
"BigNum has no multiplicative inverse. Use udiv for unsigned integer division",
568573
);
574+
// Safety: fix
569575
let result = unsafe { __div::<_, MOD_BITS>(params, lhs, rhs) };
570576
if !std::runtime::is_unconstrained() {
571577
evaluate_quadratic_expression(
@@ -593,6 +599,7 @@ pub(crate) fn udiv_mod<let N: u32, let MOD_BITS: u32>(
593599
numerator: [u128; N],
594600
divisor: [u128; N],
595601
) -> ([u128; N], [u128; N]) {
602+
// Safety: fix
596603
let (quotient, remainder) = unsafe { __udiv_mod(numerator, divisor) };
597604
if !std::runtime::is_unconstrained() {
598605
// self / divisor = quotient rounded

src/fns/expressions.nr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ pub(crate) fn evaluate_quadratic_expression<let N: u32, let MOD_BITS: u32, let L
260260
linear_flags: [bool; ADD_N],
261261
) {
262262
// use an unconstrained function to compute the value of the quotient
263+
// Safety: fix
263264
let (quotient, borrow_flags): ([u128; N], [bool; 2 * N - 2]) = unsafe {
264265
__compute_quadratic_expression_with_borrow_flags::<_, MOD_BITS, _, _, _, _>(
265266
params,

src/lib.nr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ pub use fields::bls12_381Fr::BLS12_381_Fr;
2929
pub use fields::bn254Fq::BN254_Fq;
3030
pub use fields::ed25519Fq::ED25519_Fq;
3131
pub use fields::ed25519Fr::ED25519_Fr;
32+
pub use fields::enclave_bfv_512::Enclave_BFV_512;
33+
pub use fields::enclave_bfv_8192::Enclave_BFV_8192;
34+
pub use fields::enclave_trbfv_512::Enclave_TRBFV_512;
35+
pub use fields::enclave_trbfv_8192::Enclave_TRBFV_8192;
3236
pub use fields::mnt4_753Fq::MNT4_753_Fq;
3337
pub use fields::mnt4_753Fr::MNT4_753_Fr;
3438
pub use fields::mnt6_753Fq::MNT6_753_Fq;

src/runtime_bignum.nr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ impl<let N: u32, let MOD_BITS: u32> RuntimeBigNum<N, MOD_BITS> {
116116
// UNCONSTRAINED! (Hence `__` prefix).
117117
pub fn __neg(self) -> Self {
118118
let params = self.params;
119+
// Safety: fix
119120
let limbs = unsafe { __neg(params.modulus, self.limbs) };
120121
Self { params, limbs }
121122
}
@@ -124,6 +125,7 @@ impl<let N: u32, let MOD_BITS: u32> RuntimeBigNum<N, MOD_BITS> {
124125
pub fn __add(self, other: Self) -> Self {
125126
let params = self.params;
126127
assert(params == other.params);
128+
// Safety: fix
127129
let limbs = unsafe { __add(params.modulus, self.limbs, other.limbs) };
128130
Self { params, limbs }
129131
}
@@ -132,6 +134,7 @@ impl<let N: u32, let MOD_BITS: u32> RuntimeBigNum<N, MOD_BITS> {
132134
pub fn __sub(self, other: Self) -> Self {
133135
let params = self.params;
134136
assert(params == other.params);
137+
// Safety: fix
135138
let limbs = unsafe { __sub(params.modulus, self.limbs, other.limbs) };
136139
Self { params, limbs }
137140
}
@@ -140,6 +143,7 @@ impl<let N: u32, let MOD_BITS: u32> RuntimeBigNum<N, MOD_BITS> {
140143
pub fn __mul(self, other: Self) -> Self {
141144
let params = self.params;
142145
assert(params == other.params);
146+
// Safety: fix
143147
let limbs = unsafe { __mul::<_, MOD_BITS>(params, self.limbs, other.limbs) };
144148
Self { params, limbs }
145149
}
@@ -148,6 +152,7 @@ impl<let N: u32, let MOD_BITS: u32> RuntimeBigNum<N, MOD_BITS> {
148152
pub fn __div(self, divisor: Self) -> Self {
149153
let params = self.params;
150154
assert(params == divisor.params);
155+
// Safety: fix
151156
let limbs = unsafe { __div::<_, MOD_BITS>(params, self.limbs, divisor.limbs) };
152157
Self { params, limbs }
153158
}
@@ -156,6 +161,7 @@ impl<let N: u32, let MOD_BITS: u32> RuntimeBigNum<N, MOD_BITS> {
156161
pub fn __udiv_mod(self, divisor: Self) -> (Self, Self) {
157162
let params = self.params;
158163
assert(params == divisor.params);
164+
// Safety: fix
159165
let (q, r) = unsafe { __udiv_mod(self.limbs, divisor.limbs) };
160166
(Self { limbs: q, params }, Self { limbs: r, params })
161167
}
@@ -164,6 +170,7 @@ impl<let N: u32, let MOD_BITS: u32> RuntimeBigNum<N, MOD_BITS> {
164170
pub fn __invmod(self) -> Self {
165171
let params = self.params;
166172
assert(params.has_multiplicative_inverse);
173+
// Safety: fix
167174
let limbs = unsafe { __invmod::<_, MOD_BITS>(params, self.limbs) };
168175
Self { limbs, params }
169176
}
@@ -172,13 +179,15 @@ impl<let N: u32, let MOD_BITS: u32> RuntimeBigNum<N, MOD_BITS> {
172179
pub fn __pow(self, exponent: Self) -> Self {
173180
let params = self.params;
174181
assert(params == exponent.params);
182+
// Safety: fix
175183
let limbs = unsafe { __pow::<_, MOD_BITS>(params, self.limbs, exponent.limbs) };
176184
Self { limbs, params }
177185
}
178186

179187
// UNCONSTRAINED! (Hence `__` prefix).
180188
pub fn __tonelli_shanks_sqrt(self) -> std::option::Option<Self> {
181189
let params = self.params;
190+
// Safety: fix
182191
let maybe_limbs = unsafe { __tonelli_shanks_sqrt(params, self.limbs) };
183192
maybe_limbs.map(|limbs| Self { limbs, params })
184193
}
@@ -295,6 +304,7 @@ pub fn __compute_quadratic_expression<let N: u32, let MOD_BITS: u32, let LHS_N:
295304
linear_terms: [RuntimeBigNum<N, MOD_BITS>; ADD_N],
296305
linear_flags: [bool; ADD_N],
297306
) -> (RuntimeBigNum<N, MOD_BITS>, RuntimeBigNum<N, MOD_BITS>) {
307+
// Safety: fix
298308
let (q_limbs, r_limbs) = unsafe {
299309
crate::fns::expressions::__compute_quadratic_expression::<_, MOD_BITS, _, _, _, _>(
300310
params,
@@ -335,6 +345,7 @@ pub fn __batch_invert<let N: u32, let MOD_BITS: u32, let M: u32>(
335345
) -> [RuntimeBigNum<N, MOD_BITS>; M] {
336346
let params = x[0].params;
337347
assert(params.has_multiplicative_inverse);
348+
// Safety: fix
338349
let all_limbs = unsafe {
339350
crate::fns::unconstrained_ops::batch_invert::<_, MOD_BITS, _>(
340351
params,

src/tests/bignum_test.nr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,7 @@ fn test_SecP224r1_mul_regression() {
10471047
let mut x: SecP224r1 =
10481048
SecP224r1 { limbs: [0x03c1d356c21122343280d6115c1d21, 0xb70e0cbd6bb4bf7f321390b94a] };
10491049
let res = x * x;
1050+
// Safety: fix
10501051
let expected = unsafe { SecP224r1::__mul(x, x) };
10511052
res.validate_in_field();
10521053
expected.validate_in_field();

0 commit comments

Comments
 (0)