|
| 1 | +use crate::fixed_base::FixedBaseTable as GenericFixedBaseTable; |
| 2 | +use crate::traits::{GlvCapable, MsmGroup}; |
| 3 | +use jolt_inlines_grumpkin::{GrumpkinFr, GrumpkinPoint, UnwrapOrSpoilProof}; |
| 4 | + |
| 5 | +// ============================================================ |
| 6 | +// Curve Parameters |
| 7 | +// ============================================================ |
| 8 | + |
| 9 | +pub const SCALAR_BITS: usize = 256; |
| 10 | +pub const GLV_SCALAR_BITS: usize = 128; |
| 11 | + |
| 12 | +// Pippenger parameters for baseline (256-bit scalars). |
| 13 | +pub const BASELINE_WINDOW: usize = 12; |
| 14 | +pub const BASELINE_BUCKETS: usize = 1 << BASELINE_WINDOW; |
| 15 | +pub const BASELINE_WINDOWS: usize = SCALAR_BITS.div_ceil(BASELINE_WINDOW); |
| 16 | + |
| 17 | +// Pippenger parameters for GLV (128-bit scalars). |
| 18 | +pub const GLV_WINDOW: usize = 8; |
| 19 | +pub const GLV_BUCKETS: usize = 1 << GLV_WINDOW; |
| 20 | +pub const GLV_WINDOWS: usize = GLV_SCALAR_BITS.div_ceil(GLV_WINDOW); |
| 21 | + |
| 22 | +// Fixed-base (generator) windowed multiplication parameters (256-bit scalars). |
| 23 | +pub const FIXED_BASE_WINDOW: usize = 14; |
| 24 | +pub const FIXED_BASE_BUCKETS: usize = 1 << FIXED_BASE_WINDOW; |
| 25 | +pub const FIXED_BASE_WINDOWS: usize = SCALAR_BITS.div_ceil(FIXED_BASE_WINDOW); |
| 26 | + |
| 27 | +pub type FixedBaseTable = |
| 28 | + GenericFixedBaseTable<GrumpkinPoint, FIXED_BASE_WINDOWS, FIXED_BASE_BUCKETS>; |
| 29 | + |
| 30 | +// ============================================================ |
| 31 | +// Trait Implementations |
| 32 | +// ============================================================ |
| 33 | + |
| 34 | +impl MsmGroup for GrumpkinPoint { |
| 35 | + #[inline(always)] |
| 36 | + fn identity() -> Self { |
| 37 | + GrumpkinPoint::infinity() |
| 38 | + } |
| 39 | + |
| 40 | + #[inline(always)] |
| 41 | + fn is_identity(&self) -> bool { |
| 42 | + self.is_infinity() |
| 43 | + } |
| 44 | + |
| 45 | + #[inline(always)] |
| 46 | + fn add(&self, other: &Self) -> Self { |
| 47 | + GrumpkinPoint::add(self, other) |
| 48 | + } |
| 49 | + |
| 50 | + #[inline(always)] |
| 51 | + fn neg(&self) -> Self { |
| 52 | + GrumpkinPoint::neg(self) |
| 53 | + } |
| 54 | + |
| 55 | + #[inline(always)] |
| 56 | + fn double(&self) -> Self { |
| 57 | + GrumpkinPoint::double(self) |
| 58 | + } |
| 59 | + |
| 60 | + #[inline(always)] |
| 61 | + fn double_and_add(&self, other: &Self) -> Self { |
| 62 | + GrumpkinPoint::double_and_add(self, other) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl GlvCapable for GrumpkinPoint { |
| 67 | + type HalfScalar = u128; |
| 68 | + type FullScalar = GrumpkinFr; |
| 69 | + |
| 70 | + #[inline(always)] |
| 71 | + fn endomorphism(&self) -> Self { |
| 72 | + GrumpkinPoint::endomorphism(self) |
| 73 | + } |
| 74 | + |
| 75 | + #[inline(always)] |
| 76 | + fn decompose_scalar(k: &GrumpkinFr) -> [(bool, u128); 2] { |
| 77 | + GrumpkinPoint::decompose_scalar(k) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// ============================================================ |
| 82 | +// Benchmark Helpers |
| 83 | +// ============================================================ |
| 84 | + |
| 85 | +/// Grumpkin Fr modulus limbs for scalar reduction. |
| 86 | +const FR_MODULUS_LIMBS: [u64; 4] = [ |
| 87 | + 4332616871279656263, |
| 88 | + 10917124144477883021, |
| 89 | + 13281191951274694749, |
| 90 | + 3486998266802970665, |
| 91 | +]; |
| 92 | + |
| 93 | +#[inline(always)] |
| 94 | +fn is_ge_modulus(x: &[u64; 4]) -> bool { |
| 95 | + let m = FR_MODULUS_LIMBS; |
| 96 | + if x[3] > m[3] { |
| 97 | + return true; |
| 98 | + } |
| 99 | + if x[3] < m[3] { |
| 100 | + return false; |
| 101 | + } |
| 102 | + if x[2] > m[2] { |
| 103 | + return true; |
| 104 | + } |
| 105 | + if x[2] < m[2] { |
| 106 | + return false; |
| 107 | + } |
| 108 | + if x[1] > m[1] { |
| 109 | + return true; |
| 110 | + } |
| 111 | + if x[1] < m[1] { |
| 112 | + return false; |
| 113 | + } |
| 114 | + x[0] >= m[0] |
| 115 | +} |
| 116 | + |
| 117 | +#[inline(always)] |
| 118 | +fn sub_modulus(x: [u64; 4]) -> [u64; 4] { |
| 119 | + let m = FR_MODULUS_LIMBS; |
| 120 | + let mut out = [0u64; 4]; |
| 121 | + let mut borrow = 0u128; |
| 122 | + let mut i = 0; |
| 123 | + while i < 4 { |
| 124 | + let xi = x[i] as u128; |
| 125 | + let mi = m[i] as u128 + borrow; |
| 126 | + if xi >= mi { |
| 127 | + out[i] = (xi - mi) as u64; |
| 128 | + borrow = 0; |
| 129 | + } else { |
| 130 | + out[i] = ((1u128 << 64) + xi - mi) as u64; |
| 131 | + borrow = 1; |
| 132 | + } |
| 133 | + i += 1; |
| 134 | + } |
| 135 | + out |
| 136 | +} |
| 137 | + |
| 138 | +/// Reduce scalar modulo Fr. |
| 139 | +#[inline(always)] |
| 140 | +pub fn reduce_scalar(mut scalar: [u64; 4]) -> [u64; 4] { |
| 141 | + while is_ge_modulus(&scalar) { |
| 142 | + scalar = sub_modulus(scalar); |
| 143 | + } |
| 144 | + scalar |
| 145 | +} |
| 146 | + |
| 147 | +/// Generate deterministic test scalars using a simple LCG. |
| 148 | +#[inline(always)] |
| 149 | +pub fn generate_scalars<const N: usize>(seed: u64) -> [[u64; 4]; N] { |
| 150 | + let mut scalars = [[0u64; 4]; N]; |
| 151 | + let (a, c) = (6364136223846793005u64, 1442695040888963407u64); |
| 152 | + let mut state = seed; |
| 153 | + |
| 154 | + for scalar in scalars.iter_mut() { |
| 155 | + for limb in scalar.iter_mut() { |
| 156 | + state = state.wrapping_mul(a).wrapping_add(c); |
| 157 | + *limb = state; |
| 158 | + } |
| 159 | + *scalar = reduce_scalar(*scalar); |
| 160 | + } |
| 161 | + scalars |
| 162 | +} |
| 163 | + |
| 164 | +/// Generate deterministic test points by fixed-base scalar multiplication of generator. |
| 165 | +#[inline(always)] |
| 166 | +pub fn generate_points_fixed_base<const N: usize>( |
| 167 | + seed: u64, |
| 168 | + table_g: &FixedBaseTable, |
| 169 | +) -> [GrumpkinPoint; N] { |
| 170 | + let mut points: [GrumpkinPoint; N] = core::array::from_fn(|_| GrumpkinPoint::infinity()); |
| 171 | + let (a, c) = (6364136223846793005u64, 1442695040888963407u64); |
| 172 | + let mut state = seed; |
| 173 | + |
| 174 | + for point in points.iter_mut() { |
| 175 | + state = state.wrapping_mul(a).wrapping_add(c); |
| 176 | + let small_scalar = [state, 0, 0, 0]; |
| 177 | + *point = table_g.scalar_mul(&small_scalar); |
| 178 | + } |
| 179 | + points |
| 180 | +} |
| 181 | + |
| 182 | +/// Convert [u64; 4] to GrumpkinFr. |
| 183 | +#[inline(always)] |
| 184 | +pub fn scalar_to_fr(scalar: &[u64; 4]) -> GrumpkinFr { |
| 185 | + GrumpkinFr::from_u64_arr(scalar).unwrap_or_spoil_proof() |
| 186 | +} |
0 commit comments