Skip to content

Commit 90acda8

Browse files
authored
Enable rustfmt in CI (BitVM#252)
* enable rustfmt in CI * remove ignore in .rustfmt.toml * cargo fmt * remove the condition of draft * run rustfmt before build and guest
1 parent 0c4d463 commit 90acda8

File tree

121 files changed

+4548
-2406
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+4548
-2406
lines changed

.github/workflows/CI.yml

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ env:
1111
CARGO_TERM_COLOR: always
1212

1313
jobs:
14+
rustfmt:
15+
timeout-minutes: 60
16+
runs-on: self-hosted
17+
steps:
18+
- uses: actions/checkout@v4
19+
- name: Run rustfmt
20+
run: cargo fmt --all -- --check
21+
1422
clippy:
1523
if: github.event.pull_request.draft == false
1624
timeout-minutes: 60
@@ -22,6 +30,7 @@ jobs:
2230

2331
guest:
2432
if: github.event.pull_request.draft == false
33+
needs: rustfmt
2534
timeout-minutes: 60
2635
runs-on: self-hosted
2736
steps:
@@ -33,6 +42,7 @@ jobs:
3342
3443
build:
3544
if: github.event.pull_request.draft == false
45+
needs: rustfmt
3646
timeout-minutes: 60
3747
runs-on: self-hosted
3848
steps:

.rustfmt.toml

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
fn_single_line = true
2-
ignore = [
3-
"src/utils/merkle.rs",
4-
]
52

63

bitvm/src/bigint/add.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
6464
}
6565

6666
/// Double the BigInt on top of the stack
67-
///
67+
///
6868
/// # Note
6969
///
7070
/// This function allows overflow of the underlying integer types during
@@ -106,7 +106,7 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
106106

107107
// When we got {limb} {base} {carry} on the stack, we drop the base
108108
OP_NIP // {limb} {carry}
109-
{ n + 9 } OP_PICK { limb_double_with_carry_allow_overflow(Self::HEAD_OFFSET) }
109+
{ n + 9 } OP_PICK { limb_double_with_carry_allow_overflow(Self::HEAD_OFFSET) }
110110

111111
// Take all limbs from the alt stack to the main stack
112112
for _ in 0..Self::N_LIMBS - 1 {
@@ -116,7 +116,7 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
116116
}
117117

118118
/// Double the BigInt on top of the stack
119-
///
119+
///
120120
/// # Note
121121
///
122122
/// This function prevents overflow of the underlying integer types during
@@ -145,7 +145,7 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
145145
}
146146

147147
/// Left shift the BigInt on top of the stack by `bits`
148-
///
148+
///
149149
/// # Note
150150
///
151151
/// This function prevents overflow of the underlying integer types during
@@ -179,11 +179,11 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
179179
OP_OVER
180180
OP_ADD
181181

182-
// OP_DEPTH OP_1SUB OP_PICK
182+
// OP_DEPTH OP_1SUB OP_PICK
183183
{ 1 << LIMB_SIZE }
184184
OP_SWAP
185185

186-
{ limb_add_create_carry() }
186+
{ limb_add_create_carry() }
187187
OP_TOALTSTACK
188188

189189
for i in 0..Self::N_LIMBS - 2 {
@@ -205,8 +205,6 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
205205
}
206206
}
207207

208-
209-
210208
/// Add BigInt on top of the stack to a BigInt at `b` depth in the stack
211209
///
212210
/// # Note
@@ -220,11 +218,11 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
220218
{ b_depth } OP_PICK
221219
OP_ADD
222220

223-
// OP_DEPTH OP_1SUB OP_PICK
221+
// OP_DEPTH OP_1SUB OP_PICK
224222
{ 1 << LIMB_SIZE }
225223
OP_SWAP
226224

227-
{ limb_add_create_carry() }
225+
{ limb_add_create_carry() }
228226
OP_TOALTSTACK
229227

230228
for _ in 0..Self::N_LIMBS - 2 {
@@ -346,7 +344,7 @@ pub fn limb_add_nocarry(head_offset: u32) -> Script {
346344
}
347345

348346
fn limb_add_with_carry_prevent_overflow(head_offset: u32) -> Script {
349-
script!{
347+
script! {
350348
// {a} {b} {c:carry}
351349
OP_3DUP OP_ADD OP_ADD OP_NIP // {a} {b} {a+b+c}
352350
{ head_offset >> 1 } // {a} {b} {a+b+c} {x}
@@ -480,7 +478,7 @@ fn limb_lshift_with_carry_prevent_overflow(bits: u32, head: u32) -> Script {
480478
}
481479
OP_ADD
482480
} // {result_signed} | {x}
483-
481+
484482
OP_FROMALTSTACK // {result_signed} {x}
485483
OP_OVER // {result_signed} {x} {result_signed}
486484
OP_2DUP OP_SWAP // {result_signed} {x} {result_signed} {result_signed} {x}

bitvm/src/bigint/cmp.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
6969
}
7070

7171
// return if a <= b
72-
pub fn lessthanorequal(a: u32, b: u32) -> Script { Self::greaterthanorequal(b, a) }
72+
pub fn lessthanorequal(a: u32, b: u32) -> Script {
73+
Self::greaterthanorequal(b, a)
74+
}
7375

7476
// return if a > b
7577
pub fn greaterthan(a: u32, b: u32) -> Script {

bitvm/src/bigint/std.rs

+35-29
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use bitcoin::script::read_scriptint;
22
use num_bigint::BigUint;
33
use num_traits::Num;
4-
use std::str::FromStr;
54
use std::cmp::Ordering;
5+
use std::str::FromStr;
66

77
use crate::bigint::BigIntImpl;
88
use crate::pseudo::{push_to_stack, NMUL};
@@ -328,7 +328,7 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
328328
{ n_limbs_target - 1 } OP_ROLL
329329
}
330330
}
331-
},
331+
}
332332
Ordering::Less => {
333333
let n_limbs_to_remove = n_limbs_self - n_limbs_target;
334334
script! {
@@ -345,7 +345,6 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
345345
/// used as a helper function for `transform_limbsize`
346346
347347
fn get_transform_steps(source_limb_size: u32, target_limb_size: u32) -> Vec<TransformStep> {
348-
349348
//define an empty vector to store Transform steps
350349
let mut transform_steps: Vec<TransformStep> = Vec::new();
351350

@@ -398,7 +397,8 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
398397
drop_currentlimb: false,
399398
initiate_targetlimb: first_iter_flag,
400399
});
401-
limb_sizes[source_limb_last_idx] = source_limb_remaining_bits - target_limb_remaining_bits;
400+
limb_sizes[source_limb_last_idx] =
401+
source_limb_remaining_bits - target_limb_remaining_bits;
402402
target_limb_remaining_bits = 0;
403403
}
404404
}
@@ -411,10 +411,10 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
411411
}
412412

413413
/// Transform Limbsize for BigInt
414-
/// This function changes the representation of BigInt present on stack as multiple limbs of source limbsize to
415-
/// any another limbsize within 1 and 31 (inclusive).
414+
/// This function changes the representation of BigInt present on stack as multiple limbs of source limbsize to
415+
/// any another limbsize within 1 and 31 (inclusive).
416416
/// Specifically, This can be used to transform limbs into nibbles, limbs into bits ans vice-versa to aid optimizetions.
417-
///
417+
///
418418
/// ## Assumptions:
419419
/// - Does NOT do input validation.
420420
/// - The message is placed such that LSB is on top of stack. (MSB pushed first)
@@ -493,21 +493,24 @@ impl<const N_BITS: u32, const LIMB_SIZE: u32> BigIntImpl<N_BITS, LIMB_SIZE> {
493493
}
494494

495495
/// Extracts a window of bits from a u32 limb on top of stack
496-
///
496+
///
497497
/// ## Assumptions;
498498
/// Doesn't do input validation
499499
/// All the bits before start_index must be 0 for the extract to work properly
500-
///
501-
/// ## Panics:
500+
///
501+
/// ## Panics:
502502
/// - If the start_index is not between the range 1 and 31 (inclusive), fails with assertion error
503503
/// - If the window is larger than the start_index, fails with assertion error
504-
///
504+
///
505505
/// ## Stack behaviour:
506506
/// - extracts the desired window as a stack element
507507
/// - leaves the original limb with extracted bits set to zero on top of stack
508508
pub fn extract_digits(start_index: u32, window: u32) -> Script {
509509
// doesnot work if start_index is 32
510-
assert!(start_index < 32 && start_index > 0, "start_index must lie between 1 and 31 (inclusive)");
510+
assert!(
511+
start_index < 32 && start_index > 0,
512+
"start_index must lie between 1 and 31 (inclusive)"
513+
);
511514

512515
//panics if the window exceeds the number of bits on the left of start_index
513516
assert!(
@@ -539,7 +542,7 @@ mod test {
539542
use crate::bigint::std::extract_digits;
540543
use crate::bigint::{BigIntImpl, U254};
541544
use crate::run;
542-
545+
543546
use bitcoin_script::script;
544547
use rand::{Rng, SeedableRng};
545548
use rand_chacha::ChaCha20Rng;
@@ -798,10 +801,10 @@ mod test {
798801

799802
// test the extract window fn
800803
#[test]
801-
fn test_extract_window(){
804+
fn test_extract_window() {
802805
let mut prng = ChaCha20Rng::seed_from_u64(8);
803806

804-
for _ in 0..100{
807+
for _ in 0..100 {
805808
// generate random start_index and window
806809
let start_index = prng.gen_range(1..=31);
807810
let window = prng.gen_range(1..=start_index);
@@ -812,7 +815,11 @@ mod test {
812815
// compute the values by shifting
813816
let initial_limb = random_u32 >> (32u32 - start_index);
814817
let expected_window = initial_limb >> (start_index - window);
815-
let modified_limb = if start_index == window {0} else {(initial_limb << (32u32 - start_index + window)) >> (32u32 - start_index + window)};
818+
let modified_limb = if start_index == window {
819+
0
820+
} else {
821+
(initial_limb << (32u32 - start_index + window)) >> (32u32 - start_index + window)
822+
};
816823

817824
let script = script!(
818825
{initial_limb}
@@ -873,7 +880,7 @@ mod test {
873880
{i}
874881
OP_ROLL
875882
OP_EQUALVERIFY
876-
}
883+
}
877884
OP_EQUAL
878885
);
879886
let res = crate::execute_script(script.clone());
@@ -896,7 +903,7 @@ mod test {
896903
{U1773::transform_limbsize(2,3)}
897904
{U1773::transform_limbsize(3,19)}
898905
{U1773::transform_limbsize(19,23)}
899-
906+
900907
for _ in 0..77{
901908
{0b11111111111111111111111}
902909
OP_EQUALVERIFY
@@ -1019,7 +1026,7 @@ mod test {
10191026
for step in steps {
10201027
extract_windows_sum += step.extract_window;
10211028
drop_currentlimb_count += if step.drop_currentlimb { 1 } else { 0 };
1022-
initiate_targetlimb_count+= if step.initiate_targetlimb { 1 } else { 0 };
1029+
initiate_targetlimb_count += if step.initiate_targetlimb { 1 } else { 0 };
10231030
}
10241031
assert_eq!(extract_windows_sum, U256::N_BITS);
10251032
assert_eq!(drop_currentlimb_count, U256::N_BITS.div_ceil(source));
@@ -1028,22 +1035,21 @@ mod test {
10281035
}
10291036

10301037
#[test]
1031-
fn test_transform_limbsize_u256_random_vals(){
1032-
type U256 = BigIntImpl<256,29>;
1038+
fn test_transform_limbsize_u256_random_vals() {
1039+
type U256 = BigIntImpl<256, 29>;
10331040
let mut prng = ChaCha20Rng::seed_from_u64(1);
10341041

1035-
for _ in 0..100{
1036-
1042+
for _ in 0..100 {
10371043
// create a vector to store the inputs
10381044
let mut input_vals: Vec<u32> = Vec::new();
1039-
1045+
10401046
// generate random u32 for input
1041-
for i in 0..9{
1042-
let input_val:u32 = prng.gen();
1047+
for i in 0..9 {
1048+
let input_val: u32 = prng.gen();
10431049
// ensure that the initial bits are zero as needed
1044-
if i == 0{
1050+
if i == 0 {
10451051
input_vals.push(input_val >> 8);
1046-
}else{
1052+
} else {
10471053
input_vals.push(input_val >> 3);
10481054
}
10491055
}
@@ -1072,7 +1078,7 @@ mod test {
10721078
OP_TRUE
10731079
);
10741080
let res = crate::execute_script(script.clone());
1075-
assert!(res.success);
1081+
assert!(res.success);
10761082
}
10771083
}
10781084
}

bitvm/src/bn254/ell_coeffs.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Rephrased from https://github.com/arkworks-rs/algebra/blob/master/ec/src/models/bn/g2.rs#L185
22
// Cannot directly obtain G2 because of visibility
33

4-
use ark_ec::bn::g2::G2Prepared as ark_G2Prepared;
54
use ark_bn254::Config;
5+
use ark_ec::bn::g2::G2Prepared as ark_G2Prepared;
66
use ark_ec::bn::{BnConfig, TwistType};
77
use ark_ec::pairing::{MillerLoopOutput, Pairing, PairingOutput};
88
use ark_ec::short_weierstrass::Affine;
@@ -184,7 +184,7 @@ impl From<ark_bn254::G2Affine> for G2Prepared {
184184
infinity: true,
185185
}
186186
} else {
187-
Self::from_affine(q)
187+
Self::from_affine(q)
188188
}
189189
}
190190
}
@@ -233,7 +233,9 @@ impl<'a> From<&'a ark_G2Prepared<ark_bn254::Config>> for G2Prepared {
233233
}
234234

235235
impl G2Prepared {
236-
pub fn is_zero(&self) -> bool { self.infinity }
236+
pub fn is_zero(&self) -> bool {
237+
self.infinity
238+
}
237239
}
238240

239241
pub fn mul_by_char(r: ark_bn254::G2Affine) -> ark_bn254::G2Affine {
@@ -276,12 +278,7 @@ pub trait AffinePairing {
276278
pub struct BnAffinePairing;
277279

278280
// Helper function to perform line function evaluation in affine coordinates
279-
fn ell_affine(
280-
f: &mut ark_bn254::Fq12,
281-
coeffs: &EllCoeff,
282-
xx: &ark_bn254::Fq,
283-
yy: &ark_bn254::Fq,
284-
) {
281+
fn ell_affine(f: &mut ark_bn254::Fq12, coeffs: &EllCoeff, xx: &ark_bn254::Fq, yy: &ark_bn254::Fq) {
285282
// c0 is a trivial value 1
286283
let c0 = coeffs.0;
287284
let mut c1 = coeffs.1;
@@ -292,13 +289,13 @@ fn ell_affine(
292289
c1.mul_assign_by_fp(xx);
293290
c2.mul_assign_by_fp(yy);
294291
f.mul_by_014(&c0, &c1, &c2);
295-
},
292+
}
296293
// line evaluation is y' * f_Q(P), coefficients are (1, x' * lambda, -y' * bias)
297294
TwistType::D => {
298295
c1.mul_assign_by_fp(xx);
299296
c2.mul_assign_by_fp(yy);
300297
f.mul_by_034(&c0, &c1, &(c2));
301-
},
298+
}
302299
}
303300
}
304301

@@ -341,12 +338,7 @@ impl AffinePairing for BnAffinePairing {
341338
let bit = Config::ATE_LOOP_COUNT[i - 1];
342339
if bit == 1 || bit == -1 {
343340
for (coeff_1, coeff_2, coeffs) in pairs.iter_mut() {
344-
ell_affine(
345-
&mut f,
346-
&coeffs.next().unwrap(),
347-
coeff_1,
348-
coeff_2,
349-
);
341+
ell_affine(&mut f, &coeffs.next().unwrap(), coeff_1, coeff_2);
350342
}
351343
}
352344
}

0 commit comments

Comments
 (0)