Added serialization and test vectors for U256 and Fp elements.

This commit is contained in:
Sean Bowe
2016-09-07 01:20:21 -06:00
parent 77df6c9ee5
commit cb2ff5c1ad
6 changed files with 20172 additions and 0 deletions
+16
View File
@@ -4,6 +4,8 @@ use std::fmt;
use std::marker::PhantomData;
use super::FieldElement;
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
use arith::U256;
pub trait FpParams {
@@ -44,6 +46,20 @@ pub fn const_fp<P: FpParams, I: Into<U256>>(i: I) -> Fp<P> {
Fp(i.into(), PhantomData)
}
impl<P: FpParams> Encodable for Fp<P> {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
let normalized = U256::from(*self);
normalized.encode(s)
}
}
impl<P: FpParams> Decodable for Fp<P> {
fn decode<S: Decoder>(s: &mut S) -> Result<Fp<P>, S::Error> {
Fp::new(try!(U256::decode(s))).ok_or_else(|| s.error("integer is not less than modulus"))
}
}
impl<P: FpParams> Fp<P> {
pub fn from_str(s: &str) -> Option<Self> {
let ints: Vec<_> = {