Converting an element in u8 to a field element bls12_381

How can we convert a u8 element into an element in the elliptic curve bls12_381.

I have written these lines,

let a_prior: [u8;1] = [10;1];
	println!("{}",a_prior[1]);
	let b_prior: [u8;1] = [20;1];
	println!("{}",b_prior[1]);
	let a_bits = multipack::bytes_to_bits_le(&a_prior);
	let a = multipack::compute_multipacking::<bls12_381::Scalar>(&a_bits);
	println!("{:?}",a);
	let b_bits = multipack::bytes_to_bits_le(&b_prior);
	let b = multipack::compute_multipacking::<bls12_381::Scalar>(&b_bits);
	println!("{:?}",b);
	let w = redact(a,b);

Unfortunately it converts the elements to the vector in the curve but I want to use it for

let w = redact(a,b);

My code for the function is

fn redact<S: PrimeField>(mut a: S, mut b: S) -> S{

	let c = a*b;
	c
}

And this gives me error


error[E0277]: the trait bound `Vec<bls12_381::Scalar>: PrimeField` is not satisfied
  --> src/main.rs:43:10
   |
43 |     let w = redact(a,b);
   |             ^^^^^^ the trait `PrimeField` is not implemented for `Vec<bls12_381::Scalar>`

Is there a way to fix it directly to an element in such that it passes through the function?

Hmm, I have no idea about the big picture of what’s going on in these libraries, but judging from the source code of compute_multipacking, this is really just about converting a number into a field element in one way or another. That function just runs some binary decoding, using methods from PrimeField/SqrtField/etc. Looks like the conversion from u8 to bls12_381::Scalar should never fail; looking into source code of PrimeField::from_str seems to indicate that Self::from_repr(Self::Repr::from(some_u64)).unwrap() seems to be a way to create an element of the field. Indeed something like

fn convert_u8<S: PrimeField>(x: u8) -> S {
    S::from_repr(S::Repr::from(u64::from(x))).unwrap()
}

seems to compile.

Edit: I’m not entirely sure if we’re talking about ff::PrimeField or fff::PrimeField. The ff-version just makes the PrimeField itself inherit From<u64>, so you’d need nothing more than

fn convert_u8<S: ff::PrimeField>(x: u8) -> S {
    S::from(u64::from(x))
}

Its ff primefield I think