zkhash/fields/
utils.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use ark_ff::{PrimeField};
// use rand::{thread_rng, Rng};
// use sha3::digest::XofReader;
// use std::cmp::min;
// use core::num::ParseIntError;

use hex::FromHex;

pub fn from_hex<F: PrimeField>(s: &str) -> F {
    let a = Vec::from_hex(&s[2..]).expect("Invalid Hex String");
    F::from_be_bytes_mod_order(&a as &[u8])
}

pub fn random_scalar<F: PrimeField>() -> F {
    let mut rng = ark_std::rand::thread_rng();
    F::rand(&mut rng)
}

pub fn random_scalar_without_0<F: PrimeField>() -> F {
    loop {
        let element = random_scalar::<F>();
        if !element.is_zero() {
            return element;
        }
    }
}

//-----------------------------------------------------------------------------
// pub fn from_u64<F: PrimeField>(val: u64) -> F {
//     F::from_repr(F::Repr::from(val)).unwrap()
// }

// pub fn random_scalar_rng<F: PrimeField, R: Rng>(allow_zero: bool, rng: &mut R) -> F {
//     loop {
//         let s = F::rand(rng);
//         if allow_zero || s != F::zero() {
//             return s;
//         }
//     }
// }

// pub fn random_scalar<F: PrimeField>(allow_zero: bool) -> F {
//     loop {
//         let s = F::rand(&mut thread_rng());
//         if allow_zero || s != F::zero() {
//             return s;
//         }
//     }
// }

// pub fn into_limbs<F: PrimeField>(val: &F) -> Vec<u64> {
//     val.into_repr().as_ref().to_owned()
// }

// pub fn from_limbs<F: PrimeField>(repr: &[u64]) -> F {
//     let mut tmp = F::Repr::default();
//     tmp.as_mut().copy_from_slice(repr);
//     F::from_repr(tmp).unwrap()
// }

// fn from_limbs_with_error<F: PrimeField>(repr: &[u64]) -> F {
//     let mut tmp = F::Repr::default();
//     tmp.as_mut().copy_from_slice(repr);
//     F::from_repr(tmp)
// }

// pub fn field_element_from_shake<F: PrimeField>(reader: &mut dyn XofReader) -> F {
//     let bytes = f64::ceil(F::MODULUS_BIT_SIZE as f64 / 8f64) as usize;
//     let mut words = f64::ceil(bytes as f64 / 8f64) as usize;
//     if F::MODULUS_BIT_SIZE == 64 { // Quick and dirty fix for Goldilocks
//         words = 2;
//     }
    
//     let mod_ = F::NUM_BITS % 8;
//     let mask = if mod_ == 0 { 0xFF } else { (1u8 << mod_) - 1 };
//     let mut buf = vec![0u8; bytes];
//     let mut word_buf = vec![0u64; words];

//     let len = buf.len();
//     loop {
//         reader.read(&mut buf);
//         buf[len - 1] &= mask;
//         for i in 0..words {
//             let mut byte_array = [0u8; 8];
//             for j in i * 8..min((i + 1) * 8, len) {
//                 byte_array[j - i * 8] = buf[j];
//             }
//             word_buf[i] = u64::from_le_bytes(byte_array);
//         }
//         let res = from_limbs_with_error::<F>(&word_buf);
//         if let Ok(el) = res {
//             return el;
//         }
//     }
// }

// pub fn field_element_from_shake_without_0<F: PrimeField>(reader: &mut dyn XofReader) -> F {
//     loop {
//         let element = field_element_from_shake::<F>(reader);
//         if !element.is_zero() {
//             return element;
//         }
//     }
// }