CKB Contract Flow

Noir 隐私计数(private tally v2)约束Noir Private Tally (v2) Constraints

来源:wamimi/noir-ckb-verifier @e4ba787 contracts/crates/private-tally/src/main.rs、build.rs、docs/private-tally-v2-spec.md · testnet 0x3cc4…37d1 (data1) · 部署见 evidence/private-tally-testnet.mdSource: wamimi/noir-ckb-verifier @e4ba787 contracts/crates/private-tally/src/main.rs, build.rs, docs/private-tally-v2-spec.md · testnet 0x3cc4…37d1 (data1) · deployment in evidence/private-tally-testnet.md
Type 脚本Type script零知识证明 / ZKZero-knowledge / ZK测试网Testnet开源Open source社区Community
贡献者:Contributed by @wamimi @wamimi

给 AI agent:For AI agents: noir_private_tally.md · llms.txt

0 → 1创建 tally(证明初始承诺,c_old = 0)Create a tally (proof of the initial commitment, c_old = 0)

Inputs 输入Inputs
任意资金 cellany funding cellsinputs[0] 决定 type_idinputs[0] determines the type_id
Outputs 输出Outputs
tally cell(唯一)tally cell (exactly one)Cell{ capacity: C, lock: GUARD(always-success,data1,args 空), type: PRIVATE_TALLY{ args: 0x01 ‖ type_id }, data: 0x01 ‖ c_new }Cell{ capacity: C, lock: GUARD (always-success, data1, empty args), type: PRIVATE_TALLY{ args: 0x01 ‖ type_id }, data: 0x01 ‖ c_new }
witness[group 输出下标]witness[at the group output index]WitnessArgs{ output_type: Groth16Witness v1(proof, public) } 总长 ≤ 512BWitnessArgs{ output_type: Groth16Witness v1(proof, public) } total ≤ 512B
type.args 字节布局(33B)type.args byte layout (33B)
ver1B
type_id32B
  • ver1B版本version= 0x01;且 hash_type = data1= 0x01; and hash_type = data141
  • type_id32Bckb-std type_id:创建时 = blake2b(inputs[0] ‖ group 输出序号)ckb-std type_id: on creation = blake2b(inputs[0] ‖ group output index)validate_type_id 通过validate_type_id passes44
ver0x01
type_id0xe56d70c7bc38fbb7ecff7ab2a7ac2c7338b259a9d860ac54c6c593d08cffaced
示例:测试网 tx 0xa93b9ed5…4a73 输出 0Example: output 0 of testnet tx 0xa93b9ed5…4a73
cell data 字节布局(33B)cell data byte layout (33B)
ver1B
commitment32B LE
  • ver1B状态版本state version= 0x01,总长恰好 33= 0x01, total exactly 3343
  • commitment32B LE隐藏计数的承诺(Fr 元素,小端)commitment hiding the tally (Fr element, LE)≠ 0 且 < BN254 标量域模数≠ 0 and < BN254 scalar modulus54
ver0x01
commitment0xaaa3578eb5b7e27392fd7d4e8765b5eafb2076d1033facbae99e49cdf4c9dd0a
公开输入向量(132B,按 verifier 编码)public-input vector (132B, verifier encoding)
count4B u32 LE
c_old32B
c_new32B
ctx_lo16B ‖ 16B 0
ctx_hi16B ‖ 16B 0
  • count4B u32 LE固定 4fixed 4= 4= 449
  • c_old32B旧承诺;创建时全 0old commitment; all zero on creation49
  • c_new32B新承诺 = 输出 data[1..33]new commitment = output data[1..33]49
  • ctx_lo16B ‖ 16B 0context digest[0..16],高 16 字节补 0context digest[0..16], upper 16 bytes zero49
  • ctx_hi16B ‖ 16B 0context digest[16..32],高 16 字节补 0context digest[16..32], upper 16 bytes zero49
witness 中 Groth16Witness.public_inputs 必须与此向量逐字节相等,否则 49Groth16Witness.public_inputs in the witness must equal this vector byte for byte, else 49
context digest = blake2b-256(ckb-default-hash, …)context digest = blake2b-256(ckb-default-hash, …)
prefix34B
network_domain32B
profile32B
script_hash32B
type_id32B
op1B
outpoint36B
c_old ‖ c_new64B
lock_hash32B
capacity8B LE
  • prefix34B"noir-ckb/private-tally/context/v2\0""noir-ckb/private-tally/context/v2\0"
  • network_domain32B编译进脚本:测试网创世哈希 0x10639e08…9606compiled in: testnet genesis hash 0x10639e08…9606
  • profile32Bblake2b("noir-ckb/private-tally/v2")blake2b("noir-ckb/private-tally/v2")
  • script_hash32B本 type script 哈希this type script hash
  • type_id32Bargs[1..33]args[1..33]
  • op1B0 = 创建,1 = 更新0 = creation, 1 = update
  • outpoint36B更新:group 输入 OutPoint;创建:全 0update: group input OutPoint; creation: zeros
  • c_old ‖ c_new64B旧 / 新承诺old / new commitment
  • lock_hash32B输出 lock hashoutput lock hash
  • capacity8B LE输出 capacityoutput capacity
cell_deps: [ PRIVATE_TALLY code, GUARD code, VK cell(data hash 0x91ad…2e5b,恰好 1 个) ]cell_deps: [ PRIVATE_TALLY code, GUARD code, VK cell (data hash 0x91ad…2e5b, exactly one) ]

限制Constraints

  • scriptargs 33B、args[0] = 1、hash_type = data1args 33B, args[0] = 1, hash_type = data141
  • inputs / outputs / cell_deps每类 ≤ 64 个(cell_deps 按展开后计)≤ 64 each (cell_deps counted after expansion)52
  • 同 code 的 type cellsame-code type cells输入 ≤ 1、输出 = 1、group 输出 = 1、group 输入 = 同 code 输入数(不同 args 也算,不能批量)inputs ≤ 1, outputs = 1, group outputs = 1, group inputs = same-code inputs (any args; no batching)42
  • type_idckb-std validate_type_id(args[1..33])ckb-std validate_type_id(args[1..33])44
  • 输出 lockoutput lockcode_hash = GUARD_DATA_HASH(0xe683…60d7)、data1、args 空code_hash = GUARD_DATA_HASH (0xe683…60d7), data1, empty args45
  • 输出 dataoutput data恰好 33B 且 data[0] = 1exactly 33B and data[0] = 143
  • c_new≠ 0 且 < BN254 模数≠ 0 and < BN254 modulus54
  • witness读取 group 输出下标处的 witness(≤ 512B),WitnessArgs.output_type 存在并可解码witness at the group output index (≤ 512B), WitnessArgs.output_type present and decodable48
  • public inputs= 重建的 [c_old, c_new, ctx_lo, ctx_hi]= rebuilt [c_old, c_new, ctx_lo, ctx_hi]49
  • cell_depsdata hash = VK_DATA_HASH(0x91ad…2e5b)的 cell 恰好 1 个,且可解码为 Groth16VerifyingKey v1(≤ 512B)exactly one cell with data hash = VK_DATA_HASH (0x91ad…2e5b), decodable as Groth16VerifyingKey v1 (≤ 512B)50
  • Groth16BN254 证明对重建的公开输入验证通过BN254 proof verifies against the rebuilt public inputs51
  • 任何读取any loadsyscall 失败syscall failure40

1 → 1更新:加入隐藏的数量(含 +0 掩护更新)Update: add a hidden amount (incl. +0 cover update)

Inputs 输入Inputs
旧 tally cell(group 输入)old tally cell (group input)Cell{ capacity: C, lock: GUARD, type: PRIVATE_TALLY(args), data: 0x01 ‖ c_old }Cell{ capacity: C, lock: GUARD, type: PRIVATE_TALLY(args), data: 0x01 ‖ c_old }
witness[group 输入下标]witness[at the group input index]WitnessArgs{ input_type: Groth16Witness v1(proof, public) } 总长 ≤ 512BWitnessArgs{ input_type: Groth16Witness v1(proof, public) } total ≤ 512B
Outputs 输出Outputs
新 tally cell(唯一)new tally cell (exactly one)Cell{ capacity: = C, lock: GUARD, type: PRIVATE_TALLY(args 不变), data: 0x01 ‖ c_new }Cell{ capacity: = C, lock: GUARD, type: PRIVATE_TALLY(args unchanged), data: 0x01 ‖ c_new }
cell data 字节布局(33B)cell data byte layout (33B)
ver1B
commitment32B LE
  • ver1B状态版本state version= 0x01,总长恰好 33= 0x01, total exactly 3343
  • commitment32B LE隐藏计数的承诺(Fr 元素,小端)commitment hiding the tally (Fr element, LE)≠ 0 且 < BN254 标量域模数≠ 0 and < BN254 scalar modulus54
ver0x01
commitment0xaaa3578eb5b7e27392fd7d4e8765b5eafb2076d1033facbae99e49cdf4c9dd0a
公开输入向量(132B,按 verifier 编码)public-input vector (132B, verifier encoding)
count4B u32 LE
c_old32B
c_new32B
ctx_lo16B ‖ 16B 0
ctx_hi16B ‖ 16B 0
  • count4B u32 LE固定 4fixed 4= 4= 449
  • c_old32B旧承诺;创建时全 0old commitment; all zero on creation49
  • c_new32B新承诺 = 输出 data[1..33]new commitment = output data[1..33]49
  • ctx_lo16B ‖ 16B 0context digest[0..16],高 16 字节补 0context digest[0..16], upper 16 bytes zero49
  • ctx_hi16B ‖ 16B 0context digest[16..32],高 16 字节补 0context digest[16..32], upper 16 bytes zero49
witness 中 Groth16Witness.public_inputs 必须与此向量逐字节相等,否则 49Groth16Witness.public_inputs in the witness must equal this vector byte for byte, else 49
cell_deps: [ PRIVATE_TALLY code, GUARD code, VK cell(恰好 1 个) ]cell_deps: [ PRIVATE_TALLY code, GUARD code, VK cell (exactly one) ]

限制Constraints

  • scriptargs 33B、args[0] = 1、hash_type = data1args 33B, args[0] = 1, hash_type = data141
  • inputs / outputs / cell_deps每类 ≤ 64 个(cell_deps 按展开后计)≤ 64 each (cell_deps counted after expansion)52
  • 同 code 的 type cellsame-code type cells输入 ≤ 1、输出 = 1、group 输出 = 1、group 输入 = 同 code 输入数(不同 args 也算,不能批量)inputs ≤ 1, outputs = 1, group outputs = 1, group inputs = same-code inputs (any args; no batching)42
  • type_idckb-std validate_type_id(args[1..33])ckb-std validate_type_id(args[1..33])44
  • 输出 lockoutput lockcode_hash = GUARD_DATA_HASH(0xe683…60d7)、data1、args 空code_hash = GUARD_DATA_HASH (0xe683…60d7), data1, empty args45
  • 输出 dataoutput data恰好 33B 且 data[0] = 1exactly 33B and data[0] = 143
  • c_new≠ 0 且 < BN254 模数≠ 0 and < BN254 modulus54
  • 输入 lockinput lock= GUARD= GUARD45
  • 输入 datainput data33B、data[0] = 1;c_old ≠ 0 且 < 模数33B, data[0] = 1; c_old ≠ 0 and < modulus43 / 54
  • capacity输入 capacity = 输出 capacityinput capacity = output capacity47
  • witness读取 group 输入下标处的 witness(≤ 512B),WitnessArgs.input_type 存在并可解码witness at the group input index (≤ 512B), WitnessArgs.input_type present and decodable48
  • public inputs= 重建的 [c_old, c_new, ctx_lo, ctx_hi]= rebuilt [c_old, c_new, ctx_lo, ctx_hi]49
  • cell_depsdata hash = VK_DATA_HASH(0x91ad…2e5b)的 cell 恰好 1 个,且可解码为 Groth16VerifyingKey v1(≤ 512B)exactly one cell with data hash = VK_DATA_HASH (0x91ad…2e5b), decodable as Groth16VerifyingKey v1 (≤ 512B)50
  • Groth16BN254 证明对重建的公开输入验证通过BN254 proof verifies against the rebuilt public inputs51
  • 任何读取any loadsyscall 失败syscall failure40
部署与来源Deployments & source 未核验unverified
源码source
wamimi/noir-ckb-verifier@e4ba7876 · contracts/crates/private-tally/src/main.rs · contracts/crates/private-tally/build.rs · docs/private-tally-v2-spec.md (commit read when writing this spec (default-branch HEAD 2026-11-10). Deployment + compiled-in policy values from evidence/private-tally-testnet/deployment.json and evidence/private-tally-testnet.md (deploy tx 0x39ef90b8…b1f9: type :0, VK :1, guard :2). wire-decode / verifier-core pinned at CECILIA-MULANDI/groth16-ckb@d64c769 (read; differs from e969ad1 only by formatting in wire-decode).)
二进制核验binary check
未核验unverified hash appears in the repo only as a deployment record or doc, which does not prove the recorded commit builds this binary (reproducible build not attempted)
0x3cc448db51…9e8337d1: listed in evidence/private-tally-testnet/deployment.json
网络networkcode_hashcell_deptype_id args数据哈希data hash部署交易deploy tx
testnet0x3cc448db51cdc14453fff7bdf2c3d39e1dd7a238091b982069d20e169e8337d1 data10x39ef90b8e6…4547b1f9:0 code—0x3cc448db51…9e8337d10x39ef90b8e6…4547b1f9 #22966804
链上数据核对于 on-chain data checked 2026-11-12
错误码来自 main.rs 常量:40 LOAD、41 ARGS、42 SHAPE、43 STATE、44 ID、45 GUARD、47 CAPACITY、48 WITNESS、49 PUBLIC、50 VK、51 PROOF、52 LIMIT、54 COMMITMENT(46、53 未使用)。检查顺序:41 → 52 → 42 → 44 → 45 → 43/54 →(更新)45/43/54/47 → 48 → 49 → 50 → 51。Error codes are the constants in main.rs: 40 LOAD, 41 ARGS, 42 SHAPE, 43 STATE, 44 ID, 45 GUARD, 47 CAPACITY, 48 WITNESS, 49 PUBLIC, 50 VK, 51 PROOF, 52 LIMIT, 54 COMMITMENT (46 and 53 unused). Check order: 41 → 52 → 42 → 44 → 45 → 43/54 → (update) 45/43/54/47 → 48 → 49 → 50 → 51.
NETWORK_DOMAIN、GUARD_DATA_HASH、VK_DATA_HASH 在构建时由 build.rs 从环境变量编译进二进制;本页使用 evidence/private-tally-testnet/deployment.json 记录的测试网值。没有签名:证明(绑定 context digest)是唯一授权,guard lock 是 always-success。NETWORK_DOMAIN, GUARD_DATA_HASH and VK_DATA_HASH are compiled in by build.rs from environment variables; this page uses the testnet values recorded in evidence/private-tally-testnet/deployment.json. There is no signature: the proof (bound to the context digest) is the only authority, and the guard lock is always-success.
数量本身与"新 = 旧 + 隐藏量"关系由 Noir 电路强制,不是脚本代码直接比较;链上只看到承诺变化。解码器重算 type_id、context digest 与公开输入;VK 查找和 Groth16 配对为 NOT_CHECKED。The amount and the "new = old + hidden amount" relation are enforced by the Noir circuit, not compared by the script code; on chain only the commitment changes. The decoder recomputes type_id, the context digest and the public inputs; the VK lookup and Groth16 pairing are NOT_CHECKED.