mirror of
https://github.com/Qortal/pirate-librustzcash.git
synced 2025-07-30 20:11:23 +00:00
ff: Add PrimeField::ReprEndianness associated type
This enables generic code to reliably operate on the bits of an encoded field element, by converting them to and from a known (little) endianness. The BitAnd and Shr bounds on PrimeField are now removed, as users can perform these operations themselves as needed.
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
use byteorder::{ByteOrder, LittleEndian};
|
||||
use ff::{adc, mac_with_carry, sbb, BitIterator, Field, PowVartime, PrimeField};
|
||||
use rand_core::RngCore;
|
||||
use std::mem;
|
||||
use std::ops::{Add, AddAssign, BitAnd, Mul, MulAssign, Neg, Shr, Sub, SubAssign};
|
||||
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
|
||||
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
|
||||
|
||||
use super::ToUniform;
|
||||
@@ -328,53 +327,9 @@ impl MulAssign for Fs {
|
||||
}
|
||||
}
|
||||
|
||||
impl BitAnd<u64> for Fs {
|
||||
type Output = u64;
|
||||
|
||||
#[inline(always)]
|
||||
fn bitand(mut self, rhs: u64) -> u64 {
|
||||
self.mont_reduce(self.0[0], self.0[1], self.0[2], self.0[3], 0, 0, 0, 0);
|
||||
self.0[0] & rhs
|
||||
}
|
||||
}
|
||||
|
||||
impl Shr<u32> for Fs {
|
||||
type Output = Self;
|
||||
|
||||
#[inline(always)]
|
||||
fn shr(mut self, mut n: u32) -> Self {
|
||||
if n as usize >= 64 * 4 {
|
||||
return Self::from(0);
|
||||
}
|
||||
|
||||
// Convert from Montgomery to native representation.
|
||||
self.mont_reduce(self.0[0], self.0[1], self.0[2], self.0[3], 0, 0, 0, 0);
|
||||
|
||||
while n >= 64 {
|
||||
let mut t = 0;
|
||||
for i in self.0.iter_mut().rev() {
|
||||
mem::swap(&mut t, i);
|
||||
}
|
||||
n -= 64;
|
||||
}
|
||||
|
||||
if n > 0 {
|
||||
let mut t = 0;
|
||||
for i in self.0.iter_mut().rev() {
|
||||
let t2 = *i << (64 - n);
|
||||
*i >>= n;
|
||||
*i |= t;
|
||||
t = t2;
|
||||
}
|
||||
}
|
||||
|
||||
// Convert back to Montgomery representation
|
||||
self * R2
|
||||
}
|
||||
}
|
||||
|
||||
impl PrimeField for Fs {
|
||||
type Repr = FsRepr;
|
||||
type ReprEndianness = byteorder::LittleEndian;
|
||||
|
||||
fn from_repr(r: FsRepr) -> Option<Fs> {
|
||||
let r = {
|
||||
@@ -1003,61 +958,6 @@ fn test_fs_mul_assign() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fs_shr() {
|
||||
let mut a = Fs::from_repr(FsRepr([
|
||||
0x3f, 0x28, 0x2a, 0x48, 0xec, 0xba, 0x3f, 0xb3, 0xdf, 0xb3, 0x8c, 0xa8, 0xd3, 0xe0, 0x7d,
|
||||
0x99, 0x25, 0x55, 0x0e, 0x9a, 0x2a, 0x2d, 0xf6, 0x9a, 0xa1, 0x0d, 0xe7, 0x8d, 0xb0, 0x3a,
|
||||
0x00, 0x06,
|
||||
]))
|
||||
.unwrap();
|
||||
a = a >> 0;
|
||||
assert_eq!(
|
||||
a.into_repr(),
|
||||
FsRepr([
|
||||
0x3f, 0x28, 0x2a, 0x48, 0xec, 0xba, 0x3f, 0xb3, 0xdf, 0xb3, 0x8c, 0xa8, 0xd3, 0xe0,
|
||||
0x7d, 0x99, 0x25, 0x55, 0x0e, 0x9a, 0x2a, 0x2d, 0xf6, 0x9a, 0xa1, 0x0d, 0xe7, 0x8d,
|
||||
0xb0, 0x3a, 0x00, 0x06,
|
||||
])
|
||||
);
|
||||
a = a >> 1;
|
||||
assert_eq!(
|
||||
a.into_repr(),
|
||||
FsRepr([
|
||||
0x1f, 0x14, 0x15, 0x24, 0x76, 0xdd, 0x9f, 0xd9, 0xef, 0x59, 0x46, 0xd4, 0x69, 0xf0,
|
||||
0xbe, 0xcc, 0x92, 0x2a, 0x07, 0x4d, 0x95, 0x16, 0x7b, 0xcd, 0xd0, 0x86, 0xf3, 0x46,
|
||||
0x58, 0x1d, 0x00, 0x03,
|
||||
])
|
||||
);
|
||||
a = a >> 50;
|
||||
assert_eq!(
|
||||
a.into_repr(),
|
||||
FsRepr([
|
||||
0x67, 0xf6, 0x7b, 0x96, 0x11, 0x75, 0x1a, 0xbc, 0x2f, 0xb3, 0xa4, 0xca, 0x41, 0x53,
|
||||
0xa5, 0xc5, 0x5e, 0x33, 0xb4, 0xe1, 0xbc, 0x11, 0x56, 0x07, 0xc0, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
])
|
||||
);
|
||||
a = a >> 130;
|
||||
assert_eq!(
|
||||
a.into_repr(),
|
||||
FsRepr([
|
||||
0xd7, 0x0c, 0x6d, 0x38, 0x6f, 0x84, 0xd5, 0x01, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
])
|
||||
);
|
||||
a = a >> 64;
|
||||
assert_eq!(
|
||||
a.into_repr(),
|
||||
FsRepr([
|
||||
0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fs_squaring() {
|
||||
let a = Fs([
|
||||
|
@@ -1,6 +1,6 @@
|
||||
use super::{edwards, montgomery, JubjubEngine, JubjubParams, PrimeOrder};
|
||||
|
||||
use ff::{Field, PrimeField};
|
||||
use ff::{Endianness, Field, PrimeField};
|
||||
use std::ops::{AddAssign, MulAssign, Neg, SubAssign};
|
||||
|
||||
use rand_core::{RngCore, SeedableRng};
|
||||
@@ -372,7 +372,23 @@ fn test_jubjub_params<E: JubjubEngine>(params: &E::Params) {
|
||||
|
||||
let mut cur = E::Fs::one();
|
||||
|
||||
let max = (-E::Fs::one()) >> 1;
|
||||
let max = {
|
||||
// Grab char - 1 in little endian.
|
||||
let mut tmp = (-E::Fs::one()).into_repr();
|
||||
<E::Fs as PrimeField>::ReprEndianness::toggle_little_endian(&mut tmp);
|
||||
|
||||
// Shift right by 1 bit.
|
||||
let mut borrow = 0;
|
||||
for b in tmp.as_mut().iter_mut().rev() {
|
||||
let new_borrow = *b & 1;
|
||||
*b = (borrow << 7) | (*b >> 1);
|
||||
borrow = new_borrow;
|
||||
}
|
||||
|
||||
// Convert back to a field element.
|
||||
<E::Fs as PrimeField>::ReprEndianness::toggle_little_endian(&mut tmp);
|
||||
E::Fs::from_repr(tmp).unwrap()
|
||||
};
|
||||
|
||||
let mut pacc = E::Fs::zero();
|
||||
let mut nacc = E::Fs::zero();
|
||||
|
@@ -1,7 +1,8 @@
|
||||
//! Implementation of the Pedersen hash function used in Sapling.
|
||||
|
||||
use crate::jubjub::*;
|
||||
use ff::Field;
|
||||
use byteorder::{ByteOrder, LittleEndian};
|
||||
use ff::{Endianness, Field, PrimeField};
|
||||
use std::ops::{AddAssign, Neg};
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
@@ -85,17 +86,32 @@ where
|
||||
|
||||
let mut table: &[Vec<edwards::Point<E, _>>] =
|
||||
&generators.next().expect("we don't have enough generators");
|
||||
let window = JubjubBls12::pedersen_hash_exp_window_size();
|
||||
let window_mask = (1 << window) - 1;
|
||||
let window = JubjubBls12::pedersen_hash_exp_window_size() as usize;
|
||||
let window_mask = (1u64 << window) - 1;
|
||||
|
||||
let mut acc = acc.into_repr();
|
||||
<E::Fs as PrimeField>::ReprEndianness::toggle_little_endian(&mut acc);
|
||||
let num_limbs: usize = acc.as_ref().len() / 8;
|
||||
let mut limbs = vec![0u64; num_limbs + 1];
|
||||
LittleEndian::read_u64_into(acc.as_ref(), &mut limbs[..num_limbs]);
|
||||
|
||||
let mut tmp = edwards::Point::zero();
|
||||
|
||||
while !acc.is_zero() {
|
||||
let i = (acc & window_mask) as usize;
|
||||
let mut pos = 0;
|
||||
while pos < E::Fs::NUM_BITS as usize {
|
||||
let u64_idx = pos / 64;
|
||||
let bit_idx = pos % 64;
|
||||
let i = (if bit_idx + window < 64 {
|
||||
// This window's bits are contained in a single u64.
|
||||
limbs[u64_idx] >> bit_idx
|
||||
} else {
|
||||
// Combine the current u64's bits with the bits from the next u64.
|
||||
(limbs[u64_idx] >> bit_idx) | (limbs[u64_idx + 1] << (64 - bit_idx))
|
||||
} & window_mask) as usize;
|
||||
|
||||
tmp = tmp.add(&table[0][i], params);
|
||||
|
||||
acc = acc >> window;
|
||||
pos += window;
|
||||
table = &table[1..];
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user