RustCrypto / utils

Utility crates used in RustCrypto
440 stars 129 forks source link

fiat-constify: Fix implementation for fiat-crypto >= 0.0.21 #978

Closed MasterAwesome closed 10 months ago

MasterAwesome commented 1 year ago

fiat-crypto introduced new types instead of just type aliases, this causes the generated code to no longer compile. This is a breaking change since this will no longer compile code generated by fiat-crypto version < 0.0.21

Tests:

Codegen

Multiple output args

Before
#[inline]
pub fn fiat_p224_divstep(out1: &mut u64, out2: &mut [u64; 5], out3: &mut [u64; 5], out4: &mut [u64; 4], out5: &mut [u64; 4], arg1: u64, arg2: &[u64; 5], arg3: &[u64; 5], arg4: &[u64; 4], arg5: &[u64; 4]) {
  // <snipped>
  *out1 = x112;
  out2[0] = x7;
  out2[1] = x8;
  out2[2] = x9;
  out2[3] = x10;
  out2[4] = x11;
  out3[0] = x114;
  out3[1] = x115;
  out3[2] = x116;
  out3[3] = x117;
  out3[4] = x118;
  out4[0] = x119;
  out4[1] = x120;
  out4[2] = x121;
  out4[3] = x122;
  out5[0] = x123;
  out5[1] = x124;
  out5[2] = x125;
  out5[3] = x126;
}
After
#[inline]
pub const fn fiat_p224_divstep(
    arg1: u64,
    arg2: &[u64; 5],
    arg3: &[u64; 5],
    arg4: &[u64; 4],
    arg5: &[u64; 4],
) -> (u64, [u64; 5], [u64; 5], [u64; 4], [u64; 4]) {
    // <snipped> 
    (
        x112,
        [x7, x8, x9, x10, x11],
        [x114, x115, x116, x117, x118],
        [x119, x120, x121, x122],
        [x123, x124, x125, x126],
    )
}

Newtyped const output/input

Before
pub fn fiat_p521_relax(out1: &mut fiat_p521_loose_field_element, arg1: &fiat_p521_tight_field_element) {
  let x1: u64 = (arg1[0]);
  let x2: u64 = (arg1[1]);
  let x3: u64 = (arg1[2]);
  let x4: u64 = (arg1[3]);
  let x5: u64 = (arg1[4]);
  let x6: u64 = (arg1[5]);
  let x7: u64 = (arg1[6]);
  let x8: u64 = (arg1[7]);
  let x9: u64 = (arg1[8]);
  out1[0] = x1;
  out1[1] = x2;
  out1[2] = x3;
  out1[3] = x4;
  out1[4] = x5;
  out1[5] = x6;
  out1[6] = x7;
  out1[7] = x8;
  out1[8] = x9;
}
After
pub const fn fiat_p521_relax(
    arg1: &fiat_p521_tight_field_element,
) -> fiat_p521_loose_field_element {
    let arg1 = &arg1.0;
    let x1: u64 = (arg1[0]);
    let x2: u64 = (arg1[1]);
    let x3: u64 = (arg1[2]);
    let x4: u64 = (arg1[3]);
    let x5: u64 = (arg1[4]);
    let x6: u64 = (arg1[5]);
    let x7: u64 = (arg1[6]);
    let x8: u64 = (arg1[7]);
    let x9: u64 = (arg1[8]);
    (fiat_p521_loose_field_element([x1, x2, x3, x4, x5, x6, x7, x8, x9]))
}
MasterAwesome commented 1 year ago

@tarcieri, I've added support for fiat-constify for the newer fiat-crypto generated Rust code

tarcieri commented 10 months ago

Still interested in this, FWIW

MasterAwesome commented 10 months ago

Oh yes my b kinda forgot about this, should I go ahead and update one of the elliptic-curve crates with this codegen and ensure tests pass?

I'll create a tracking issue too for the migration of the remaining crates that depend on fiat_crypto. If I remember right, there are changes required to primeorder too for the inversion etc. How should we approach this?

tarcieri commented 10 months ago

If you can PoC it on a single elliptic curve crate (e.g. p521 like you were working on before) and get all of the tests passing in a PR I'd be fine with merging this.

We'll begin making breaking changes to several of the crates early next year and can take care of getting all of the other crates updated then, including primeorder.