plonky2/field/src/cosets.rs

53 lines
1.7 KiB
Rust
Raw Normal View History

2021-07-22 13:08:14 -07:00
use num::bigint::BigUint;
2021-07-21 13:05:40 -07:00
use crate::types::Field;
2021-04-04 15:26:38 -07:00
/// Finds a set of shifts that result in unique cosets for the multiplicative subgroup of size
/// `2^subgroup_bits`.
pub fn get_unique_coset_shifts<F: Field>(subgroup_size: usize, num_shifts: usize) -> Vec<F> {
2021-07-20 15:42:27 -07:00
// From Lagrange's theorem.
2021-07-21 13:05:32 -07:00
let num_cosets = (F::order() - 1u32) / (subgroup_size as u32);
2021-07-20 15:42:27 -07:00
assert!(
BigUint::from(num_shifts) <= num_cosets,
"The subgroup does not have enough distinct cosets"
);
2021-04-04 15:26:38 -07:00
// Let g be a generator of the entire multiplicative group. Let n be the order of the subgroup.
// The subgroup can be written as <g^(|F*| / n)>. We can use g^0, ..., g^(num_shifts - 1) as our
// shifts, since g^i <g^(|F*| / n)> are distinct cosets provided i < |F*| / n, which we checked.
2021-04-21 22:31:45 +02:00
F::MULTIPLICATIVE_GROUP_GENERATOR
.powers()
2021-04-05 11:39:16 -07:00
.take(num_shifts)
2021-04-04 15:26:38 -07:00
.collect()
}
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use crate::cosets::get_unique_coset_shifts;
use crate::goldilocks_field::GoldilocksField;
use crate::types::Field;
#[test]
fn distinct_cosets() {
2021-11-02 12:04:42 -07:00
type F = GoldilocksField;
const SUBGROUP_BITS: usize = 5;
const NUM_SHIFTS: usize = 50;
let generator = F::primitive_root_of_unity(SUBGROUP_BITS);
let subgroup_size = 1 << SUBGROUP_BITS;
2021-07-19 17:11:42 -07:00
let shifts = get_unique_coset_shifts::<F>(subgroup_size, NUM_SHIFTS);
let mut union = HashSet::new();
for shift in shifts {
let coset = F::cyclic_subgroup_coset_known_order(generator, shift, subgroup_size);
assert!(
coset.into_iter().all(|x| union.insert(x)),
2021-04-21 22:31:45 +02:00
"Duplicate element!"
);
}
}
}