Skip to content

Commit d9128b4

Browse files
committed
remove for return const
1 parent ccd207e commit d9128b4

File tree

1 file changed

+17
-103
lines changed

1 file changed

+17
-103
lines changed

packages/crypto/src/ecvrf.rs

Lines changed: 17 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ macro_rules! some_or_return_false {
1414

1515
lazy_static! {
1616
static ref SUITE_STRING: Vec<u8> = decode("04").unwrap();
17-
static ref DST: Vec<u8> =
18-
decode("45435652465f6564776172647332353531395f584d443a5348412d3531325f454c4c325f4e555f04")
19-
.unwrap();
2017
static ref BITS: usize = 256;
2118
static ref PRIME: Integer =
2219
"57896044618658097711785492504343953926634992332820282019728792003956564819949"
@@ -166,68 +163,24 @@ pub fn ecvrf_decode_proof(pi: &[u8]) -> Option<((Integer, Integer), Integer, Int
166163
Some((gamma, c, s))
167164
}
168165

169-
pub fn i2osp(x: &Integer, x_len: u8) -> Option<Vec<u8>> {
170-
match x_len {
171-
1 => {
172-
if Integer::from(x >> 8) >= 1 {
173-
return None;
174-
}
175-
}
176-
2 => {
177-
if Integer::from(x >> 16) >= 1 {
178-
return None;
179-
}
180-
}
181-
128 => {
182-
if Integer::from(x >> 1024) >= 1 {
183-
return None;
184-
}
185-
}
186-
_ => return None,
187-
}
188-
let mut digits = vec![0u8; x_len as usize];
189-
let mut tmp_x = x.clone();
190-
for i in (0..(x_len as usize)).rev() {
191-
digits[i] = (&tmp_x & Integer::from(255)).to_u8().unwrap();
192-
tmp_x >>= 8;
193-
if tmp_x == 0 {
194-
break;
195-
}
196-
}
197-
198-
Some(digits)
166+
pub fn expand_message_xmd(msg: &[u8]) -> Option<Vec<u8>> {
167+
let dst_prime = vec![
168+
69, 67, 86, 82, 70, 95, 101, 100, 119, 97, 114, 100, 115, 50, 53, 53, 49, 57, 95, 88, 77,
169+
68, 58, 83, 72, 65, 45, 53, 49, 50, 95, 69, 76, 76, 50, 95, 78, 85, 95, 4, 40,
170+
];
171+
let msg_prime = [&[0u8; 128], msg, &[0, 48], &[0], &dst_prime].concat();
172+
Some(hash(&[hash(&msg_prime), vec![1], dst_prime].concat()))
199173
}
200174

201-
pub fn os2ip(x: &[u8]) -> Integer {
202-
Integer::from_digits(x, Order::Msf)
203-
}
204-
205-
pub fn expand_message_xmd(msg: &[u8], dst: &[u8], len_in_bytes: &Integer) -> Option<Vec<u8>> {
206-
let dst_prime = [dst, &i2osp(&Integer::from(dst.len()), 1)?].concat();
207-
let z_pad = i2osp(&Integer::from(0), 128)?;
208-
let l_i_b_str = i2osp(len_in_bytes, 2)?;
209-
let msg_prime = [
210-
&z_pad,
211-
msg,
212-
&l_i_b_str,
213-
&i2osp(&Integer::from(0), 1)?,
214-
&dst_prime,
215-
]
216-
.concat();
217-
Some(hash(
218-
&[hash(&msg_prime), i2osp(&Integer::from(1), 1)?, dst_prime].concat(),
175+
pub fn hash_to_field(msg: &[u8]) -> Option<Integer> {
176+
Some(modulus(
177+
&Integer::from_digits(&expand_message_xmd(msg)?[..48], Order::Msf),
178+
&*PRIME,
219179
))
220180
}
221181

222-
pub fn hash_to_field(msg: &[u8], count: &Integer) -> Option<Integer> {
223-
let m = Integer::from(1);
224-
let l = Integer::from(48);
225-
let uniform_bytes = expand_message_xmd(msg, &*DST, &(count * m * l))?;
226-
Some(modulus(&os2ip(&uniform_bytes[..48]), &*PRIME))
227-
}
228-
229182
pub fn ecvrf_hash_to_curve_elligator2_25519(y: &[u8], alpha: &[u8]) -> Option<Vec<u8>> {
230-
let u = hash_to_field(&[y, alpha].concat(), &Integer::from(1))?;
183+
let u = hash_to_field(&[y, alpha].concat())?;
231184

232185
let mut tv1 = (&u * &u).complete();
233186
tv1 = modulus(&(2 * tv1), &*PRIME);
@@ -586,69 +539,32 @@ mod tests {
586539
);
587540
}
588541

589-
#[test]
590-
fn i2osp_test() {
591-
assert_eq!(i2osp(&"300".parse::<Integer>().unwrap(), 1), None);
592-
assert_eq!(
593-
encode(i2osp(&"200".parse::<Integer>().unwrap(), 1).unwrap()),
594-
"c8"
595-
);
596-
assert_eq!(i2osp(&"55555".parse::<Integer>().unwrap(), 1), None);
597-
assert_eq!(
598-
encode(i2osp(&"55555".parse::<Integer>().unwrap(), 2).unwrap()),
599-
"d903"
600-
);
601-
assert_eq!(i2osp(&"179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216".parse::<Integer>().unwrap(), 128), None);
602-
assert_eq!(
603-
encode(i2osp(&"179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137215".parse::<Integer>().unwrap(), 128).unwrap()),
604-
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
605-
);
606-
assert_eq!(
607-
encode(i2osp(&"12156183745850511073089323218562745643254017618359848732866684019020326374996".parse::<Integer>().unwrap(), 128).unwrap()),
608-
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001ae027fb30a0ad4249925c94f4a2bfc6e0912f56ec69132788daf248bff20e54"
609-
);
610-
}
611-
612-
#[test]
613-
fn os2ip_test() {
614-
assert_eq!(os2ip(&vec![1, 2, 3]), "66051".parse::<Integer>().unwrap());
615-
assert_eq!(
616-
os2ip(&vec![
617-
87, 99, 51, 71, 222, 66, 154, 126, 184, 43, 4, 251, 92, 179, 117, 77, 198, 21, 225,
618-
149, 206, 82, 57, 150, 44, 149, 111, 204, 203, 185, 28, 254
619-
]),
620-
"39526489612783863363061450190903516942151074871840484375217699171994072325374"
621-
.parse::<Integer>()
622-
.unwrap()
623-
);
624-
}
625-
626542
#[test]
627543
fn expand_message_xmd_test() {
628544
assert_eq!(
629-
expand_message_xmd(&vec![] ,&*DST,&Integer::from(48)).unwrap(),
545+
expand_message_xmd(&vec![]).unwrap(),
630546
decode("de5b8109b80da1d4861defe3e20710c8ac2efe65d815bb79d0b0087ddb0667718adb94fa478843979611e80749109ca55881a12b9d64c9ae5f7b36075f8e0354").unwrap()
631547
);
632548
assert_eq!(
633-
expand_message_xmd(&decode("0102040810204080ff").unwrap() ,&*DST,&Integer::from(48)).unwrap(),
549+
expand_message_xmd(&decode("0102040810204080ff").unwrap()).unwrap(),
634550
decode("916b471e7c4d60e8a4ba6d0310e4e8de5a59d94011c4e8d2843d452a1651b9f854f5582788dec477b3811cd56973dbbba346a98877ffd1b61d045caccbdddbe8").unwrap()
635551
);
636552
assert_eq!(
637-
expand_message_xmd(&decode("756f547ab8accc336a280f96343cfdbe9621935dcb452bba4f3460ef8f090883").unwrap() ,&*DST,&Integer::from(48)).unwrap(),
553+
expand_message_xmd(&decode("756f547ab8accc336a280f96343cfdbe9621935dcb452bba4f3460ef8f090883").unwrap()).unwrap(),
638554
decode("365d2351f19838da62f7b68464f61e961a01cbc3fdde0099bdc3db6b3a9c3f8d23eeacc1865e570b063263d3e8ded3c4cd4a11566f96ca5f63d06bb65d815bb8").unwrap()
639555
);
640556
}
641557

642558
#[test]
643559
fn hash_to_field_test() {
644560
assert_eq!(
645-
hash_to_field(&vec![], &Integer::from(1)).unwrap(),
561+
hash_to_field(&vec![]).unwrap(),
646562
"19984796091926620114398603282246129530205018809106914407141744082303129033320"
647563
.parse::<Integer>()
648564
.unwrap()
649565
);
650566
assert_eq!(
651-
hash_to_field(&decode("0102040810204080ff").unwrap(), &Integer::from(1)).unwrap(),
567+
hash_to_field(&decode("0102040810204080ff").unwrap()).unwrap(),
652568
"40866905167524404221649250981304847553674991259516901614549124933108104064175"
653569
.parse::<Integer>()
654570
.unwrap()
@@ -657,7 +573,6 @@ mod tests {
657573
hash_to_field(
658574
&decode("6073bd567edb2e1d6ef03cb70a54017ffd5b874b136bbbddfbc5a8af6606b697")
659575
.unwrap(),
660-
&Integer::from(1)
661576
)
662577
.unwrap(),
663578
"42190151610809284644600066009282933920020180701265092905748556772002395560942"
@@ -668,7 +583,6 @@ mod tests {
668583
hash_to_field(
669584
&decode("1152c7e217f100d85a6b7e51cb8e6c838a8fc8c95a5ab43ac7412a085cd67307431cd149b898b98c017fe1003bf848ad1dc2254b093497bfab90159ea54c5559")
670585
.unwrap(),
671-
&Integer::from(1)
672586
)
673587
.unwrap(),
674588
"7289615016767941863395051431412729080032480398674317575538643993554362504793"

0 commit comments

Comments
 (0)