-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathlib.rs
105 lines (93 loc) · 3.41 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#![warn(unreachable_pub)]
#![warn(clippy::semicolon_if_nothing_returned)]
#![cfg_attr(not(test), warn(unused_crate_dependencies, unused_extern_crates))]
use acvm_blackbox_solver::{BlackBoxFunctionSolver, BlackBoxResolutionError};
mod embedded_curve_ops;
mod generator;
mod pedersen;
mod poseidon2;
mod schnorr;
use ark_ec::AffineRepr;
pub use embedded_curve_ops::{embedded_curve_add, multi_scalar_mul};
pub use generator::generators::derive_generators;
pub use poseidon2::{
field_from_hex, poseidon2_permutation, poseidon_hash, Poseidon2Config, Poseidon2Sponge,
POSEIDON2_CONFIG,
};
// Temporary hack, this ensure that we always use a bn254 field here
// without polluting the feature flags of the `acir_field` crate.
type FieldElement = acir::acir_field::GenericFieldElement<ark_bn254::Fr>;
#[derive(Default)]
pub struct Bn254BlackBoxSolver;
impl BlackBoxFunctionSolver<FieldElement> for Bn254BlackBoxSolver {
fn schnorr_verify(
&self,
public_key_x: &FieldElement,
public_key_y: &FieldElement,
signature: &[u8; 64],
message: &[u8],
) -> Result<bool, BlackBoxResolutionError> {
let sig_s: [u8; 32] = signature[0..32].try_into().unwrap();
let sig_e: [u8; 32] = signature[32..64].try_into().unwrap();
Ok(schnorr::verify_signature(
public_key_x.into_repr(),
public_key_y.into_repr(),
sig_s,
sig_e,
message,
))
}
fn pedersen_commitment(
&self,
inputs: &[FieldElement],
domain_separator: u32,
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> {
let inputs: Vec<grumpkin::Fq> = inputs.iter().map(|input| input.into_repr()).collect();
let result = pedersen::commitment::commit_native_with_index(&inputs, domain_separator);
let result = if let Some((x, y)) = result.xy() {
(FieldElement::from_repr(*x), FieldElement::from_repr(*y))
} else {
(FieldElement::from(0_u128), FieldElement::from(0_u128))
};
Ok(result)
}
fn pedersen_hash(
&self,
inputs: &[FieldElement],
domain_separator: u32,
) -> Result<FieldElement, BlackBoxResolutionError> {
let inputs: Vec<grumpkin::Fq> = inputs.iter().map(|input| input.into_repr()).collect();
let result = pedersen::hash::hash_with_index(&inputs, domain_separator);
let result = FieldElement::from_repr(result);
Ok(result)
}
fn multi_scalar_mul(
&self,
points: &[FieldElement],
scalars_lo: &[FieldElement],
scalars_hi: &[FieldElement],
) -> Result<(FieldElement, FieldElement, FieldElement), BlackBoxResolutionError> {
multi_scalar_mul(points, scalars_lo, scalars_hi)
}
fn ec_add(
&self,
input1_x: &FieldElement,
input1_y: &FieldElement,
input1_infinite: &FieldElement,
input2_x: &FieldElement,
input2_y: &FieldElement,
input2_infinite: &FieldElement,
) -> Result<(FieldElement, FieldElement, FieldElement), BlackBoxResolutionError> {
embedded_curve_add(
[*input1_x, *input1_y, *input1_infinite],
[*input2_x, *input2_y, *input2_infinite],
)
}
fn poseidon2_permutation(
&self,
inputs: &[FieldElement],
len: u32,
) -> Result<Vec<FieldElement>, BlackBoxResolutionError> {
poseidon2_permutation(inputs, len)
}
}