From 84e5a7309757e3ff61bbc4b1ff0dc27540625c7b Mon Sep 17 00:00:00 2001 From: Xander van der Goot Date: Wed, 25 Nov 2026 11:30:49 +0800 Subject: [PATCH 1/5] Ignore Python bytecode --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index fe666147b..84d37231a 100644 --- a/.gitignore +++ b/.gitignore @@ -61,3 +61,7 @@ tooling/provekit-wasm/pkg/* # logs for passport circuit benchmarks noir-examples/noir-passport/merkle_age_check/benchmark-inputs/logs/ .omx/ + +# Python bytecode +__pycache__/ +*.pyc From a1f9d4634501db06e2a37970d81965ca88a3ac16 Mon Sep 17 00:00:00 2001 From: Xander van der Goot Date: Wed, 25 Nov 2026 13:30:14 +0800 Subject: [PATCH 2/5] hla: reserved register pool, shift kinds, standalone generate --- skyscraper/hla/src/backend.rs | 18 ++-- skyscraper/hla/src/builder.rs | 29 ++++-- skyscraper/hla/src/codegen.rs | 21 +++-- skyscraper/hla/src/instructions.rs | 143 ++++++++++++++++++++++++++--- skyscraper/hla/src/ir.rs | 27 +++++- 5 files changed, 198 insertions(+), 40 deletions(-) diff --git a/skyscraper/hla/src/backend.rs b/skyscraper/hla/src/backend.rs index 774e68b82..3cd5f536d 100644 --- a/skyscraper/hla/src/backend.rs +++ b/skyscraper/hla/src/backend.rs @@ -159,6 +159,14 @@ pub fn reserve_output_variable( } } +/// General-purpose registers that are never allocated: +/// - x18: platform register, reserved by the OS +/// - x19: used internally by LLVM, rejected as an `asm!` operand +/// - x29: frame pointer +/// - x30: link register, left to the compiler +/// - x31: stack pointer / zero register +const RESERVED_GENERAL_PURPOSE: [u64; 5] = [18, 19, 29, 30, 31]; + /// Manages pools of hardware registers for allocation. /// /// RegisterBank maintains separate pools for general-purpose registers and @@ -182,14 +190,12 @@ impl RegisterBank { /// # Returns /// /// A new RegisterBank with general-purpose and vector register pools. - /// Certain registers are excluded: - /// - Register 18 (reserved by OS) - /// - Register 19 (reserved by LLVM) - /// - Register 30 (reserved for link register) - /// - Register 31 (reserved for stack pointer) + /// The general-purpose pool excludes `RESERVED_GENERAL_PURPOSE`. pub fn new() -> Self { Self { - general_purpose: RegisterAllocator::new((0..=17).chain(20..29)), + general_purpose: RegisterAllocator::new( + (0..=31).filter(|register| !RESERVED_GENERAL_PURPOSE.contains(register)), + ), vector: RegisterAllocator::new(0..=31), } } diff --git a/skyscraper/hla/src/builder.rs b/skyscraper/hla/src/builder.rs index c0f3a196d..49e4b5de5 100644 --- a/skyscraper/hla/src/builder.rs +++ b/skyscraper/hla/src/builder.rs @@ -70,6 +70,24 @@ where &[AllocatedVariable], &[Instruction], ) -> String, +{ + let assembly = generate(algos, codegen); + + use std::io::Write; + let mut file = std::fs::File::create(&path) + .unwrap_or_else(|_| panic!("Unable to create file: {:#?}", path.as_ref())); + file.write_all(assembly.as_bytes()) + .unwrap_or_else(|_| panic!("Unable to write assembly to file: {:#?}", path.as_ref())); +} + +/// Runs the setups, allocates registers and returns `codegen`'s output. +pub fn generate(algos: Interleaving, codegen: C) -> String +where + C: FnOnce( + &[AllocatedVariable], + &[AllocatedVariable], + &[Instruction], + ) -> String, { let mut alloc = FreshAllocator::new(); let mut mapping = RegisterMapping::new(); @@ -110,18 +128,11 @@ where .map(|fresh_variable| mapping.get_allocated_variable(fresh_variable)) .collect(); - // Write this info in the assembly file - let assembly = codegen( + codegen( &input_hw_registers, &output_hw_registers, &hardware_instructions, - ); - - use std::io::Write; - let mut file = std::fs::File::create(&path) - .unwrap_or_else(|_| panic!("Unable to create file: {:#?}", path.as_ref())); - file.write_all(assembly.as_bytes()) - .unwrap_or_else(|_| panic!("Unable to write assembly to file: {:#?}", path.as_ref())); + ) } /// Runs setup functions according to their interleaving pattern. diff --git a/skyscraper/hla/src/codegen.rs b/skyscraper/hla/src/codegen.rs index d23b44925..1f3f2ddff 100644 --- a/skyscraper/hla/src/codegen.rs +++ b/skyscraper/hla/src/codegen.rs @@ -51,7 +51,13 @@ pub fn generate_rust_global_asm( outputs_registers: &[AllocatedVariable], instructions: &[Instruction], ) -> String { - let operands = generate_asm_operands(inputs_registers, outputs_registers, instructions); + // The standalone function is reached with `bl`, which overwrites the link + // register. Inlined blocks never touch it. + let operands = [ + generate_asm_operands(inputs_registers, outputs_registers, instructions), + "lateout(\"lr\") _".to_string(), + ] + .join(",\n"); let standalone = generate_standalone_asm(label, instructions); let operands_with_comments: String = operands @@ -123,13 +129,12 @@ pub fn generate_asm_operands( let clobbers = format_clobbers(&clobber_registers); - [ - input_operands, - output_operands, - clobbers, - "lateout(\"lr\") _".to_string(), - ] - .join(",\n") + // Skip empty groups so that no empty operand is joined in. + [input_operands, output_operands, clobbers] + .into_iter() + .filter(|operands| !operands.is_empty()) + .collect::>() + .join(",\n") } /// Clobber registers are all the registers that have been used in the assembly diff --git a/skyscraper/hla/src/instructions.rs b/skyscraper/hla/src/instructions.rs index d12466385..2bd36188f 100644 --- a/skyscraper/hla/src/instructions.rs +++ b/skyscraper/hla/src/instructions.rs @@ -33,8 +33,8 @@ use { crate::{ frontend::{Assembler, FreshAllocator, PointerReg, Reg, Simd, SizedIdx, D, SIMD}, - ir::{FreshRegister, Instruction, Modifier}, - reification::ReifyRegister, + ir::{FreshRegister, Instruction, Modifier, ShiftKind}, + reification::{ReifiedRegister, ReifyRegister}, }, paste::paste, }; @@ -97,6 +97,17 @@ pub mod scalar { } } + /// Compares a register with an immediate: sets the flags of `a - imm`. + pub fn cmp_imm_inst(a: &Reg, imm: u64) -> Instruction { + assert!(imm < 4096, "cmp immediates are 12 bits"); + Instruction { + opcode: "cmp".to_string(), + results: vec![], + operands: vec![a.reify()], + modifiers: Modifier::Imm(imm), + } + } + pub fn csel_inst( dest: &Reg, a: &Reg, @@ -120,6 +131,16 @@ pub mod scalar { } } + /// All ones when `cond` holds, otherwise zero. + pub fn csetm_inst(dest: &Reg, cond: &str) -> Instruction { + Instruction { + opcode: "csetm".to_string(), + results: vec![dest.reify()], + operands: vec![], + modifiers: Modifier::Cond(cond.to_string()), + } + } + pub fn cinc_inst(dest: &Reg, a: &Reg, cond: String) -> Instruction { Instruction { opcode: "cinc".to_string(), @@ -157,6 +178,64 @@ pub mod scalar { embed_asm!(add, "add", (a: u64, b: u64) -> u64); embed_asm!(and, "and", (a: u64, b: u64) -> u64); + embed_asm!(eor, "eor", (a: u64, b: u64) -> u64); + + /// `a + (b << shift)`: an addition with a shifted register operand. + pub fn add_lsl( + alloc: &mut FreshAllocator, + asm: &mut Assembler, + a: &Reg, + b: &Reg, + shift: u8, + ) -> Reg { + assert!(shift < 64); + let out = alloc.fresh(); + asm.append_instruction(vec![Instruction { + opcode: "add".to_owned(), + results: vec![out.reify()], + operands: vec![a.reify(), b.reify()], + modifiers: Modifier::ShiftedOperand(ShiftKind::Lsl, shift), + }]); + out + } + + /// Logical right shift of a scalar register by an immediate count. + pub fn lsr( + alloc: &mut FreshAllocator, + asm: &mut Assembler, + a: &Reg, + shift: u8, + ) -> Reg { + shift_immediate(alloc, asm, a, shift, "lsr") + } + + /// Logical left shift of a scalar register by an immediate count. + pub fn lsl( + alloc: &mut FreshAllocator, + asm: &mut Assembler, + a: &Reg, + shift: u8, + ) -> Reg { + shift_immediate(alloc, asm, a, shift, "lsl") + } + + fn shift_immediate( + alloc: &mut FreshAllocator, + asm: &mut Assembler, + a: &Reg, + shift: u8, + opcode: &str, + ) -> Reg { + assert!(shift < 64); + let out = alloc.fresh(); + asm.append_instruction(vec![Instruction { + opcode: opcode.to_owned(), + results: vec![out.reify()], + operands: vec![a.reify()], + modifiers: Modifier::Shift(shift), + }]); + out + } } pub mod load_store { @@ -164,21 +243,57 @@ pub mod load_store { super::*, crate::frontend::{MutablePointer, Pointer}, }; + + /// Loads two adjacent words from an address held in a general register. + /// The byte offset is fixed at generation time; the base may be computed. + pub fn ldp_address( + alloc: &mut FreshAllocator, + asm: &mut Assembler, + address: &Reg, + byte_offset: usize, + ) -> (Reg, Reg) { + assert!(byte_offset <= 504 && byte_offset % 8 == 0); + let a = alloc.fresh(); + let b = alloc.fresh(); + let mut pointer = address.reify(); + pointer.idx = crate::reification::Index::Pointer(byte_offset); + asm.append_instruction(vec![ldp_inst(&a, &b, pointer)]); + (a, b) + } + + /// Loads one word from an address held in a general register. + /// The byte offset is fixed at generation time; the base may be computed. + pub fn ldr_address( + alloc: &mut FreshAllocator, + asm: &mut Assembler, + address: &Reg, + byte_offset: usize, + ) -> Reg { + assert!(byte_offset <= 32760 && byte_offset % 8 == 0); + let out = alloc.fresh(); + let mut pointer = address.reify(); + pointer.idx = crate::reification::Index::Pointer(byte_offset); + asm.append_instruction(vec![ldr_inst(&out, pointer)]); + out + } pub fn ldr( alloc: &mut FreshAllocator, asm: &mut Assembler, ptr: &PointerReg, ) -> Reg { let ret = alloc.fresh(); - asm.append_instruction(vec![ldr_inst(&ret, ptr)]); + asm.append_instruction(vec![ldr_inst(&ret, ptr.reify())]); ret } - pub fn ldr_inst(dest: &Reg, ptr: &PointerReg) -> Instruction { + pub fn ldr_inst( + dest: &Reg, + ptr: ReifiedRegister, + ) -> Instruction { Instruction { opcode: "ldr".to_string(), results: vec![dest.reify()], - operands: vec![ptr.reify()], + operands: vec![ptr], modifiers: Modifier::None, } } @@ -190,19 +305,19 @@ pub mod load_store { ) -> (Reg, Reg) { let ret0 = alloc.fresh(); let ret1 = alloc.fresh(); - asm.append_instruction(vec![ldp_inst(&ret0, &ret1, ptr)]); + asm.append_instruction(vec![ldp_inst(&ret0, &ret1, ptr.reify())]); (ret0, ret1) } - pub fn ldp_inst( + pub fn ldp_inst( dest: &Reg, dest2: &Reg, - ptr: &PTR, + ptr: ReifiedRegister, ) -> Instruction { Instruction { opcode: "ldp".to_string(), results: vec![dest.reify(), dest2.reify()], - operands: vec![ptr.reify()], + operands: vec![ptr], modifiers: Modifier::None, } } @@ -339,7 +454,7 @@ pub mod simd { opcode: "sli.2d".to_string(), results: vec![dest.reify()], operands: vec![source.reify()], - modifiers: Modifier::Lsl(shl), + modifiers: Modifier::Shift(shl), } } @@ -387,7 +502,7 @@ pub mod simd { opcode: "shl.2d".to_string(), results: vec![dest.reify()], operands: vec![a.reify()], - modifiers: Modifier::Lsl(imm), + modifiers: Modifier::Shift(imm), } } @@ -411,7 +526,7 @@ pub mod simd { opcode: "ushr.2d".to_string(), results: vec![dest.reify()], operands: vec![a.reify()], - modifiers: Modifier::Lsl(imm), + modifiers: Modifier::Shift(imm), } } @@ -435,7 +550,7 @@ pub mod simd { opcode: "usra.2d".to_string(), results: vec![dest.reify()], operands: vec![a.reify()], - modifiers: Modifier::Lsl(imm), + modifiers: Modifier::Shift(imm), } } @@ -459,7 +574,7 @@ pub mod simd { opcode: "ssra.2d".to_string(), results: vec![dest.reify()], operands: vec![a.reify()], - modifiers: Modifier::Lsl(imm), + modifiers: Modifier::Shift(imm), } } } diff --git a/skyscraper/hla/src/ir.rs b/skyscraper/hla/src/ir.rs index c89324c60..4a0368262 100644 --- a/skyscraper/hla/src/ir.rs +++ b/skyscraper/hla/src/ir.rs @@ -30,13 +30,33 @@ pub struct Instruction { pub(crate) modifiers: Modifier, } +/// Direction of a shifted register operand. +#[derive(Debug, PartialEq, Clone, Copy)] +pub(crate) enum ShiftKind { + Lsl, + // Lsr, + // Asr, +} + +impl std::fmt::Display for ShiftKind { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { + ShiftKind::Lsl => "lsl", + // ShiftKind::Lsr => "lsr", + // ShiftKind::Asr => "asr", + }) + } +} + #[derive(Debug, PartialEq)] pub(crate) enum Modifier { None, Imm(u64), ImmLsl(u16, u8), - // Logical shift left - Lsl(u8), + // Immediate shift count of an instruction whose opcode fixes the direction + Shift(u8), + // Shift of the last register operand, with the direction as an argument + ShiftedOperand(ShiftKind, u8), Cond(String), } @@ -53,7 +73,8 @@ impl std::fmt::Display for Instruction { Modifier::Imm(imm) => format!(", #{imm}"), Modifier::Cond(cond) => format!(", {cond}"), Modifier::ImmLsl(imm, shift) => format!(", #{imm}, lsl {shift}"), - Modifier::Lsl(imm) => format!(", #{imm}"), + Modifier::Shift(imm) => format!(", #{imm}"), + Modifier::ShiftedOperand(kind, shift) => format!(", {kind} #{shift}"), }; let inst = &self.opcode; From bfaa9cf697cb4abba3adca094311992c89937c84 Mon Sep 17 00:00:00 2001 From: Xander van der Goot Date: Wed, 25 Nov 2026 14:36:16 +0800 Subject: [PATCH 3/5] codegen: Karatsuba MUL kernel, rename scalar to schoolbook --- .../bn254-multiplier-codegen/src/karatsuba.rs | 305 ++++++++++++++++++ .../bn254-multiplier-codegen/src/lib.rs | 3 +- .../bn254-multiplier-codegen/src/main.rs | 31 +- .../src/{scalar.rs => schoolbook.rs} | 0 skyscraper/bn254-multiplier/build.rs | 15 +- 5 files changed, 335 insertions(+), 19 deletions(-) create mode 100644 skyscraper/bn254-multiplier-codegen/src/karatsuba.rs rename skyscraper/bn254-multiplier-codegen/src/{scalar.rs => schoolbook.rs} (100%) diff --git a/skyscraper/bn254-multiplier-codegen/src/karatsuba.rs b/skyscraper/bn254-multiplier-codegen/src/karatsuba.rs new file mode 100644 index 000000000..2b81372cd --- /dev/null +++ b/skyscraper/bn254-multiplier-codegen/src/karatsuba.rs @@ -0,0 +1,305 @@ +//! The scalar Montgomery multiplication kernel [`MUL`]. +//! +//! `MUL` uses Domb's single-step reduction with a Karatsuba product, a +//! balanced fold with constants from [`REDUCTION_TABLE`] and a split final +//! step. It returns a loose residue below `2^256`. Inputs must satisfy the +//! caller bound `a, b <= 2^256 - 2p`. + +use { + crate::{constants::*, schoolbook}, + hla::{builder::Setup, *}, + std::array, +}; + +// Limb positions in `REDUCTION_TABLE`. +const I3_LIMB: usize = 0; +const I2_LIMB: usize = 4; +const I1_LIMB: usize = 8; +const P_LIMB: usize = 12; +const MU0_LIMB: usize = 16; + +/// Reduction constants in the layout `MUL` loads them from. +pub const REDUCTION_TABLE: [u64; 17] = { + let mut table = [0; 17]; + let mut i = 0; + while i < 4 { + table[I3_LIMB + i] = U64_I3[i]; + table[I2_LIMB + i] = U64_I2[i]; + table[I1_LIMB + i] = U64_I1[i]; + table[P_LIMB + i] = U64_P[i]; + i += 1; + } + table[MU0_LIMB] = U64_MU0; + table +}; + +/// Montgomery multiplication: the Karatsuba widening product, the balanced +/// fold of `t0..t2` and the split final step. Inputs `a`, `b` and `table` +/// (the address of `REDUCTION_TABLE`); loose output +pub const MUL: Setup = |alloc, asm| { + let a = alloc.fresh_array(); + let b = alloc.fresh_array(); + let table = alloc.fresh(); + let t = widening_karatsuba(alloc, asm, &a, &b); + let s = fold_balanced(alloc, asm, t, &table); + let out = finish(alloc, asm, s, &table); + ( + vec![ + FreshVariable::new("a", &a), + FreshVariable::new("b", &b), + FreshVariable::new("table", std::slice::from_ref(&table)), + ], + FreshVariable::new("out", &out), + ) +}; + +/// Widening product with one Karatsuba level on the 128-bit halves +fn widening_karatsuba( + alloc: &mut FreshAllocator, + asm: &mut Assembler, + a: &[Reg; 4], + b: &[Reg; 4], +) -> [Reg; 8] { + let zero = mov(alloc, asm, 0); + let fresh2 = + |alloc: &mut FreshAllocator| -> [Reg; 2] { array::from_fn(|_| alloc.fresh()) }; + + // Write a = a0 + a1·φ and b = b0 + b1·φ with φ = 2^128. Then + // a·b = a0·b0 + (a0·b1 + a1·b0)·φ + a1·b1·φ² + // = a0·b0 + ((a0 + a1)·(b0 + b1) − a0·b0 − a1·b1)·φ + a1·b1·φ², + // so the two cross products cost one product, (a0 + a1)·(b0 + b1). + // + // The sums a0 + a1 and b0 + b1 may carry out of 128 bits. Write them as + // sa = ca·φ + sa' and sb = cb·φ + sb', with carries ca, cb ∈ {0, 1}: + // (a0 + a1)·(b0 + b1) = sa'·sb' + (ca·sb' + cb·sa')·φ + ca·cb·φ². + // So the middle product is the 128-bit product sa'·sb' plus two carry + // terms. The carries become all-ones masks, which select sb' and sa' + // with `and` instead of a multiplication. + + let (sa, mask_a) = (fresh2(alloc), alloc.fresh()); + asm.append_instruction(vec![ + adds_inst(&sa[0], &a[0], &a[2]), + adcs_inst(&sa[1], &a[1], &a[3]), + csetm_inst(&mask_a, "hs"), + ]); + let (sb, mask_b) = (fresh2(alloc), alloc.fresh()); + asm.append_instruction(vec![ + adds_inst(&sb[0], &b[0], &b[2]), + adcs_inst(&sb[1], &b[1], &b[3]), + csetm_inst(&mask_b, "hs"), + ]); + let ca_sb: [Reg; 2] = array::from_fn(|i| and(alloc, asm, &sb[i], &mask_a)); + let cb_sa: [Reg; 2] = array::from_fn(|i| and(alloc, asm, &sa[i], &mask_b)); + let both = and(alloc, asm, &mask_a, &mask_b); + let both = lsr(alloc, asm, &both, 63); + + let (cross, cross_top) = (fresh2(alloc), alloc.fresh()); + asm.append_instruction(vec![ + adds_inst(&cross[0], &ca_sb[0], &cb_sa[0]), + adcs_inst(&cross[1], &ca_sb[1], &cb_sa[1]), + adc_inst(&cross_top, &both, &zero), + ]); + let (a_lo, a_hi) = (a.first_chunk().unwrap(), a.last_chunk().unwrap()); + let (b_lo, b_hi) = (b.first_chunk().unwrap(), b.last_chunk().unwrap()); + let low = widening_mul_u128(alloc, asm, a_lo, b_lo); + let high = widening_mul_u128(alloc, asm, a_hi, b_hi); + let [m0, m1, m2, m3] = widening_mul_u128(alloc, asm, &sa, &sb); + + let upper: [Reg; 3] = array::from_fn(|_| alloc.fresh()); + asm.append_instruction(vec![ + adds_inst(&upper[0], &m2, &cross[0]), + adcs_inst(&upper[1], &m3, &cross[1]), + adc_inst(&upper[2], &cross_top, &zero), + ]); + let [u2, u3, u4] = upper; + let middle = [m0, m1, u2, u3, u4]; + let minus_low = sub_five(alloc, asm, &middle, &low, &zero); + let cross_terms = sub_five(alloc, asm, &minus_low, &high, &zero); + + let [l0, l1, l2, l3] = low; + let [h0, h1, h2, h3] = high; + let out: [Reg; 6] = array::from_fn(|_| alloc.fresh()); + asm.append_instruction(vec![ + adds_inst(&out[0], &l2, &cross_terms[0]), + adcs_inst(&out[1], &l3, &cross_terms[1]), + adcs_inst(&out[2], &h0, &cross_terms[2]), + adcs_inst(&out[3], &h1, &cross_terms[3]), + adcs_inst(&out[4], &h2, &cross_terms[4]), + adc_inst(&out[5], &h3, &zero), + ]); + let [o2, o3, o4, o5, o6, o7] = out; + [l0, l1, o2, o3, o4, o5, o6, o7] +} + +/// Widening multiplication u128 x u128 -> u256 of two-limb `x`, `y`: two rows, +/// one shifted addition. +fn widening_mul_u128( + alloc: &mut FreshAllocator, + asm: &mut Assembler, + x: &[Reg; 2], + y: &[Reg; 2], +) -> [Reg; 4] { + let row = |alloc: &mut FreshAllocator, asm: &mut Assembler, j: usize| { + let [l0, h0] = schoolbook::widening_mul(alloc, asm, &x[0], &y[j]); + let [l1, h1] = schoolbook::widening_mul(alloc, asm, &x[1], &y[j]); + let (middle, top) = (alloc.fresh(), alloc.fresh()); + asm.append_instruction(vec![ + adds_inst(&middle, &l1, &h0), + cinc_inst(&top, &h1, "hs".to_owned()), + ]); + [l0, middle, top] + }; + let [r0, r1, r2] = row(alloc, asm, 0); + let [s0, s1, s2] = row(alloc, asm, 1); + let out: [Reg; 3] = array::from_fn(|_| alloc.fresh()); + asm.append_instruction(vec![ + adds_inst(&out[0], &r1, &s0), + adcs_inst(&out[1], &r2, &s1), + cinc_inst(&out[2], &s2, "hs".to_owned()), + ]); + let [o1, o2, o3] = out; + [r0, o1, o2, o3] +} + +/// `x - y` for a five-limb `x` and a four-limb `y`; the caller guarantees +/// `x >= y`. +fn sub_five( + alloc: &mut FreshAllocator, + asm: &mut Assembler, + x: &[Reg; 5], + y: &[Reg; 4], + zero: &Reg, +) -> [Reg; 5] { + let out: [Reg; 5] = array::from_fn(|_| alloc.fresh()); + let mut instructions = vec![subs_inst(&out[0], &x[0], &y[0])]; + instructions.extend((1..4).map(|i| sbcs_inst(&out[i], &x[i], &y[i]))); + instructions.push(sbcs_inst(&out[4], &x[4], zero)); + asm.append_instruction(instructions); + out +} + +/// The balanced fold `s = (upper + t0*I3) + (t1*I2 + t2*I1)` of the product +/// `t`, five limbs. +fn fold_balanced( + alloc: &mut FreshAllocator, + asm: &mut Assembler, + t: [Reg; 8], + table: &Reg, +) -> [Reg; 5] { + let [t0, t1, t2, upper @ ..] = t; + let p0 = mul_constant(alloc, asm, &t0, I3_LIMB, table); + // Merge the first pair early to bound live registers. The second pair + // remains arithmetically independent of this first addition chain. + let left = add_five(alloc, asm, upper, p0); + let p1 = mul_constant(alloc, asm, &t1, I2_LIMB, table); + let p2 = mul_constant(alloc, asm, &t2, I1_LIMB, table); + let right = add_five(alloc, asm, p1, p2); + add_five(alloc, asm, left, right) +} + +/// `limb * c` for the four-limb constant of `REDUCTION_TABLE` at `first`: five +/// limbs. The eight limb products first, then one carry chain; the table +/// limbs are loaded in pairs, each pair before its products. +fn mul_constant( + alloc: &mut FreshAllocator, + asm: &mut Assembler, + limb: &Reg, + first: usize, + table: &Reg, +) -> [Reg; 5] { + let (c0, c1) = ldp_address(alloc, asm, table, first * 8); + let p0 = schoolbook::widening_mul(alloc, asm, limb, &c0); + let p1 = schoolbook::widening_mul(alloc, asm, limb, &c1); + let (c2, c3) = ldp_address(alloc, asm, table, (first + 2) * 8); + let p2 = schoolbook::widening_mul(alloc, asm, limb, &c2); + let p3 = schoolbook::widening_mul(alloc, asm, limb, &c3); + add_rows(alloc, asm, [p0, p1, p2, p3]) +} + +/// Sums the 128-bit products `x * c[i]` into five limbs: the low halves +/// occupy limbs 0..4 and the high halves limbs 1..5. One carry chain, which +/// cannot overflow because the full product is below 2^320. +fn add_rows( + alloc: &mut FreshAllocator, + asm: &mut Assembler, + products: [[Reg; 2]; 4], +) -> [Reg; 5] { + let [[l0, h0], [l1, h1], [l2, h2], [l3, h3]] = products; + let out: [Reg; 4] = array::from_fn(|_| alloc.fresh()); + asm.append_instruction(vec![ + adds_inst(&out[0], &l1, &h0), + adcs_inst(&out[1], &l2, &h1), + adcs_inst(&out[2], &l3, &h2), + cinc_inst(&out[3], &h3, "hs".to_owned()), + ]); + let [o1, o2, o3, o4] = out; + [l0, o1, o2, o3, o4] +} + +fn add_five( + alloc: &mut FreshAllocator, + asm: &mut Assembler, + a: [Reg; 5], + b: [Reg; 5], +) -> [Reg; 5] { + let out = array::from_fn(|_| alloc.fresh()); + asm.append_instruction(vec![ + adds_inst(&out[0], &a[0], &b[0]), + adcs_inst(&out[1], &a[1], &b[1]), + adcs_inst(&out[2], &a[2], &b[2]), + adcs_inst(&out[3], &a[3], &b[3]), + adc_inst(&out[4], &a[4], &b[4]), + ]); + out +} + +/// The final step on the fold `s`: `m = mu0 * s0`, then `(s + m*p) / 2^64` +/// without `low(m * p[0])` (see [`cancel_split`]). +fn finish( + alloc: &mut FreshAllocator, + asm: &mut Assembler, + s: [Reg; 5], + table: &Reg, +) -> [Reg; 4] { + let mu = ldr_address(alloc, asm, table, MU0_LIMB * 8); + let m = hla::mul(alloc, asm, &mu, &s[0]); + let (p0, p1) = ldp_address(alloc, asm, table, P_LIMB * 8); + let (p2, p3) = ldp_address(alloc, asm, table, (P_LIMB + 2) * 8); + cancel_split(alloc, asm, s, &[p0, p1, p2, p3], &m) +} + +/// Computes `(s + m*p) / 2^64` truncated to four limbs for the Montgomery `m` +/// that cancels the low limb. Then `s[0] + low(m*p[0])` is `0` or `2^64`, so +/// its carry is `s[0] != 0` and `low(m*p[0])` is never computed. The low +/// halves of the remaining products are added to `s` in one carry chain and +/// the high halves in a second one; the chains pipeline instead of rippling +/// limb by limb. +fn cancel_split( + alloc: &mut FreshAllocator, + asm: &mut Assembler, + s: [Reg; 5], + p: &[Reg; 4], + m: &Reg, +) -> [Reg; 4] { + let high0 = umulh(alloc, asm, m, &p[0]); + let [low1, high1] = schoolbook::widening_mul(alloc, asm, m, &p[1]); + let [low2, high2] = schoolbook::widening_mul(alloc, asm, m, &p[2]); + let [low3, high3] = schoolbook::widening_mul(alloc, asm, m, &p[3]); + let [s0, s1, s2, s3, s4] = s; + let low: [Reg; 4] = array::from_fn(|_| alloc.fresh()); + asm.append_instruction(vec![ + cmp_imm_inst(&s0, 1), + adcs_inst(&low[0], &s1, &low1), + adcs_inst(&low[1], &s2, &low2), + adcs_inst(&low[2], &s3, &low3), + cinc_inst(&low[3], &s4, "hs".to_string()), + ]); + let out = array::from_fn(|_| alloc.fresh()); + asm.append_instruction(vec![ + adds_inst(&out[0], &low[0], &high0), + adcs_inst(&out[1], &low[1], &high1), + adcs_inst(&out[2], &low[2], &high2), + adc_inst(&out[3], &low[3], &high3), + ]); + out +} diff --git a/skyscraper/bn254-multiplier-codegen/src/lib.rs b/skyscraper/bn254-multiplier-codegen/src/lib.rs index 76f915527..12b0584fd 100644 --- a/skyscraper/bn254-multiplier-codegen/src/lib.rs +++ b/skyscraper/bn254-multiplier-codegen/src/lib.rs @@ -1,4 +1,5 @@ pub mod constants; +pub mod karatsuba; pub mod load_store; -pub mod scalar; +pub mod schoolbook; pub mod simd; diff --git a/skyscraper/bn254-multiplier-codegen/src/main.rs b/skyscraper/bn254-multiplier-codegen/src/main.rs index b467bbfaf..0232f3fed 100644 --- a/skyscraper/bn254-multiplier-codegen/src/main.rs +++ b/skyscraper/bn254-multiplier-codegen/src/main.rs @@ -1,5 +1,5 @@ use { - bn254_multiplier_codegen::{scalar, simd}, + bn254_multiplier_codegen::{karatsuba, schoolbook, simd}, hla::builder::{build_includable, Interleaving}, }; @@ -7,28 +7,31 @@ fn main() { build_includable( "./asm/montgomery_interleaved_3.s", Interleaving::par( - Interleaving::single(scalar::setup_single_step), + Interleaving::single(karatsuba::MUL), Interleaving::single(simd::setup_single_step), ), ); build_includable( "./asm/montgomery_square_interleaved_3.s", Interleaving::par( - Interleaving::single(scalar::setup_square_single_step), + Interleaving::single(schoolbook::setup_square_single_step), Interleaving::single(simd::setup_square_single_step), ), ); build_includable( "./asm/montgomery_square_log_interleaved_3.s", Interleaving::par( - Interleaving::single(scalar::setup_square_log_jump), + Interleaving::single(schoolbook::setup_square_log_jump), Interleaving::single(simd::setup_square_single_step), ), ); build_includable( "./asm/montgomery_interleaved_4.s", Interleaving::par( - Interleaving::seq(vec![scalar::setup_single_step, scalar::setup_single_step]), + Interleaving::seq(vec![ + schoolbook::setup_single_step, + schoolbook::setup_single_step, + ]), Interleaving::single(simd::setup_single_step), ), ); @@ -36,8 +39,8 @@ fn main() { "./asm/montgomery_square_interleaved_4.s", Interleaving::par( Interleaving::seq(vec![ - scalar::setup_square_single_step, - scalar::setup_square_single_step, + schoolbook::setup_square_single_step, + schoolbook::setup_square_single_step, ]), Interleaving::single(simd::setup_square_single_step), ), @@ -46,22 +49,26 @@ fn main() { "./asm/montgomery_square_log_interleaved_4.s", Interleaving::par( Interleaving::seq(vec![ - scalar::setup_square_log_jump, - scalar::setup_square_log_jump, + schoolbook::setup_square_log_jump, + schoolbook::setup_square_log_jump, ]), Interleaving::single(simd::setup_square_single_step), ), ); build_includable( "./asm/montgomery.s", - Interleaving::single(scalar::setup_single_step), + Interleaving::single(schoolbook::setup_single_step), ); build_includable( "./asm/montgomery_square.s", - Interleaving::single(scalar::setup_square_single_step), + Interleaving::single(schoolbook::setup_square_single_step), ); build_includable( "./asm/montgomery_log_jump.s", - Interleaving::single(scalar::setup_log_jump), + Interleaving::single(schoolbook::setup_log_jump), + ); + build_includable( + "./asm/montgomery_mul.s", + Interleaving::single(karatsuba::MUL), ); } diff --git a/skyscraper/bn254-multiplier-codegen/src/scalar.rs b/skyscraper/bn254-multiplier-codegen/src/schoolbook.rs similarity index 100% rename from skyscraper/bn254-multiplier-codegen/src/scalar.rs rename to skyscraper/bn254-multiplier-codegen/src/schoolbook.rs diff --git a/skyscraper/bn254-multiplier/build.rs b/skyscraper/bn254-multiplier/build.rs index 8d2137a59..d1e692bdf 100644 --- a/skyscraper/bn254-multiplier/build.rs +++ b/skyscraper/bn254-multiplier/build.rs @@ -1,5 +1,5 @@ use { - bn254_multiplier_codegen::{scalar, simd}, + bn254_multiplier_codegen::{schoolbook, simd}, hla::builder::{build_includable, Interleaving}, std::path::Path, }; @@ -10,7 +10,7 @@ fn main() { build_includable( path, Interleaving::par( - Interleaving::single(scalar::setup_single_step), + Interleaving::single(schoolbook::setup_single_step), Interleaving::single(simd::setup_single_step), ), ); @@ -20,7 +20,10 @@ fn main() { build_includable( path, Interleaving::par( - Interleaving::seq(vec![scalar::setup_single_step, scalar::setup_single_step]), + Interleaving::seq(vec![ + schoolbook::setup_single_step, + schoolbook::setup_single_step, + ]), Interleaving::single(simd::setup_single_step), ), ); @@ -30,7 +33,7 @@ fn main() { build_includable( path, Interleaving::par( - Interleaving::single(scalar::setup_square_single_step), + Interleaving::single(schoolbook::setup_square_single_step), Interleaving::single(simd::setup_square_single_step), ), ); @@ -41,8 +44,8 @@ fn main() { path, Interleaving::par( Interleaving::seq(vec![ - scalar::setup_square_single_step, - scalar::setup_square_single_step, + schoolbook::setup_square_single_step, + schoolbook::setup_square_single_step, ]), Interleaving::single(simd::setup_square_single_step), ), From f002c1d71b239a66f8249b1df52b4c15876f10da Mon Sep 17 00:00:00 2001 From: Xander van der Goot Date: Wed, 25 Nov 2026 14:53:54 +0800 Subject: [PATCH 4/5] bn254-multiplier: montgomery_mul kernel, shared constants --- .../bn254-multiplier-codegen/src/constants.rs | 42 +- .../bn254-multiplier-codegen/src/karatsuba.rs | 24 +- skyscraper/bn254-multiplier/benches/bench.rs | 8 + skyscraper/bn254-multiplier/build.rs | 8 +- .../rtz/portable_simd.txt | 7 + .../bn254-multiplier/src/aarch64/mod.rs | 99 ++- .../src/aarch64/montgomery_interleaved_3.s | 708 ++++++++---------- .../src/aarch64/montgomery_interleaved_4.s | 3 +- .../src/aarch64/montgomery_mul.s | 163 ++++ .../aarch64/montgomery_square_interleaved_3.s | 3 +- .../aarch64/montgomery_square_interleaved_4.s | 3 +- .../montgomery_square_log_interleaved_3.s | 3 +- .../montgomery_square_log_interleaved_4.s | 3 +- skyscraper/bn254-multiplier/src/constants.rs | 26 + skyscraper/bn254-multiplier/src/lib.rs | 6 +- 15 files changed, 628 insertions(+), 478 deletions(-) create mode 100644 skyscraper/bn254-multiplier/proptest-regressions/rtz/portable_simd.txt create mode 100644 skyscraper/bn254-multiplier/src/aarch64/montgomery_mul.s diff --git a/skyscraper/bn254-multiplier-codegen/src/constants.rs b/skyscraper/bn254-multiplier-codegen/src/constants.rs index 42f74a8f1..65414904f 100644 --- a/skyscraper/bn254-multiplier-codegen/src/constants.rs +++ b/skyscraper/bn254-multiplier-codegen/src/constants.rs @@ -1,39 +1,13 @@ // -- [Scalar CONSTANTS] -- // -pub const U64_P: [u64; 4] = [ - 0x43e1f593f0000001, - 0x2833e84879b97091, - 0xb85045b68181585d, - 0x30644e72e131a029, -]; - -pub const U64_2P: [u64; 4] = [ - 0x87c3eb27e0000002, - 0x5067d090f372e122, - 0x70a08b6d0302b0ba, - 0x60c89ce5c2634053, -]; - -pub const U64_I1: [u64; 4] = [ - 0x2d3e8053e396ee4d, - 0xca478dbeab3c92cd, - 0xb2d8f06f77f52a93, - 0x24d6ba07f7aa8f04, -]; -pub const U64_I2: [u64; 4] = [ - 0x18ee753c76f9dc6f, - 0x54ad7e14a329e70f, - 0x2b16366f4f7684df, - 0x133100d71fdf3579, -]; - -pub const U64_I3: [u64; 4] = [ - 0x9bacb016127cbe4e, - 0x0b2051fa31944124, - 0xb064eea46091c76c, - 0x2b062aaa49f80c7d, -]; -pub const U64_MU0: u64 = 0xc2e1f593efffffff; +// The scalar constants are defined once, in `bn254-multiplier`, which loads +// them at runtime through the same table the kernels here are generated for. +#[allow(dead_code)] +#[path = "../../bn254-multiplier/src/constants.rs"] +mod multiplier; +pub use multiplier::{ + I1_LIMB, I2_LIMB, I3_LIMB, MU0_LIMB, P_LIMB, U64_2P, U64_I1, U64_I2, U64_I3, U64_MU0, U64_P, +}; // -- [FP SIMD CONSTANTS] -- // diff --git a/skyscraper/bn254-multiplier-codegen/src/karatsuba.rs b/skyscraper/bn254-multiplier-codegen/src/karatsuba.rs index 2b81372cd..02dd8b8d3 100644 --- a/skyscraper/bn254-multiplier-codegen/src/karatsuba.rs +++ b/skyscraper/bn254-multiplier-codegen/src/karatsuba.rs @@ -1,7 +1,7 @@ //! The scalar Montgomery multiplication kernel [`MUL`]. //! //! `MUL` uses Domb's single-step reduction with a Karatsuba product, a -//! balanced fold with constants from [`REDUCTION_TABLE`] and a split final +//! balanced fold with constants from `REDUCTION_TABLE` and a split final //! step. It returns a loose residue below `2^256`. Inputs must satisfy the //! caller bound `a, b <= 2^256 - 2p`. @@ -11,28 +11,6 @@ use { std::array, }; -// Limb positions in `REDUCTION_TABLE`. -const I3_LIMB: usize = 0; -const I2_LIMB: usize = 4; -const I1_LIMB: usize = 8; -const P_LIMB: usize = 12; -const MU0_LIMB: usize = 16; - -/// Reduction constants in the layout `MUL` loads them from. -pub const REDUCTION_TABLE: [u64; 17] = { - let mut table = [0; 17]; - let mut i = 0; - while i < 4 { - table[I3_LIMB + i] = U64_I3[i]; - table[I2_LIMB + i] = U64_I2[i]; - table[I1_LIMB + i] = U64_I1[i]; - table[P_LIMB + i] = U64_P[i]; - i += 1; - } - table[MU0_LIMB] = U64_MU0; - table -}; - /// Montgomery multiplication: the Karatsuba widening product, the balanced /// fold of `t0..t2` and the split final step. Inputs `a`, `b` and `table` /// (the address of `REDUCTION_TABLE`); loose output diff --git a/skyscraper/bn254-multiplier/benches/bench.rs b/skyscraper/bn254-multiplier/benches/bench.rs index 5577cec51..3c63d9493 100644 --- a/skyscraper/bn254-multiplier/benches/bench.rs +++ b/skyscraper/bn254-multiplier/benches/bench.rs @@ -31,6 +31,14 @@ mod mul { .bench_local_values(|(a, b)| a * b); } + #[cfg(target_arch = "aarch64")] + #[divan::bench] + fn montgomery_mul(bencher: Bencher) { + bencher + .with_inputs(|| rng().random()) + .bench_local_values(|(a, b)| bn254_multiplier::montgomery_mul(a, b)); + } + #[divan::bench] fn mono_mul_b51(bencher: Bencher) { bencher diff --git a/skyscraper/bn254-multiplier/build.rs b/skyscraper/bn254-multiplier/build.rs index d1e692bdf..9bf40e7f2 100644 --- a/skyscraper/bn254-multiplier/build.rs +++ b/skyscraper/bn254-multiplier/build.rs @@ -1,5 +1,5 @@ use { - bn254_multiplier_codegen::{schoolbook, simd}, + bn254_multiplier_codegen::{karatsuba, schoolbook, simd}, hla::builder::{build_includable, Interleaving}, std::path::Path, }; @@ -10,7 +10,7 @@ fn main() { build_includable( path, Interleaving::par( - Interleaving::single(schoolbook::setup_single_step), + Interleaving::single(karatsuba::MUL), Interleaving::single(simd::setup_single_step), ), ); @@ -51,4 +51,8 @@ fn main() { ), ); } + let path = Path::new("./src/aarch64/montgomery_mul.s"); + if !path.exists() { + build_includable(path, Interleaving::single(karatsuba::MUL)); + } } diff --git a/skyscraper/bn254-multiplier/proptest-regressions/rtz/portable_simd.txt b/skyscraper/bn254-multiplier/proptest-regressions/rtz/portable_simd.txt new file mode 100644 index 000000000..869960120 --- /dev/null +++ b/skyscraper/bn254-multiplier/proptest-regressions/rtz/portable_simd.txt @@ -0,0 +1,7 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. +cc 4e2cf7dbc2166496b17094ff80e4aafbb67e014b13b0ecbd5e46a77c79102543 # shrinks to a = [7137538727021999, 8870018641335179124, 6460532845558593155, 25478065274611985], b = [13642952633415579304, 7310865260948233383, 7291270478705058809, 9748366773357865478], c = [15562761338313749541, 9135201743719035978, 9362672512288933307, 2681182608534948319] diff --git a/skyscraper/bn254-multiplier/src/aarch64/mod.rs b/skyscraper/bn254-multiplier/src/aarch64/mod.rs index bbe50c5d0..5a0c21597 100644 --- a/skyscraper/bn254-multiplier/src/aarch64/mod.rs +++ b/skyscraper/bn254-multiplier/src/aarch64/mod.rs @@ -10,11 +10,40 @@ //! 3p | 4.75 [0.54] | 3.94 [1.3517] | 3.86 [1.43] //! 2ˆ256-2p | 5.09 [0.20] | 4.28 [1.0067] | 4.20 [1.09] use { + crate::constants::REDUCTION_TABLE, core::{arch::asm, simd::Simd}, fp_rounding::{RoundingGuard, Zero}, }; +/// Montgomery multiplication `a * b * 2^-256 mod p` without interleaving: +/// Karatsuba product (one level on the 128-bit halves), balanced single-step +/// reduction and split final step, with the reduction constants loaded from +/// `REDUCTION_TABLE`. Inputs up to `OUTPUT_MAX`; the output is loose, as for +/// the other single-step kernels. +#[inline] +pub fn montgomery_mul(a: [u64; 4], b: [u64; 4]) -> [u64; 4] { + let mut out = [0; 4]; + let table = REDUCTION_TABLE.as_ptr(); + // SAFETY: the generated block only reads the immutable `REDUCTION_TABLE`; + // every register it writes is declared as an output or clobber. + unsafe { + asm!(include_str!("montgomery_mul.s"), + in("x0") a[0], in("x1") a[1], in("x2") a[2], in("x3") a[3], + in("x4") b[0], in("x5") b[1], in("x6") b[2], in("x7") b[3], + in("x8") table, + lateout("x0") out[0], lateout("x1") out[1], lateout("x2") out[2], lateout("x3") out[3], + lateout("x4") _, lateout("x5") _, lateout("x6") _, lateout("x7") _, lateout("x8") _, lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _, lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _, lateout("x17") _, lateout("x20") _, lateout("x21") _, lateout("x22") _, + options(readonly, nostack) + ) + }; + out +} + /// A block multiplier with 3 concurrent multiplications. +/// +/// The scalar input uses the kernel of [`montgomery_mul`] (constants from +/// `REDUCTION_TABLE`) and the SIMD input uses `single_step_simd`. See the +/// module documentation for the implications of this. #[inline] pub fn montgomery_interleaved_3( _rtz: &RoundingGuard, @@ -25,17 +54,20 @@ pub fn montgomery_interleaved_3( ) -> ([u64; 4], [Simd; 4]) { let mut out = [0; 4]; let mut outv = [Simd::splat(0); 4]; + let table = REDUCTION_TABLE.as_ptr(); + // SAFETY: the generated block only reads the immutable `REDUCTION_TABLE`; + // every register it writes is declared as an output or clobber. unsafe { asm!(include_str!("montgomery_interleaved_3.s"), in("x0") a[0], in("x1") a[1], in("x2") a[2], in("x3") a[3], in("x4") b[0], in("x5") b[1], in("x6") b[2], in("x7") b[3], + in("x8") table, in("v0") av[0], in("v1") av[1], in("v2") av[2], in("v3") av[3], in("v4") bv[0], in("v5") bv[1], in("v6") bv[2], in("v7") bv[3], lateout("x0") out[0], lateout("x1") out[1], lateout("x2") out[2], lateout("x3") out[3], lateout("v0") outv[0], lateout("v1") outv[1], lateout("v2") outv[2], lateout("v3") outv[3], - lateout("x4") _, lateout("x5") _, lateout("x6") _, lateout("x7") _, lateout("x8") _, lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _, lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _, lateout("v4") _, lateout("v5") _, lateout("v6") _, lateout("v7") _, lateout("v8") _, lateout("v9") _, lateout("v10") _, lateout("v11") _, lateout("v12") _, lateout("v13") _, lateout("v14") _, lateout("v15") _, lateout("v16") _, lateout("v17") _, lateout("v18") _, lateout("v19") _, lateout("v20") _, lateout("v21") _, lateout("v22") _, lateout("v23") _, lateout("v24") _, - lateout("lr") _, - options(nomem, nostack) + lateout("x4") _, lateout("x5") _, lateout("x6") _, lateout("x7") _, lateout("x8") _, lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _, lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _, lateout("x17") _, lateout("x20") _, lateout("x21") _, lateout("x22") _, lateout("x23") _, lateout("x24") _, lateout("v4") _, lateout("v5") _, lateout("v6") _, lateout("v7") _, lateout("v8") _, lateout("v9") _, lateout("v10") _, lateout("v11") _, lateout("v12") _, lateout("v13") _, lateout("v14") _, lateout("v15") _, lateout("v16") _, lateout("v17") _, lateout("v18") _, lateout("v19") _, lateout("v20") _, lateout("v21") _, lateout("v22") _, lateout("v23") _, lateout("v24") _, + options(readonly, nostack) ) }; (out, outv) @@ -61,7 +93,6 @@ pub fn montgomery_square_interleaved_3( lateout("x0") out[0], lateout("x1") out[1], lateout("x2") out[2], lateout("x3") out[3], lateout("v0") outv[0], lateout("v1") outv[1], lateout("v2") outv[2], lateout("v3") outv[3], lateout("x4") _, lateout("x5") _, lateout("x6") _, lateout("x7") _, lateout("x8") _, lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _, lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _, lateout("x17") _, lateout("v4") _, lateout("v5") _, lateout("v6") _, lateout("v7") _, lateout("v8") _, lateout("v9") _, lateout("v10") _, lateout("v11") _, lateout("v12") _, lateout("v13") _, lateout("v14") _, lateout("v15") _, lateout("v16") _, lateout("v17") _, lateout("v18") _, lateout("v19") _, - lateout("lr") _, options(nomem, nostack) ) }; @@ -95,7 +126,7 @@ pub fn montgomery_square_log_interleaved_3( lateout("v6") _, lateout("v7") _, lateout("v8") _, lateout("v9") _, lateout("v10") _, lateout("v11") _, lateout("v12") _, lateout("v13") _, lateout("v14") _, lateout("v15") _, lateout("v16") _, lateout("v17") _, - lateout("v18") _, lateout("v19") _, lateout("lr") _, + lateout("v18") _, lateout("v19") _, options(nomem, nostack) ) }; @@ -132,8 +163,7 @@ pub fn montgomery_interleaved_4( lateout("x4") out1[0], lateout("x5") out1[1], lateout("x6") out1[2], lateout("x7") out1[3], lateout("v0") outv[0], lateout("v1") outv[1], lateout("v2") outv[2], lateout("v3") outv[3], lateout("x8") _, lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _, lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _, lateout("x17") _, lateout("x20") _, lateout("x21") _, lateout("x22") _, lateout("x23") _, lateout("x24") _, lateout("x25") _, lateout("x26") _, lateout("v4") _, lateout("v5") _, lateout("v6") _, lateout("v7") _, lateout("v8") _, lateout("v9") _, lateout("v10") _, lateout("v11") _, lateout("v12") _, lateout("v13") _, lateout("v14") _, lateout("v15") _, lateout("v16") _, lateout("v17") _, lateout("v18") _, lateout("v19") _, lateout("v20") _, lateout("v21") _, lateout("v22") _, lateout("v23") _, lateout("v24") _, - lateout("lr") _, - options(nomem, nostack) + options(nomem, nostack) ) }; (out, out1, outv) @@ -171,7 +201,7 @@ pub fn montgomery_square_interleaved_4( lateout("v7") _, lateout("v8") _, lateout("v9") _, lateout("v10") _, lateout("v11") _, lateout("v12") _, lateout("v13") _, lateout("v14") _, lateout("v15") _, lateout("v16") _, lateout("v17") _, lateout("v18") _, - lateout("v19") _, lateout("lr") _, + lateout("v19") _, options(nomem, nostack) ) }; @@ -210,7 +240,7 @@ pub fn montgomery_square_log_interleaved_4( lateout("v7") _, lateout("v8") _, lateout("v9") _, lateout("v10") _, lateout("v11") _, lateout("v12") _, lateout("v13") _, lateout("v14") _, lateout("v15") _, lateout("v16") _, lateout("v17") _, lateout("v18") _, - lateout("v19") _, lateout("lr") _, + lateout("v19") _, options(nomem, nostack) ) }; @@ -221,7 +251,11 @@ pub fn montgomery_square_log_interleaved_4( mod tests { use { super::*, - crate::test_utils::{ark_ff_reference, safe_bn254_montgomery_input}, + crate::{ + scalar_mul, + test_utils::{ark_ff_reference, safe_bn254_montgomery_input}, + utils::reduce_ct, + }, ark_bn254::Fr, ark_ff::BigInt, fp_rounding::with_rounding_mode, @@ -280,4 +314,49 @@ mod tests { } }); } + + /// The multiplication kernel computes the same residue as `scalar_mul`, + /// which applies `reduce_ct` to the single-step result. + #[test] + fn test_montgomery_mul() { + proptest!(|( + a in safe_bn254_montgomery_input(), + b in safe_bn254_montgomery_input(), + )| { + let result = montgomery_mul(a, b); + assert_eq!(reduce_ct(result), scalar_mul(a, b)); + assert_eq!(ark_ff_reference(a, b), Fr::new(BigInt(result))); + }); + } + + /// The scalar lane (the `MUL` kernel with its constants table) and both + /// SIMD lanes of the 3-way block agree with arkworks. + #[test] + fn test_montgomery_interleaved_3() { + proptest!(|( + a in safe_bn254_montgomery_input(), + b in safe_bn254_montgomery_input(), + c in safe_bn254_montgomery_input(), + d in safe_bn254_montgomery_input(), + e in safe_bn254_montgomery_input(), + f in safe_bn254_montgomery_input(), + )| { + let av = array::from_fn(|i| Simd::from_array([c[i], d[i]])); + let bv = array::from_fn(|i| Simd::from_array([e[i], f[i]])); + unsafe { + with_rounding_mode((), |rtz, _| { + let (out, outv) = montgomery_interleaved_3(rtz, a, b, av, bv); + assert_eq!(ark_ff_reference(a, b), Fr::new(BigInt(out))); + assert_eq!( + ark_ff_reference(c, e), + Fr::new(BigInt(outv.map(|x| x[0]))) + ); + assert_eq!( + ark_ff_reference(d, f), + Fr::new(BigInt(outv.map(|x| x[1]))) + ); + }); + } + }); + } } diff --git a/skyscraper/bn254-multiplier/src/aarch64/montgomery_interleaved_3.s b/skyscraper/bn254-multiplier/src/aarch64/montgomery_interleaved_3.s index 50c551599..affd74e6e 100644 --- a/skyscraper/bn254-multiplier/src/aarch64/montgomery_interleaved_3.s +++ b/skyscraper/bn254-multiplier/src/aarch64/montgomery_interleaved_3.s @@ -1,772 +1,688 @@ // GENERATED FILE, DO NOT EDIT! // in("x0") a[0], in("x1") a[1], in("x2") a[2], in("x3") a[3], // in("x4") b[0], in("x5") b[1], in("x6") b[2], in("x7") b[3], +// in("x8") table, // in("v0") av[0], in("v1") av[1], in("v2") av[2], in("v3") av[3], // in("v4") bv[0], in("v5") bv[1], in("v6") bv[2], in("v7") bv[3], // lateout("x0") out[0], lateout("x1") out[1], lateout("x2") out[2], lateout("x3") out[3], // lateout("v0") outv[0], lateout("v1") outv[1], lateout("v2") outv[2], lateout("v3") outv[3], -// lateout("x4") _, lateout("x5") _, lateout("x6") _, lateout("x7") _, lateout("x8") _, lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _, lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _, lateout("v4") _, lateout("v5") _, lateout("v6") _, lateout("v7") _, lateout("v8") _, lateout("v9") _, lateout("v10") _, lateout("v11") _, lateout("v12") _, lateout("v13") _, lateout("v14") _, lateout("v15") _, lateout("v16") _, lateout("v17") _, lateout("v18") _, lateout("v19") _, lateout("v20") _, lateout("v21") _, lateout("v22") _, lateout("v23") _, lateout("v24") _, -// lateout("lr") _ - mov x8, #4503599627370495 - dup.2d v8, x8 - mul x9, x0, x4 +// lateout("x4") _, lateout("x5") _, lateout("x6") _, lateout("x7") _, lateout("x8") _, lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _, lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _, lateout("x17") _, lateout("x20") _, lateout("x21") _, lateout("x22") _, lateout("x23") _, lateout("x24") _, lateout("v4") _, lateout("v5") _, lateout("v6") _, lateout("v7") _, lateout("v8") _, lateout("v9") _, lateout("v10") _, lateout("v11") _, lateout("v12") _, lateout("v13") _, lateout("v14") _, lateout("v15") _, lateout("v16") _, lateout("v17") _, lateout("v18") _, lateout("v19") _, lateout("v20") _, lateout("v21") _, lateout("v22") _, lateout("v23") _, lateout("v24") _ + mov x9, #4503599627370495 + dup.2d v8, x9 mov x10, #5075556780046548992 dup.2d v9, x10 mov x10, #1 - umulh x11, x0, x4 + mov x11, #0 movk x10, #18032, lsl 48 dup.2d v10, x10 shl.2d v11, v1, #14 - mul x10, x1, x4 shl.2d v12, v2, #26 shl.2d v13, v3, #38 + adds x10, x0, x2 + adcs x12, x1, x3 + csetm x13, hs ushr.2d v3, v3, #14 - umulh x12, x1, x4 shl.2d v14, v0, #2 usra.2d v11, v0, #50 - adds x10, x10, x11 - cinc x11, x12, hs usra.2d v12, v1, #38 usra.2d v13, v2, #26 and.16b v0, v14, v8 - mul x12, x2, x4 + adds x14, x4, x6 + adcs x15, x5, x7 + csetm x16, hs and.16b v1, v11, v8 and.16b v2, v12, v8 and.16b v11, v13, v8 - umulh x13, x2, x4 shl.2d v12, v5, #14 shl.2d v13, v6, #26 + and x17, x14, x13 shl.2d v14, v7, #38 - adds x11, x12, x11 - cinc x12, x13, hs ushr.2d v7, v7, #14 shl.2d v15, v4, #2 - mul x13, x3, x4 usra.2d v12, v4, #50 usra.2d v13, v5, #38 usra.2d v14, v6, #26 - umulh x4, x3, x4 + and x20, x15, x13 and.16b v4, v15, v8 and.16b v5, v12, v8 and.16b v6, v13, v8 - adds x12, x13, x12 - cinc x4, x4, hs and.16b v12, v14, v8 - mov x13, #13605374474286268416 - dup.2d v13, x13 - mul x13, x0, x5 - mov x14, #6440147467139809280 - dup.2d v14, x14 - umulh x14, x0, x5 - mov x15, #3688448094816436224 - dup.2d v15, x15 - mov x15, #9209861237972664320 - adds x10, x13, x10 - cinc x13, x14, hs - dup.2d v16, x15 - mov x14, #12218265789056155648 - dup.2d v17, x14 - mul x14, x1, x5 - mov x15, #17739678932212383744 - dup.2d v18, x15 - mov x15, #2301339409586323456 - umulh x16, x1, x5 - dup.2d v19, x15 - mov x15, #7822752552742551552 - adds x13, x14, x13 - cinc x14, x16, hs - dup.2d v20, x15 - mov x15, #5071053180419178496 - dup.2d v21, x15 - adds x11, x13, x11 - cinc x13, x14, hs - mov x14, #16352570246982270976 - dup.2d v22, x14 + mov x21, #13605374474286268416 + and x22, x10, x16 + dup.2d v13, x21 + mov x21, #6440147467139809280 + dup.2d v14, x21 + mov x21, #3688448094816436224 + dup.2d v15, x21 + and x21, x12, x16 + mov x23, #9209861237972664320 + dup.2d v16, x23 + mov x23, #12218265789056155648 + dup.2d v17, x23 + mov x23, #17739678932212383744 + dup.2d v18, x23 + and x13, x13, x16 + mov x16, #2301339409586323456 + dup.2d v19, x16 + mov x16, #7822752552742551552 + dup.2d v20, x16 + mov x16, #5071053180419178496 + lsr x13, x13, #63 + dup.2d v21, x16 + mov x16, #16352570246982270976 + dup.2d v22, x16 ucvtf.2d v0, v0 - mul x14, x2, x5 ucvtf.2d v1, v1 ucvtf.2d v2, v2 + adds x16, x17, x22 + adcs x17, x20, x21 + adc x13, x13, x11 ucvtf.2d v11, v11 - umulh x15, x2, x5 ucvtf.2d v3, v3 ucvtf.2d v4, v4 - adds x13, x14, x13 - cinc x14, x15, hs ucvtf.2d v5, v5 ucvtf.2d v6, v6 + mul x20, x0, x4 ucvtf.2d v12, v12 - adds x12, x13, x12 - cinc x13, x14, hs ucvtf.2d v7, v7 mov.16b v23, v9 fmla.2d v23, v0, v4 - mul x14, x3, x5 fsub.2d v24, v10, v23 fmla.2d v24, v0, v4 + umulh x21, x0, x4 add.2d v15, v15, v23 - umulh x5, x3, x5 add.2d v13, v13, v24 mov.16b v23, v9 - adds x13, x14, x13 - cinc x5, x5, hs fmla.2d v23, v0, v5 fsub.2d v24, v10, v23 + mul x22, x1, x4 fmla.2d v24, v0, v5 - adds x4, x13, x4 - cinc x5, x5, hs add.2d v17, v17, v23 add.2d v15, v15, v24 mov.16b v23, v9 - mul x13, x0, x6 fmla.2d v23, v0, v6 + umulh x4, x1, x4 fsub.2d v24, v10, v23 fmla.2d v24, v0, v6 - umulh x14, x0, x6 add.2d v19, v19, v23 add.2d v17, v17, v24 - adds x11, x13, x11 - cinc x13, x14, hs mov.16b v23, v9 fmla.2d v23, v0, v12 + adds x21, x22, x21 + cinc x4, x4, hs fsub.2d v24, v10, v23 - mul x14, x1, x6 fmla.2d v24, v0, v12 add.2d v21, v21, v23 add.2d v19, v19, v24 - umulh x15, x1, x6 mov.16b v23, v9 + mul x22, x0, x5 fmla.2d v23, v0, v7 fsub.2d v24, v10, v23 - adds x13, x14, x13 - cinc x14, x15, hs fmla.2d v24, v0, v7 add.2d v0, v22, v23 - adds x12, x13, x12 - cinc x13, x14, hs add.2d v21, v21, v24 mov.16b v22, v9 + umulh x0, x0, x5 fmla.2d v22, v1, v4 - mul x14, x2, x6 fsub.2d v23, v10, v22 fmla.2d v23, v1, v4 add.2d v17, v17, v22 - umulh x15, x2, x6 add.2d v15, v15, v23 + mul x23, x1, x5 mov.16b v22, v9 fmla.2d v22, v1, v5 - adds x13, x14, x13 - cinc x14, x15, hs fsub.2d v23, v10, v22 fmla.2d v23, v1, v5 - adds x4, x13, x4 - cinc x13, x14, hs add.2d v19, v19, v22 add.2d v17, v17, v23 + umulh x1, x1, x5 mov.16b v22, v9 - mul x14, x3, x6 fmla.2d v22, v1, v6 fsub.2d v23, v10, v22 fmla.2d v23, v1, v6 - umulh x6, x3, x6 add.2d v21, v21, v22 + adds x0, x23, x0 + cinc x1, x1, hs add.2d v19, v19, v23 mov.16b v22, v9 - adds x13, x14, x13 - cinc x6, x6, hs fmla.2d v22, v1, v12 fsub.2d v23, v10, v22 fmla.2d v23, v1, v12 - adds x5, x13, x5 - cinc x6, x6, hs + adds x5, x21, x22 + adcs x0, x4, x0 + cinc x1, x1, hs add.2d v0, v0, v22 add.2d v21, v21, v23 - mul x13, x0, x7 mov.16b v22, v9 fmla.2d v22, v1, v7 fsub.2d v23, v10, v22 - umulh x0, x0, x7 fmla.2d v23, v1, v7 + mul x4, x2, x6 add.2d v1, v20, v22 add.2d v0, v0, v23 - adds x12, x13, x12 - cinc x0, x0, hs mov.16b v20, v9 fmla.2d v20, v2, v4 fsub.2d v22, v10, v20 - mul x13, x1, x7 + umulh x21, x2, x6 fmla.2d v22, v2, v4 add.2d v19, v19, v20 - umulh x1, x1, x7 add.2d v17, v17, v22 mov.16b v20, v9 fmla.2d v20, v2, v5 - adds x0, x13, x0 - cinc x1, x1, hs fsub.2d v22, v10, v20 + mul x22, x3, x6 fmla.2d v22, v2, v5 add.2d v20, v21, v20 - adds x0, x0, x4 - cinc x1, x1, hs add.2d v19, v19, v22 mov.16b v21, v9 fmla.2d v21, v2, v6 - mul x4, x2, x7 + umulh x6, x3, x6 fsub.2d v22, v10, v21 fmla.2d v22, v2, v6 - umulh x2, x2, x7 add.2d v0, v0, v21 add.2d v20, v20, v22 mov.16b v21, v9 - adds x1, x4, x1 - cinc x2, x2, hs fmla.2d v21, v2, v12 + adds x21, x22, x21 + cinc x6, x6, hs fsub.2d v22, v10, v21 fmla.2d v22, v2, v12 - adds x1, x1, x5 - cinc x2, x2, hs add.2d v1, v1, v21 add.2d v0, v0, v22 mov.16b v21, v9 - mul x4, x3, x7 + mul x22, x2, x7 fmla.2d v21, v2, v7 fsub.2d v22, v10, v21 - umulh x3, x3, x7 fmla.2d v22, v2, v7 add.2d v2, v18, v21 add.2d v1, v1, v22 - adds x2, x4, x2 - cinc x3, x3, hs + umulh x2, x2, x7 mov.16b v18, v9 fmla.2d v18, v11, v4 fsub.2d v21, v10, v18 - adds x2, x2, x6 - cinc x3, x3, hs fmla.2d v21, v11, v4 add.2d v18, v20, v18 add.2d v19, v19, v21 - mov x4, #48718 + mul x23, x3, x7 mov.16b v20, v9 fmla.2d v20, v11, v5 - movk x4, #4732, lsl 16 fsub.2d v21, v10, v20 fmla.2d v21, v11, v5 add.2d v0, v0, v20 - movk x4, #45078, lsl 32 + umulh x3, x3, x7 add.2d v18, v18, v21 mov.16b v20, v9 fmla.2d v20, v11, v6 - movk x4, #39852, lsl 48 fsub.2d v21, v10, v20 fmla.2d v21, v11, v6 add.2d v1, v1, v20 - mov x5, #16676 + adds x2, x23, x2 + cinc x3, x3, hs add.2d v0, v0, v21 mov.16b v20, v9 - movk x5, #12692, lsl 16 fmla.2d v20, v11, v12 fsub.2d v21, v10, v20 fmla.2d v21, v11, v12 - movk x5, #20986, lsl 32 + adds x7, x21, x22 + adcs x2, x6, x2 + cinc x3, x3, hs add.2d v2, v2, v20 add.2d v1, v1, v21 mov.16b v20, v9 - movk x5, #2848, lsl 48 fmla.2d v20, v11, v7 fsub.2d v21, v10, v20 fmla.2d v21, v11, v7 - mov x6, #51052 + mul x6, x10, x14 add.2d v11, v16, v20 add.2d v2, v2, v21 - movk x6, #24721, lsl 16 mov.16b v16, v9 fmla.2d v16, v3, v4 fsub.2d v20, v10, v16 - movk x6, #61092, lsl 32 + umulh x21, x10, x14 fmla.2d v20, v3, v4 add.2d v0, v0, v16 add.2d v4, v18, v20 - movk x6, #45156, lsl 48 mov.16b v16, v9 fmla.2d v16, v3, v5 + mul x22, x12, x14 fsub.2d v18, v10, v16 - mov x7, #3197 fmla.2d v18, v3, v5 add.2d v1, v1, v16 - movk x7, #18936, lsl 16 add.2d v0, v0, v18 mov.16b v5, v9 fmla.2d v5, v3, v6 - movk x7, #10922, lsl 32 + umulh x14, x12, x14 fsub.2d v16, v10, v5 fmla.2d v16, v3, v6 add.2d v2, v2, v5 - movk x7, #11014, lsl 48 add.2d v1, v1, v16 mov.16b v5, v9 + adds x21, x22, x21 + cinc x14, x14, hs fmla.2d v5, v3, v12 - mul x13, x4, x9 fsub.2d v6, v10, v5 fmla.2d v6, v3, v12 - umulh x4, x4, x9 add.2d v5, v11, v5 add.2d v2, v2, v6 mov.16b v6, v9 - adds x12, x13, x12 - cinc x4, x4, hs + mul x22, x10, x15 fmla.2d v6, v3, v7 fsub.2d v11, v10, v6 fmla.2d v11, v3, v7 - mul x13, x5, x9 add.2d v3, v14, v6 add.2d v5, v5, v11 + umulh x10, x10, x15 usra.2d v15, v13, #52 - umulh x5, x5, x9 usra.2d v17, v15, #52 usra.2d v19, v17, #52 usra.2d v4, v19, #52 - adds x4, x13, x4 - cinc x5, x5, hs and.16b v6, v13, v8 and.16b v7, v15, v8 - adds x0, x4, x0 - cinc x4, x5, hs + mul x23, x12, x15 and.16b v11, v17, v8 and.16b v8, v19, v8 ucvtf.2d v6, v6 - mul x5, x6, x9 - mov x13, #37864 - movk x13, #1815, lsl 16 - movk x13, #28960, lsl 32 - umulh x6, x6, x9 - movk x13, #17153, lsl 48 - dup.2d v12, x13 + mov x24, #37864 + movk x24, #1815, lsl 16 + umulh x12, x12, x15 + movk x24, #28960, lsl 32 + movk x24, #17153, lsl 48 + dup.2d v12, x24 mov.16b v13, v9 - adds x4, x5, x4 - cinc x5, x6, hs fmla.2d v13, v6, v12 + adds x10, x23, x10 + cinc x12, x12, hs fsub.2d v14, v10, v13 - adds x1, x4, x1 - cinc x4, x5, hs fmla.2d v14, v6, v12 add.2d v0, v0, v13 add.2d v4, v4, v14 - mul x5, x7, x9 - mov x6, #46128 - movk x6, #29964, lsl 16 - movk x6, #7587, lsl 32 - umulh x7, x7, x9 - movk x6, #17161, lsl 48 - dup.2d v12, x6 + mov x15, #46128 + movk x15, #29964, lsl 16 + adds x21, x21, x22 + adcs x10, x14, x10 + cinc x12, x12, hs + movk x15, #7587, lsl 32 + movk x15, #17161, lsl 48 + dup.2d v12, x15 mov.16b v13, v9 - adds x4, x5, x4 - cinc x5, x7, hs fmla.2d v13, v6, v12 + adds x10, x10, x16 + adcs x12, x12, x17 + adc x13, x13, x11 fsub.2d v14, v10, v13 - adds x2, x4, x2 - cinc x4, x5, hs fmla.2d v14, v6, v12 add.2d v1, v1, v13 add.2d v0, v0, v14 - add x3, x3, x4 - mov x4, #52826 - movk x4, #57790, lsl 16 - movk x4, #55431, lsl 32 - mov x5, #56431 - movk x4, #17196, lsl 48 - dup.2d v12, x4 + mov x14, #52826 + movk x14, #57790, lsl 16 + subs x6, x6, x20 + sbcs x15, x21, x5 + sbcs x10, x10, x0 + sbcs x12, x12, x1 + sbcs x13, x13, x11 + movk x14, #55431, lsl 32 + movk x14, #17196, lsl 48 + dup.2d v12, x14 mov.16b v13, v9 - movk x5, #30457, lsl 16 fmla.2d v13, v6, v12 + subs x6, x6, x4 + sbcs x14, x15, x7 + sbcs x10, x10, x2 + sbcs x12, x12, x3 + sbcs x13, x13, x11 fsub.2d v14, v10, v13 - movk x5, #30012, lsl 32 fmla.2d v14, v6, v12 add.2d v2, v2, v13 add.2d v1, v1, v14 - movk x5, #6382, lsl 48 - mov x4, #31276 - movk x4, #21262, lsl 16 - movk x4, #2304, lsl 32 - mov x6, #59151 - movk x4, #17182, lsl 48 - dup.2d v12, x4 + mov x15, #31276 + movk x15, #21262, lsl 16 + adds x0, x0, x6 + adcs x1, x1, x14 + adcs x4, x4, x10 + adcs x6, x7, x12 + adcs x2, x2, x13 + adc x3, x3, x11 + movk x15, #2304, lsl 32 + movk x15, #17182, lsl 48 + dup.2d v12, x15 mov.16b v13, v9 - movk x6, #41769, lsl 16 fmla.2d v13, v6, v12 + ldp x7, x10, [x8, #0] fsub.2d v14, v10, v13 - movk x6, #32276, lsl 32 fmla.2d v14, v6, v12 add.2d v5, v5, v13 add.2d v2, v2, v14 - movk x6, #21677, lsl 48 - mov x4, #28672 - movk x4, #24515, lsl 16 - movk x4, #54929, lsl 32 - mov x7, #34015 - movk x4, #17064, lsl 48 - dup.2d v12, x4 + mov x11, #28672 + mul x12, x20, x7 + movk x11, #24515, lsl 16 + movk x11, #54929, lsl 32 + movk x11, #17064, lsl 48 + dup.2d v12, x11 mov.16b v13, v9 - movk x7, #20342, lsl 16 fmla.2d v13, v6, v12 + umulh x7, x20, x7 fsub.2d v14, v10, v13 - movk x7, #13935, lsl 32 fmla.2d v14, v6, v12 add.2d v3, v3, v13 add.2d v5, v5, v14 - movk x7, #11030, lsl 48 ucvtf.2d v6, v7 - mov x4, #44768 - movk x4, #51919, lsl 16 - mov x9, #13689 - movk x4, #6346, lsl 32 - movk x4, #17133, lsl 48 - dup.2d v7, x4 - movk x9, #8159, lsl 16 + mul x11, x20, x10 + mov x13, #44768 + movk x13, #51919, lsl 16 + movk x13, #6346, lsl 32 + movk x13, #17133, lsl 48 + dup.2d v7, x13 mov.16b v12, v9 + umulh x10, x20, x10 fmla.2d v12, v6, v7 - movk x9, #215, lsl 32 fsub.2d v13, v10, v12 fmla.2d v13, v6, v7 add.2d v0, v0, v12 - movk x9, #4913, lsl 48 add.2d v4, v4, v13 - mov x4, #47492 - movk x4, #23630, lsl 16 - mul x13, x5, x10 - movk x4, #49985, lsl 32 - movk x4, #17168, lsl 48 - dup.2d v7, x4 - umulh x4, x5, x10 + ldp x13, x14, [x8, #16] + mov x15, #47492 + movk x15, #23630, lsl 16 + movk x15, #49985, lsl 32 + movk x15, #17168, lsl 48 + dup.2d v7, x15 mov.16b v12, v9 + mul x15, x20, x13 fmla.2d v12, v6, v7 - adds x5, x13, x12 - cinc x4, x4, hs fsub.2d v13, v10, v12 fmla.2d v13, v6, v7 add.2d v1, v1, v12 - mul x12, x6, x10 add.2d v0, v0, v13 - mov x13, #57936 - movk x13, #54828, lsl 16 - umulh x6, x6, x10 - movk x13, #18292, lsl 32 - movk x13, #17197, lsl 48 - dup.2d v7, x13 - adds x4, x12, x4 - cinc x6, x6, hs + umulh x13, x20, x13 + mov x16, #57936 + movk x16, #54828, lsl 16 + movk x16, #18292, lsl 32 + movk x16, #17197, lsl 48 + dup.2d v7, x16 + mul x16, x20, x14 mov.16b v12, v9 fmla.2d v12, v6, v7 - adds x0, x4, x0 - cinc x4, x6, hs fsub.2d v13, v10, v12 fmla.2d v13, v6, v7 add.2d v2, v2, v12 - mul x6, x7, x10 add.2d v1, v1, v13 - mov x12, #17708 - movk x12, #43915, lsl 16 - umulh x7, x7, x10 - movk x12, #64348, lsl 32 - movk x12, #17188, lsl 48 - dup.2d v7, x12 - adds x4, x6, x4 - cinc x6, x7, hs + umulh x14, x20, x14 + mov x17, #17708 + movk x17, #43915, lsl 16 + movk x17, #64348, lsl 32 + movk x17, #17188, lsl 48 + dup.2d v7, x17 + adds x7, x11, x7 + adcs x10, x15, x10 + adcs x11, x16, x13 + cinc x13, x14, hs mov.16b v12, v9 fmla.2d v12, v6, v7 fsub.2d v13, v10, v12 - adds x1, x4, x1 - cinc x4, x6, hs fmla.2d v13, v6, v7 add.2d v5, v5, v12 - mul x6, x9, x10 add.2d v2, v2, v13 + adds x1, x1, x12 + adcs x4, x4, x7 + adcs x6, x6, x10 + adcs x2, x2, x11 + adc x3, x3, x13 mov x7, #29184 movk x7, #20789, lsl 16 - umulh x9, x9, x10 movk x7, #19197, lsl 32 movk x7, #17083, lsl 48 dup.2d v7, x7 - adds x4, x6, x4 - cinc x6, x9, hs + ldp x7, x10, [x8, #32] mov.16b v12, v9 fmla.2d v12, v6, v7 fsub.2d v13, v10, v12 - adds x2, x4, x2 - cinc x4, x6, hs fmla.2d v13, v6, v7 add.2d v3, v3, v12 - add x3, x3, x4 add.2d v5, v5, v13 + mul x11, x5, x7 ucvtf.2d v6, v11 - mov x4, #58856 - mov x6, #61005 - movk x4, #14953, lsl 16 - movk x4, #15155, lsl 32 - movk x4, #17181, lsl 48 - movk x6, #58262, lsl 16 - dup.2d v7, x4 + mov x12, #58856 + movk x12, #14953, lsl 16 + movk x12, #15155, lsl 32 + movk x12, #17181, lsl 48 + umulh x7, x5, x7 + dup.2d v7, x12 mov.16b v11, v9 fmla.2d v11, v6, v7 - movk x6, #32851, lsl 32 fsub.2d v12, v10, v11 fmla.2d v12, v6, v7 - movk x6, #11582, lsl 48 + mul x12, x5, x10 add.2d v0, v0, v11 add.2d v4, v4, v12 - mov x4, #35392 - mov x7, #37581 - movk x4, #12477, lsl 16 - movk x4, #56780, lsl 32 - movk x4, #17142, lsl 48 - movk x7, #43836, lsl 16 - dup.2d v7, x4 + mov x13, #35392 + movk x13, #12477, lsl 16 + movk x13, #56780, lsl 32 + movk x13, #17142, lsl 48 + umulh x10, x5, x10 + dup.2d v7, x13 mov.16b v11, v9 fmla.2d v11, v6, v7 - movk x7, #36286, lsl 32 fsub.2d v12, v10, v11 fmla.2d v12, v6, v7 - movk x7, #51783, lsl 48 + ldp x13, x14, [x8, #48] add.2d v1, v1, v11 add.2d v0, v0, v12 - mov x4, #9848 - mov x9, #10899 - movk x4, #54501, lsl 16 - movk x4, #31540, lsl 32 - movk x4, #17170, lsl 48 - movk x9, #30709, lsl 16 - dup.2d v7, x4 + mov x15, #9848 + movk x15, #54501, lsl 16 + movk x15, #31540, lsl 32 + movk x15, #17170, lsl 48 + mul x16, x5, x13 + dup.2d v7, x15 mov.16b v11, v9 fmla.2d v11, v6, v7 - movk x9, #61551, lsl 32 fsub.2d v12, v10, v11 fmla.2d v12, v6, v7 - movk x9, #45784, lsl 48 + umulh x13, x5, x13 add.2d v2, v2, v11 add.2d v1, v1, v12 - mov x4, #9584 - mov x10, #36612 - movk x4, #63883, lsl 16 - movk x4, #18253, lsl 32 - movk x4, #17190, lsl 48 - movk x10, #63402, lsl 16 - dup.2d v7, x4 + mov x15, #9584 + movk x15, #63883, lsl 16 + movk x15, #18253, lsl 32 + movk x15, #17190, lsl 48 + mul x17, x5, x14 + dup.2d v7, x15 mov.16b v11, v9 fmla.2d v11, v6, v7 - movk x10, #47623, lsl 32 fsub.2d v12, v10, v11 fmla.2d v12, v6, v7 - movk x10, #9430, lsl 48 + umulh x5, x5, x14 add.2d v5, v5, v11 add.2d v2, v2, v12 - mov x4, #51712 - mul x12, x6, x11 - movk x4, #16093, lsl 16 - movk x4, #30633, lsl 32 - movk x4, #17068, lsl 48 - umulh x6, x6, x11 - dup.2d v7, x4 + mov x14, #51712 + movk x14, #16093, lsl 16 + movk x14, #30633, lsl 32 + adds x7, x12, x7 + adcs x10, x16, x10 + adcs x12, x17, x13 + cinc x5, x5, hs + movk x14, #17068, lsl 48 + dup.2d v7, x14 mov.16b v11, v9 fmla.2d v11, v6, v7 - adds x4, x12, x5 - cinc x5, x6, hs fsub.2d v12, v10, v11 fmla.2d v12, v6, v7 - mul x6, x7, x11 + ldp x13, x14, [x8, #64] add.2d v3, v3, v11 add.2d v5, v5, v12 ucvtf.2d v6, v8 - umulh x7, x7, x11 - mov x12, #34724 - movk x12, #40393, lsl 16 - movk x12, #23752, lsl 32 - adds x5, x6, x5 - cinc x6, x7, hs - movk x12, #17184, lsl 48 - dup.2d v7, x12 + mov x15, #34724 + movk x15, #40393, lsl 16 + mul x16, x0, x13 + movk x15, #23752, lsl 32 + movk x15, #17184, lsl 48 + dup.2d v7, x15 mov.16b v8, v9 - adds x0, x5, x0 - cinc x5, x6, hs fmla.2d v8, v6, v7 fsub.2d v11, v10, v8 - mul x6, x9, x11 + umulh x13, x0, x13 fmla.2d v11, v6, v7 add.2d v0, v0, v8 add.2d v4, v4, v11 - umulh x7, x9, x11 - mov x9, #25532 - movk x9, #31025, lsl 16 - movk x9, #10002, lsl 32 - adds x5, x6, x5 - cinc x6, x7, hs - movk x9, #17199, lsl 48 - dup.2d v7, x9 + mov x15, #25532 + movk x15, #31025, lsl 16 + mul x17, x0, x14 + movk x15, #10002, lsl 32 + movk x15, #17199, lsl 48 + dup.2d v7, x15 mov.16b v8, v9 - adds x1, x5, x1 - cinc x5, x6, hs fmla.2d v8, v6, v7 fsub.2d v11, v10, v8 - mul x6, x10, x11 + umulh x14, x0, x14 fmla.2d v11, v6, v7 add.2d v1, v1, v8 add.2d v0, v0, v11 - umulh x7, x10, x11 - mov x9, #18830 - movk x9, #2465, lsl 16 - movk x9, #36348, lsl 32 - adds x5, x6, x5 - cinc x6, x7, hs - movk x9, #17194, lsl 48 - dup.2d v7, x9 + mov x15, #18830 + movk x15, #2465, lsl 16 + ldp x20, x21, [x8, #80] + movk x15, #36348, lsl 32 + movk x15, #17194, lsl 48 + dup.2d v7, x15 mov.16b v8, v9 - adds x2, x5, x2 - cinc x5, x6, hs fmla.2d v8, v6, v7 + mul x15, x0, x20 fsub.2d v11, v10, v8 fmla.2d v11, v6, v7 - add x3, x3, x5 add.2d v2, v2, v8 add.2d v1, v1, v11 - mov x5, #65535 - mov x6, #21566 - movk x6, #43708, lsl 16 - movk x6, #57685, lsl 32 - movk x5, #61439, lsl 16 - movk x6, #17185, lsl 48 - dup.2d v7, x6 + mov x22, #21566 + movk x22, #43708, lsl 16 + umulh x20, x0, x20 + movk x22, #57685, lsl 32 + movk x22, #17185, lsl 48 + dup.2d v7, x22 mov.16b v8, v9 - movk x5, #62867, lsl 32 fmla.2d v8, v6, v7 + mul x22, x0, x21 fsub.2d v11, v10, v8 fmla.2d v11, v6, v7 - movk x5, #49889, lsl 48 add.2d v5, v5, v8 add.2d v2, v2, v11 - mul x5, x5, x4 - mov x6, #3072 - movk x6, #8058, lsl 16 - movk x6, #46097, lsl 32 - mov x7, #1 - movk x6, #17047, lsl 48 - dup.2d v7, x6 + mov x23, #3072 + movk x23, #8058, lsl 16 + umulh x0, x0, x21 + movk x23, #46097, lsl 32 + movk x23, #17047, lsl 48 + dup.2d v7, x23 mov.16b v8, v9 - movk x7, #61440, lsl 16 fmla.2d v8, v6, v7 + adds x13, x17, x13 + adcs x14, x15, x14 + adcs x15, x22, x20 + cinc x0, x0, hs fsub.2d v11, v10, v8 fmla.2d v11, v6, v7 - movk x7, #62867, lsl 32 add.2d v3, v3, v8 add.2d v5, v5, v11 - movk x7, #17377, lsl 48 - mov x6, #65535 - movk x6, #61439, lsl 16 - movk x6, #62867, lsl 32 - mov x9, #28817 - movk x6, #1, lsl 48 - umov x10, v4.d[0] - umov x11, v4.d[1] - movk x9, #31161, lsl 16 - mul x10, x10, x6 - mul x6, x11, x6 - and x10, x10, x8 - movk x9, #59464, lsl 32 - and x6, x6, x8 - ins v6.d[0], x10 - ins v6.d[1], x6 - movk x9, #10291, lsl 48 + mov x17, #65535 + movk x17, #61439, lsl 16 + adds x11, x11, x16 + adcs x7, x7, x13 + adcs x10, x10, x14 + adcs x12, x12, x15 + adc x0, x5, x0 + movk x17, #62867, lsl 32 + movk x17, #1, lsl 48 + umov x5, v4.d[0] + umov x13, v4.d[1] + mul x5, x5, x17 + adds x1, x1, x11 + adcs x4, x4, x7 + adcs x6, x6, x10 + adcs x2, x2, x12 + adc x0, x3, x0 + mul x3, x13, x17 + and x5, x5, x9 + and x3, x3, x9 + ins v6.d[0], x5 + ins v6.d[1], x3 ucvtf.2d v6, v6 - mov x6, #16 - movk x6, #22847, lsl 32 - mov x8, #22621 - movk x6, #17151, lsl 48 - dup.2d v7, x6 + ldr x3, [x8, #128] + mov x5, #16 + movk x5, #22847, lsl 32 + movk x5, #17151, lsl 48 + dup.2d v7, x5 mov.16b v8, v9 - movk x8, #33153, lsl 16 fmla.2d v8, v6, v7 + mul x3, x3, x1 fsub.2d v11, v10, v8 fmla.2d v11, v6, v7 - movk x8, #17846, lsl 32 add.2d v0, v0, v8 add.2d v4, v4, v11 - movk x8, #47184, lsl 48 - mov x6, #20728 - movk x6, #23588, lsl 16 - movk x6, #7790, lsl 32 - mov x10, #41001 - movk x6, #17170, lsl 48 - dup.2d v7, x6 + mov x5, #20728 + ldp x7, x9, [x8, #96] + movk x5, #23588, lsl 16 + movk x5, #7790, lsl 32 + movk x5, #17170, lsl 48 + dup.2d v7, x5 mov.16b v8, v9 - movk x10, #57649, lsl 16 fmla.2d v8, v6, v7 + ldp x5, x8, [x8, #112] fsub.2d v11, v10, v8 fmla.2d v11, v6, v7 - movk x10, #20082, lsl 32 add.2d v1, v1, v8 add.2d v0, v0, v11 - movk x10, #12388, lsl 48 - mov x6, #16000 - movk x6, #53891, lsl 16 - movk x6, #5509, lsl 32 - mul x11, x7, x5 - movk x6, #17144, lsl 48 - dup.2d v7, x6 + mov x10, #16000 + umulh x7, x3, x7 + movk x10, #53891, lsl 16 + movk x10, #5509, lsl 32 + movk x10, #17144, lsl 48 + dup.2d v7, x10 mov.16b v8, v9 - umulh x6, x7, x5 fmla.2d v8, v6, v7 + mul x10, x3, x9 fsub.2d v11, v10, v8 fmla.2d v11, v6, v7 - cmn x11, x4 - cinc x6, x6, hs add.2d v2, v2, v8 add.2d v7, v1, v11 - mul x4, x9, x5 - mov x7, #46800 - movk x7, #2568, lsl 16 - movk x7, #1335, lsl 32 - umulh x9, x9, x5 - movk x7, #17188, lsl 48 - dup.2d v1, x7 + mov x11, #46800 + umulh x9, x3, x9 + movk x11, #2568, lsl 16 + movk x11, #1335, lsl 32 + movk x11, #17188, lsl 48 + dup.2d v1, x11 mov.16b v8, v9 - adds x4, x4, x6 - cinc x6, x9, hs + mul x11, x3, x5 fmla.2d v8, v6, v1 fsub.2d v11, v10, v8 fmla.2d v11, v6, v1 - adds x0, x4, x0 - cinc x4, x6, hs add.2d v1, v5, v8 add.2d v5, v2, v11 - mul x6, x8, x5 - mov x7, #39040 - movk x7, #14704, lsl 16 - movk x7, #12839, lsl 32 - umulh x8, x8, x5 - movk x7, #17096, lsl 48 - dup.2d v2, x7 + mov x12, #39040 + umulh x5, x3, x5 + movk x12, #14704, lsl 16 + movk x12, #12839, lsl 32 + movk x12, #17096, lsl 48 + dup.2d v2, x12 mov.16b v8, v9 - adds x4, x6, x4 - cinc x6, x8, hs + mul x12, x3, x8 fmla.2d v8, v6, v2 fsub.2d v9, v10, v8 fmla.2d v9, v6, v2 - adds x1, x4, x1 - cinc x4, x6, hs add.2d v6, v3, v8 add.2d v8, v1, v9 - mul x6, x10, x5 ssra.2d v0, v4, #52 + umulh x3, x3, x8 ssra.2d v7, v0, #52 ssra.2d v5, v7, #52 - umulh x5, x10, x5 ssra.2d v8, v5, #52 ssra.2d v6, v8, #52 ushr.2d v1, v7, #12 - adds x4, x6, x4 - cinc x5, x5, hs + cmp x1, #1 + adcs x1, x4, x10 + adcs x4, x6, x11 + adcs x2, x2, x12 + cinc x6, x0, hs ushr.2d v2, v5, #24 ushr.2d v3, v8, #36 sli.2d v0, v7, #52 - adds x2, x4, x2 - cinc x4, x5, hs sli.2d v1, v5, #40 sli.2d v2, v8, #28 sli.2d v3, v6, #16 - add x3, x3, x4 + adds x0, x1, x7 + adcs x1, x4, x9 + adcs x2, x2, x5 + adc x3, x6, x3 diff --git a/skyscraper/bn254-multiplier/src/aarch64/montgomery_interleaved_4.s b/skyscraper/bn254-multiplier/src/aarch64/montgomery_interleaved_4.s index 34a4f930a..84ed9c8b3 100644 --- a/skyscraper/bn254-multiplier/src/aarch64/montgomery_interleaved_4.s +++ b/skyscraper/bn254-multiplier/src/aarch64/montgomery_interleaved_4.s @@ -8,8 +8,7 @@ // lateout("x0") out[0], lateout("x1") out[1], lateout("x2") out[2], lateout("x3") out[3], // lateout("x4") out1[0], lateout("x5") out1[1], lateout("x6") out1[2], lateout("x7") out1[3], // lateout("v0") outv[0], lateout("v1") outv[1], lateout("v2") outv[2], lateout("v3") outv[3], -// lateout("x8") _, lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _, lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _, lateout("x17") _, lateout("x20") _, lateout("x21") _, lateout("x22") _, lateout("x23") _, lateout("x24") _, lateout("x25") _, lateout("x26") _, lateout("v4") _, lateout("v5") _, lateout("v6") _, lateout("v7") _, lateout("v8") _, lateout("v9") _, lateout("v10") _, lateout("v11") _, lateout("v12") _, lateout("v13") _, lateout("v14") _, lateout("v15") _, lateout("v16") _, lateout("v17") _, lateout("v18") _, lateout("v19") _, lateout("v20") _, lateout("v21") _, lateout("v22") _, lateout("v23") _, lateout("v24") _, -// lateout("lr") _ +// lateout("x8") _, lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _, lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _, lateout("x17") _, lateout("x20") _, lateout("x21") _, lateout("x22") _, lateout("x23") _, lateout("x24") _, lateout("x25") _, lateout("x26") _, lateout("v4") _, lateout("v5") _, lateout("v6") _, lateout("v7") _, lateout("v8") _, lateout("v9") _, lateout("v10") _, lateout("v11") _, lateout("v12") _, lateout("v13") _, lateout("v14") _, lateout("v15") _, lateout("v16") _, lateout("v17") _, lateout("v18") _, lateout("v19") _, lateout("v20") _, lateout("v21") _, lateout("v22") _, lateout("v23") _, lateout("v24") _ mov x16, #4503599627370495 mul x17, x0, x4 dup.2d v8, x16 diff --git a/skyscraper/bn254-multiplier/src/aarch64/montgomery_mul.s b/skyscraper/bn254-multiplier/src/aarch64/montgomery_mul.s new file mode 100644 index 000000000..ff80e1507 --- /dev/null +++ b/skyscraper/bn254-multiplier/src/aarch64/montgomery_mul.s @@ -0,0 +1,163 @@ +// GENERATED FILE, DO NOT EDIT! +// in("x0") a[0], in("x1") a[1], in("x2") a[2], in("x3") a[3], +// in("x4") b[0], in("x5") b[1], in("x6") b[2], in("x7") b[3], +// in("x8") table, +// lateout("x0") out[0], lateout("x1") out[1], lateout("x2") out[2], lateout("x3") out[3], +// lateout("x4") _, lateout("x5") _, lateout("x6") _, lateout("x7") _, lateout("x8") _, lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _, lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _, lateout("x17") _, lateout("x20") _, lateout("x21") _, lateout("x22") _ + mov x9, #0 + adds x10, x0, x2 + adcs x11, x1, x3 + csetm x12, hs + adds x13, x4, x6 + adcs x14, x5, x7 + csetm x15, hs + and x16, x13, x12 + and x17, x14, x12 + and x20, x10, x15 + and x21, x11, x15 + and x12, x12, x15 + lsr x12, x12, #63 + adds x15, x16, x20 + adcs x16, x17, x21 + adc x12, x12, x9 + mul x17, x0, x4 + umulh x20, x0, x4 + mul x21, x1, x4 + umulh x4, x1, x4 + adds x20, x21, x20 + cinc x4, x4, hs + mul x21, x0, x5 + umulh x0, x0, x5 + mul x22, x1, x5 + umulh x1, x1, x5 + adds x0, x22, x0 + cinc x1, x1, hs + adds x5, x20, x21 + adcs x0, x4, x0 + cinc x1, x1, hs + mul x4, x2, x6 + umulh x20, x2, x6 + mul x21, x3, x6 + umulh x6, x3, x6 + adds x20, x21, x20 + cinc x6, x6, hs + mul x21, x2, x7 + umulh x2, x2, x7 + mul x22, x3, x7 + umulh x3, x3, x7 + adds x2, x22, x2 + cinc x3, x3, hs + adds x7, x20, x21 + adcs x2, x6, x2 + cinc x3, x3, hs + mul x6, x10, x13 + umulh x20, x10, x13 + mul x21, x11, x13 + umulh x13, x11, x13 + adds x20, x21, x20 + cinc x13, x13, hs + mul x21, x10, x14 + umulh x10, x10, x14 + mul x22, x11, x14 + umulh x11, x11, x14 + adds x10, x22, x10 + cinc x11, x11, hs + adds x14, x20, x21 + adcs x10, x13, x10 + cinc x11, x11, hs + adds x10, x10, x15 + adcs x11, x11, x16 + adc x12, x12, x9 + subs x6, x6, x17 + sbcs x13, x14, x5 + sbcs x10, x10, x0 + sbcs x11, x11, x1 + sbcs x12, x12, x9 + subs x6, x6, x4 + sbcs x13, x13, x7 + sbcs x10, x10, x2 + sbcs x11, x11, x3 + sbcs x12, x12, x9 + adds x0, x0, x6 + adcs x1, x1, x13 + adcs x4, x4, x10 + adcs x6, x7, x11 + adcs x2, x2, x12 + adc x3, x3, x9 + ldp x7, x9, [x8, #0] + mul x10, x17, x7 + umulh x7, x17, x7 + mul x11, x17, x9 + umulh x9, x17, x9 + ldp x12, x13, [x8, #16] + mul x14, x17, x12 + umulh x12, x17, x12 + mul x15, x17, x13 + umulh x13, x17, x13 + adds x7, x11, x7 + adcs x9, x14, x9 + adcs x11, x15, x12 + cinc x12, x13, hs + adds x1, x1, x10 + adcs x4, x4, x7 + adcs x6, x6, x9 + adcs x2, x2, x11 + adc x3, x3, x12 + ldp x7, x9, [x8, #32] + mul x10, x5, x7 + umulh x7, x5, x7 + mul x11, x5, x9 + umulh x9, x5, x9 + ldp x12, x13, [x8, #48] + mul x14, x5, x12 + umulh x12, x5, x12 + mul x15, x5, x13 + umulh x5, x5, x13 + adds x7, x11, x7 + adcs x9, x14, x9 + adcs x11, x15, x12 + cinc x5, x5, hs + ldp x12, x13, [x8, #64] + mul x14, x0, x12 + umulh x12, x0, x12 + mul x15, x0, x13 + umulh x13, x0, x13 + ldp x16, x17, [x8, #80] + mul x20, x0, x16 + umulh x16, x0, x16 + mul x21, x0, x17 + umulh x0, x0, x17 + adds x12, x15, x12 + adcs x13, x20, x13 + adcs x15, x21, x16 + cinc x0, x0, hs + adds x10, x10, x14 + adcs x7, x7, x12 + adcs x9, x9, x13 + adcs x11, x11, x15 + adc x0, x5, x0 + adds x1, x1, x10 + adcs x4, x4, x7 + adcs x5, x6, x9 + adcs x2, x2, x11 + adc x0, x3, x0 + ldr x3, [x8, #128] + mul x3, x3, x1 + ldp x6, x7, [x8, #96] + ldp x8, x9, [x8, #112] + umulh x6, x3, x6 + mul x10, x3, x7 + umulh x7, x3, x7 + mul x11, x3, x8 + umulh x8, x3, x8 + mul x12, x3, x9 + umulh x3, x3, x9 + cmp x1, #1 + adcs x1, x4, x10 + adcs x4, x5, x11 + adcs x2, x2, x12 + cinc x5, x0, hs + adds x0, x1, x6 + adcs x1, x4, x7 + adcs x2, x2, x8 + adc x3, x5, x3 diff --git a/skyscraper/bn254-multiplier/src/aarch64/montgomery_square_interleaved_3.s b/skyscraper/bn254-multiplier/src/aarch64/montgomery_square_interleaved_3.s index 78de47acb..fe94c2721 100644 --- a/skyscraper/bn254-multiplier/src/aarch64/montgomery_square_interleaved_3.s +++ b/skyscraper/bn254-multiplier/src/aarch64/montgomery_square_interleaved_3.s @@ -3,8 +3,7 @@ // in("v0") av[0], in("v1") av[1], in("v2") av[2], in("v3") av[3], // lateout("x0") out[0], lateout("x1") out[1], lateout("x2") out[2], lateout("x3") out[3], // lateout("v0") outv[0], lateout("v1") outv[1], lateout("v2") outv[2], lateout("v3") outv[3], -// lateout("x4") _, lateout("x5") _, lateout("x6") _, lateout("x7") _, lateout("x8") _, lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _, lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _, lateout("x17") _, lateout("v4") _, lateout("v5") _, lateout("v6") _, lateout("v7") _, lateout("v8") _, lateout("v9") _, lateout("v10") _, lateout("v11") _, lateout("v12") _, lateout("v13") _, lateout("v14") _, lateout("v15") _, lateout("v16") _, lateout("v17") _, lateout("v18") _, lateout("v19") _, -// lateout("lr") _ +// lateout("x4") _, lateout("x5") _, lateout("x6") _, lateout("x7") _, lateout("x8") _, lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _, lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _, lateout("x17") _, lateout("v4") _, lateout("v5") _, lateout("v6") _, lateout("v7") _, lateout("v8") _, lateout("v9") _, lateout("v10") _, lateout("v11") _, lateout("v12") _, lateout("v13") _, lateout("v14") _, lateout("v15") _, lateout("v16") _, lateout("v17") _, lateout("v18") _, lateout("v19") _ mov x4, #4503599627370495 dup.2d v4, x4 mul x5, x0, x0 diff --git a/skyscraper/bn254-multiplier/src/aarch64/montgomery_square_interleaved_4.s b/skyscraper/bn254-multiplier/src/aarch64/montgomery_square_interleaved_4.s index 8902ccd32..e659ed5b3 100644 --- a/skyscraper/bn254-multiplier/src/aarch64/montgomery_square_interleaved_4.s +++ b/skyscraper/bn254-multiplier/src/aarch64/montgomery_square_interleaved_4.s @@ -5,8 +5,7 @@ // lateout("x0") out[0], lateout("x1") out[1], lateout("x2") out[2], lateout("x3") out[3], // lateout("x4") out1[0], lateout("x5") out1[1], lateout("x6") out1[2], lateout("x7") out1[3], // lateout("v0") outv[0], lateout("v1") outv[1], lateout("v2") outv[2], lateout("v3") outv[3], -// lateout("x8") _, lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _, lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _, lateout("x17") _, lateout("x20") _, lateout("x21") _, lateout("x22") _, lateout("x23") _, lateout("x24") _, lateout("v4") _, lateout("v5") _, lateout("v6") _, lateout("v7") _, lateout("v8") _, lateout("v9") _, lateout("v10") _, lateout("v11") _, lateout("v12") _, lateout("v13") _, lateout("v14") _, lateout("v15") _, lateout("v16") _, lateout("v17") _, lateout("v18") _, lateout("v19") _, -// lateout("lr") _ +// lateout("x8") _, lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _, lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _, lateout("x17") _, lateout("x20") _, lateout("x21") _, lateout("x22") _, lateout("x23") _, lateout("x24") _, lateout("v4") _, lateout("v5") _, lateout("v6") _, lateout("v7") _, lateout("v8") _, lateout("v9") _, lateout("v10") _, lateout("v11") _, lateout("v12") _, lateout("v13") _, lateout("v14") _, lateout("v15") _, lateout("v16") _, lateout("v17") _, lateout("v18") _, lateout("v19") _ mov x8, #4503599627370495 mul x9, x0, x0 dup.2d v4, x8 diff --git a/skyscraper/bn254-multiplier/src/aarch64/montgomery_square_log_interleaved_3.s b/skyscraper/bn254-multiplier/src/aarch64/montgomery_square_log_interleaved_3.s index edbeb0389..36d433cf0 100644 --- a/skyscraper/bn254-multiplier/src/aarch64/montgomery_square_log_interleaved_3.s +++ b/skyscraper/bn254-multiplier/src/aarch64/montgomery_square_log_interleaved_3.s @@ -3,8 +3,7 @@ // in("v0") av[0], in("v1") av[1], in("v2") av[2], in("v3") av[3], // lateout("x0") out[0], lateout("x1") out[1], lateout("x2") out[2], lateout("x3") out[3], // lateout("v0") outv[0], lateout("v1") outv[1], lateout("v2") outv[2], lateout("v3") outv[3], -// lateout("x4") _, lateout("x5") _, lateout("x6") _, lateout("x7") _, lateout("x8") _, lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _, lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _, lateout("x17") _, lateout("v4") _, lateout("v5") _, lateout("v6") _, lateout("v7") _, lateout("v8") _, lateout("v9") _, lateout("v10") _, lateout("v11") _, lateout("v12") _, lateout("v13") _, lateout("v14") _, lateout("v15") _, lateout("v16") _, lateout("v17") _, lateout("v18") _, lateout("v19") _, -// lateout("lr") _ +// lateout("x4") _, lateout("x5") _, lateout("x6") _, lateout("x7") _, lateout("x8") _, lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _, lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _, lateout("x17") _, lateout("v4") _, lateout("v5") _, lateout("v6") _, lateout("v7") _, lateout("v8") _, lateout("v9") _, lateout("v10") _, lateout("v11") _, lateout("v12") _, lateout("v13") _, lateout("v14") _, lateout("v15") _, lateout("v16") _, lateout("v17") _, lateout("v18") _, lateout("v19") _ mov x4, #4503599627370495 dup.2d v4, x4 mul x5, x0, x0 diff --git a/skyscraper/bn254-multiplier/src/aarch64/montgomery_square_log_interleaved_4.s b/skyscraper/bn254-multiplier/src/aarch64/montgomery_square_log_interleaved_4.s index f54940fda..f2025a3d2 100644 --- a/skyscraper/bn254-multiplier/src/aarch64/montgomery_square_log_interleaved_4.s +++ b/skyscraper/bn254-multiplier/src/aarch64/montgomery_square_log_interleaved_4.s @@ -5,8 +5,7 @@ // lateout("x0") out[0], lateout("x1") out[1], lateout("x2") out[2], lateout("x3") out[3], // lateout("x4") out1[0], lateout("x5") out1[1], lateout("x6") out1[2], lateout("x7") out1[3], // lateout("v0") outv[0], lateout("v1") outv[1], lateout("v2") outv[2], lateout("v3") outv[3], -// lateout("x8") _, lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _, lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _, lateout("x17") _, lateout("x20") _, lateout("x21") _, lateout("x22") _, lateout("x23") _, lateout("x24") _, lateout("v4") _, lateout("v5") _, lateout("v6") _, lateout("v7") _, lateout("v8") _, lateout("v9") _, lateout("v10") _, lateout("v11") _, lateout("v12") _, lateout("v13") _, lateout("v14") _, lateout("v15") _, lateout("v16") _, lateout("v17") _, lateout("v18") _, lateout("v19") _, -// lateout("lr") _ +// lateout("x8") _, lateout("x9") _, lateout("x10") _, lateout("x11") _, lateout("x12") _, lateout("x13") _, lateout("x14") _, lateout("x15") _, lateout("x16") _, lateout("x17") _, lateout("x20") _, lateout("x21") _, lateout("x22") _, lateout("x23") _, lateout("x24") _, lateout("v4") _, lateout("v5") _, lateout("v6") _, lateout("v7") _, lateout("v8") _, lateout("v9") _, lateout("v10") _, lateout("v11") _, lateout("v12") _, lateout("v13") _, lateout("v14") _, lateout("v15") _, lateout("v16") _, lateout("v17") _, lateout("v18") _, lateout("v19") _ mov x8, #4503599627370495 mul x9, x0, x0 dup.2d v4, x8 diff --git a/skyscraper/bn254-multiplier/src/constants.rs b/skyscraper/bn254-multiplier/src/constants.rs index b831593f3..7bfcd258c 100644 --- a/skyscraper/bn254-multiplier/src/constants.rs +++ b/skyscraper/bn254-multiplier/src/constants.rs @@ -89,6 +89,32 @@ pub const U64_I3: [u64; 4] = [ ]; pub const U64_MU0: u64 = 0xc2e1f593efffffff; +// Limb positions in `REDUCTION_TABLE`. +pub const I3_LIMB: usize = 0; +pub const I2_LIMB: usize = 4; +pub const I1_LIMB: usize = 8; +pub const P_LIMB: usize = 12; +pub const MU0_LIMB: usize = 16; + +/// Reduction constants in the layout the single assembly kernels load them +/// from: `U64_I3`, `U64_I2`, `U64_I1`, `U64_P` (four limbs each), then +/// `U64_MU0`. The kernel `bn254_multiplier_codegen::karatsuba::MUL` takes its +/// address as the input `table` and loads from the limb positions above. The +/// codegen crate includes this file, so the layout is defined once. +pub static REDUCTION_TABLE: [u64; 17] = { + let mut table = [0; 17]; + let mut i = 0; + while i < 4 { + table[I3_LIMB + i] = U64_I3[i]; + table[I2_LIMB + i] = U64_I2[i]; + table[I1_LIMB + i] = U64_I1[i]; + table[P_LIMB + i] = U64_P[i]; + i += 1; + } + table[MU0_LIMB] = U64_MU0; + table +}; + // BOUNDS /// Upper bound of 2**256-2p pub const OUTPUT_MAX: [u64; 4] = [ diff --git a/skyscraper/bn254-multiplier/src/lib.rs b/skyscraper/bn254-multiplier/src/lib.rs index 3b4d99bfa..cf1854674 100644 --- a/skyscraper/bn254-multiplier/src/lib.rs +++ b/skyscraper/bn254-multiplier/src/lib.rs @@ -20,9 +20,9 @@ mod test_utils; #[cfg(target_arch = "aarch64")] pub use crate::aarch64::{ - montgomery_interleaved_3, montgomery_interleaved_4, montgomery_square_interleaved_3, - montgomery_square_interleaved_4, montgomery_square_log_interleaved_3, - montgomery_square_log_interleaved_4, + montgomery_interleaved_3, montgomery_interleaved_4, montgomery_mul, + montgomery_square_interleaved_3, montgomery_square_interleaved_4, + montgomery_square_log_interleaved_3, montgomery_square_log_interleaved_4, }; pub use crate::scalar::{scalar_mul, scalar_sqr}; From 5680c3353c9db4147804fede75db580d27679c68 Mon Sep 17 00:00:00 2001 From: Xander van der Goot Date: Wed, 25 Nov 2026 15:30:32 +0800 Subject: [PATCH 5/5] Share BN254 constants, restore typed loads, and fix SIMD rounding tests --- Cargo.lock | 7 +++ Cargo.toml | 2 + ntt/Cargo.toml | 1 + ntt/src/b51_interleaved.rs | 7 +-- .../bn254-multiplier-codegen/Cargo.toml | 1 + .../bn254-multiplier-codegen/src/constants.rs | 7 +-- .../bn254-multiplier-codegen/src/karatsuba.rs | 17 +++--- .../bn254-multiplier-constants/Cargo.toml | 12 +++++ .../src/lib.rs} | 16 +++--- skyscraper/bn254-multiplier/Cargo.toml | 3 ++ .../bn254-multiplier/src/aarch64/mod.rs | 2 +- skyscraper/bn254-multiplier/src/lib.rs | 1 - .../bn254-multiplier/src/rtz/block_simd.rs | 2 +- .../bn254-multiplier/src/rtz/portable_simd.rs | 28 +++++----- skyscraper/bn254-multiplier/src/scalar.rs | 31 ++++++----- skyscraper/bn254-multiplier/src/test_utils.rs | 2 +- skyscraper/bn254-multiplier/src/utils.rs | 10 ++-- skyscraper/hla/src/instructions.rs | 52 +++---------------- 18 files changed, 93 insertions(+), 108 deletions(-) create mode 100644 skyscraper/bn254-multiplier-constants/Cargo.toml rename skyscraper/{bn254-multiplier/src/constants.rs => bn254-multiplier-constants/src/lib.rs} (86%) diff --git a/Cargo.lock b/Cargo.lock index d2fec1056..cb5c989a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -712,6 +712,7 @@ dependencies = [ "ark-bn254", "ark-ff", "bn254-multiplier-codegen", + "bn254-multiplier-constants", "divan", "fp-rounding", "hla", @@ -725,9 +726,14 @@ dependencies = [ name = "bn254-multiplier-codegen" version = "0.1.0" dependencies = [ + "bn254-multiplier-constants", "hla", ] +[[package]] +name = "bn254-multiplier-constants" +version = "0.1.0" + [[package]] name = "bn254_blackbox_solver" version = "1.0.0-beta.26" @@ -3773,6 +3779,7 @@ dependencies = [ "ark-ff", "ark-std", "bn254-multiplier", + "bn254-multiplier-constants", "divan", "proptest", "rayon", diff --git a/Cargo.toml b/Cargo.toml index bce3ac548..d3bea694d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ members = [ "skyscraper/hla", "skyscraper/bn254-multiplier", "skyscraper/bn254-multiplier-codegen", + "skyscraper/bn254-multiplier-constants", "skyscraper/core", "provekit/common", "provekit/backend/bn254", @@ -93,6 +94,7 @@ opt-level = 3 # Workspace members - Skyscraper bn254-multiplier = { path = "skyscraper/bn254-multiplier" } bn254-multiplier-codegen = { path = "skyscraper/bn254-multiplier-codegen" } +bn254-multiplier-constants = { path = "skyscraper/bn254-multiplier-constants" } fp-rounding = { path = "skyscraper/fp-rounding" } hla = { path = "skyscraper/hla" } skyscraper = { path = "skyscraper/core" } diff --git a/ntt/Cargo.toml b/ntt/Cargo.toml index 79ad4d64a..cfa36fb9c 100644 --- a/ntt/Cargo.toml +++ b/ntt/Cargo.toml @@ -13,6 +13,7 @@ ark-bn254.workspace = true ark-ff.workspace = true rayon.workspace = true bn254-multiplier.workspace = true +bn254-multiplier-constants.workspace = true [dev-dependencies] divan.workspace = true diff --git a/ntt/src/b51_interleaved.rs b/ntt/src/b51_interleaved.rs index d6fcef158..4f9c7d57a 100644 --- a/ntt/src/b51_interleaved.rs +++ b/ntt/src/b51_interleaved.rs @@ -2,10 +2,10 @@ use { crate::{define_ntt, extend_roots_table}, ark_bn254::Fr, bn254_multiplier::{ - constants::{self, U64_P_MULTIPLES}, rne, utils::{self, addv, div_p_2b, subtraction_reduce}, }, + bn254_multiplier_constants::U64_P_MULTIPLES, rayon::iter::{IntoParallelRefMutIterator, ParallelIterator}, std::mem, }; @@ -31,10 +31,7 @@ fn b51_kernel(even: &mut [u64; 4], odd: &mut [u64; 4], omega: &[u64; 4]) { // creating our invariant let f = rne::mono::mul(*odd, *omega); let l = subtraction_reduce(div_p_2b, addv(*even, f)); - let r = subtraction_reduce( - div_p_2b, - addv(*even, utils::sub(constants::U64_P_MULTIPLES[3], f)), - ); + let r = subtraction_reduce(div_p_2b, addv(*even, utils::sub(U64_P_MULTIPLES[3], f))); (*even, *odd) = (l, r); } diff --git a/skyscraper/bn254-multiplier-codegen/Cargo.toml b/skyscraper/bn254-multiplier-codegen/Cargo.toml index d8a7b8f17..8646f69d9 100644 --- a/skyscraper/bn254-multiplier-codegen/Cargo.toml +++ b/skyscraper/bn254-multiplier-codegen/Cargo.toml @@ -10,6 +10,7 @@ repository.workspace = true [dependencies] # Workspace crates +bn254-multiplier-constants.workspace = true hla.workspace = true [lints] diff --git a/skyscraper/bn254-multiplier-codegen/src/constants.rs b/skyscraper/bn254-multiplier-codegen/src/constants.rs index 65414904f..2469acc02 100644 --- a/skyscraper/bn254-multiplier-codegen/src/constants.rs +++ b/skyscraper/bn254-multiplier-codegen/src/constants.rs @@ -1,11 +1,6 @@ // -- [Scalar CONSTANTS] -- // -// The scalar constants are defined once, in `bn254-multiplier`, which loads -// them at runtime through the same table the kernels here are generated for. -#[allow(dead_code)] -#[path = "../../bn254-multiplier/src/constants.rs"] -mod multiplier; -pub use multiplier::{ +pub use bn254_multiplier_constants::{ I1_LIMB, I2_LIMB, I3_LIMB, MU0_LIMB, P_LIMB, U64_2P, U64_I1, U64_I2, U64_I3, U64_MU0, U64_P, }; diff --git a/skyscraper/bn254-multiplier-codegen/src/karatsuba.rs b/skyscraper/bn254-multiplier-codegen/src/karatsuba.rs index 02dd8b8d3..0ca0a722e 100644 --- a/skyscraper/bn254-multiplier-codegen/src/karatsuba.rs +++ b/skyscraper/bn254-multiplier-codegen/src/karatsuba.rs @@ -7,6 +7,7 @@ use { crate::{constants::*, schoolbook}, + bn254_multiplier_constants::ReductionTable, hla::{builder::Setup, *}, std::array, }; @@ -162,7 +163,7 @@ fn fold_balanced( alloc: &mut FreshAllocator, asm: &mut Assembler, t: [Reg; 8], - table: &Reg, + table: &Reg<*const ReductionTable>, ) -> [Reg; 5] { let [t0, t1, t2, upper @ ..] = t; let p0 = mul_constant(alloc, asm, &t0, I3_LIMB, table); @@ -183,12 +184,12 @@ fn mul_constant( asm: &mut Assembler, limb: &Reg, first: usize, - table: &Reg, + table: &Reg<*const ReductionTable>, ) -> [Reg; 5] { - let (c0, c1) = ldp_address(alloc, asm, table, first * 8); + let (c0, c1) = ldp(alloc, asm, &table.get(first)); let p0 = schoolbook::widening_mul(alloc, asm, limb, &c0); let p1 = schoolbook::widening_mul(alloc, asm, limb, &c1); - let (c2, c3) = ldp_address(alloc, asm, table, (first + 2) * 8); + let (c2, c3) = ldp(alloc, asm, &table.get(first + 2)); let p2 = schoolbook::widening_mul(alloc, asm, limb, &c2); let p3 = schoolbook::widening_mul(alloc, asm, limb, &c3); add_rows(alloc, asm, [p0, p1, p2, p3]) @@ -237,12 +238,12 @@ fn finish( alloc: &mut FreshAllocator, asm: &mut Assembler, s: [Reg; 5], - table: &Reg, + table: &Reg<*const ReductionTable>, ) -> [Reg; 4] { - let mu = ldr_address(alloc, asm, table, MU0_LIMB * 8); + let mu = ldr(alloc, asm, &table.get(MU0_LIMB)); let m = hla::mul(alloc, asm, &mu, &s[0]); - let (p0, p1) = ldp_address(alloc, asm, table, P_LIMB * 8); - let (p2, p3) = ldp_address(alloc, asm, table, (P_LIMB + 2) * 8); + let (p0, p1) = ldp(alloc, asm, &table.get(P_LIMB)); + let (p2, p3) = ldp(alloc, asm, &table.get(P_LIMB + 2)); cancel_split(alloc, asm, s, &[p0, p1, p2, p3], &m) } diff --git a/skyscraper/bn254-multiplier-constants/Cargo.toml b/skyscraper/bn254-multiplier-constants/Cargo.toml new file mode 100644 index 000000000..2ff773280 --- /dev/null +++ b/skyscraper/bn254-multiplier-constants/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "bn254-multiplier-constants" +version = "0.1.0" +edition.workspace = true +rust-version.workspace = true +authors.workspace = true +license.workspace = true +homepage.workspace = true +repository.workspace = true + +[lints] +workspace = true diff --git a/skyscraper/bn254-multiplier/src/constants.rs b/skyscraper/bn254-multiplier-constants/src/lib.rs similarity index 86% rename from skyscraper/bn254-multiplier/src/constants.rs rename to skyscraper/bn254-multiplier-constants/src/lib.rs index 7bfcd258c..113aa8505 100644 --- a/skyscraper/bn254-multiplier/src/constants.rs +++ b/skyscraper/bn254-multiplier-constants/src/lib.rs @@ -1,3 +1,7 @@ +//! BN254 scalar-field constants shared by the multiplier and its assembly +//! generator. +#![no_std] + pub const U64_P: [u64; 4] = U64_P_MULTIPLES[1]; pub const U64_2P: [u64; 4] = U64_P_MULTIPLES[2]; @@ -96,12 +100,12 @@ pub const I1_LIMB: usize = 8; pub const P_LIMB: usize = 12; pub const MU0_LIMB: usize = 16; -/// Reduction constants in the layout the single assembly kernels load them -/// from: `U64_I3`, `U64_I2`, `U64_I1`, `U64_P` (four limbs each), then -/// `U64_MU0`. The kernel `bn254_multiplier_codegen::karatsuba::MUL` takes its -/// address as the input `table` and loads from the limb positions above. The -/// codegen crate includes this file, so the layout is defined once. -pub static REDUCTION_TABLE: [u64; 17] = { +/// Limb array addressed by the scalar Montgomery kernel. +pub type ReductionTable = [u64; 17]; + +/// Four limbs each for I3, I2, I1 and the modulus, followed by the Montgomery +/// factor. +pub static REDUCTION_TABLE: ReductionTable = { let mut table = [0; 17]; let mut i = 0; while i < 4 { diff --git a/skyscraper/bn254-multiplier/Cargo.toml b/skyscraper/bn254-multiplier/Cargo.toml index ed19e1b76..8809b0c0f 100644 --- a/skyscraper/bn254-multiplier/Cargo.toml +++ b/skyscraper/bn254-multiplier/Cargo.toml @@ -13,6 +13,9 @@ repository.workspace = true fp-rounding.workspace = true [dependencies] +# Workspace crates +bn254-multiplier-constants.workspace = true + # 3rd party seq-macro.workspace = true diff --git a/skyscraper/bn254-multiplier/src/aarch64/mod.rs b/skyscraper/bn254-multiplier/src/aarch64/mod.rs index 5a0c21597..c83dbbeb8 100644 --- a/skyscraper/bn254-multiplier/src/aarch64/mod.rs +++ b/skyscraper/bn254-multiplier/src/aarch64/mod.rs @@ -10,7 +10,7 @@ //! 3p | 4.75 [0.54] | 3.94 [1.3517] | 3.86 [1.43] //! 2ˆ256-2p | 5.09 [0.20] | 4.28 [1.0067] | 4.20 [1.09] use { - crate::constants::REDUCTION_TABLE, + bn254_multiplier_constants::REDUCTION_TABLE, core::{arch::asm, simd::Simd}, fp_rounding::{RoundingGuard, Zero}, }; diff --git a/skyscraper/bn254-multiplier/src/lib.rs b/skyscraper/bn254-multiplier/src/lib.rs index cf1854674..76e8096c9 100644 --- a/skyscraper/bn254-multiplier/src/lib.rs +++ b/skyscraper/bn254-multiplier/src/lib.rs @@ -10,7 +10,6 @@ mod aarch64; #[cfg(target_arch = "aarch64")] pub mod rtz; -pub mod constants; pub mod rne; mod scalar; pub mod utils; diff --git a/skyscraper/bn254-multiplier/src/rtz/block_simd.rs b/skyscraper/bn254-multiplier/src/rtz/block_simd.rs index b261cb45b..c767041d3 100644 --- a/skyscraper/bn254-multiplier/src/rtz/block_simd.rs +++ b/skyscraper/bn254-multiplier/src/rtz/block_simd.rs @@ -1,6 +1,5 @@ use { crate::{ - constants::*, rtz::{ constants::*, simd_utils::{ @@ -11,6 +10,7 @@ use { subarray, utils::{addv, carrying_mul_add, reduce_ct}, }, + bn254_multiplier_constants::{U64_I1, U64_I2, U64_I3, U64_MU0, U64_P}, core::{ arch::aarch64::vcvtq_f64_u64, ops::BitAnd, diff --git a/skyscraper/bn254-multiplier/src/rtz/portable_simd.rs b/skyscraper/bn254-multiplier/src/rtz/portable_simd.rs index b5d559681..b41b8f524 100644 --- a/skyscraper/bn254-multiplier/src/rtz/portable_simd.rs +++ b/skyscraper/bn254-multiplier/src/rtz/portable_simd.rs @@ -391,7 +391,7 @@ mod tests { crate::test_utils::{ark_ff_reference, safe_bn254_montgomery_input}, ark_bn254::Fr, ark_ff::BigInt, - fp_rounding::{with_rounding_mode, Zero}, + fp_rounding::with_rounding_mode, proptest::{prop_assert_eq, proptest}, }; @@ -402,10 +402,9 @@ mod tests { b in safe_bn254_montgomery_input(), c in safe_bn254_montgomery_input(), )| { - let (ab,bc) = unsafe { - with_rounding_mode((), |rtz : &fp_rounding::RoundingGuard, _| { - simd_mul(&rtz, a, b, b,c) - })}; + let (ab, bc) = unsafe { + with_rounding_mode((a, b, c), |rtz, (a, b, c)| simd_mul(rtz, a, b, b, c)) + }; let ab_ref = ark_ff_reference(a, b); let bc_ref = ark_ff_reference(b, c); let ab = Fr::new(BigInt(ab)); @@ -418,16 +417,15 @@ mod tests { #[test] fn test_simd_sqr() { proptest!(|( - a in safe_bn254_montgomery_input(), - b in safe_bn254_montgomery_input(), - )| { - let (mul,sqr) = unsafe { - with_rounding_mode((), |rtz : &fp_rounding::RoundingGuard, _| { - let mul = simd_mul(&rtz,a, a, b, b); - let sqr = simd_sqr(&rtz,a, b); - (mul, sqr) - })}; - prop_assert_eq!(mul,sqr) + a in safe_bn254_montgomery_input(), + b in safe_bn254_montgomery_input(), + )| { + let (mul, sqr) = unsafe { + with_rounding_mode((a, b), |rtz, (a, b)| { + (simd_mul(rtz, a, a, b, b), simd_sqr(rtz, a, b)) + }) + }; + prop_assert_eq!(mul, sqr) }) } } diff --git a/skyscraper/bn254-multiplier/src/scalar.rs b/skyscraper/bn254-multiplier/src/scalar.rs index 93bb5c489..0a05fc098 100644 --- a/skyscraper/bn254-multiplier/src/scalar.rs +++ b/skyscraper/bn254-multiplier/src/scalar.rs @@ -1,7 +1,9 @@ -use crate::{ - constants::*, - subarray, - utils::{addv, carrying_mul_add, reduce_ct}, +use { + crate::{ + subarray, + utils::{addv, carrying_mul_add, reduce_ct}, + }, + bn254_multiplier_constants::{U64_I1, U64_I2, U64_I3, U64_MU0, U64_P}, }; /// Montgomery squaring in Bn254 scalar field. @@ -135,10 +137,11 @@ pub fn scalar_mul(a: [u64; 4], b: [u64; 4]) -> [u64; 4] { #[cfg(test)] mod tests { use { - super::*, - crate::{constants, test_utils::*}, + super::{scalar_mul, scalar_sqr}, + crate::test_utils::*, ark_bn254::Fr, ark_ff::BigInt, + bn254_multiplier_constants::{OUTPUT_MAX, U64_P, U64_R, U64_R_INV}, primitive_types::U256, proptest::proptest, rand::{rngs, Rng, SeedableRng}, @@ -155,7 +158,7 @@ mod tests { } fn mod_mul(a: U256, b: U256) -> U256 { - let p = U256(constants::U64_P); + let p = U256(U64_P); let mut c = [0u64; 4]; c.copy_from_slice(&(a.full_mul(b) % p).0[0..4]); U256(c) @@ -164,9 +167,9 @@ mod tests { #[test] fn test_scalar_mul() { let mut rng = rngs::StdRng::seed_from_u64(0); - let p = U256(constants::U64_P); - let r = U256(constants::U64_R); - let r_inv = U256(constants::U64_R_INV); + let p = U256(U64_P); + let r = U256(U64_R); + let r_inv = U256(U64_R_INV); let mut s0_a_bytes = [0u8; 32]; let mut s0_b_bytes = [0u8; 32]; @@ -180,7 +183,7 @@ mod tests { let s0_b_mont = mod_mul(s0_b, r); let s0 = scalar_mul(s0_a_mont.0, s0_b_mont.0); - assert!(U256(s0) < U256(constants::OUTPUT_MAX)); + assert!(U256(s0) < U256(OUTPUT_MAX)); assert_eq!(mod_mul(U256(s0), r_inv), mod_mul(s0_a, s0_b)); } } @@ -188,9 +191,9 @@ mod tests { #[test] fn test_scalar_sqr() { let mut rng = rngs::StdRng::seed_from_u64(0); - let p = U256(constants::U64_P); - let r = U256(constants::U64_R); - let r_inv = U256(constants::U64_R_INV); + let p = U256(U64_P); + let r = U256(U64_R); + let r_inv = U256(U64_R_INV); let mut s0_a_bytes = [0u8; 32]; diff --git a/skyscraper/bn254-multiplier/src/test_utils.rs b/skyscraper/bn254-multiplier/src/test_utils.rs index 6dceec140..60e1d6979 100644 --- a/skyscraper/bn254-multiplier/src/test_utils.rs +++ b/skyscraper/bn254-multiplier/src/test_utils.rs @@ -1,9 +1,9 @@ #![cfg(test)] use { - crate::constants::OUTPUT_MAX, ark_bn254::Fr, ark_ff::{BigInt, Field}, + bn254_multiplier_constants::OUTPUT_MAX, proptest::{ collection, prelude::{any, prop, Strategy}, diff --git a/skyscraper/bn254-multiplier/src/utils.rs b/skyscraper/bn254-multiplier/src/utils.rs index 693cfcc03..aaa41eda9 100644 --- a/skyscraper/bn254-multiplier/src/utils.rs +++ b/skyscraper/bn254-multiplier/src/utils.rs @@ -1,4 +1,4 @@ -use crate::constants::{self, U64_2P}; +use bn254_multiplier_constants::{U64_2P, U64_P, U64_P_MULTIPLES}; /// Macro to extract a subarray from an array. /// @@ -119,8 +119,6 @@ impl MulShift { // Based on Warren's "Hacker's Delight" (integer // division by constants) to find a magic multiplier for each bit-width. - use crate::constants::U64_P; - let d = (U64_P[3] >> (max_bit_size - 192 - precision)) + 1; // d = divisor = ceil(p / 2^(max_bit_size-w)) let nc = 2_u64.pow(precision) - 1 - (2_u64.pow(precision) % d); // nc = largest value s.t. nc mod d == d-1 let mut s = precision; // start at precision; s < precision values are skipped @@ -193,14 +191,14 @@ pub fn subtraction_reduce u64>(div_p: F, x: [u64; 4]) -> [u64; 4] // No clamping as the max value of x can't go past 5. Which is the maximum of // the table. let q = div_p(x[3]) as usize; - sub(x, constants::U64_P_MULTIPLES[q]) + sub(x, U64_P_MULTIPLES[q]) } #[cfg(kani)] mod proofs { use { - super::{constants::U64_2P, div_p_32b, div_p_6b}, - crate::constants::U64_P_MULTIPLES, + super::{div_p_32b, div_p_6b}, + bn254_multiplier_constants::{U64_2P, U64_P_MULTIPLES}, }; /// Lexicographic ≤ on little-endian 256-bit integers. diff --git a/skyscraper/hla/src/instructions.rs b/skyscraper/hla/src/instructions.rs index 2bd36188f..2bd07261d 100644 --- a/skyscraper/hla/src/instructions.rs +++ b/skyscraper/hla/src/instructions.rs @@ -34,7 +34,7 @@ use { crate::{ frontend::{Assembler, FreshAllocator, PointerReg, Reg, Simd, SizedIdx, D, SIMD}, ir::{FreshRegister, Instruction, Modifier, ShiftKind}, - reification::{ReifiedRegister, ReifyRegister}, + reification::ReifyRegister, }, paste::paste, }; @@ -243,57 +243,21 @@ pub mod load_store { super::*, crate::frontend::{MutablePointer, Pointer}, }; - - /// Loads two adjacent words from an address held in a general register. - /// The byte offset is fixed at generation time; the base may be computed. - pub fn ldp_address( - alloc: &mut FreshAllocator, - asm: &mut Assembler, - address: &Reg, - byte_offset: usize, - ) -> (Reg, Reg) { - assert!(byte_offset <= 504 && byte_offset % 8 == 0); - let a = alloc.fresh(); - let b = alloc.fresh(); - let mut pointer = address.reify(); - pointer.idx = crate::reification::Index::Pointer(byte_offset); - asm.append_instruction(vec![ldp_inst(&a, &b, pointer)]); - (a, b) - } - - /// Loads one word from an address held in a general register. - /// The byte offset is fixed at generation time; the base may be computed. - pub fn ldr_address( - alloc: &mut FreshAllocator, - asm: &mut Assembler, - address: &Reg, - byte_offset: usize, - ) -> Reg { - assert!(byte_offset <= 32760 && byte_offset % 8 == 0); - let out = alloc.fresh(); - let mut pointer = address.reify(); - pointer.idx = crate::reification::Index::Pointer(byte_offset); - asm.append_instruction(vec![ldr_inst(&out, pointer)]); - out - } pub fn ldr( alloc: &mut FreshAllocator, asm: &mut Assembler, ptr: &PointerReg, ) -> Reg { let ret = alloc.fresh(); - asm.append_instruction(vec![ldr_inst(&ret, ptr.reify())]); + asm.append_instruction(vec![ldr_inst(&ret, ptr)]); ret } - pub fn ldr_inst( - dest: &Reg, - ptr: ReifiedRegister, - ) -> Instruction { + pub fn ldr_inst(dest: &Reg, ptr: &PointerReg) -> Instruction { Instruction { opcode: "ldr".to_string(), results: vec![dest.reify()], - operands: vec![ptr], + operands: vec![ptr.reify()], modifiers: Modifier::None, } } @@ -305,19 +269,19 @@ pub mod load_store { ) -> (Reg, Reg) { let ret0 = alloc.fresh(); let ret1 = alloc.fresh(); - asm.append_instruction(vec![ldp_inst(&ret0, &ret1, ptr.reify())]); + asm.append_instruction(vec![ldp_inst(&ret0, &ret1, ptr)]); (ret0, ret1) } - pub fn ldp_inst( + pub fn ldp_inst( dest: &Reg, dest2: &Reg, - ptr: ReifiedRegister, + ptr: &PTR, ) -> Instruction { Instruction { opcode: "ldp".to_string(), results: vec![dest.reify(), dest2.reify()], - operands: vec![ptr], + operands: vec![ptr.reify()], modifiers: Modifier::None, } }