@caatinga/core 2.3.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{browser-Cq4ZofIq.d.cts → artifact.schema-BH5K-IiZ.d.cts} +2 -1
- package/dist/{browser-Cq4ZofIq.d.ts → artifact.schema-BH5K-IiZ.d.ts} +2 -1
- package/dist/browser.cjs +20 -0
- package/dist/browser.d.cts +5 -1
- package/dist/browser.d.ts +5 -1
- package/dist/browser.js +19 -0
- package/dist/index.cjs +440 -29
- package/dist/index.d.cts +52 -12
- package/dist/index.d.ts +52 -12
- package/dist/index.js +430 -29
- package/package.json +2 -1
- package/scaffolds/soroban-contract-stub/Cargo.toml +24 -0
- package/scaffolds/soroban-contract-stub/src/lib.rs +42 -0
- package/scaffolds/zk-circuit-stub/input.json +3 -0
- package/scaffolds/zk-circuit-stub/main.circom +9 -0
- package/scaffolds/zk-verifier/Cargo.lock +1880 -0
- package/scaffolds/zk-verifier/Cargo.toml +27 -0
- package/scaffolds/zk-verifier/src/lib.rs +64 -0
- package/scaffolds/zk-verifier/src/test.rs +100 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "verifier"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
rust-version = "1.89.0"
|
|
6
|
+
|
|
7
|
+
[lib]
|
|
8
|
+
crate-type = ["cdylib"]
|
|
9
|
+
|
|
10
|
+
[dependencies]
|
|
11
|
+
soroban-sdk = "25.1.0"
|
|
12
|
+
|
|
13
|
+
[dev-dependencies]
|
|
14
|
+
soroban-sdk = { version = "25.1.0", features = ["testutils"] }
|
|
15
|
+
ark-bls12-381 = "0.4.0"
|
|
16
|
+
ark-serialize = "0.4.2"
|
|
17
|
+
ark-ff = "0.4.2"
|
|
18
|
+
|
|
19
|
+
[profile.release]
|
|
20
|
+
opt-level = "z"
|
|
21
|
+
overflow-checks = true
|
|
22
|
+
debug = 0
|
|
23
|
+
strip = "symbols"
|
|
24
|
+
debug-assertions = false
|
|
25
|
+
panic = "abort"
|
|
26
|
+
codegen-units = 1
|
|
27
|
+
lto = true
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#![no_std]
|
|
2
|
+
use soroban_sdk::{
|
|
3
|
+
contract, contracterror, contractimpl, contracttype,
|
|
4
|
+
crypto::bls12_381::{Fr, G1Affine, G2Affine},
|
|
5
|
+
vec, Env, Vec,
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
#[contracterror]
|
|
9
|
+
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
|
10
|
+
#[repr(u32)]
|
|
11
|
+
pub enum Groth16Error {
|
|
12
|
+
MalformedVerifyingKey = 0,
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
#[derive(Clone)]
|
|
16
|
+
#[contracttype]
|
|
17
|
+
pub struct VerificationKey {
|
|
18
|
+
pub alpha: G1Affine,
|
|
19
|
+
pub beta: G2Affine,
|
|
20
|
+
pub gamma: G2Affine,
|
|
21
|
+
pub delta: G2Affine,
|
|
22
|
+
pub ic: Vec<G1Affine>,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
#[derive(Clone)]
|
|
26
|
+
#[contracttype]
|
|
27
|
+
pub struct Proof {
|
|
28
|
+
pub a: G1Affine,
|
|
29
|
+
pub b: G2Affine,
|
|
30
|
+
pub c: G1Affine,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
#[contract]
|
|
34
|
+
pub struct Groth16Verifier;
|
|
35
|
+
|
|
36
|
+
#[contractimpl]
|
|
37
|
+
impl Groth16Verifier {
|
|
38
|
+
pub fn verify_proof(
|
|
39
|
+
env: Env,
|
|
40
|
+
vk: VerificationKey,
|
|
41
|
+
proof: Proof,
|
|
42
|
+
pub_signals: Vec<Fr>,
|
|
43
|
+
) -> Result<bool, Groth16Error> {
|
|
44
|
+
let bls = env.crypto().bls12_381();
|
|
45
|
+
|
|
46
|
+
if pub_signals.len() + 1 != vk.ic.len() {
|
|
47
|
+
return Err(Groth16Error::MalformedVerifyingKey);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let mut vk_x = vk.ic.get(0).unwrap();
|
|
51
|
+
for (s, v) in pub_signals.iter().zip(vk.ic.iter().skip(1)) {
|
|
52
|
+
let prod = bls.g1_mul(&v, &s);
|
|
53
|
+
vk_x = bls.g1_add(&vk_x, &prod);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let neg_a = -proof.a;
|
|
57
|
+
let vp1 = vec![&env, neg_a, vk.alpha, vk_x, proof.c];
|
|
58
|
+
let vp2 = vec![&env, proof.b, vk.beta, vk.gamma, vk.delta];
|
|
59
|
+
|
|
60
|
+
Ok(bls.pairing_check(vp1, vp2))
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
mod test;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
#![cfg(test)]
|
|
2
|
+
extern crate std;
|
|
3
|
+
|
|
4
|
+
use ark_bls12_381::{Fq, Fq2};
|
|
5
|
+
use ark_serialize::CanonicalSerialize;
|
|
6
|
+
use core::str::FromStr;
|
|
7
|
+
use soroban_sdk::{
|
|
8
|
+
crypto::bls12_381::{Fr, G1Affine, G2Affine, G1_SERIALIZED_SIZE, G2_SERIALIZED_SIZE},
|
|
9
|
+
Env, Vec, U256,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
use crate::{Groth16Verifier, Groth16VerifierClient, Proof, VerificationKey};
|
|
13
|
+
|
|
14
|
+
fn g1_from_coords(env: &Env, x: &str, y: &str) -> G1Affine {
|
|
15
|
+
let ark_g1 = ark_bls12_381::G1Affine::new(Fq::from_str(x).unwrap(), Fq::from_str(y).unwrap());
|
|
16
|
+
let mut buf = [0u8; G1_SERIALIZED_SIZE];
|
|
17
|
+
ark_g1.serialize_uncompressed(&mut buf[..]).unwrap();
|
|
18
|
+
G1Affine::from_array(env, &buf)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
fn g2_from_coords(env: &Env, x1: &str, x2: &str, y1: &str, y2: &str) -> G2Affine {
|
|
22
|
+
let x = Fq2::new(Fq::from_str(x1).unwrap(), Fq::from_str(x2).unwrap());
|
|
23
|
+
let y = Fq2::new(Fq::from_str(y1).unwrap(), Fq::from_str(y2).unwrap());
|
|
24
|
+
let ark_g2 = ark_bls12_381::G2Affine::new(x, y);
|
|
25
|
+
let mut buf = [0u8; G2_SERIALIZED_SIZE];
|
|
26
|
+
ark_g2.serialize_uncompressed(&mut buf[..]).unwrap();
|
|
27
|
+
G2Affine::from_array(env, &buf)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
fn create_client(e: &Env) -> Groth16VerifierClient<'_> {
|
|
31
|
+
Groth16VerifierClient::new(e, &e.register(Groth16Verifier {}, ()))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
#[test]
|
|
35
|
+
fn test() {
|
|
36
|
+
let env = Env::default();
|
|
37
|
+
|
|
38
|
+
let alphax = "851850525556173310373115880154698084608631105506432893865500290442025919078535925294035153152030470398262539759609";
|
|
39
|
+
let alphay = "2637289349983507610125993281171282870664683328789064436670091381805667870657250691837988574635646688089951719927247";
|
|
40
|
+
|
|
41
|
+
let betax1 = "1312620381151154625549413690218290437739613987001512553647554932245743783919690104921577716179019375920325686841943";
|
|
42
|
+
let betax2 = "1853421227732662200477195678252233549930451033531229987959164216695698667330234953033341200627605777603511819497457";
|
|
43
|
+
let betay1 = "3215807833988244618006117550809420301978856703407297742347804415291049013404133666905173282837707341742014140541018";
|
|
44
|
+
let betay2 = "812366606879346135498483310623227330050424196838294715759414425317592599094348477520229174120664109186562798527696";
|
|
45
|
+
|
|
46
|
+
let gammax1 = "352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160";
|
|
47
|
+
let gammax2 = "3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758";
|
|
48
|
+
let gammay1 = "1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905";
|
|
49
|
+
let gammay2 = "927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582";
|
|
50
|
+
|
|
51
|
+
let deltax1 = "2981843938988033214458466658185878126396080429969635248100956025957789319926032198626745120548947333202362392267114";
|
|
52
|
+
let deltax2 = "2236695112259305382987038341098587500598216646308901956168137697892380899086228863246537938263638056666003066263342";
|
|
53
|
+
let deltay1 = "717163810166643254871951856655865822196000925757284470845197358532703820821048809982340614428800986999944933231635";
|
|
54
|
+
let deltay2 = "3496058064578305387608803828034117220735807855182872031001942587835768203820179263722136810383631418598310938506798";
|
|
55
|
+
|
|
56
|
+
let ic0x = "829685638389803071404995253486571779300247099942205634643821309129201420207693030476756893332812706176564514055395";
|
|
57
|
+
let ic0y = "3455508165409829148751617737772894557887792278044850553785496869183933597103951941805834639972489587640583544390358";
|
|
58
|
+
|
|
59
|
+
let ic1x = "2645559270376031734407122278942646687260452979296081924477586893972449945444985371392950465676350735694002713633589";
|
|
60
|
+
let ic1y = "2241039659097418315097403108596818813895651201896886552939297756980670248638746432560267634304593609165964274111037";
|
|
61
|
+
|
|
62
|
+
let vk = VerificationKey {
|
|
63
|
+
alpha: g1_from_coords(&env, alphax, alphay),
|
|
64
|
+
beta: g2_from_coords(&env, betax1, betax2, betay1, betay2),
|
|
65
|
+
gamma: g2_from_coords(&env, gammax1, gammax2, gammay1, gammay2),
|
|
66
|
+
delta: g2_from_coords(&env, deltax1, deltax2, deltay1, deltay2),
|
|
67
|
+
ic: Vec::from_array(
|
|
68
|
+
&env,
|
|
69
|
+
[
|
|
70
|
+
g1_from_coords(&env, ic0x, ic0y),
|
|
71
|
+
g1_from_coords(&env, ic1x, ic1y),
|
|
72
|
+
],
|
|
73
|
+
),
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
let pi_ax = "314442236668110257304682488877371582255161413673331360366570443799415414639292047869143313601702131653514009114222";
|
|
77
|
+
let pi_ay = "2384632327855835824635705027009217874826122107057894594162233214798350178691568018290025994699762298534539543934607";
|
|
78
|
+
let pi_bx1 = "428844167033934720609657613212495751617651348480870890908850335525890280786532876634895457032623422366474694342656";
|
|
79
|
+
let pi_bx2 = "3083139526360252775789959298805261067575555607578161553873977966165446991459924053189383038704105379290158793353905";
|
|
80
|
+
let pi_by1 = "1590919422794657666432683000821892403620510405626533455397042191265963587891653562867091397248216891852168698286910";
|
|
81
|
+
let pi_by2 = "3617931039814164588401589536353142503544155307022467123698224064329647390280346725086550997337076315487486714327146";
|
|
82
|
+
let pi_cx = "3052934797502613468327963344215392478880720823583493172692775426011388142569325036386650708808320216973179639719187";
|
|
83
|
+
let pi_cy = "2028185281516938724429867827057869371578022471499780916652824405212207527699373814371051328341613972789943854539597";
|
|
84
|
+
|
|
85
|
+
let proof = Proof {
|
|
86
|
+
a: g1_from_coords(&env, pi_ax, pi_ay),
|
|
87
|
+
b: g2_from_coords(&env, pi_bx1, pi_bx2, pi_by1, pi_by2),
|
|
88
|
+
c: g1_from_coords(&env, pi_cx, pi_cy),
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
let client = create_client(&env);
|
|
92
|
+
|
|
93
|
+
let output = Vec::from_array(&env, [Fr::from_u256(U256::from_u32(&env, 33))]);
|
|
94
|
+
let res = client.verify_proof(&vk, &proof, &output);
|
|
95
|
+
assert_eq!(res, true);
|
|
96
|
+
|
|
97
|
+
let bad_output = Vec::from_array(&env, [Fr::from_u256(U256::from_u32(&env, 22))]);
|
|
98
|
+
let res = client.verify_proof(&vk, &proof, &bad_output);
|
|
99
|
+
assert_eq!(res, false);
|
|
100
|
+
}
|