|
| 1 | +use bls12_381::{ |
| 2 | + hash_to_curve::{ExpandMsgXmd, HashToCurve}, |
| 3 | + pairing, G1Affine, G1Projective, G2Affine, G2Projective, Scalar, |
| 4 | +}; |
| 5 | + |
| 6 | +use bls_org; |
| 7 | +use dvt_abi::{self}; |
| 8 | +use sha2::Sha256; |
| 9 | + |
| 10 | +pub fn hash_message_to_g2(msg: &[u8]) -> G2Projective { |
| 11 | + let domain = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_"; |
| 12 | + <G2Projective as HashToCurve<ExpandMsgXmd<Sha256>>>::hash_to_curve(msg, domain) |
| 13 | +} |
| 14 | + |
| 15 | +pub fn bls_verify_precomputed_hash( |
| 16 | + pubkey: &G1Affine, |
| 17 | + signature: &G2Affine, |
| 18 | + hashed_msg: &G2Affine, |
| 19 | +) -> bool { |
| 20 | + let left = pairing(&pubkey, &hashed_msg); |
| 21 | + let right = pairing(&G1Affine::generator(), &signature); |
| 22 | + |
| 23 | + left == right |
| 24 | +} |
| 25 | +pub fn bls_verify(pubkey: &G1Affine, signature: &G2Affine, message: &[u8]) -> bool { |
| 26 | + let hashed_msg = hash_message_to_g2(message); |
| 27 | + let msg_affine = G2Affine::from(hashed_msg); |
| 28 | + bls_verify_precomputed_hash(pubkey, signature, &msg_affine) |
| 29 | +} |
| 30 | + |
| 31 | +pub fn bls_id_from_u32(id: u32) -> Scalar { |
| 32 | + let unwrapped_le: [u8; 4] = (id as u32).to_le_bytes(); |
| 33 | + let mut bytes = [0u8; 32]; |
| 34 | + bytes[..4].copy_from_slice(&unwrapped_le); |
| 35 | + Scalar::from_bytes(&bytes).expect("Invalid id") |
| 36 | +} |
| 37 | + |
| 38 | +fn uncompress_bls_pubkey_slow( |
| 39 | + pubkey: &dvt_abi::BLSPubkey, |
| 40 | +) -> Result<[u8; 96], Box<dyn std::error::Error>> { |
| 41 | + // We use the original bls library to verify the key |
| 42 | + // Becaus the sp1 library will crash if the key is invalid |
| 43 | + let key = bls_org::G1Affine::from_compressed(&pubkey); |
| 44 | + |
| 45 | + return match key.into_option() { |
| 46 | + Some(key) => Ok(key.to_uncompressed()), |
| 47 | + None => Err(Box::new(std::io::Error::new( |
| 48 | + std::io::ErrorKind::InvalidData, |
| 49 | + format!("Invalid public key {}", hex::encode(&pubkey)), |
| 50 | + ))), |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +pub fn to_g1_affine_slow( |
| 55 | + pubkey: &dvt_abi::BLSPubkey, |
| 56 | +) -> Result<G1Affine, Box<dyn std::error::Error>> { |
| 57 | + let bytes = uncompress_bls_pubkey_slow(&pubkey)?; |
| 58 | + |
| 59 | + let key = G1Affine::from_uncompressed(&bytes); |
| 60 | + match key.into_option() { |
| 61 | + Some(key) => Ok(key), |
| 62 | + None => Err(Box::new(std::io::Error::new( |
| 63 | + std::io::ErrorKind::InvalidData, |
| 64 | + format!("Invalid public key {}", hex::encode(&pubkey)), |
| 65 | + ))), |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +fn uncompress_bls_signature_slow( |
| 70 | + signature: &dvt_abi::BLSSignature, |
| 71 | +) -> Result<[u8; 192], Box<dyn std::error::Error>> { |
| 72 | + // We use the original bls library to verify the key |
| 73 | + // Becaus the sp1 library will crash if the key is invalid |
| 74 | + let key = bls_org::G2Affine::from_compressed(&signature); |
| 75 | + |
| 76 | + return match key.into_option() { |
| 77 | + Some(key) => Ok(key.to_uncompressed()), |
| 78 | + None => Err(Box::new(std::io::Error::new( |
| 79 | + std::io::ErrorKind::InvalidData, |
| 80 | + "Invalid signature", |
| 81 | + ))), |
| 82 | + }; |
| 83 | +} |
| 84 | +pub fn to_g2_affine_slow( |
| 85 | + signature: &dvt_abi::BLSSignature, |
| 86 | +) -> Result<G2Affine, Box<dyn std::error::Error>> { |
| 87 | + let bytes = uncompress_bls_signature_slow(&signature)?; |
| 88 | + |
| 89 | + let key = G2Affine::from_uncompressed(&bytes); |
| 90 | + match key.into_option() { |
| 91 | + Some(key) => Ok(key), |
| 92 | + None => Err(Box::new(std::io::Error::new( |
| 93 | + std::io::ErrorKind::InvalidData, |
| 94 | + "Invalid signature", |
| 95 | + ))), |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +pub fn to_g1_affine(pubkey: &dvt_abi::BLSPubkey) -> G1Affine { |
| 100 | + G1Affine::from_compressed(&pubkey) |
| 101 | + .into_option() |
| 102 | + .expect("G1 point is not torsion free.") |
| 103 | +} |
| 104 | + |
| 105 | +pub fn to_g1_projection(pubkey: &dvt_abi::BLSPubkey) -> G1Projective { |
| 106 | + G1Projective::from(to_g1_affine(pubkey)) |
| 107 | +} |
0 commit comments