Skip to content

Commit

Permalink
Remove use of unwrap() in hash_to_point test
Browse files Browse the repository at this point in the history
  • Loading branch information
therealyingtong committed Jun 5, 2021
1 parent 1e9fa9e commit edc6ec4
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions src/circuit/gadget/sinsemilla/chip/hash_to_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,34 +58,39 @@ impl<C: CurveAffine, const K: usize, const MAX_WORDS: usize> SinsemillaChip<C, K
#[allow(non_snake_case)]
// Check equivalence to result from primitives::sinsemilla::hash_to_point
{
use crate::primitives::sinsemilla::S_PERSONALIZATION;
use crate::circuit::gadget::sinsemilla::message::MessagePiece;
use crate::primitives::sinsemilla::S_PERSONALIZATION;
use halo2::arithmetic::CurveExt;

// Get message as a bitstring.
let bitstring: Vec<bool> = message
.0
.iter()
.map(|piece: &MessagePiece<C::Base, K>| {
piece
.field_elem()
.unwrap()
.to_le_bits()
.into_iter()
.take(K * piece.num_words())
.collect::<Vec<_>>()
})
.flatten()
.collect();

let hasher_S = C::CurveExt::hash_to_curve(S_PERSONALIZATION);
let S = |chunk: &[bool]| hasher_S(&lebs2ip_k(chunk).to_le_bytes());

let expected_point = bitstring
.chunks(K)
.fold(Q.to_curve(), |acc, chunk| (acc + S(chunk)) + acc);
let actual_point = C::from_xy(x_a.value().unwrap(), y_a.unwrap()).unwrap();
assert_eq!(expected_point.to_affine(), actual_point);
let field_elems: Option<Vec<C::Base>> =
message.0.iter().map(|piece| piece.field_elem()).collect();

if let Some(_) = field_elems {
// Get message as a bitstring.
let bitstring: Vec<bool> = message
.0
.iter()
.map(|piece: &MessagePiece<C::Base, K>| {
piece
.field_elem()
.unwrap()
.to_le_bits()
.into_iter()
.take(K * piece.num_words())
.collect::<Vec<_>>()
})
.flatten()
.collect();

let hasher_S = C::CurveExt::hash_to_curve(S_PERSONALIZATION);
let S = |chunk: &[bool]| hasher_S(&lebs2ip_k(chunk).to_le_bytes());

let expected_point = bitstring
.chunks(K)
.fold(Q.to_curve(), |acc, chunk| (acc + S(chunk)) + acc);
let actual_point = C::from_xy(x_a.value().unwrap(), y_a.unwrap()).unwrap();
assert_eq!(expected_point.to_affine(), actual_point);
}
}

// Enable `Sinsemilla` selector on the last double-and-add row for expr1
Expand Down

0 comments on commit edc6ec4

Please sign in to comment.