Merge pull request #110 from str4d/crate-cleanups

Crate cleanups
This commit is contained in:
str4d
2019-09-04 19:44:47 -04:00
committed by GitHub
47 changed files with 344 additions and 336 deletions

View File

@@ -50,7 +50,7 @@ fn main() {
nsk: nsk.clone(),
};
let viewing_key = proof_generation_key.into_viewing_key(jubjub_params);
let viewing_key = proof_generation_key.to_viewing_key(jubjub_params);
let payment_address;
@@ -61,7 +61,7 @@ fn main() {
Diversifier(d)
};
if let Some(p) = viewing_key.into_payment_address(diversifier, jubjub_params) {
if let Some(p) = viewing_key.to_payment_address(diversifier, jubjub_params) {
payment_address = p;
break;
}

View File

@@ -41,16 +41,16 @@ where
{
let chunk_a = chunk
.get(0)
.map(|e| e.clone())
.unwrap_or(Boolean::constant(false));
.cloned()
.unwrap_or_else(|| Boolean::constant(false));
let chunk_b = chunk
.get(1)
.map(|e| e.clone())
.unwrap_or(Boolean::constant(false));
.cloned()
.unwrap_or_else(|| Boolean::constant(false));
let chunk_c = chunk
.get(2)
.map(|e| e.clone())
.unwrap_or(Boolean::constant(false));
.cloned()
.unwrap_or_else(|| Boolean::constant(false));
let (x, y) = lookup3_xy(
cs.namespace(|| format!("window table lookup {}", i)),
@@ -58,7 +58,7 @@ where
window,
)?;
let p = EdwardsPoint { x: x, y: y };
let p = EdwardsPoint { x, y };
if result.is_none() {
result = Some(p);
@@ -121,9 +121,9 @@ impl<E: JubjubEngine> EdwardsPoint<E> {
{
let mut tmp = vec![];
let x = self.x.into_bits_le_strict(cs.namespace(|| "unpack x"))?;
let x = self.x.to_bits_le_strict(cs.namespace(|| "unpack x"))?;
let y = self.y.into_bits_le_strict(cs.namespace(|| "unpack y"))?;
let y = self.y.to_bits_le_strict(cs.namespace(|| "unpack y"))?;
tmp.extend(y);
tmp.push(x[0].clone());
@@ -141,7 +141,7 @@ impl<E: JubjubEngine> EdwardsPoint<E> {
where
CS: ConstraintSystem<E>,
{
let p = p.map(|p| p.into_xy());
let p = p.map(|p| p.to_xy());
// Allocate x
let x = AllocatedNum::alloc(cs.namespace(|| "x"), || Ok(p.get()?.0))?;
@@ -508,7 +508,7 @@ impl<E: JubjubEngine> MontgomeryPoint<E> {
/// a point in the birationally equivalent twisted
/// Edwards curve.
pub fn into_edwards<CS>(
&self,
self,
mut cs: CS,
params: &E::Params,
) -> Result<EdwardsPoint<E>, SynthesisError>
@@ -570,7 +570,7 @@ impl<E: JubjubEngine> MontgomeryPoint<E> {
/// on the curve. Useful for constants and
/// window table lookups.
pub fn interpret_unchecked(x: Num<E>, y: Num<E>) -> Self {
MontgomeryPoint { x: x, y: y }
MontgomeryPoint { x, y }
}
/// Performs an affine point addition, not defined for
@@ -688,8 +688,8 @@ mod test {
let mut cs = TestConstraintSystem::<Bls12>::new();
let p = montgomery::Point::<Bls12, _>::rand(rng, params);
let (u, v) = edwards::Point::from_montgomery(&p, params).into_xy();
let (x, y) = p.into_xy().unwrap();
let (u, v) = edwards::Point::from_montgomery(&p, params).to_xy();
let (x, y) = p.to_xy().unwrap();
let numx = AllocatedNum::alloc(cs.namespace(|| "mont x"), || Ok(x)).unwrap();
let numy = AllocatedNum::alloc(cs.namespace(|| "mont y"), || Ok(y)).unwrap();
@@ -728,7 +728,7 @@ mod test {
let mut cs = TestConstraintSystem::<Bls12>::new();
let q = EdwardsPoint::witness(&mut cs, Some(p.clone()), &params).unwrap();
let p = p.into_xy();
let p = p.to_xy();
assert!(cs.is_satisfied());
assert_eq!(q.x.get_value().unwrap(), p.0);
@@ -737,7 +737,7 @@ mod test {
for _ in 0..100 {
let p = edwards::Point::<Bls12, _>::rand(rng, &params);
let (x, y) = p.into_xy();
let (x, y) = p.to_xy();
let mut cs = TestConstraintSystem::<Bls12>::new();
let numx = AllocatedNum::alloc(cs.namespace(|| "x"), || Ok(x)).unwrap();
@@ -779,7 +779,7 @@ mod test {
let p = params.generator(FixedGenerators::NoteCommitmentRandomness);
let s = Fs::random(rng);
let q = p.mul(s, params);
let (x1, y1) = q.into_xy();
let (x1, y1) = q.to_xy();
let mut s_bits = BitIterator::new(s.into_repr()).collect::<Vec<_>>();
s_bits.reverse();
@@ -823,8 +823,8 @@ mod test {
let s = Fs::random(rng);
let q = p.mul(s, params);
let (x0, y0) = p.into_xy();
let (x1, y1) = q.into_xy();
let (x0, y0) = p.to_xy();
let (x1, y1) = q.to_xy();
let num_x0 = AllocatedNum::alloc(cs.namespace(|| "x0"), || Ok(x0)).unwrap();
let num_y0 = AllocatedNum::alloc(cs.namespace(|| "y0"), || Ok(y0)).unwrap();
@@ -873,7 +873,7 @@ mod test {
let p = edwards::Point::<Bls12, _>::rand(rng, params);
let (x0, y0) = p.into_xy();
let (x0, y0) = p.to_xy();
let num_x0 = AllocatedNum::alloc(cs.namespace(|| "x0"), || Ok(x0)).unwrap();
let num_y0 = AllocatedNum::alloc(cs.namespace(|| "y0"), || Ok(y0)).unwrap();
@@ -941,9 +941,9 @@ mod test {
let p3 = p1.add(&p2, params);
let (x0, y0) = p1.into_xy();
let (x1, y1) = p2.into_xy();
let (x2, y2) = p3.into_xy();
let (x0, y0) = p1.to_xy();
let (x1, y1) = p2.to_xy();
let (x2, y2) = p3.to_xy();
let mut cs = TestConstraintSystem::<Bls12>::new();
@@ -1002,8 +1002,8 @@ mod test {
let p1 = edwards::Point::<Bls12, _>::rand(rng, params);
let p2 = p1.double(params);
let (x0, y0) = p1.into_xy();
let (x1, y1) = p2.into_xy();
let (x0, y0) = p1.to_xy();
let (x1, y1) = p2.to_xy();
let mut cs = TestConstraintSystem::<Bls12>::new();
@@ -1053,9 +1053,9 @@ mod test {
let p3 = p1.add(&p2, params);
let (x0, y0) = p1.into_xy().unwrap();
let (x1, y1) = p2.into_xy().unwrap();
let (x2, y2) = p3.into_xy().unwrap();
let (x0, y0) = p1.to_xy().unwrap();
let (x1, y1) = p2.to_xy().unwrap();
let (x2, y2) = p3.to_xy().unwrap();
let mut cs = TestConstraintSystem::<Bls12>::new();

View File

@@ -9,7 +9,7 @@ fn get_constant_bools(person: &Personalization) -> Vec<Boolean> {
person
.get_bits()
.into_iter()
.map(|e| Boolean::constant(e))
.map(Boolean::constant)
.collect()
}
@@ -65,7 +65,7 @@ where
segment_windows = &segment_windows[1..];
if segment_windows.len() == 0 {
if segment_windows.is_empty() {
break;
}
@@ -189,7 +189,7 @@ mod test {
input.clone().into_iter(),
params,
)
.into_xy();
.to_xy();
assert_eq!(res.get_x().get_value().unwrap(), expected.0);
assert_eq!(res.get_y().get_value().unwrap(), expected.1);
@@ -200,7 +200,7 @@ mod test {
input.into_iter(),
params,
)
.into_xy();
.to_xy();
assert!(res.get_x().get_value().unwrap() != unexpected.0);
assert!(res.get_y().get_value().unwrap() != unexpected.1);

View File

@@ -150,7 +150,7 @@ impl<'a, E: JubjubEngine> Circuit<E> for Spend<'a, E> {
// Witness nsk as bits
let nsk = boolean::field_into_boolean_vec_le(
cs.namespace(|| "nsk"),
self.proof_generation_key.as_ref().map(|k| k.nsk.clone()),
self.proof_generation_key.as_ref().map(|k| k.nsk),
)?;
// NB: We don't ensure that the bit representation of nsk
@@ -336,8 +336,8 @@ impl<'a, E: JubjubEngine> Circuit<E> for Spend<'a, E> {
// they will be unable to find an authentication path in the
// tree with high probability.
let mut preimage = vec![];
preimage.extend(xl.into_bits_le(cs.namespace(|| "xl into bits"))?);
preimage.extend(xr.into_bits_le(cs.namespace(|| "xr into bits"))?);
preimage.extend(xl.to_bits_le(cs.namespace(|| "xl into bits"))?);
preimage.extend(xr.to_bits_le(cs.namespace(|| "xr into bits"))?);
// Compute the new subtree value
cur = pedersen_hash::pedersen_hash(
@@ -464,7 +464,7 @@ impl<'a, E: JubjubEngine> Circuit<E> for Output<'a, E> {
// they would like.
{
// Just grab pk_d from the witness
let pk_d = self.payment_address.as_ref().map(|e| e.pk_d.into_xy());
let pk_d = self.payment_address.as_ref().map(|e| e.pk_d.to_xy());
// Witness the y-coordinate, encoded as little
// endian bits (to match the representation)
@@ -567,7 +567,7 @@ fn test_input_circuit_with_bls12_381() {
nsk: nsk.clone(),
};
let viewing_key = proof_generation_key.into_viewing_key(params);
let viewing_key = proof_generation_key.to_viewing_key(params);
let payment_address;
@@ -578,7 +578,7 @@ fn test_input_circuit_with_bls12_381() {
Diversifier(d)
};
if let Some(p) = viewing_key.into_payment_address(diversifier, params) {
if let Some(p) = viewing_key.to_payment_address(diversifier, params) {
payment_address = p;
break;
}
@@ -590,8 +590,8 @@ fn test_input_circuit_with_bls12_381() {
let ar = fs::Fs::random(rng);
{
let rk = viewing_key.rk(ar, params).into_xy();
let expected_value_cm = value_commitment.cm(params).into_xy();
let rk = viewing_key.rk(ar, params).to_xy();
let expected_value_cm = value_commitment.cm(params).to_xy();
let note = Note {
value: value_commitment.value,
g_d: g_d.clone(),
@@ -626,7 +626,7 @@ fn test_input_circuit_with_bls12_381() {
.chain(rhs.into_iter().take(Fr::NUM_BITS as usize)),
params,
)
.into_xy()
.to_xy()
.0;
if b {
@@ -642,7 +642,7 @@ fn test_input_circuit_with_bls12_381() {
let mut cs = TestConstraintSystem::<Bls12>::new();
let instance = Spend {
params: params,
params,
value_commitment: Some(value_commitment.clone()),
proof_generation_key: Some(proof_generation_key.clone()),
payment_address: Some(payment_address.clone()),
@@ -714,7 +714,7 @@ fn test_output_circuit_with_bls12_381() {
nsk: nsk.clone(),
};
let viewing_key = proof_generation_key.into_viewing_key(params);
let viewing_key = proof_generation_key.to_viewing_key(params);
let payment_address;
@@ -725,7 +725,7 @@ fn test_output_circuit_with_bls12_381() {
Diversifier(d)
};
if let Some(p) = viewing_key.into_payment_address(diversifier, params) {
if let Some(p) = viewing_key.to_payment_address(diversifier, params) {
payment_address = p;
break;
}
@@ -738,7 +738,7 @@ fn test_output_circuit_with_bls12_381() {
let mut cs = TestConstraintSystem::<Bls12>::new();
let instance = Output {
params: params,
params,
value_commitment: Some(value_commitment.clone()),
payment_address: Some(payment_address.clone()),
commitment_randomness: Some(commitment_randomness),
@@ -759,13 +759,13 @@ fn test_output_circuit_with_bls12_381() {
.expect("should be valid")
.cm(params);
let expected_value_cm = value_commitment.cm(params).into_xy();
let expected_value_cm = value_commitment.cm(params).to_xy();
let expected_epk = payment_address
.g_d(params)
.expect("should be valid")
.mul(esk, params);
let expected_epk_xy = expected_epk.into_xy();
let expected_epk_xy = expected_epk.to_xy();
assert_eq!(cs.num_inputs(), 6);
assert_eq!(cs.get_input(0, "ONE"), Fr::one());

View File

@@ -54,7 +54,7 @@ impl InputNote {
// Witness into the merkle tree
let mut cur = cm.clone();
for (i, layer) in auth_path.into_iter().enumerate() {
for (i, layer) in auth_path.iter().enumerate() {
let cs = &mut cs.namespace(|| format!("layer {}", i));
let cur_is_right = AllocatedBit::alloc(
@@ -112,7 +112,7 @@ impl InputNote {
);
}
Ok(InputNote { mac: mac, nf: nf })
Ok(InputNote { mac, nf })
}
}

View File

@@ -234,10 +234,7 @@ impl NoteValue {
)?);
}
Ok(NoteValue {
value: value,
bits: bits,
})
Ok(NoteValue { value, bits })
}
/// Encodes the bits of the value into little-endian
@@ -247,7 +244,7 @@ impl NoteValue {
.chunks(8)
.flat_map(|v| v.iter().rev())
.cloned()
.map(|e| Boolean::from(e))
.map(Boolean::from)
.collect()
}
@@ -380,11 +377,11 @@ fn test_sprout_constraints() {
let a_sk = Some(SpendingKey(get_u256(&mut test_vector)));
inputs.push(JSInput {
value: value,
a_sk: a_sk,
rho: rho,
r: r,
auth_path: auth_path,
value,
a_sk,
rho,
r,
auth_path,
});
}
@@ -396,11 +393,7 @@ fn test_sprout_constraints() {
get_u256(&mut test_vector);
let r = Some(CommitmentRandomness(get_u256(&mut test_vector)));
outputs.push(JSOutput {
value: value,
a_pk: a_pk,
r: r,
});
outputs.push(JSOutput { value, a_pk, r });
}
let vpub_old = Some(test_vector.read_u64::<LittleEndian>().unwrap());
@@ -416,13 +409,13 @@ fn test_sprout_constraints() {
let mac2 = get_u256(&mut test_vector);
let js = JoinSplit {
vpub_old: vpub_old,
vpub_new: vpub_new,
h_sig: h_sig,
phi: phi,
inputs: inputs,
outputs: outputs,
rt: rt,
vpub_old,
vpub_new,
h_sig,
phi,
inputs,
outputs,
rt,
};
js.synthesize(&mut cs).unwrap();

View File

@@ -11,7 +11,7 @@ pub struct OutputNote {
}
impl OutputNote {
pub fn compute<'a, E, CS>(
pub fn compute<E, CS>(
mut cs: CS,
a_pk: Option<PayingKey>,
value: &NoteValue,
@@ -41,6 +41,6 @@ impl OutputNote {
&r,
)?;
Ok(OutputNote { cm: cm })
Ok(OutputNote { cm })
}
}

View File

@@ -11,7 +11,7 @@ impl<R: Read> HashReader<R> {
/// Construct a new `HashReader` given an existing `reader` by value.
pub fn new(reader: R) -> Self {
HashReader {
reader: reader,
reader,
hasher: State::new(),
}
}

View File

@@ -65,7 +65,7 @@ impl SaplingProvingContext {
// Accumulate the value commitment randomness in the context
{
let mut tmp = rcv.clone();
let mut tmp = rcv;
tmp.add_assign(&self.bsk);
// Update the context
@@ -74,15 +74,15 @@ impl SaplingProvingContext {
// Construct the value commitment
let value_commitment = ValueCommitment::<Bls12> {
value: value,
value,
randomness: rcv,
};
// Construct the viewing key
let viewing_key = proof_generation_key.into_viewing_key(params);
let viewing_key = proof_generation_key.to_viewing_key(params);
// Construct the payment address with the viewing key / diversifier
let payment_address = match viewing_key.into_payment_address(diversifier, params) {
let payment_address = match viewing_key.to_payment_address(diversifier, params) {
Some(p) => p,
None => return Err(()),
};
@@ -96,7 +96,7 @@ impl SaplingProvingContext {
// Let's compute the nullifier while we have the position
let note = Note {
value: value,
value,
g_d: diversifier
.g_d::<Bls12>(params)
.expect("was a valid diversifier before"),
@@ -130,12 +130,12 @@ impl SaplingProvingContext {
// Construct public input for circuit
let mut public_input = [Fr::zero(); 7];
{
let (x, y) = rk.0.into_xy();
let (x, y) = rk.0.to_xy();
public_input[0] = x;
public_input[1] = y;
}
{
let (x, y) = value_commitment.cm(params).into_xy();
let (x, y) = value_commitment.cm(params).to_xy();
public_input[2] = x;
public_input[3] = y;
}
@@ -200,7 +200,7 @@ impl SaplingProvingContext {
// Accumulate the value commitment randomness in the context
{
let mut tmp = rcv.clone();
let mut tmp = rcv;
tmp.negate(); // Outputs subtract from the total.
tmp.add_assign(&self.bsk);
@@ -210,7 +210,7 @@ impl SaplingProvingContext {
// Construct the value commitment for the proof instance
let value_commitment = ValueCommitment::<Bls12> {
value: value,
value,
randomness: rcv,
};
@@ -220,7 +220,7 @@ impl SaplingProvingContext {
value_commitment: Some(value_commitment.clone()),
payment_address: Some(payment_address.clone()),
commitment_randomness: Some(rcm),
esk: Some(esk.clone()),
esk: Some(esk),
};
// Create proof

View File

@@ -82,12 +82,12 @@ impl SaplingVerificationContext {
// Construct public input for circuit
let mut public_input = [Fr::zero(); 7];
{
let (x, y) = rk.0.into_xy();
let (x, y) = rk.0.to_xy();
public_input[0] = x;
public_input[1] = y;
}
{
let (x, y) = cv.into_xy();
let (x, y) = cv.to_xy();
public_input[2] = x;
public_input[3] = y;
}
@@ -146,12 +146,12 @@ impl SaplingVerificationContext {
// Construct public input for circuit
let mut public_input = [Fr::zero(); 5];
{
let (x, y) = cv.into_xy();
let (x, y) = cv.to_xy();
public_input[0] = x;
public_input[1] = y;
}
{
let (x, y) = epk.into_xy();
let (x, y) = epk.to_xy();
public_input[2] = x;
public_input[3] = y;
}