Skip to content

Commit

Permalink
Merge pull request #35 from Grandi0z/fix_field_function
Browse files Browse the repository at this point in the history
fixed sample and primitive_nth_root
  • Loading branch information
elielnfinic authored Mar 26, 2024
2 parents 7e9b80f + f8a894b commit 607378c
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions baby-stark/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,26 +157,29 @@ impl Field{
}
}

// TBD
// pub fn primitive_nth_root(self, n : i128) -> Option<FieldElement>{
// if self.p == 85408008396924667383611388730472331217 {
// let mut root = FieldElement::from(85408008396924667383611388730472331217, self);
// let mut order : i128 = 1 << 119;
// while order != n {
// root = root ^ 2;
// order = order / 2;
// }
// Some(root)
// }else {
// None
// }
// }

// TO BE FIXED
pub fn sample(self, byte_array : Vec<u8>) -> FieldElement{
let mut acc = 0_i128;
for b in byte_array.iter(){
acc = (acc << 8) ^ (acc as i128);
// done
pub fn primitive_nth_root(self, n : i128) -> Option<FieldElement>{
if self.p == 85408008396924667383611388730472331217 {
if !(n <= 1 << 119 && (n & (n-1)) == 0) {
println!("Field does not have nth root of unity where n > 2^119 or not power of two.")
}
let mut root = FieldElement::from(85408008396924667383611388730472331217, self);
let mut order : i128 = 1 << 119;
while order != n {
root = root.__mul__(root);
order = order / 2;
}
Some(root)
}else {
None
}
}

// FIXED
pub fn sample(self, byte_array: Vec<u8>) -> FieldElement {
let mut acc: i128 = 0;
for &b in byte_array.iter() {
acc = (acc << 8) ^ b as i128;
}
FieldElement::from(acc % self.p, self)
}
Expand Down

0 comments on commit 607378c

Please sign in to comment.