@sip-protocol/sdk 0.2.0 → 0.2.2

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/index.js CHANGED
@@ -32,7 +32,6 @@ var index_exports = {};
32
32
  __export(index_exports, {
33
33
  ATTESTATION_VERSION: () => ATTESTATION_VERSION,
34
34
  BaseWalletAdapter: () => BaseWalletAdapter,
35
- BrowserNoirProvider: () => BrowserNoirProvider,
36
35
  CHAIN_NUMERIC_IDS: () => CHAIN_NUMERIC_IDS,
37
36
  ComplianceManager: () => ComplianceManager,
38
37
  CryptoError: () => CryptoError,
@@ -59,7 +58,6 @@ __export(index_exports, {
59
58
  NATIVE_TOKENS: () => import_types33.NATIVE_TOKENS,
60
59
  NEARIntentsAdapter: () => NEARIntentsAdapter,
61
60
  NetworkError: () => NetworkError,
62
- NoirProofProvider: () => NoirProofProvider,
63
61
  ORACLE_DOMAIN: () => ORACLE_DOMAIN,
64
62
  OneClickClient: () => OneClickClient,
65
63
  OneClickDepositMode: () => import_types37.OneClickDepositMode,
@@ -3302,1763 +3300,6 @@ var MockProofProvider = class {
3302
3300
  }
3303
3301
  };
3304
3302
 
3305
- // src/proofs/noir.ts
3306
- var import_noir_js = require("@noir-lang/noir_js");
3307
- var import_bb = require("@aztec/bb.js");
3308
- var import_secp256k13 = require("@noble/curves/secp256k1");
3309
-
3310
- // src/proofs/circuits/funding_proof.json
3311
- var funding_proof_default = { noir_version: "1.0.0-beta.15+83245db91dcf63420ef4bcbbd85b98f397fee663", hash: "13977419962319221401", abi: { parameters: [{ name: "commitment_hash", type: { kind: "field" }, visibility: "public" }, { name: "minimum_required", type: { kind: "integer", sign: "unsigned", width: 64 }, visibility: "public" }, { name: "asset_id", type: { kind: "field" }, visibility: "public" }, { name: "balance", type: { kind: "integer", sign: "unsigned", width: 64 }, visibility: "private" }, { name: "blinding", type: { kind: "field" }, visibility: "private" }], return_type: null, error_types: { "2900908756532713827": { error_kind: "string", string: "Insufficient balance" }, "15764276373176857197": { error_kind: "string", string: "Stack too deep" }, "17719928407928969950": { error_kind: "string", string: "Commitment hash mismatch" } } }, bytecode: "H4sIAAAAAAAA/+VYbUhTURg+d7vTTdfmnJlK1g360LJolUEUJaUZ/qgooS9Taq5aoCudlhHV6LuglPplBH0gLUuiCCv6osA+LImItCQ0wUyRTJSJWkG7ds58PV53PzT60Qt3z+65z/ue533u8exeGfQnWIxZm+zZ4V4043PGe6gx8kcCNabGYyw+V6HBQcY4jDMzl+c0WC7E3l2ZVO5yrd0YM7t5WcG9HUVLGjxn2nEdDkkKRi3OZfxd5JC0XNiXBmMAYRAz+IEEqgJLTfS3mh+ibiGuy2hkaAhAykxlZPYKNYn1yvqZmJ5XrJaEReMLeOMDMWppknoYAsRMChyam0ZxGa10DWgkDWWRMkN1GINoklxDoQAxQ3VIuqFB0jX0mcbfgAAwxmHULiwwfYjX5ce2B+RZfo6u/FXgPtf2al7hIvuaOKsjZT3kRu1P7y3bb0mbdDWiU/+iZvai19f21Lw0htW5HlTE9JzZCLlSgnA1Ke7tua9OzFmVvuFRdeP8i5Gnjhgz5q2cfHpnfVLRw0YV5HLn3zyO+7Gmp4t1JNZEPevtzkm98TxhL9u6OWrz0conkyFXLOBCC0T9PvGowxhEaRUJJtj7ofceo9DILuRgpGwhGzAaaZLchQwFiC1kA5K+kI1I2Q0bJBDJ60ePlBkagtFEk+QaCgWIGRqCpBtqQv/GUBVSZmgoRjNNkmsoFCBmaCiSbqhZugbfFqIHYxzG/3mrhdyxiR0l3F7X0xMHJ5S40ppvWkIm3v9mjoi8X+u5VOZOXga56tK2uU2Lp0YzRdapz9YVt7SWXI8b437JlS64cfJ4RbcbcuVomN59L+HLccNy867Pq3N7m4qj81bY45uuHCjfctZp6aiqgtwZVcfertv6YPXdw0UzRoUf2ZR6vbz06bvu9CmV+77felJ4EHLFgjyg8evEgNGIMQSjCWMoRjOlXSTUMrhy6jJh3o/R3iOcuiD3LQpypcwpkevTwRvAov79o68QGrjpCL01WT92dk2MXBwDa5LNqa7Ycwe9b/HQ+eTnNdNmdWTtcOTaMrbZs53j8KiWYpNXMg5JCgauFvn5B5Lp1wGZ8yeTh6Hh6Cc5CvJ9D6yJIJ/Wwoce9f8fAFE5/IOdAXw3ghw+kkA9hrq2VGDeYfaURPJZZfmqUDR4flKLf1jle4zA52oBLlxLGsAR8hUJjDECdWhv4H3gMJotqGZ8fXzBtPC5jhX5h+pTy/aFXY79aoxoy1uQ3/PJQfei8qNd70eDXqAf6A/5m1Dm/+5kMifRpUGD/YL1WYofjVEH5oc6OeQ/ais81bdTZmWZqHw+SM98n1H4e6Y9x2Z12vNtGd6NybbVlpOxM8/htNuyncQJLcgiFeWsSJIfrCx/wGsporTAur4JMbICecwQ5yoK/XHpcTimF7hGapLfCqiX9PEbVAIFlc4UAAA=", debug_symbols: "pZbNbsMgDMffhXMO2HwE8irTVKUtmyJFaZUlk6aq7z6TQpYcYBW9QN3k/4sx2PjGzu44fx664ePyxZq3GzuOXd93n4f+cmqn7jLQvzfG/QCaNVgxqB+TYY2gybJGVgzpDXm/VyzKDtPonFdtOES/tqMbJtYMc99X7Lvt5+Wlr2s7LPPUjvSUV8wNZ5oJ+NH1zv+6V39qnpYajkFsUK9yqHd6SOu1lEGvlSjRG4h6Y4r0cfE1T34/t36I/hsJq149HT/gwgYAcKWKCGBWgq5ThDpD0BCDCHrjA9gdwWQI1kYfkGOSYNMECaACQYIyJQQUdSRgrcoI8CoBxROEbCSNjnthbQkBuYpHEgEhRfDhTiGEkjEQQnNe4gRgvTqhRdKJzKmUysZ1SC03tUUXImwKkUsukLG+AdhkemIGgWjW3BDbGrkPBWaKFBpct9So5JYivpxfWcRzCfYPAl5GPJVi2XBaHmsuWrAlCAE83hsCQJQh/pIMbP0qAtN5mjvfyNfLAyXfne93stpTN+5bE6SHFRPLKJdRLaP210/FqFkB2h/zsOzDAupW/CUF4HOAZoIglTkQwZbBVsHWwfatjw29jz/23+3YtcfeeU+8r/Nwio6ROf1c45PYVV3Hy8md59H5RWxaKxrfKKfQvt/9Qn8B", file_map: { "16": { source: "use crate::cmp::Eq;\nuse crate::hash::Hash;\nuse crate::ops::arith::{Add, Neg, Sub};\n\n/// A point on the embedded elliptic curve\n/// By definition, the base field of the embedded curve is the scalar field of the proof system curve, i.e the Noir Field.\n/// x and y denotes the Weierstrass coordinates of the point, if is_infinite is false.\npub struct EmbeddedCurvePoint {\n pub x: Field,\n pub y: Field,\n pub is_infinite: bool,\n}\n\nimpl EmbeddedCurvePoint {\n /// Elliptic curve point doubling operation\n /// returns the doubled point of a point P, i.e P+P\n pub fn double(self) -> EmbeddedCurvePoint {\n embedded_curve_add(self, self)\n }\n\n /// Returns the null element of the curve; 'the point at infinity'\n pub fn point_at_infinity() -> EmbeddedCurvePoint {\n EmbeddedCurvePoint { x: 0, y: 0, is_infinite: true }\n }\n\n /// Returns the curve's generator point.\n pub fn generator() -> EmbeddedCurvePoint {\n // Generator point for the grumpkin curve (y^2 = x^3 - 17)\n EmbeddedCurvePoint {\n x: 1,\n y: 17631683881184975370165255887551781615748388533673675138860, // sqrt(-16)\n is_infinite: false,\n }\n }\n}\n\nimpl Add for EmbeddedCurvePoint {\n /// Adds two points P+Q, using the curve addition formula, and also handles point at infinity\n fn add(self, other: EmbeddedCurvePoint) -> EmbeddedCurvePoint {\n embedded_curve_add(self, other)\n }\n}\n\nimpl Sub for EmbeddedCurvePoint {\n /// Points subtraction operation, using addition and negation\n fn sub(self, other: EmbeddedCurvePoint) -> EmbeddedCurvePoint {\n self + other.neg()\n }\n}\n\nimpl Neg for EmbeddedCurvePoint {\n /// Negates a point P, i.e returns -P, by negating the y coordinate.\n /// If the point is at infinity, then the result is also at infinity.\n fn neg(self) -> EmbeddedCurvePoint {\n EmbeddedCurvePoint { x: self.x, y: -self.y, is_infinite: self.is_infinite }\n }\n}\n\nimpl Eq for EmbeddedCurvePoint {\n /// Checks whether two points are equal\n fn eq(self: Self, b: EmbeddedCurvePoint) -> bool {\n (self.is_infinite & b.is_infinite)\n | ((self.is_infinite == b.is_infinite) & (self.x == b.x) & (self.y == b.y))\n }\n}\n\nimpl Hash for EmbeddedCurvePoint {\n fn hash<H>(self, state: &mut H)\n where\n H: crate::hash::Hasher,\n {\n if self.is_infinite {\n self.is_infinite.hash(state);\n } else {\n self.x.hash(state);\n self.y.hash(state);\n }\n }\n}\n\n/// Scalar for the embedded curve represented as low and high limbs\n/// By definition, the scalar field of the embedded curve is base field of the proving system curve.\n/// It may not fit into a Field element, so it is represented with two Field elements; its low and high limbs.\npub struct EmbeddedCurveScalar {\n pub lo: Field,\n pub hi: Field,\n}\n\nimpl EmbeddedCurveScalar {\n pub fn new(lo: Field, hi: Field) -> Self {\n EmbeddedCurveScalar { lo, hi }\n }\n\n #[field(bn254)]\n pub fn from_field(scalar: Field) -> EmbeddedCurveScalar {\n let (a, b) = crate::field::bn254::decompose(scalar);\n EmbeddedCurveScalar { lo: a, hi: b }\n }\n\n //Bytes to scalar: take the first (after the specified offset) 16 bytes of the input as the lo value, and the next 16 bytes as the hi value\n #[field(bn254)]\n pub(crate) fn from_bytes(bytes: [u8; 64], offset: u32) -> EmbeddedCurveScalar {\n let mut v = 1;\n let mut lo = 0 as Field;\n let mut hi = 0 as Field;\n for i in 0..16 {\n lo = lo + (bytes[offset + 31 - i] as Field) * v;\n hi = hi + (bytes[offset + 15 - i] as Field) * v;\n v = v * 256;\n }\n let sig_s = crate::embedded_curve_ops::EmbeddedCurveScalar { lo, hi };\n sig_s\n }\n}\n\nimpl Eq for EmbeddedCurveScalar {\n fn eq(self, other: Self) -> bool {\n (other.hi == self.hi) & (other.lo == self.lo)\n }\n}\n\nimpl Hash for EmbeddedCurveScalar {\n fn hash<H>(self, state: &mut H)\n where\n H: crate::hash::Hasher,\n {\n self.hi.hash(state);\n self.lo.hash(state);\n }\n}\n\n// Computes a multi scalar multiplication over the embedded curve.\n// For bn254, We have Grumpkin and Baby JubJub.\n// For bls12-381, we have JubJub and Bandersnatch.\n//\n// The embedded curve being used is decided by the\n// underlying proof system.\n// docs:start:multi_scalar_mul\npub fn multi_scalar_mul<let N: u32>(\n points: [EmbeddedCurvePoint; N],\n scalars: [EmbeddedCurveScalar; N],\n) -> EmbeddedCurvePoint\n// docs:end:multi_scalar_mul\n{\n multi_scalar_mul_array_return(points, scalars, true)[0]\n}\n\n#[foreign(multi_scalar_mul)]\npub(crate) fn multi_scalar_mul_array_return<let N: u32>(\n points: [EmbeddedCurvePoint; N],\n scalars: [EmbeddedCurveScalar; N],\n predicate: bool,\n) -> [EmbeddedCurvePoint; 1] {}\n\n// docs:start:fixed_base_scalar_mul\npub fn fixed_base_scalar_mul(scalar: EmbeddedCurveScalar) -> EmbeddedCurvePoint\n// docs:end:fixed_base_scalar_mul\n{\n multi_scalar_mul([EmbeddedCurvePoint::generator()], [scalar])\n}\n\n/// This function only assumes that the points are on the curve\n/// It handles corner cases around the infinity point causing some overhead compared to embedded_curve_add_not_nul and embedded_curve_add_unsafe\n// docs:start:embedded_curve_add\npub fn embedded_curve_add(\n point1: EmbeddedCurvePoint,\n point2: EmbeddedCurvePoint,\n) -> EmbeddedCurvePoint {\n // docs:end:embedded_curve_add\n if crate::runtime::is_unconstrained() {\n // `embedded_curve_add_unsafe` requires the inputs not to be the infinity point, so we check it here.\n // This is because `embedded_curve_add_unsafe` uses the `embedded_curve_add` opcode.\n // For efficiency, the backend does not check the inputs for the infinity point, but it assumes that they are not the infinity point\n // so that it can apply the ec addition formula directly.\n if point1.is_infinite {\n point2\n } else if point2.is_infinite {\n point1\n } else {\n embedded_curve_add_unsafe(point1, point2)\n }\n } else {\n // In a constrained context, we also need to check the inputs are not the infinity point because we also use `embedded_curve_add_unsafe`\n // However we also need to identify the case where the two inputs are the same, because then\n // the addition formula does not work and we need to use the doubling formula instead.\n // In unconstrained context, we can check directly if the input values are the same when solving the opcode, so it is not an issue.\n\n // x_coordinates_match is true if both abscissae are the same\n let x_coordinates_match = point1.x == point2.x;\n // y_coordinates_match is true if both ordinates are the same\n let y_coordinates_match = point1.y == point2.y;\n // double_predicate is true if both abscissae and ordinates are the same\n let double_predicate = (x_coordinates_match & y_coordinates_match);\n // If the abscissae are the same, but not the ordinates, then one point is the opposite of the other\n let infinity_predicate = (x_coordinates_match & !y_coordinates_match);\n\n // `embedded_curve_add_unsafe` would not perform doubling, even if the inputs point1 and point2 are the same, because it cannot know this without adding some logic (and some constraints)\n // However we did this logic when we computed `double_predicate`, so we set the result to 2*point1 if point1 and point2 are the same\n let mut result = if double_predicate {\n // `embedded_curve_add_unsafe` is doing a doubling if the input is the same variable, because in this case it is guaranteed (at 'compile time') that the input is the same.\n embedded_curve_add_unsafe(point1, point1)\n } else {\n let point1_1 = EmbeddedCurvePoint {\n x: point1.x + (x_coordinates_match as Field),\n y: point1.y,\n is_infinite: false,\n };\n let point2_1 = EmbeddedCurvePoint { x: point2.x, y: point2.y, is_infinite: false };\n // point1_1 is guaranteed to have a different abscissa than point2:\n // - if x_coordinates_match is 0, that means point1.x != point2.x, and point1_1.x = point1.x + 0\n // - if x_coordinates_match is 1, that means point1.x = point2.x, but point1_1.x = point1.x + 1 in this case\n // Because the abscissa is different, the addition formula is guaranteed to succeed, so we can safely use `embedded_curve_add_unsafe`\n // Note that this computation may be garbage: if x_coordinates_match is 1, or if one of the input is the point at infinity.\n // therefore we only want to do this if we need the result, otherwise it needs to be eliminated as a dead instruction, lest we want the circuit to fail.\n embedded_curve_add_unsafe(point1_1, point2_1)\n };\n\n // Same logic as above for unconstrained context, we set the proper result when one of the inputs is the infinity point\n if point1.is_infinite {\n result = point2;\n }\n if point2.is_infinite {\n result = point1;\n }\n\n // Finally, we set the is_infinity flag of the result:\n // Opposite points should sum into the infinity point, however, if one of them is point at infinity, their coordinates are not meaningful\n // so we should not use the fact that the inputs are opposite in this case:\n let mut result_is_infinity =\n infinity_predicate & (!point1.is_infinite & !point2.is_infinite);\n // However, if both of them are at infinity, then the result is also at infinity\n result.is_infinite = result_is_infinity | (point1.is_infinite & point2.is_infinite);\n result\n }\n}\n\n#[foreign(embedded_curve_add)]\nfn embedded_curve_add_array_return(\n _point1: EmbeddedCurvePoint,\n _point2: EmbeddedCurvePoint,\n _predicate: bool,\n) -> [EmbeddedCurvePoint; 1] {}\n\n/// This function assumes that:\n/// The points are on the curve, and\n/// The points don't share an x-coordinate, and\n/// Neither point is the infinity point.\n/// If it is used with correct input, the function ensures the correct non-zero result is returned.\n/// Except for points on the curve, the other assumptions are checked by the function. It will cause assertion failure if they are not respected.\npub fn embedded_curve_add_not_nul(\n point1: EmbeddedCurvePoint,\n point2: EmbeddedCurvePoint,\n) -> EmbeddedCurvePoint {\n assert(point1.x != point2.x);\n assert(!point1.is_infinite);\n assert(!point2.is_infinite);\n // Ensure is_infinite is comptime\n let point1_1 = EmbeddedCurvePoint { x: point1.x, y: point1.y, is_infinite: false };\n let point2_1 = EmbeddedCurvePoint { x: point2.x, y: point2.y, is_infinite: false };\n embedded_curve_add_unsafe(point1_1, point2_1)\n}\n\n/// Unsafe ec addition\n/// If the inputs are the same, it will perform a doubling, but only if point1 and point2 are the same variable.\n/// If they have the same value but are different variables, the result will be incorrect because in this case\n/// it assumes (but does not check) that the points' x-coordinates are not equal.\n/// It also assumes neither point is the infinity point.\npub fn embedded_curve_add_unsafe(\n point1: EmbeddedCurvePoint,\n point2: EmbeddedCurvePoint,\n) -> EmbeddedCurvePoint {\n embedded_curve_add_array_return(point1, point2, true)[0]\n}\n", path: "std/embedded_curve_ops.nr" }, "17": { source: `use crate::field::field_less_than;
3312
- use crate::runtime::is_unconstrained;
3313
-
3314
- // The low and high decomposition of the field modulus
3315
- global PLO: Field = 53438638232309528389504892708671455233;
3316
- global PHI: Field = 64323764613183177041862057485226039389;
3317
-
3318
- pub(crate) global TWO_POW_128: Field = 0x100000000000000000000000000000000;
3319
-
3320
- // Decomposes a single field into two 16 byte fields.
3321
- fn compute_decomposition(x: Field) -> (Field, Field) {
3322
- // Here's we're taking advantage of truncating 128 bit limbs from the input field
3323
- // and then subtracting them from the input such the field division is equivalent to integer division.
3324
- let low = (x as u128) as Field;
3325
- let high = (x - low) / TWO_POW_128;
3326
-
3327
- (low, high)
3328
- }
3329
-
3330
- pub(crate) unconstrained fn decompose_hint(x: Field) -> (Field, Field) {
3331
- compute_decomposition(x)
3332
- }
3333
-
3334
- unconstrained fn lte_hint(x: Field, y: Field) -> bool {
3335
- if x == y {
3336
- true
3337
- } else {
3338
- field_less_than(x, y)
3339
- }
3340
- }
3341
-
3342
- // Assert that (alo > blo && ahi >= bhi) || (alo <= blo && ahi > bhi)
3343
- fn assert_gt_limbs(a: (Field, Field), b: (Field, Field)) {
3344
- let (alo, ahi) = a;
3345
- let (blo, bhi) = b;
3346
- // Safety: borrow is enforced to be boolean due to its type.
3347
- // if borrow is 0, it asserts that (alo > blo && ahi >= bhi)
3348
- // if borrow is 1, it asserts that (alo <= blo && ahi > bhi)
3349
- unsafe {
3350
- let borrow = lte_hint(alo, blo);
3351
-
3352
- let rlo = alo - blo - 1 + (borrow as Field) * TWO_POW_128;
3353
- let rhi = ahi - bhi - (borrow as Field);
3354
-
3355
- rlo.assert_max_bit_size::<128>();
3356
- rhi.assert_max_bit_size::<128>();
3357
- }
3358
- }
3359
-
3360
- /// Decompose a single field into two 16 byte fields.
3361
- pub fn decompose(x: Field) -> (Field, Field) {
3362
- if is_unconstrained() {
3363
- compute_decomposition(x)
3364
- } else {
3365
- // Safety: decomposition is properly checked below
3366
- unsafe {
3367
- // Take hints of the decomposition
3368
- let (xlo, xhi) = decompose_hint(x);
3369
-
3370
- // Range check the limbs
3371
- xlo.assert_max_bit_size::<128>();
3372
- xhi.assert_max_bit_size::<128>();
3373
-
3374
- // Check that the decomposition is correct
3375
- assert_eq(x, xlo + TWO_POW_128 * xhi);
3376
-
3377
- // Assert that the decomposition of P is greater than the decomposition of x
3378
- assert_gt_limbs((PLO, PHI), (xlo, xhi));
3379
- (xlo, xhi)
3380
- }
3381
- }
3382
- }
3383
-
3384
- pub fn assert_gt(a: Field, b: Field) {
3385
- if is_unconstrained() {
3386
- assert(
3387
- // Safety: already unconstrained
3388
- unsafe { field_less_than(b, a) },
3389
- );
3390
- } else {
3391
- // Decompose a and b
3392
- let a_limbs = decompose(a);
3393
- let b_limbs = decompose(b);
3394
-
3395
- // Assert that a_limbs is greater than b_limbs
3396
- assert_gt_limbs(a_limbs, b_limbs)
3397
- }
3398
- }
3399
-
3400
- pub fn assert_lt(a: Field, b: Field) {
3401
- assert_gt(b, a);
3402
- }
3403
-
3404
- pub fn gt(a: Field, b: Field) -> bool {
3405
- if is_unconstrained() {
3406
- // Safety: unsafe in unconstrained
3407
- unsafe {
3408
- field_less_than(b, a)
3409
- }
3410
- } else if a == b {
3411
- false
3412
- } else {
3413
- // Safety: Take a hint of the comparison and verify it
3414
- unsafe {
3415
- if field_less_than(a, b) {
3416
- assert_gt(b, a);
3417
- false
3418
- } else {
3419
- assert_gt(a, b);
3420
- true
3421
- }
3422
- }
3423
- }
3424
- }
3425
-
3426
- pub fn lt(a: Field, b: Field) -> bool {
3427
- gt(b, a)
3428
- }
3429
-
3430
- mod tests {
3431
- // TODO: Allow imports from "super"
3432
- use crate::field::bn254::{assert_gt, decompose, gt, lt, lte_hint, PHI, PLO, TWO_POW_128};
3433
-
3434
- #[test]
3435
- fn check_decompose() {
3436
- assert_eq(decompose(TWO_POW_128), (0, 1));
3437
- assert_eq(decompose(TWO_POW_128 + 0x1234567890), (0x1234567890, 1));
3438
- assert_eq(decompose(0x1234567890), (0x1234567890, 0));
3439
- }
3440
-
3441
- #[test]
3442
- unconstrained fn check_lte_hint() {
3443
- assert(lte_hint(0, 1));
3444
- assert(lte_hint(0, 0x100));
3445
- assert(lte_hint(0x100, TWO_POW_128 - 1));
3446
- assert(!lte_hint(0 - 1, 0));
3447
-
3448
- assert(lte_hint(0, 0));
3449
- assert(lte_hint(0x100, 0x100));
3450
- assert(lte_hint(0 - 1, 0 - 1));
3451
- }
3452
-
3453
- #[test]
3454
- fn check_gt() {
3455
- assert(gt(1, 0));
3456
- assert(gt(0x100, 0));
3457
- assert(gt((0 - 1), (0 - 2)));
3458
- assert(gt(TWO_POW_128, 0));
3459
- assert(!gt(0, 0));
3460
- assert(!gt(0, 0x100));
3461
- assert(gt(0 - 1, 0 - 2));
3462
- assert(!gt(0 - 2, 0 - 1));
3463
- assert_gt(0 - 1, 0);
3464
- }
3465
-
3466
- #[test]
3467
- fn check_plo_phi() {
3468
- assert_eq(PLO + PHI * TWO_POW_128, 0);
3469
- let p_bytes = crate::field::modulus_le_bytes();
3470
- let mut p_low: Field = 0;
3471
- let mut p_high: Field = 0;
3472
-
3473
- let mut offset = 1;
3474
- for i in 0..16 {
3475
- p_low += (p_bytes[i] as Field) * offset;
3476
- p_high += (p_bytes[i + 16] as Field) * offset;
3477
- offset *= 256;
3478
- }
3479
- assert_eq(p_low, PLO);
3480
- assert_eq(p_high, PHI);
3481
- }
3482
-
3483
- #[test]
3484
- fn check_decompose_edge_cases() {
3485
- assert_eq(decompose(0), (0, 0));
3486
- assert_eq(decompose(TWO_POW_128 - 1), (TWO_POW_128 - 1, 0));
3487
- assert_eq(decompose(TWO_POW_128 + 1), (1, 1));
3488
- assert_eq(decompose(TWO_POW_128 * 2), (0, 2));
3489
- assert_eq(decompose(TWO_POW_128 * 2 + 0x1234567890), (0x1234567890, 2));
3490
- }
3491
-
3492
- #[test]
3493
- fn check_decompose_large_values() {
3494
- let large_field = 0xffffffffffffffff;
3495
- let (lo, hi) = decompose(large_field);
3496
- assert_eq(large_field, lo + TWO_POW_128 * hi);
3497
-
3498
- let large_value = large_field - TWO_POW_128;
3499
- let (lo2, hi2) = decompose(large_value);
3500
- assert_eq(large_value, lo2 + TWO_POW_128 * hi2);
3501
- }
3502
-
3503
- #[test]
3504
- fn check_lt_comprehensive() {
3505
- assert(lt(0, 1));
3506
- assert(!lt(1, 0));
3507
- assert(!lt(0, 0));
3508
- assert(!lt(42, 42));
3509
-
3510
- assert(lt(TWO_POW_128 - 1, TWO_POW_128));
3511
- assert(!lt(TWO_POW_128, TWO_POW_128 - 1));
3512
- }
3513
- }
3514
- `, path: "std/field/bn254.nr" }, "19": { source: '// Exposed only for usage in `std::meta`\npub(crate) mod poseidon2;\n\nuse crate::default::Default;\nuse crate::embedded_curve_ops::{\n EmbeddedCurvePoint, EmbeddedCurveScalar, multi_scalar_mul, multi_scalar_mul_array_return,\n};\nuse crate::meta::derive_via;\n\n#[foreign(sha256_compression)]\n// docs:start:sha256_compression\npub fn sha256_compression(input: [u32; 16], state: [u32; 8]) -> [u32; 8] {}\n// docs:end:sha256_compression\n\n#[foreign(keccakf1600)]\n// docs:start:keccakf1600\npub fn keccakf1600(input: [u64; 25]) -> [u64; 25] {}\n// docs:end:keccakf1600\n\npub mod keccak {\n #[deprecated("This function has been moved to std::hash::keccakf1600")]\n pub fn keccakf1600(input: [u64; 25]) -> [u64; 25] {\n super::keccakf1600(input)\n }\n}\n\n#[foreign(blake2s)]\n// docs:start:blake2s\npub fn blake2s<let N: u32>(input: [u8; N]) -> [u8; 32]\n// docs:end:blake2s\n{}\n\n// docs:start:blake3\npub fn blake3<let N: u32>(input: [u8; N]) -> [u8; 32]\n// docs:end:blake3\n{\n if crate::runtime::is_unconstrained() {\n // Temporary measure while Barretenberg is main proving system.\n // Please open an issue if you\'re working on another proving system and running into problems due to this.\n crate::static_assert(\n N <= 1024,\n "Barretenberg cannot prove blake3 hashes with inputs larger than 1024 bytes",\n );\n }\n __blake3(input)\n}\n\n#[foreign(blake3)]\nfn __blake3<let N: u32>(input: [u8; N]) -> [u8; 32] {}\n\n// docs:start:pedersen_commitment\npub fn pedersen_commitment<let N: u32>(input: [Field; N]) -> EmbeddedCurvePoint {\n // docs:end:pedersen_commitment\n pedersen_commitment_with_separator(input, 0)\n}\n\n#[inline_always]\npub fn pedersen_commitment_with_separator<let N: u32>(\n input: [Field; N],\n separator: u32,\n) -> EmbeddedCurvePoint {\n let mut points = [EmbeddedCurveScalar { lo: 0, hi: 0 }; N];\n for i in 0..N {\n // we use the unsafe version because the multi_scalar_mul will constrain the scalars.\n points[i] = from_field_unsafe(input[i]);\n }\n let generators = derive_generators("DEFAULT_DOMAIN_SEPARATOR".as_bytes(), separator);\n multi_scalar_mul(generators, points)\n}\n\n// docs:start:pedersen_hash\npub fn pedersen_hash<let N: u32>(input: [Field; N]) -> Field\n// docs:end:pedersen_hash\n{\n pedersen_hash_with_separator(input, 0)\n}\n\n#[no_predicates]\npub fn pedersen_hash_with_separator<let N: u32>(input: [Field; N], separator: u32) -> Field {\n let mut scalars: [EmbeddedCurveScalar; N + 1] = [EmbeddedCurveScalar { lo: 0, hi: 0 }; N + 1];\n let mut generators: [EmbeddedCurvePoint; N + 1] =\n [EmbeddedCurvePoint::point_at_infinity(); N + 1];\n let domain_generators: [EmbeddedCurvePoint; N] =\n derive_generators("DEFAULT_DOMAIN_SEPARATOR".as_bytes(), separator);\n\n for i in 0..N {\n scalars[i] = from_field_unsafe(input[i]);\n generators[i] = domain_generators[i];\n }\n scalars[N] = EmbeddedCurveScalar { lo: N as Field, hi: 0 as Field };\n\n let length_generator: [EmbeddedCurvePoint; 1] =\n derive_generators("pedersen_hash_length".as_bytes(), 0);\n generators[N] = length_generator[0];\n multi_scalar_mul_array_return(generators, scalars, true)[0].x\n}\n\n#[field(bn254)]\n#[inline_always]\npub fn derive_generators<let N: u32, let M: u32>(\n domain_separator_bytes: [u8; M],\n starting_index: u32,\n) -> [EmbeddedCurvePoint; N] {\n crate::assert_constant(domain_separator_bytes);\n // TODO(https://github.com/noir-lang/noir/issues/5672): Add back assert_constant on starting_index\n __derive_generators(domain_separator_bytes, starting_index)\n}\n\n#[builtin(derive_pedersen_generators)]\n#[field(bn254)]\nfn __derive_generators<let N: u32, let M: u32>(\n domain_separator_bytes: [u8; M],\n starting_index: u32,\n) -> [EmbeddedCurvePoint; N] {}\n\n#[field(bn254)]\n// Same as from_field but:\n// does not assert the limbs are 128 bits\n// does not assert the decomposition does not overflow the EmbeddedCurveScalar\nfn from_field_unsafe(scalar: Field) -> EmbeddedCurveScalar {\n // Safety: xlo and xhi decomposition is checked below\n let (xlo, xhi) = unsafe { crate::field::bn254::decompose_hint(scalar) };\n // Check that the decomposition is correct\n assert_eq(scalar, xlo + crate::field::bn254::TWO_POW_128 * xhi);\n EmbeddedCurveScalar { lo: xlo, hi: xhi }\n}\n\npub fn poseidon2_permutation<let N: u32>(input: [Field; N], state_len: u32) -> [Field; N] {\n assert_eq(input.len(), state_len);\n poseidon2_permutation_internal(input)\n}\n\n#[foreign(poseidon2_permutation)]\nfn poseidon2_permutation_internal<let N: u32>(input: [Field; N]) -> [Field; N] {}\n\n// Generic hashing support.\n// Partially ported and impacted by rust.\n\n// Hash trait shall be implemented per type.\n#[derive_via(derive_hash)]\npub trait Hash {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher;\n}\n\n// docs:start:derive_hash\ncomptime fn derive_hash(s: TypeDefinition) -> Quoted {\n let name = quote { $crate::hash::Hash };\n let signature = quote { fn hash<H>(_self: Self, _state: &mut H) where H: $crate::hash::Hasher };\n let for_each_field = |name| quote { _self.$name.hash(_state); };\n crate::meta::make_trait_impl(\n s,\n name,\n signature,\n for_each_field,\n quote {},\n |fields| fields,\n )\n}\n// docs:end:derive_hash\n\n// Hasher trait shall be implemented by algorithms to provide hash-agnostic means.\n// TODO: consider making the types generic here ([u8], [Field], etc.)\npub trait Hasher {\n fn finish(self) -> Field;\n\n fn write(&mut self, input: Field);\n}\n\n// BuildHasher is a factory trait, responsible for production of specific Hasher.\npub trait BuildHasher {\n type H: Hasher;\n\n fn build_hasher(self) -> H;\n}\n\npub struct BuildHasherDefault<H>;\n\nimpl<H> BuildHasher for BuildHasherDefault<H>\nwhere\n H: Hasher + Default,\n{\n type H = H;\n\n fn build_hasher(_self: Self) -> H {\n H::default()\n }\n}\n\nimpl<H> Default for BuildHasherDefault<H>\nwhere\n H: Hasher + Default,\n{\n fn default() -> Self {\n BuildHasherDefault {}\n }\n}\n\nimpl Hash for Field {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self);\n }\n}\n\nimpl Hash for u1 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u8 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u16 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u32 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u64 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u128 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i8 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as u8 as Field);\n }\n}\n\nimpl Hash for i16 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as u16 as Field);\n }\n}\n\nimpl Hash for i32 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as u32 as Field);\n }\n}\n\nimpl Hash for i64 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as u64 as Field);\n }\n}\n\nimpl Hash for bool {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for () {\n fn hash<H>(_self: Self, _state: &mut H)\n where\n H: Hasher,\n {}\n}\n\nimpl<T, let N: u32> Hash for [T; N]\nwhere\n T: Hash,\n{\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl<T> Hash for [T]\nwhere\n T: Hash,\n{\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n self.len().hash(state);\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl<A, B> Hash for (A, B)\nwhere\n A: Hash,\n B: Hash,\n{\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n self.0.hash(state);\n self.1.hash(state);\n }\n}\n\nimpl<A, B, C> Hash for (A, B, C)\nwhere\n A: Hash,\n B: Hash,\n C: Hash,\n{\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n }\n}\n\nimpl<A, B, C, D> Hash for (A, B, C, D)\nwhere\n A: Hash,\n B: Hash,\n C: Hash,\n D: Hash,\n{\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n }\n}\n\nimpl<A, B, C, D, E> Hash for (A, B, C, D, E)\nwhere\n A: Hash,\n B: Hash,\n C: Hash,\n D: Hash,\n E: Hash,\n{\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n self.4.hash(state);\n }\n}\n\n// Some test vectors for Pedersen hash and Pedersen Commitment.\n// They have been generated using the same functions so the tests are for now useless\n// but they will be useful when we switch to Noir implementation.\n#[test]\nfn assert_pedersen() {\n assert_eq(\n pedersen_hash_with_separator([1], 1),\n 0x1b3f4b1a83092a13d8d1a59f7acb62aba15e7002f4440f2275edb99ebbc2305f,\n );\n assert_eq(\n pedersen_commitment_with_separator([1], 1),\n EmbeddedCurvePoint {\n x: 0x054aa86a73cb8a34525e5bbed6e43ba1198e860f5f3950268f71df4591bde402,\n y: 0x209dcfbf2cfb57f9f6046f44d71ac6faf87254afc7407c04eb621a6287cac126,\n is_infinite: false,\n },\n );\n\n assert_eq(\n pedersen_hash_with_separator([1, 2], 2),\n 0x26691c129448e9ace0c66d11f0a16d9014a9e8498ee78f4d69f0083168188255,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2], 2),\n EmbeddedCurvePoint {\n x: 0x2e2b3b191e49541fe468ec6877721d445dcaffe41728df0a0eafeb15e87b0753,\n y: 0x2ff4482400ad3a6228be17a2af33e2bcdf41be04795f9782bd96efe7e24f8778,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3], 3),\n 0x0bc694b7a1f8d10d2d8987d07433f26bd616a2d351bc79a3c540d85b6206dbe4,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3], 3),\n EmbeddedCurvePoint {\n x: 0x1fee4e8cf8d2f527caa2684236b07c4b1bad7342c01b0f75e9a877a71827dc85,\n y: 0x2f9fedb9a090697ab69bf04c8bc15f7385b3e4b68c849c1536e5ae15ff138fd1,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4], 4),\n 0xdae10fb32a8408521803905981a2b300d6a35e40e798743e9322b223a5eddc,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4], 4),\n EmbeddedCurvePoint {\n x: 0x07ae3e202811e1fca39c2d81eabe6f79183978e6f12be0d3b8eda095b79bdbc9,\n y: 0x0afc6f892593db6fbba60f2da558517e279e0ae04f95758587760ba193145014,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4, 5], 5),\n 0xfc375b062c4f4f0150f7100dfb8d9b72a6d28582dd9512390b0497cdad9c22,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4, 5], 5),\n EmbeddedCurvePoint {\n x: 0x1754b12bd475a6984a1094b5109eeca9838f4f81ac89c5f0a41dbce53189bb29,\n y: 0x2da030e3cfcdc7ddad80eaf2599df6692cae0717d4e9f7bfbee8d073d5d278f7,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4, 5, 6], 6),\n 0x1696ed13dc2730062a98ac9d8f9de0661bb98829c7582f699d0273b18c86a572,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4, 5, 6], 6),\n EmbeddedCurvePoint {\n x: 0x190f6c0e97ad83e1e28da22a98aae156da083c5a4100e929b77e750d3106a697,\n y: 0x1f4b60f34ef91221a0b49756fa0705da93311a61af73d37a0c458877706616fb,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4, 5, 6, 7], 7),\n 0x128c0ff144fc66b6cb60eeac8a38e23da52992fc427b92397a7dffd71c45ede3,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4, 5, 6, 7], 7),\n EmbeddedCurvePoint {\n x: 0x015441e9d29491b06563fac16fc76abf7a9534c715421d0de85d20dbe2965939,\n y: 0x1d2575b0276f4e9087e6e07c2cb75aa1baafad127af4be5918ef8a2ef2fea8fc,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4, 5, 6, 7, 8], 8),\n 0x2f960e117482044dfc99d12fece2ef6862fba9242be4846c7c9a3e854325a55c,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4, 5, 6, 7, 8], 8),\n EmbeddedCurvePoint {\n x: 0x1657737676968887fceb6dd516382ea13b3a2c557f509811cd86d5d1199bc443,\n y: 0x1f39f0cb569040105fa1e2f156521e8b8e08261e635a2b210bdc94e8d6d65f77,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4, 5, 6, 7, 8, 9], 9),\n 0x0c96db0790602dcb166cc4699e2d306c479a76926b81c2cb2aaa92d249ec7be7,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4, 5, 6, 7, 8, 9], 9),\n EmbeddedCurvePoint {\n x: 0x0a3ceae42d14914a432aa60ec7fded4af7dad7dd4acdbf2908452675ec67e06d,\n y: 0xfc19761eaaf621ad4aec9a8b2e84a4eceffdba78f60f8b9391b0bd9345a2f2,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 10),\n 0x2cd37505871bc460a62ea1e63c7fe51149df5d0801302cf1cbc48beb8dff7e94,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 10),\n EmbeddedCurvePoint {\n x: 0x2fb3f8b3d41ddde007c8c3c62550f9a9380ee546fcc639ffbb3fd30c8d8de30c,\n y: 0x300783be23c446b11a4c0fabf6c91af148937cea15fcf5fb054abf7f752ee245,\n is_infinite: false,\n },\n );\n}\n', path: "std/hash/mod.nr" }, "50": { source: `/// Funding Proof Circuit
3515
- ///
3516
- /// Proves: "I have sufficient funds to fulfill this intent, without revealing
3517
- /// my exact balance, wallet address, or source of funds."
3518
- ///
3519
- /// @see docs/specs/FUNDING-PROOF.md
3520
-
3521
- use std::hash::pedersen_hash;
3522
- use std::hash::pedersen_commitment;
3523
-
3524
- // --- Main Circuit ---
3525
-
3526
- /// Main funding proof entry point
3527
- ///
3528
- /// Public inputs: commitment_hash, minimum_required, asset_id
3529
- /// Private inputs: balance, blinding
3530
- ///
3531
- /// Constraints:
3532
- /// 1. balance >= minimum_required (range proof via u64)
3533
- /// 2. commitment = Pedersen(balance, blinding)
3534
- /// 3. hash(commitment, asset_id) == commitment_hash
3535
- pub fn main(
3536
- commitment_hash: pub Field,
3537
- minimum_required: pub u64,
3538
- asset_id: pub Field,
3539
- balance: u64,
3540
- blinding: Field,
3541
- ) {
3542
- // Constraint 1: Sufficient Funds
3543
- assert(balance >= minimum_required, "Insufficient balance");
3544
-
3545
- // Constraint 2: Compute Pedersen Commitment
3546
- // Uses Noir's built-in pedersen_commitment which returns (x, y) point
3547
- let commitment = pedersen_commitment([balance as Field, blinding]);
3548
-
3549
- // Constraint 3: Verify Commitment Hash
3550
- let computed_hash = pedersen_hash([commitment.x, commitment.y, asset_id]);
3551
- assert(computed_hash == commitment_hash, "Commitment hash mismatch");
3552
- }
3553
-
3554
- // --- Tests ---
3555
-
3556
- #[test]
3557
- fn test_valid_funding_proof() {
3558
- let balance: u64 = 100;
3559
- let minimum_required: u64 = 50;
3560
- let blinding: Field = 12345;
3561
- let asset_id: Field = 0xABCD;
3562
-
3563
- // Compute commitment using same method as circuit
3564
- let commitment = pedersen_commitment([balance as Field, blinding]);
3565
- let commitment_hash = pedersen_hash([commitment.x, commitment.y, asset_id]);
3566
-
3567
- // This should pass
3568
- main(commitment_hash, minimum_required, asset_id, balance, blinding);
3569
- }
3570
-
3571
- #[test(should_fail_with = "Insufficient balance")]
3572
- fn test_insufficient_balance() {
3573
- let balance: u64 = 50;
3574
- let minimum_required: u64 = 100;
3575
- let blinding: Field = 12345;
3576
- let asset_id: Field = 0xABCD;
3577
-
3578
- let commitment = pedersen_commitment([balance as Field, blinding]);
3579
- let commitment_hash = pedersen_hash([commitment.x, commitment.y, asset_id]);
3580
-
3581
- // This should fail - balance < minimum
3582
- main(commitment_hash, minimum_required, asset_id, balance, blinding);
3583
- }
3584
-
3585
- #[test(should_fail_with = "Commitment hash mismatch")]
3586
- fn test_wrong_commitment_hash() {
3587
- let balance: u64 = 100;
3588
- let minimum_required: u64 = 50;
3589
- let blinding: Field = 12345;
3590
- let asset_id: Field = 0xABCD;
3591
- let wrong_hash: Field = 0xDEADBEEF;
3592
-
3593
- // This should fail - wrong hash
3594
- main(wrong_hash, minimum_required, asset_id, balance, blinding);
3595
- }
3596
-
3597
- #[test(should_fail_with = "Commitment hash mismatch")]
3598
- fn test_wrong_blinding() {
3599
- let balance: u64 = 100;
3600
- let minimum_required: u64 = 50;
3601
- let correct_blinding: Field = 12345;
3602
- let wrong_blinding: Field = 54321;
3603
- let asset_id: Field = 0xABCD;
3604
-
3605
- // Compute hash with correct blinding
3606
- let commitment = pedersen_commitment([balance as Field, correct_blinding]);
3607
- let commitment_hash = pedersen_hash([commitment.x, commitment.y, asset_id]);
3608
-
3609
- // Try to prove with wrong blinding - should fail
3610
- main(commitment_hash, minimum_required, asset_id, balance, wrong_blinding);
3611
- }
3612
- `, path: "/Users/rz/local-dev/sip-protocol/packages/circuits/funding_proof/src/main.nr" } }, expression_width: { Bounded: { width: 4 } } };
3613
-
3614
- // src/proofs/circuits/validity_proof.json
3615
- var validity_proof_default = { noir_version: "1.0.0-beta.15+83245db91dcf63420ef4bcbbd85b98f397fee663", hash: "17105369051450454041", abi: { parameters: [{ name: "intent_hash", type: { kind: "field" }, visibility: "public" }, { name: "sender_commitment_x", type: { kind: "field" }, visibility: "public" }, { name: "sender_commitment_y", type: { kind: "field" }, visibility: "public" }, { name: "nullifier", type: { kind: "field" }, visibility: "public" }, { name: "timestamp", type: { kind: "integer", sign: "unsigned", width: 64 }, visibility: "public" }, { name: "expiry", type: { kind: "integer", sign: "unsigned", width: 64 }, visibility: "public" }, { name: "sender_address", type: { kind: "field" }, visibility: "private" }, { name: "sender_blinding", type: { kind: "field" }, visibility: "private" }, { name: "sender_secret", type: { kind: "field" }, visibility: "private" }, { name: "pub_key_x", type: { kind: "array", length: 32, type: { kind: "integer", sign: "unsigned", width: 8 } }, visibility: "private" }, { name: "pub_key_y", type: { kind: "array", length: 32, type: { kind: "integer", sign: "unsigned", width: 8 } }, visibility: "private" }, { name: "signature", type: { kind: "array", length: 64, type: { kind: "integer", sign: "unsigned", width: 8 } }, visibility: "private" }, { name: "message_hash", type: { kind: "array", length: 32, type: { kind: "integer", sign: "unsigned", width: 8 } }, visibility: "private" }, { name: "nonce", type: { kind: "field" }, visibility: "private" }], return_type: null, error_types: { "4743545721632785176": { error_kind: "string", string: "Nullifier mismatch" }, "4924752953922582949": { error_kind: "string", string: "Invalid ECDSA signature" }, "5940733937471987676": { error_kind: "string", string: "Intent expired" }, "9872184990886929843": { error_kind: "string", string: "Sender commitment X mismatch" }, "14620555433709191364": { error_kind: "string", string: "Sender commitment Y mismatch" }, "15764276373176857197": { error_kind: "string", string: "Stack too deep" } } }, bytecode: "H4sIAAAAAAAA/+WZaXBV5QGGz81CAwTCTogsF2XfJEBQBCFAQjAqIETZQgIkAYJmXyCIkMgaUEgENxBli8SQlEpCCAK1MsVWLNPptCMdxyl1xlI7Tqm1QwdoO9O85D345fAm997AjD88M+FJnvOdc77zeXPufaLLatgCyLQlqeln61nHn131X/4kxkQ6XKBwreu/ghyujXBthQsWrp1w7YULEa6DcB2F6yRcZ+G6CNdVuG7CdRcuVLgewoUJd59wPYXrJVxv4foI5xaur3D3C/eAcP2E6y/cAOEGCjdIuMHCDRFuqHDDhBsu3AjhHhRupHDhwo0SbrRwY4SLEG6scA8J97Bw44R7RLjxwk0Q7lHhJgo3SbhI4SYLN0W4qcJFCRct3DThYoSbLtxjwsUK97hwTwj3pHAzhJsp3CzhnhJutnBzhIsT7mnhnhFurnDzhJsv3ALhFgoXL9wi4RKESxRusXBLhFsqXJJwycKlCLdMuOXCrRAuVbiVwj0r3HPCpQmXLlyGcJnCZQmXLVyOcLnC5QmXL9wq4VYLVyDcGuGeF26tcC8It0649cIVClck3IvCbRBuo3CbhNss3BbhtgpXLNw24bYL95JwLwu3Q7idwpUIVyrcK8LtEm63cK8K95pwrwv3hnBvCrdHuL3CvSXcPuHeFu4d4fYLd0C4g8IdEu6wcGXCvSvcEeHK6ex+cVl3brZzW15trlbej7X8jO8ryKPOQf53MYGRyTOyvwzfP7huVnRtUdG8RYNGfz294FRm6dQvr+36lhdtYmy8Y6zrqPdzuKcL+hOrZQtaSVY5B/m6oOYEPC1opeX9glZ5P4dbi4ZXaivDucmgiQUd/xjROn/wt63ywv/b9cL/Csr3Xv10XMmk1LnDkjJiF5hjw9Yn3KxaHx7f/73QfwX/+tLoSb85uubSJyFd/lR05vygG7sWmWO92eyxgbHlK3M+3TZmdsLCn3/21fgDPXZsDkkcN2vAzqzL0aVnv/Izx7r3/fbDYf+Ze+PfARlRl8I+vnk9O+7YryLXBnyzNGzplgsfDTDHetrMF1oFeZSsJKscc/WwuX5a/8+x+q+fOXb4Oc7j6bXgsny7ppdj72pOfpb3czpmtWxOLh/n9L7l/Zxw7la8RmuyDdmWDCbbke3JELID2ZHsRHYmu5BdyW5kdzKU7EGGkfeRPcleZG+yj/X9awLsS95PPkD2I/uTA8iB5CByMDmEHEoOI4eTI8gHyZFkODmKHE2OISPIseRD5MPkOPIRcjw5gXyUnEhOIiPJyeQUcioZRUaT08gYcjr5GBlLPk4+QT5JziBnkrPIp8jZ5BwyjnyafIacS84j55MLyIVkPLmITCATycXkEnIpmUQmkynkMnI5uYJMJVeSz5LPkWlkOplBZpJZZDaZQ+aSeWQ+uYpcTRaQa8jnybXkC+Q6cj1ZSBaRL5IbyI3kJnIzuYXcShaT28jt5Evky+QOcidZQpaSr5C7yN3kq+Rr5OvkG+Sb5B5yL/kWuY98m3yH3E8eIA+Sh8jDZBn5LnmELCfx3DxuNd5cpNvyanMdN8Z6ek7fyw91Qd6PbfShrpqscQ7y9UOdOQFPN17d9Ng7PtTVeD+He7qgPoxttKAnyFrnIF8X1BzraUFPWN4vaK31wyzoe1bLFvQkWecc5OuCmhPwtKAnLe8XtM77OdxaNPymBBvOTf6Ys8Mc2zPquzL32qJz2zb0LSuK//r98A79Tv+9c2iP059fO1hVHjPdHOtfcXXslSlDerlKk4Z8PH/P374pqxzWvfwTd8WEY9uLz18vN8f6Mofh109F/qW4/YzOq/48J+fmlT298mamRlw5Uli7bHdu+HcXL5pjR1zc+rv5y8/MqdtUOqJdt81L4iprK879/nrCwAvr/nH8o5IN5lhPWxCJ10k1WUOeIGvJk2SdY+4eNn8fxvpyXtep+n8+qP867djha1L5W75d08uxtx5mLqvxw6WpOXq6boDl/f0Eej6vq7mdbsu7Y837OkOetUfYf6GDiHScIcC7C92+YEtvvomxJfaD9IwP5z1rtWxRne9anq5jzsnTdphET+NvXPhlRlOjp9HSeOijodHPaGd0M5oZvYxWRiejkdHHaGN0MZoYPYwWRgejgdG/fTivvlZD76J10bloXDx50bboWjQtehYti45Fw6Jf0a7oVjQrehWtik5Fo6JP0aboUjQpehQtig5Fg6I/0Z6RVkNzojfRmuhMNCb6Em2JJzWaEj2JlkRHoiHRj2hHdCOaEb2IVkQnohHRh2hDdCGaEO98aEF0IN7Z0H9ov8VWQ/Oh99B66Dw0HvoObYeuQ9Oh59By6Dg0HPoN7YZuQ7Oh19Bq6DQ0GvoMbYYuQ5Ohx9Bi6DA0GPoL7VVoNTQXegtPeXQWGgt9hbZCV6Gp0FNoKXQUGgr9hHZCN6GZ0EtoJXQSGgl9hDZCF6GJ0ENoIXQQGgj9g/bZbzU0D3oHrYPXHxoHfYO2wbsfPvSYf+O0H4B4HuD3ONDYZ35/hKzuf+74P9u6io1dt86L7Ze/6BdRU3blgrnP/mt7WUHPHnUT5kWZ++zWCPX/sLQ67w+TzX32p7wv+rgz23yQNdv29kOtPZmckpSRlpmRk5K4IjU9tzdtkGO0/cRzW15tLvNt1/fjC2OCnCf06Xgrxv7/KHczf/uYFhx/+xUSZRzvnAs2+y+B5qPVPgZPvPbG9yHGMdiijfO5HPumieve5T1F28cHtOx4v07Wnde3z4UnOu4xlD/7i7HmaynQGKPW1RLOJc7jXBvzv4Ob7BxuXepzOaJgaLexGTPzN16Oq1rX5dDgv4aEXs2bkH/jiwznvfg1M/fgZuYQLO7HXB/7d6Jl6786xr6mPa9A6871Ms8f4Bjfi2xtXN+cp9tqfvv8/LXPamJHpXV0HI/NvmfcZxi/T07NTknKTc1PSax/MKUsT8lOzMrLyE1NSc+1VyLIOMo+oy+vSPv4ti07vtH7gOWYi3ne2xckA8RxriZ+9nOwubFOb7pgsc8+ZyfSnK99H/8HYaTeT0IrAAA=", debug_symbols: "pZbNjuIwDIDfJeceYsf541VGI1SgM6pUFdSBlVaId9+YxaU9JMuGS+O69dfYsV1f1aHbXb63/fh1/FGbj6vaTf0w9N/b4bhvz/1xTNqr0nwBpzbYKPB/l6A2Ji1RbahRmN6g261RYrY9T13HVgtOop/aqRvPajNehqFRv9rhcn/p59SO9/XcTumpblQ3HtKagF/90LF0a57WOm8aND6MA7rZHPzKHvL2juhh76ypsQ8g9iFU2YvzXme/X/AfCCQAQO4ZAbuOoC0QHIgL4Kx97iGuCK5AiDE8CKgxS/B5AgHYB4HAhhoCGi8E9LaOAO8S0LxAKEYyODmLGGsIqK0kNAJCjgAmjzCWJBDGaV2zCUA/b8KZ7CYKWUk2ih/kaFHZrhIRc4hicfkox2GBcsVVIlinZ8LiQP+D4IOUBgQKOQKWENpJXgKYRSDo5U0EN/eI4HWFGykvYU4JDVk3CnmJGOY2Y5bNfp1VSAVEwLk6gs1WB9q3W1UR8Vqv+gcC3ka81K2K4Yxa6gMjxBqEAS0FYlJy1iGe/QqifxeB+ZZXym+AZ9+lmkJHREksRDIrwme6a/f9tJ7SwGEae9JM5gxXNgv0UFhROO53LHguXRYCu8VC5K3yfKdFAP40C4mLbOUNnywLJBorGicaLxqeEO/miWyIR0bNfxMWQAQUwbBw43BMfbsbOvaIfb6Me3Ew3Z5/n+SJDKqn6bjvDpep42AsptV0/UhnjPHzxgH7Aw==", file_map: { "14": { source: "// docs:start:ecdsa_secp256k1\n/// Verifies a ECDSA signature over the secp256k1 curve.\n/// - inputs:\n/// - x coordinate of public key as 32 bytes\n/// - y coordinate of public key as 32 bytes\n/// - the signature, as a 64 bytes array\n/// The signature internally will be represented as `(r, s)`,\n/// where `r` and `s` are fixed-sized big endian scalar values.\n/// As the `secp256k1` has a 256-bit modulus, we have a 64 byte signature\n/// while `r` and `s` will both be 32 bytes.\n/// We expect `s` to be normalized. This means given the curve's order,\n/// `s` should be less than or equal to `order / 2`.\n/// This is done to prevent malleability.\n/// For more context regarding malleability you can reference BIP 0062.\n/// - the hash of the message, as a vector of bytes\n/// - output: false for failure and true for success\npub fn verify_signature(\n public_key_x: [u8; 32],\n public_key_y: [u8; 32],\n signature: [u8; 64],\n message_hash: [u8; 32],\n) -> bool\n// docs:end:ecdsa_secp256k1\n{\n _verify_signature(public_key_x, public_key_y, signature, message_hash, true)\n}\n\n#[foreign(ecdsa_secp256k1)]\npub fn _verify_signature(\n public_key_x: [u8; 32],\n public_key_y: [u8; 32],\n signature: [u8; 64],\n message_hash: [u8; 32],\n predicate: bool,\n) -> bool {}\n", path: "std/ecdsa_secp256k1.nr" }, "16": { source: "use crate::cmp::Eq;\nuse crate::hash::Hash;\nuse crate::ops::arith::{Add, Neg, Sub};\n\n/// A point on the embedded elliptic curve\n/// By definition, the base field of the embedded curve is the scalar field of the proof system curve, i.e the Noir Field.\n/// x and y denotes the Weierstrass coordinates of the point, if is_infinite is false.\npub struct EmbeddedCurvePoint {\n pub x: Field,\n pub y: Field,\n pub is_infinite: bool,\n}\n\nimpl EmbeddedCurvePoint {\n /// Elliptic curve point doubling operation\n /// returns the doubled point of a point P, i.e P+P\n pub fn double(self) -> EmbeddedCurvePoint {\n embedded_curve_add(self, self)\n }\n\n /// Returns the null element of the curve; 'the point at infinity'\n pub fn point_at_infinity() -> EmbeddedCurvePoint {\n EmbeddedCurvePoint { x: 0, y: 0, is_infinite: true }\n }\n\n /// Returns the curve's generator point.\n pub fn generator() -> EmbeddedCurvePoint {\n // Generator point for the grumpkin curve (y^2 = x^3 - 17)\n EmbeddedCurvePoint {\n x: 1,\n y: 17631683881184975370165255887551781615748388533673675138860, // sqrt(-16)\n is_infinite: false,\n }\n }\n}\n\nimpl Add for EmbeddedCurvePoint {\n /// Adds two points P+Q, using the curve addition formula, and also handles point at infinity\n fn add(self, other: EmbeddedCurvePoint) -> EmbeddedCurvePoint {\n embedded_curve_add(self, other)\n }\n}\n\nimpl Sub for EmbeddedCurvePoint {\n /// Points subtraction operation, using addition and negation\n fn sub(self, other: EmbeddedCurvePoint) -> EmbeddedCurvePoint {\n self + other.neg()\n }\n}\n\nimpl Neg for EmbeddedCurvePoint {\n /// Negates a point P, i.e returns -P, by negating the y coordinate.\n /// If the point is at infinity, then the result is also at infinity.\n fn neg(self) -> EmbeddedCurvePoint {\n EmbeddedCurvePoint { x: self.x, y: -self.y, is_infinite: self.is_infinite }\n }\n}\n\nimpl Eq for EmbeddedCurvePoint {\n /// Checks whether two points are equal\n fn eq(self: Self, b: EmbeddedCurvePoint) -> bool {\n (self.is_infinite & b.is_infinite)\n | ((self.is_infinite == b.is_infinite) & (self.x == b.x) & (self.y == b.y))\n }\n}\n\nimpl Hash for EmbeddedCurvePoint {\n fn hash<H>(self, state: &mut H)\n where\n H: crate::hash::Hasher,\n {\n if self.is_infinite {\n self.is_infinite.hash(state);\n } else {\n self.x.hash(state);\n self.y.hash(state);\n }\n }\n}\n\n/// Scalar for the embedded curve represented as low and high limbs\n/// By definition, the scalar field of the embedded curve is base field of the proving system curve.\n/// It may not fit into a Field element, so it is represented with two Field elements; its low and high limbs.\npub struct EmbeddedCurveScalar {\n pub lo: Field,\n pub hi: Field,\n}\n\nimpl EmbeddedCurveScalar {\n pub fn new(lo: Field, hi: Field) -> Self {\n EmbeddedCurveScalar { lo, hi }\n }\n\n #[field(bn254)]\n pub fn from_field(scalar: Field) -> EmbeddedCurveScalar {\n let (a, b) = crate::field::bn254::decompose(scalar);\n EmbeddedCurveScalar { lo: a, hi: b }\n }\n\n //Bytes to scalar: take the first (after the specified offset) 16 bytes of the input as the lo value, and the next 16 bytes as the hi value\n #[field(bn254)]\n pub(crate) fn from_bytes(bytes: [u8; 64], offset: u32) -> EmbeddedCurveScalar {\n let mut v = 1;\n let mut lo = 0 as Field;\n let mut hi = 0 as Field;\n for i in 0..16 {\n lo = lo + (bytes[offset + 31 - i] as Field) * v;\n hi = hi + (bytes[offset + 15 - i] as Field) * v;\n v = v * 256;\n }\n let sig_s = crate::embedded_curve_ops::EmbeddedCurveScalar { lo, hi };\n sig_s\n }\n}\n\nimpl Eq for EmbeddedCurveScalar {\n fn eq(self, other: Self) -> bool {\n (other.hi == self.hi) & (other.lo == self.lo)\n }\n}\n\nimpl Hash for EmbeddedCurveScalar {\n fn hash<H>(self, state: &mut H)\n where\n H: crate::hash::Hasher,\n {\n self.hi.hash(state);\n self.lo.hash(state);\n }\n}\n\n// Computes a multi scalar multiplication over the embedded curve.\n// For bn254, We have Grumpkin and Baby JubJub.\n// For bls12-381, we have JubJub and Bandersnatch.\n//\n// The embedded curve being used is decided by the\n// underlying proof system.\n// docs:start:multi_scalar_mul\npub fn multi_scalar_mul<let N: u32>(\n points: [EmbeddedCurvePoint; N],\n scalars: [EmbeddedCurveScalar; N],\n) -> EmbeddedCurvePoint\n// docs:end:multi_scalar_mul\n{\n multi_scalar_mul_array_return(points, scalars, true)[0]\n}\n\n#[foreign(multi_scalar_mul)]\npub(crate) fn multi_scalar_mul_array_return<let N: u32>(\n points: [EmbeddedCurvePoint; N],\n scalars: [EmbeddedCurveScalar; N],\n predicate: bool,\n) -> [EmbeddedCurvePoint; 1] {}\n\n// docs:start:fixed_base_scalar_mul\npub fn fixed_base_scalar_mul(scalar: EmbeddedCurveScalar) -> EmbeddedCurvePoint\n// docs:end:fixed_base_scalar_mul\n{\n multi_scalar_mul([EmbeddedCurvePoint::generator()], [scalar])\n}\n\n/// This function only assumes that the points are on the curve\n/// It handles corner cases around the infinity point causing some overhead compared to embedded_curve_add_not_nul and embedded_curve_add_unsafe\n// docs:start:embedded_curve_add\npub fn embedded_curve_add(\n point1: EmbeddedCurvePoint,\n point2: EmbeddedCurvePoint,\n) -> EmbeddedCurvePoint {\n // docs:end:embedded_curve_add\n if crate::runtime::is_unconstrained() {\n // `embedded_curve_add_unsafe` requires the inputs not to be the infinity point, so we check it here.\n // This is because `embedded_curve_add_unsafe` uses the `embedded_curve_add` opcode.\n // For efficiency, the backend does not check the inputs for the infinity point, but it assumes that they are not the infinity point\n // so that it can apply the ec addition formula directly.\n if point1.is_infinite {\n point2\n } else if point2.is_infinite {\n point1\n } else {\n embedded_curve_add_unsafe(point1, point2)\n }\n } else {\n // In a constrained context, we also need to check the inputs are not the infinity point because we also use `embedded_curve_add_unsafe`\n // However we also need to identify the case where the two inputs are the same, because then\n // the addition formula does not work and we need to use the doubling formula instead.\n // In unconstrained context, we can check directly if the input values are the same when solving the opcode, so it is not an issue.\n\n // x_coordinates_match is true if both abscissae are the same\n let x_coordinates_match = point1.x == point2.x;\n // y_coordinates_match is true if both ordinates are the same\n let y_coordinates_match = point1.y == point2.y;\n // double_predicate is true if both abscissae and ordinates are the same\n let double_predicate = (x_coordinates_match & y_coordinates_match);\n // If the abscissae are the same, but not the ordinates, then one point is the opposite of the other\n let infinity_predicate = (x_coordinates_match & !y_coordinates_match);\n\n // `embedded_curve_add_unsafe` would not perform doubling, even if the inputs point1 and point2 are the same, because it cannot know this without adding some logic (and some constraints)\n // However we did this logic when we computed `double_predicate`, so we set the result to 2*point1 if point1 and point2 are the same\n let mut result = if double_predicate {\n // `embedded_curve_add_unsafe` is doing a doubling if the input is the same variable, because in this case it is guaranteed (at 'compile time') that the input is the same.\n embedded_curve_add_unsafe(point1, point1)\n } else {\n let point1_1 = EmbeddedCurvePoint {\n x: point1.x + (x_coordinates_match as Field),\n y: point1.y,\n is_infinite: false,\n };\n let point2_1 = EmbeddedCurvePoint { x: point2.x, y: point2.y, is_infinite: false };\n // point1_1 is guaranteed to have a different abscissa than point2:\n // - if x_coordinates_match is 0, that means point1.x != point2.x, and point1_1.x = point1.x + 0\n // - if x_coordinates_match is 1, that means point1.x = point2.x, but point1_1.x = point1.x + 1 in this case\n // Because the abscissa is different, the addition formula is guaranteed to succeed, so we can safely use `embedded_curve_add_unsafe`\n // Note that this computation may be garbage: if x_coordinates_match is 1, or if one of the input is the point at infinity.\n // therefore we only want to do this if we need the result, otherwise it needs to be eliminated as a dead instruction, lest we want the circuit to fail.\n embedded_curve_add_unsafe(point1_1, point2_1)\n };\n\n // Same logic as above for unconstrained context, we set the proper result when one of the inputs is the infinity point\n if point1.is_infinite {\n result = point2;\n }\n if point2.is_infinite {\n result = point1;\n }\n\n // Finally, we set the is_infinity flag of the result:\n // Opposite points should sum into the infinity point, however, if one of them is point at infinity, their coordinates are not meaningful\n // so we should not use the fact that the inputs are opposite in this case:\n let mut result_is_infinity =\n infinity_predicate & (!point1.is_infinite & !point2.is_infinite);\n // However, if both of them are at infinity, then the result is also at infinity\n result.is_infinite = result_is_infinity | (point1.is_infinite & point2.is_infinite);\n result\n }\n}\n\n#[foreign(embedded_curve_add)]\nfn embedded_curve_add_array_return(\n _point1: EmbeddedCurvePoint,\n _point2: EmbeddedCurvePoint,\n _predicate: bool,\n) -> [EmbeddedCurvePoint; 1] {}\n\n/// This function assumes that:\n/// The points are on the curve, and\n/// The points don't share an x-coordinate, and\n/// Neither point is the infinity point.\n/// If it is used with correct input, the function ensures the correct non-zero result is returned.\n/// Except for points on the curve, the other assumptions are checked by the function. It will cause assertion failure if they are not respected.\npub fn embedded_curve_add_not_nul(\n point1: EmbeddedCurvePoint,\n point2: EmbeddedCurvePoint,\n) -> EmbeddedCurvePoint {\n assert(point1.x != point2.x);\n assert(!point1.is_infinite);\n assert(!point2.is_infinite);\n // Ensure is_infinite is comptime\n let point1_1 = EmbeddedCurvePoint { x: point1.x, y: point1.y, is_infinite: false };\n let point2_1 = EmbeddedCurvePoint { x: point2.x, y: point2.y, is_infinite: false };\n embedded_curve_add_unsafe(point1_1, point2_1)\n}\n\n/// Unsafe ec addition\n/// If the inputs are the same, it will perform a doubling, but only if point1 and point2 are the same variable.\n/// If they have the same value but are different variables, the result will be incorrect because in this case\n/// it assumes (but does not check) that the points' x-coordinates are not equal.\n/// It also assumes neither point is the infinity point.\npub fn embedded_curve_add_unsafe(\n point1: EmbeddedCurvePoint,\n point2: EmbeddedCurvePoint,\n) -> EmbeddedCurvePoint {\n embedded_curve_add_array_return(point1, point2, true)[0]\n}\n", path: "std/embedded_curve_ops.nr" }, "17": { source: `use crate::field::field_less_than;
3616
- use crate::runtime::is_unconstrained;
3617
-
3618
- // The low and high decomposition of the field modulus
3619
- global PLO: Field = 53438638232309528389504892708671455233;
3620
- global PHI: Field = 64323764613183177041862057485226039389;
3621
-
3622
- pub(crate) global TWO_POW_128: Field = 0x100000000000000000000000000000000;
3623
-
3624
- // Decomposes a single field into two 16 byte fields.
3625
- fn compute_decomposition(x: Field) -> (Field, Field) {
3626
- // Here's we're taking advantage of truncating 128 bit limbs from the input field
3627
- // and then subtracting them from the input such the field division is equivalent to integer division.
3628
- let low = (x as u128) as Field;
3629
- let high = (x - low) / TWO_POW_128;
3630
-
3631
- (low, high)
3632
- }
3633
-
3634
- pub(crate) unconstrained fn decompose_hint(x: Field) -> (Field, Field) {
3635
- compute_decomposition(x)
3636
- }
3637
-
3638
- unconstrained fn lte_hint(x: Field, y: Field) -> bool {
3639
- if x == y {
3640
- true
3641
- } else {
3642
- field_less_than(x, y)
3643
- }
3644
- }
3645
-
3646
- // Assert that (alo > blo && ahi >= bhi) || (alo <= blo && ahi > bhi)
3647
- fn assert_gt_limbs(a: (Field, Field), b: (Field, Field)) {
3648
- let (alo, ahi) = a;
3649
- let (blo, bhi) = b;
3650
- // Safety: borrow is enforced to be boolean due to its type.
3651
- // if borrow is 0, it asserts that (alo > blo && ahi >= bhi)
3652
- // if borrow is 1, it asserts that (alo <= blo && ahi > bhi)
3653
- unsafe {
3654
- let borrow = lte_hint(alo, blo);
3655
-
3656
- let rlo = alo - blo - 1 + (borrow as Field) * TWO_POW_128;
3657
- let rhi = ahi - bhi - (borrow as Field);
3658
-
3659
- rlo.assert_max_bit_size::<128>();
3660
- rhi.assert_max_bit_size::<128>();
3661
- }
3662
- }
3663
-
3664
- /// Decompose a single field into two 16 byte fields.
3665
- pub fn decompose(x: Field) -> (Field, Field) {
3666
- if is_unconstrained() {
3667
- compute_decomposition(x)
3668
- } else {
3669
- // Safety: decomposition is properly checked below
3670
- unsafe {
3671
- // Take hints of the decomposition
3672
- let (xlo, xhi) = decompose_hint(x);
3673
-
3674
- // Range check the limbs
3675
- xlo.assert_max_bit_size::<128>();
3676
- xhi.assert_max_bit_size::<128>();
3677
-
3678
- // Check that the decomposition is correct
3679
- assert_eq(x, xlo + TWO_POW_128 * xhi);
3680
-
3681
- // Assert that the decomposition of P is greater than the decomposition of x
3682
- assert_gt_limbs((PLO, PHI), (xlo, xhi));
3683
- (xlo, xhi)
3684
- }
3685
- }
3686
- }
3687
-
3688
- pub fn assert_gt(a: Field, b: Field) {
3689
- if is_unconstrained() {
3690
- assert(
3691
- // Safety: already unconstrained
3692
- unsafe { field_less_than(b, a) },
3693
- );
3694
- } else {
3695
- // Decompose a and b
3696
- let a_limbs = decompose(a);
3697
- let b_limbs = decompose(b);
3698
-
3699
- // Assert that a_limbs is greater than b_limbs
3700
- assert_gt_limbs(a_limbs, b_limbs)
3701
- }
3702
- }
3703
-
3704
- pub fn assert_lt(a: Field, b: Field) {
3705
- assert_gt(b, a);
3706
- }
3707
-
3708
- pub fn gt(a: Field, b: Field) -> bool {
3709
- if is_unconstrained() {
3710
- // Safety: unsafe in unconstrained
3711
- unsafe {
3712
- field_less_than(b, a)
3713
- }
3714
- } else if a == b {
3715
- false
3716
- } else {
3717
- // Safety: Take a hint of the comparison and verify it
3718
- unsafe {
3719
- if field_less_than(a, b) {
3720
- assert_gt(b, a);
3721
- false
3722
- } else {
3723
- assert_gt(a, b);
3724
- true
3725
- }
3726
- }
3727
- }
3728
- }
3729
-
3730
- pub fn lt(a: Field, b: Field) -> bool {
3731
- gt(b, a)
3732
- }
3733
-
3734
- mod tests {
3735
- // TODO: Allow imports from "super"
3736
- use crate::field::bn254::{assert_gt, decompose, gt, lt, lte_hint, PHI, PLO, TWO_POW_128};
3737
-
3738
- #[test]
3739
- fn check_decompose() {
3740
- assert_eq(decompose(TWO_POW_128), (0, 1));
3741
- assert_eq(decompose(TWO_POW_128 + 0x1234567890), (0x1234567890, 1));
3742
- assert_eq(decompose(0x1234567890), (0x1234567890, 0));
3743
- }
3744
-
3745
- #[test]
3746
- unconstrained fn check_lte_hint() {
3747
- assert(lte_hint(0, 1));
3748
- assert(lte_hint(0, 0x100));
3749
- assert(lte_hint(0x100, TWO_POW_128 - 1));
3750
- assert(!lte_hint(0 - 1, 0));
3751
-
3752
- assert(lte_hint(0, 0));
3753
- assert(lte_hint(0x100, 0x100));
3754
- assert(lte_hint(0 - 1, 0 - 1));
3755
- }
3756
-
3757
- #[test]
3758
- fn check_gt() {
3759
- assert(gt(1, 0));
3760
- assert(gt(0x100, 0));
3761
- assert(gt((0 - 1), (0 - 2)));
3762
- assert(gt(TWO_POW_128, 0));
3763
- assert(!gt(0, 0));
3764
- assert(!gt(0, 0x100));
3765
- assert(gt(0 - 1, 0 - 2));
3766
- assert(!gt(0 - 2, 0 - 1));
3767
- assert_gt(0 - 1, 0);
3768
- }
3769
-
3770
- #[test]
3771
- fn check_plo_phi() {
3772
- assert_eq(PLO + PHI * TWO_POW_128, 0);
3773
- let p_bytes = crate::field::modulus_le_bytes();
3774
- let mut p_low: Field = 0;
3775
- let mut p_high: Field = 0;
3776
-
3777
- let mut offset = 1;
3778
- for i in 0..16 {
3779
- p_low += (p_bytes[i] as Field) * offset;
3780
- p_high += (p_bytes[i + 16] as Field) * offset;
3781
- offset *= 256;
3782
- }
3783
- assert_eq(p_low, PLO);
3784
- assert_eq(p_high, PHI);
3785
- }
3786
-
3787
- #[test]
3788
- fn check_decompose_edge_cases() {
3789
- assert_eq(decompose(0), (0, 0));
3790
- assert_eq(decompose(TWO_POW_128 - 1), (TWO_POW_128 - 1, 0));
3791
- assert_eq(decompose(TWO_POW_128 + 1), (1, 1));
3792
- assert_eq(decompose(TWO_POW_128 * 2), (0, 2));
3793
- assert_eq(decompose(TWO_POW_128 * 2 + 0x1234567890), (0x1234567890, 2));
3794
- }
3795
-
3796
- #[test]
3797
- fn check_decompose_large_values() {
3798
- let large_field = 0xffffffffffffffff;
3799
- let (lo, hi) = decompose(large_field);
3800
- assert_eq(large_field, lo + TWO_POW_128 * hi);
3801
-
3802
- let large_value = large_field - TWO_POW_128;
3803
- let (lo2, hi2) = decompose(large_value);
3804
- assert_eq(large_value, lo2 + TWO_POW_128 * hi2);
3805
- }
3806
-
3807
- #[test]
3808
- fn check_lt_comprehensive() {
3809
- assert(lt(0, 1));
3810
- assert(!lt(1, 0));
3811
- assert(!lt(0, 0));
3812
- assert(!lt(42, 42));
3813
-
3814
- assert(lt(TWO_POW_128 - 1, TWO_POW_128));
3815
- assert(!lt(TWO_POW_128, TWO_POW_128 - 1));
3816
- }
3817
- }
3818
- `, path: "std/field/bn254.nr" }, "19": { source: '// Exposed only for usage in `std::meta`\npub(crate) mod poseidon2;\n\nuse crate::default::Default;\nuse crate::embedded_curve_ops::{\n EmbeddedCurvePoint, EmbeddedCurveScalar, multi_scalar_mul, multi_scalar_mul_array_return,\n};\nuse crate::meta::derive_via;\n\n#[foreign(sha256_compression)]\n// docs:start:sha256_compression\npub fn sha256_compression(input: [u32; 16], state: [u32; 8]) -> [u32; 8] {}\n// docs:end:sha256_compression\n\n#[foreign(keccakf1600)]\n// docs:start:keccakf1600\npub fn keccakf1600(input: [u64; 25]) -> [u64; 25] {}\n// docs:end:keccakf1600\n\npub mod keccak {\n #[deprecated("This function has been moved to std::hash::keccakf1600")]\n pub fn keccakf1600(input: [u64; 25]) -> [u64; 25] {\n super::keccakf1600(input)\n }\n}\n\n#[foreign(blake2s)]\n// docs:start:blake2s\npub fn blake2s<let N: u32>(input: [u8; N]) -> [u8; 32]\n// docs:end:blake2s\n{}\n\n// docs:start:blake3\npub fn blake3<let N: u32>(input: [u8; N]) -> [u8; 32]\n// docs:end:blake3\n{\n if crate::runtime::is_unconstrained() {\n // Temporary measure while Barretenberg is main proving system.\n // Please open an issue if you\'re working on another proving system and running into problems due to this.\n crate::static_assert(\n N <= 1024,\n "Barretenberg cannot prove blake3 hashes with inputs larger than 1024 bytes",\n );\n }\n __blake3(input)\n}\n\n#[foreign(blake3)]\nfn __blake3<let N: u32>(input: [u8; N]) -> [u8; 32] {}\n\n// docs:start:pedersen_commitment\npub fn pedersen_commitment<let N: u32>(input: [Field; N]) -> EmbeddedCurvePoint {\n // docs:end:pedersen_commitment\n pedersen_commitment_with_separator(input, 0)\n}\n\n#[inline_always]\npub fn pedersen_commitment_with_separator<let N: u32>(\n input: [Field; N],\n separator: u32,\n) -> EmbeddedCurvePoint {\n let mut points = [EmbeddedCurveScalar { lo: 0, hi: 0 }; N];\n for i in 0..N {\n // we use the unsafe version because the multi_scalar_mul will constrain the scalars.\n points[i] = from_field_unsafe(input[i]);\n }\n let generators = derive_generators("DEFAULT_DOMAIN_SEPARATOR".as_bytes(), separator);\n multi_scalar_mul(generators, points)\n}\n\n// docs:start:pedersen_hash\npub fn pedersen_hash<let N: u32>(input: [Field; N]) -> Field\n// docs:end:pedersen_hash\n{\n pedersen_hash_with_separator(input, 0)\n}\n\n#[no_predicates]\npub fn pedersen_hash_with_separator<let N: u32>(input: [Field; N], separator: u32) -> Field {\n let mut scalars: [EmbeddedCurveScalar; N + 1] = [EmbeddedCurveScalar { lo: 0, hi: 0 }; N + 1];\n let mut generators: [EmbeddedCurvePoint; N + 1] =\n [EmbeddedCurvePoint::point_at_infinity(); N + 1];\n let domain_generators: [EmbeddedCurvePoint; N] =\n derive_generators("DEFAULT_DOMAIN_SEPARATOR".as_bytes(), separator);\n\n for i in 0..N {\n scalars[i] = from_field_unsafe(input[i]);\n generators[i] = domain_generators[i];\n }\n scalars[N] = EmbeddedCurveScalar { lo: N as Field, hi: 0 as Field };\n\n let length_generator: [EmbeddedCurvePoint; 1] =\n derive_generators("pedersen_hash_length".as_bytes(), 0);\n generators[N] = length_generator[0];\n multi_scalar_mul_array_return(generators, scalars, true)[0].x\n}\n\n#[field(bn254)]\n#[inline_always]\npub fn derive_generators<let N: u32, let M: u32>(\n domain_separator_bytes: [u8; M],\n starting_index: u32,\n) -> [EmbeddedCurvePoint; N] {\n crate::assert_constant(domain_separator_bytes);\n // TODO(https://github.com/noir-lang/noir/issues/5672): Add back assert_constant on starting_index\n __derive_generators(domain_separator_bytes, starting_index)\n}\n\n#[builtin(derive_pedersen_generators)]\n#[field(bn254)]\nfn __derive_generators<let N: u32, let M: u32>(\n domain_separator_bytes: [u8; M],\n starting_index: u32,\n) -> [EmbeddedCurvePoint; N] {}\n\n#[field(bn254)]\n// Same as from_field but:\n// does not assert the limbs are 128 bits\n// does not assert the decomposition does not overflow the EmbeddedCurveScalar\nfn from_field_unsafe(scalar: Field) -> EmbeddedCurveScalar {\n // Safety: xlo and xhi decomposition is checked below\n let (xlo, xhi) = unsafe { crate::field::bn254::decompose_hint(scalar) };\n // Check that the decomposition is correct\n assert_eq(scalar, xlo + crate::field::bn254::TWO_POW_128 * xhi);\n EmbeddedCurveScalar { lo: xlo, hi: xhi }\n}\n\npub fn poseidon2_permutation<let N: u32>(input: [Field; N], state_len: u32) -> [Field; N] {\n assert_eq(input.len(), state_len);\n poseidon2_permutation_internal(input)\n}\n\n#[foreign(poseidon2_permutation)]\nfn poseidon2_permutation_internal<let N: u32>(input: [Field; N]) -> [Field; N] {}\n\n// Generic hashing support.\n// Partially ported and impacted by rust.\n\n// Hash trait shall be implemented per type.\n#[derive_via(derive_hash)]\npub trait Hash {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher;\n}\n\n// docs:start:derive_hash\ncomptime fn derive_hash(s: TypeDefinition) -> Quoted {\n let name = quote { $crate::hash::Hash };\n let signature = quote { fn hash<H>(_self: Self, _state: &mut H) where H: $crate::hash::Hasher };\n let for_each_field = |name| quote { _self.$name.hash(_state); };\n crate::meta::make_trait_impl(\n s,\n name,\n signature,\n for_each_field,\n quote {},\n |fields| fields,\n )\n}\n// docs:end:derive_hash\n\n// Hasher trait shall be implemented by algorithms to provide hash-agnostic means.\n// TODO: consider making the types generic here ([u8], [Field], etc.)\npub trait Hasher {\n fn finish(self) -> Field;\n\n fn write(&mut self, input: Field);\n}\n\n// BuildHasher is a factory trait, responsible for production of specific Hasher.\npub trait BuildHasher {\n type H: Hasher;\n\n fn build_hasher(self) -> H;\n}\n\npub struct BuildHasherDefault<H>;\n\nimpl<H> BuildHasher for BuildHasherDefault<H>\nwhere\n H: Hasher + Default,\n{\n type H = H;\n\n fn build_hasher(_self: Self) -> H {\n H::default()\n }\n}\n\nimpl<H> Default for BuildHasherDefault<H>\nwhere\n H: Hasher + Default,\n{\n fn default() -> Self {\n BuildHasherDefault {}\n }\n}\n\nimpl Hash for Field {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self);\n }\n}\n\nimpl Hash for u1 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u8 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u16 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u32 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u64 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u128 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i8 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as u8 as Field);\n }\n}\n\nimpl Hash for i16 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as u16 as Field);\n }\n}\n\nimpl Hash for i32 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as u32 as Field);\n }\n}\n\nimpl Hash for i64 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as u64 as Field);\n }\n}\n\nimpl Hash for bool {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for () {\n fn hash<H>(_self: Self, _state: &mut H)\n where\n H: Hasher,\n {}\n}\n\nimpl<T, let N: u32> Hash for [T; N]\nwhere\n T: Hash,\n{\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl<T> Hash for [T]\nwhere\n T: Hash,\n{\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n self.len().hash(state);\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl<A, B> Hash for (A, B)\nwhere\n A: Hash,\n B: Hash,\n{\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n self.0.hash(state);\n self.1.hash(state);\n }\n}\n\nimpl<A, B, C> Hash for (A, B, C)\nwhere\n A: Hash,\n B: Hash,\n C: Hash,\n{\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n }\n}\n\nimpl<A, B, C, D> Hash for (A, B, C, D)\nwhere\n A: Hash,\n B: Hash,\n C: Hash,\n D: Hash,\n{\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n }\n}\n\nimpl<A, B, C, D, E> Hash for (A, B, C, D, E)\nwhere\n A: Hash,\n B: Hash,\n C: Hash,\n D: Hash,\n E: Hash,\n{\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n self.4.hash(state);\n }\n}\n\n// Some test vectors for Pedersen hash and Pedersen Commitment.\n// They have been generated using the same functions so the tests are for now useless\n// but they will be useful when we switch to Noir implementation.\n#[test]\nfn assert_pedersen() {\n assert_eq(\n pedersen_hash_with_separator([1], 1),\n 0x1b3f4b1a83092a13d8d1a59f7acb62aba15e7002f4440f2275edb99ebbc2305f,\n );\n assert_eq(\n pedersen_commitment_with_separator([1], 1),\n EmbeddedCurvePoint {\n x: 0x054aa86a73cb8a34525e5bbed6e43ba1198e860f5f3950268f71df4591bde402,\n y: 0x209dcfbf2cfb57f9f6046f44d71ac6faf87254afc7407c04eb621a6287cac126,\n is_infinite: false,\n },\n );\n\n assert_eq(\n pedersen_hash_with_separator([1, 2], 2),\n 0x26691c129448e9ace0c66d11f0a16d9014a9e8498ee78f4d69f0083168188255,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2], 2),\n EmbeddedCurvePoint {\n x: 0x2e2b3b191e49541fe468ec6877721d445dcaffe41728df0a0eafeb15e87b0753,\n y: 0x2ff4482400ad3a6228be17a2af33e2bcdf41be04795f9782bd96efe7e24f8778,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3], 3),\n 0x0bc694b7a1f8d10d2d8987d07433f26bd616a2d351bc79a3c540d85b6206dbe4,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3], 3),\n EmbeddedCurvePoint {\n x: 0x1fee4e8cf8d2f527caa2684236b07c4b1bad7342c01b0f75e9a877a71827dc85,\n y: 0x2f9fedb9a090697ab69bf04c8bc15f7385b3e4b68c849c1536e5ae15ff138fd1,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4], 4),\n 0xdae10fb32a8408521803905981a2b300d6a35e40e798743e9322b223a5eddc,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4], 4),\n EmbeddedCurvePoint {\n x: 0x07ae3e202811e1fca39c2d81eabe6f79183978e6f12be0d3b8eda095b79bdbc9,\n y: 0x0afc6f892593db6fbba60f2da558517e279e0ae04f95758587760ba193145014,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4, 5], 5),\n 0xfc375b062c4f4f0150f7100dfb8d9b72a6d28582dd9512390b0497cdad9c22,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4, 5], 5),\n EmbeddedCurvePoint {\n x: 0x1754b12bd475a6984a1094b5109eeca9838f4f81ac89c5f0a41dbce53189bb29,\n y: 0x2da030e3cfcdc7ddad80eaf2599df6692cae0717d4e9f7bfbee8d073d5d278f7,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4, 5, 6], 6),\n 0x1696ed13dc2730062a98ac9d8f9de0661bb98829c7582f699d0273b18c86a572,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4, 5, 6], 6),\n EmbeddedCurvePoint {\n x: 0x190f6c0e97ad83e1e28da22a98aae156da083c5a4100e929b77e750d3106a697,\n y: 0x1f4b60f34ef91221a0b49756fa0705da93311a61af73d37a0c458877706616fb,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4, 5, 6, 7], 7),\n 0x128c0ff144fc66b6cb60eeac8a38e23da52992fc427b92397a7dffd71c45ede3,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4, 5, 6, 7], 7),\n EmbeddedCurvePoint {\n x: 0x015441e9d29491b06563fac16fc76abf7a9534c715421d0de85d20dbe2965939,\n y: 0x1d2575b0276f4e9087e6e07c2cb75aa1baafad127af4be5918ef8a2ef2fea8fc,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4, 5, 6, 7, 8], 8),\n 0x2f960e117482044dfc99d12fece2ef6862fba9242be4846c7c9a3e854325a55c,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4, 5, 6, 7, 8], 8),\n EmbeddedCurvePoint {\n x: 0x1657737676968887fceb6dd516382ea13b3a2c557f509811cd86d5d1199bc443,\n y: 0x1f39f0cb569040105fa1e2f156521e8b8e08261e635a2b210bdc94e8d6d65f77,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4, 5, 6, 7, 8, 9], 9),\n 0x0c96db0790602dcb166cc4699e2d306c479a76926b81c2cb2aaa92d249ec7be7,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4, 5, 6, 7, 8, 9], 9),\n EmbeddedCurvePoint {\n x: 0x0a3ceae42d14914a432aa60ec7fded4af7dad7dd4acdbf2908452675ec67e06d,\n y: 0xfc19761eaaf621ad4aec9a8b2e84a4eceffdba78f60f8b9391b0bd9345a2f2,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 10),\n 0x2cd37505871bc460a62ea1e63c7fe51149df5d0801302cf1cbc48beb8dff7e94,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 10),\n EmbeddedCurvePoint {\n x: 0x2fb3f8b3d41ddde007c8c3c62550f9a9380ee546fcc639ffbb3fd30c8d8de30c,\n y: 0x300783be23c446b11a4c0fabf6c91af148937cea15fcf5fb054abf7f752ee245,\n is_infinite: false,\n },\n );\n}\n', path: "std/hash/mod.nr" }, "50": { source: `/// Validity Proof Circuit
3819
- ///
3820
- /// Proves: "This intent is authorized by the sender, without revealing
3821
- /// the sender's identity, private key, or signature."
3822
- ///
3823
- /// @see docs/specs/VALIDITY-PROOF.md
3824
-
3825
- use std::hash::pedersen_hash;
3826
- use std::hash::pedersen_commitment;
3827
- use std::ecdsa_secp256k1::verify_signature;
3828
-
3829
- // --- Main Circuit ---
3830
-
3831
- /// Main validity proof entry point
3832
- ///
3833
- /// Public inputs: intent_hash, sender_commitment (x,y), nullifier, timestamp, expiry
3834
- /// Private inputs: sender_address (Field), sender_blinding, sender_secret,
3835
- /// pub_key_x, pub_key_y, signature, message_hash, nonce
3836
- ///
3837
- /// Constraints:
3838
- /// 1. sender_commitment = Pedersen(sender_address, sender_blinding)
3839
- /// 2. signature is valid for message_hash using pub_key
3840
- /// 3. nullifier = Pedersen_hash(sender_secret, intent_hash, nonce)
3841
- /// 4. timestamp < expiry
3842
- pub fn main(
3843
- // Public inputs
3844
- intent_hash: pub Field,
3845
- sender_commitment_x: pub Field,
3846
- sender_commitment_y: pub Field,
3847
- nullifier: pub Field,
3848
- timestamp: pub u64,
3849
- expiry: pub u64,
3850
-
3851
- // Private inputs
3852
- sender_address: Field,
3853
- sender_blinding: Field,
3854
- sender_secret: Field,
3855
- pub_key_x: [u8; 32],
3856
- pub_key_y: [u8; 32],
3857
- signature: [u8; 64],
3858
- message_hash: [u8; 32],
3859
- nonce: Field,
3860
- ) {
3861
- // Constraint 1: Verify Sender Commitment
3862
- // C = Pedersen(sender_address, sender_blinding)
3863
- let commitment = pedersen_commitment([sender_address, sender_blinding]);
3864
- assert(commitment.x == sender_commitment_x, "Sender commitment X mismatch");
3865
- assert(commitment.y == sender_commitment_y, "Sender commitment Y mismatch");
3866
-
3867
- // Constraint 2: Verify ECDSA Signature
3868
- // The signature must be valid for the message_hash using the provided public key
3869
- let valid_sig = verify_signature(pub_key_x, pub_key_y, signature, message_hash);
3870
- assert(valid_sig, "Invalid ECDSA signature");
3871
-
3872
- // Constraint 3: Verify Nullifier Derivation
3873
- // nullifier = Pedersen_hash(sender_secret, intent_hash, nonce)
3874
- let computed_nullifier = pedersen_hash([sender_secret, intent_hash, nonce]);
3875
- assert(computed_nullifier == nullifier, "Nullifier mismatch");
3876
-
3877
- // Constraint 4: Time Bounds Check
3878
- assert(timestamp < expiry, "Intent expired");
3879
- }
3880
-
3881
- // --- Tests ---
3882
-
3883
- // NOTE: Full ECDSA integration test requires TypeScript to generate valid signatures
3884
- // The NoirProofProvider in SDK will generate proper test vectors
3885
- // Here we test the commitment and nullifier logic which are pure Noir
3886
-
3887
- #[test]
3888
- fn test_commitment_and_nullifier() {
3889
- // Test just the commitment and nullifier computation (without ECDSA)
3890
- let sender_address: Field = 0x742d35Cc6634C0532925a3b844Bc9e7595f;
3891
- let sender_blinding: Field = 0xABCDEF123456;
3892
- let sender_secret: Field = 0x1234567890ABCDEF;
3893
- let intent_hash: Field = 0xDEADBEEF;
3894
- let nonce: Field = 0x99999;
3895
-
3896
- // Compute and verify commitment is consistent
3897
- let commitment1 = pedersen_commitment([sender_address, sender_blinding]);
3898
- let commitment2 = pedersen_commitment([sender_address, sender_blinding]);
3899
- assert(commitment1.x == commitment2.x, "Commitment X should be deterministic");
3900
- assert(commitment1.y == commitment2.y, "Commitment Y should be deterministic");
3901
-
3902
- // Compute and verify nullifier is consistent
3903
- let nullifier1 = pedersen_hash([sender_secret, intent_hash, nonce]);
3904
- let nullifier2 = pedersen_hash([sender_secret, intent_hash, nonce]);
3905
- assert(nullifier1 == nullifier2, "Nullifier should be deterministic");
3906
-
3907
- // Different nonce should give different nullifier
3908
- let different_nonce: Field = 0x88888;
3909
- let nullifier3 = pedersen_hash([sender_secret, intent_hash, different_nonce]);
3910
- assert(nullifier1 != nullifier3, "Different nonce should give different nullifier");
3911
- }
3912
-
3913
- #[test]
3914
- fn test_time_bounds() {
3915
- // Test timestamp < expiry constraint
3916
- let valid_timestamp: u64 = 1732600000;
3917
- let valid_expiry: u64 = 1732686400;
3918
- assert(valid_timestamp < valid_expiry, "Valid time bounds");
3919
- }
3920
-
3921
- // NOTE: Full integration tests with ECDSA require TypeScript SDK
3922
- // The NoirProofProvider will generate valid signature test vectors
3923
- `, path: "/Users/rz/local-dev/sip-protocol/packages/circuits/validity_proof/src/main.nr" } }, expression_width: { Bounded: { width: 4 } } };
3924
-
3925
- // src/proofs/circuits/fulfillment_proof.json
3926
- var fulfillment_proof_default = { noir_version: "1.0.0-beta.15+83245db91dcf63420ef4bcbbd85b98f397fee663", hash: "13146944445132352806", abi: { parameters: [{ name: "intent_hash", type: { kind: "field" }, visibility: "public" }, { name: "output_commitment_x", type: { kind: "field" }, visibility: "public" }, { name: "output_commitment_y", type: { kind: "field" }, visibility: "public" }, { name: "recipient_stealth", type: { kind: "field" }, visibility: "public" }, { name: "min_output_amount", type: { kind: "integer", sign: "unsigned", width: 64 }, visibility: "public" }, { name: "solver_id", type: { kind: "field" }, visibility: "public" }, { name: "fulfillment_time", type: { kind: "integer", sign: "unsigned", width: 64 }, visibility: "public" }, { name: "expiry", type: { kind: "integer", sign: "unsigned", width: 64 }, visibility: "public" }, { name: "output_amount", type: { kind: "integer", sign: "unsigned", width: 64 }, visibility: "private" }, { name: "output_blinding", type: { kind: "field" }, visibility: "private" }, { name: "solver_secret", type: { kind: "field" }, visibility: "private" }, { name: "attestation_recipient", type: { kind: "field" }, visibility: "private" }, { name: "attestation_amount", type: { kind: "integer", sign: "unsigned", width: 64 }, visibility: "private" }, { name: "attestation_tx_hash", type: { kind: "field" }, visibility: "private" }, { name: "attestation_block", type: { kind: "integer", sign: "unsigned", width: 64 }, visibility: "private" }, { name: "oracle_signature", type: { kind: "array", length: 64, type: { kind: "integer", sign: "unsigned", width: 8 } }, visibility: "private" }, { name: "oracle_message_hash", type: { kind: "array", length: 32, type: { kind: "integer", sign: "unsigned", width: 8 } }, visibility: "private" }, { name: "oracle_pub_key_x", type: { kind: "array", length: 32, type: { kind: "integer", sign: "unsigned", width: 8 } }, visibility: "private" }, { name: "oracle_pub_key_y", type: { kind: "array", length: 32, type: { kind: "integer", sign: "unsigned", width: 8 } }, visibility: "private" }], return_type: null, error_types: { "1811611355587044900": { error_kind: "string", string: "Unauthorized solver" }, "5682920188479059162": { error_kind: "string", string: "Amount mismatch in attestation" }, "9350488092177273812": { error_kind: "string", string: "Recipient mismatch in attestation" }, "10078784717933725989": { error_kind: "string", string: "Invalid oracle attestation signature" }, "10879340518732620616": { error_kind: "string", string: "Commitment X mismatch" }, "12297495446303487112": { error_kind: "string", string: "Output below minimum" }, "12394005467219657293": { error_kind: "string", string: "Commitment Y mismatch" }, "15764276373176857197": { error_kind: "string", string: "Stack too deep" }, "17406200060514520896": { error_kind: "string", string: "Fulfillment after expiry" } } }, bytecode: "H4sIAAAAAAAA/82aCXBV1QGGzwshhBqJEJYQBR7IFoFIgKAIQhRCIErYouwJIQkQNAlkgyBCwr4ohKXUglpcKgFBtpiyCrZjW7GM06kjHactOmO1M51au+gA6oz5yX/zDpefvPcSdTwz8OV999x7z7153Pz/Cx5TN0LJvMzc/HO1fIOvPbV/mpGYk+hyYcK1EC5cuAjhWgkXyf1td5twrYVrI1yUcG2Faydce+E6CBctXEfhYoS7Xbg7hOskXGfhugjnFa6rcN2Eu1O47sL1EK6ncL2E6y1crHB3CddHuL7C9RMuTri7hesvXLxwA4QbKNwg4RKEGyzcPcLdK9wQ4e4Tbqhww4S7X7jhwo0QLlG4B4R7ULiRwo0SLkm40cIlCzdGuLHCpQj3kHAPCzdOuFThxgs3QbiJwk0SbrJwacI9Ityjwk0Rbqpw04SbLtwM4WYKN0u4dOEyhJstXKZwc4TLEi5buBzh5go3T7j5wuUKt0C4x4R7XLg84fKFKxBuoXCLhCsUrki4YuFKhCsVbrFwS4QrE26pcE8It0y4J4VbLtwK4cqFqxBupXCrhFst3Brh1gq3Trj1wm0QbqNwm4R7Srinhdss3BbhKoXbKtw24bYLt0O4nwq3U7ifCfeMcD8Xbpdwu4V7VrjnhHteuF8It0e4F4R7UbiXhHtZuF8K94pwe4WrEm6fcPuFe1W4A8IdFO41ulC+DjE3Dsd5yf7ZqYUfxe+JPT4hqaaiYuqs3gP/MabsxMKtIz/6YvvnPJbXBDQ84f7nehra6DWB7Wtf1yHysDPDuRkQia4jhLpO9H1d/E2OW8njeg4FsYbDpnE31RPktdpr8netoQ2c2H1ef8cK4E1TP+xv/BHyqHtSsyYswN9NOnLzuTNdcz1HA1+D+S5vaEvTuBt6jKx2Twr2htoL8HdDj5nAb2h14Gu4dtPwjQ2znJcMH17W+s8JLUtjPw8rif+63flvyqp2f/bOkMoRuVP6ZhWkTLfnxqxIv3pwRfzMHvui/xfx+4sDR/zh1aUX345s+7eK02/1vrJ9lj03kOHMbZ5StaDonY2DJqXPeOP9j4e+0HHz2siMIRN6bll0KWnrmY9D7Lne59492/erKVe+DC0YdTHmt1cvF6Yd+l3istB/zomZs+78mz3tuf6G/UY7Qh4lj5HVrrX6GZ7Xa/+qqf3zK9eGYH/UeExw5wxwbpPWFGICX1ON+WHW1MwEvqZbzA+zpiCe4p4I07g1BfsT7bgJfE04dhjPUUAuJBeRhWQRWUyWkKXkYnIJWUYuJZ8gl5FPksvJFWQ5WUGuJFeRq8k15FpyHbme3EBuJDeRT5FPk5uNr7KAlcZXTcBtxldBwB3GVzXAncZXKcBnjK86gLuMryKAzxpfFQCfN77ID+4xvmgPvmh8ER582fiiOviK8UVysMr4oje43/giNnjA+KI0+BoZSd5GtibbkFFkW7Id2Z7sQEaTHckY8nbyDrIT2ZnsYnzPQrAr2Y28k+xO9iB7kr3I3mQseRfZh+xL9iPjyLvJ/mQ8OYAcSA4iE8jB5D3kveQQ8j5yKDmMvJ8cTo4gE8kHyAfJkeQoMokcTSaTY8ixZAr5EPkwOY5MJceTE8iJ5CRyMplGPkI+Sk4hp5LTyOnkDHImOYtMJzPI2WQmOYfMIrPJHHIuOY+cT+aSC8jHyMfJPDKfxHPzhLl+eEivCWh4TlhzA2lZ9jmact6fBD73uvB7kjzlnhRs+LUX4O/CT9587g3h91Tga7h2037M4bff5ROJf9/QKjVq8YeTi65+sqtTyfjchE/2ltfM3VEc/98LF+y5cRfW/3HavNOTj6/ZGndr+7WZaQdq9v/6T5fTe51f/u+jb1ausuf6G/Yb7SR5yrU2P8MTxNygjnu69q8zxve7a2cEG8Sam+DOGeDc7/QzpTAT+Bpb+J/raWij1wS2r31dZ8lzzgznMyWIRNcRgv1MqbEX7+8zpbNBrOGcadxN9QR5rfaa/F3rXn6BBxc+QcCDFO0FbeFWU/efK5DMkMqQyJDGkMSQwpDAkL6QvJC6kLiQtpC0kLKQsJCuunA9XU1dmkKSQopCgkJ6QnJCakJiQlpCUkJKQkJCOkIyQipCIkIaQhJCCkICQvpB8kHqQeJB2kHSQcpBwkG6QbJJNHWJBmkGSQYpBgkG6QXJBakFiQVpBUkFKQUJBekEyQSpBIkEaQRJBCkECQTpA8kDqQOJA09yJA2kDDypkS6QLGabukSBNIEkgRSBBIH0gOSA1IDEgLSApICUgISAdIBmhFaERoQ2hCaEFoQGhPaD5oPWg8aDtoOmg5aDhoN2g2ZTbuoaDdoMnt5oMWgwaC9oLmgtaCxoK2gqaCloKGgnaCZoJWgkaCNoImghaCBoH2geaB1oHGgbaBpoGWgYaBdoFntMXaNAm0CTQItAg0B7wHsQrQGNAW0BTQEtAQ3B+XDdGc7DEM8GPAPw4MW/7RbWHHv+PnL9oE87/H/pvv3WpvoPscdkbavYEvfVTnvbYXLcyg8zU6dfW079cD4DfS/q7NXZu39TYW9zPhf9oLr/xAtv/zXV3vY62WNslHdcy0ub7G1O8us+973L/+rWvaO97QyZ+O60qlPDt/zH8c7DsxWZnZNVkLewoCgnY35ufnFn2nDXbOfJ6jUBDU+4tV/w+5cnh7sPGNT+Jtn5dUtT1u/s04j96+PcKGt/91owIvjafoQ7++Dd2cr6OtLaByPJOp7HtW20OG8TrynJ2T+0cfuHtDE3nt85Fn6C4Bqj+bqZmGu/l5pbc9R9NcJ5xHHc98b+PnjJqHhzsculhLI+7QcXjC9dfSnt4PK2L8V+Ghn9Wcmw0it/KXBfS0gDa49oYA0R4nrs++P8m2jc/V+S7JzTWVdzc+P9so8f6prfiWxpnd9ep9c0PD5464v3q1MG5LV27Y/hXDOuM4ZfZ+cW5mQV55bmZNQ+mHLm5RRmLCopKM7NyS927kS4tZdzxGDekc7+tzRu/+tKm3GtxT5u/QnJULGf5yavQ1xsaK7b2y5CbHOO2Ya01+tcx7dZzirpuSsAAA==", debug_symbols: "pZbLjqswDIbfJWsWsXPvq4xGFW2ZERKiFdMe6ajqux+7QwJdJOoJG2Iu/oiN/eO7OHWH2/e+H7/OP2L3cReHqR+G/ns/nI/ttT+PdPUuJB/Aih02Atzv4sVO0RLETjcC6Qn9eDQiuu2vU9ex14pD9Es7deNV7MbbMDTiTzvcng/9XNrxuV7bie7KRnTjiVYCfvVDx9ajWbxl3tVLnJ092uQO7sUf8v5W69nfGlXj7yH6e1/lH4N3Mvv+QvzgpZoB4JVLBPN2BlFCDAGlUhUECEEmgsEcwRUINu0BrDFLHsILwRf34NMeMEsIeYIGMDNBg/E1BFQuEtCZOgJsJaB6g1DMpLfxW4RQQ6AiiE2FgJAjcLpzCGV0TISyUtZsAtClTViV3UShKrUJMQ5t9UpdbCUi5BDF9rQmxbHKxH80OKiQCE7XEKiSIkFBlcgom9pTyxqRQYNJpqzJ5gF1obKlTVpJOVk+hn57EzYRkJS7JgyvU1l6l00lutLX8CmXav3Te61sLDWHx9Sh3mQ7FLfLJW7XS9wumLhdMYvpDDJ1WIBQg1DU21HwqDjrEItmQnBbEZiX3WJ9L7NAWGn/+x2i5JIJuSpvJnzSWXvsp9dpFSxNqlSMYN28+nkNv6uT3PxsABk8xTqMV1S8onmCYsOw6LLB8y+wwRPwcxL23HJsBBYkHoplNIAlgQ3kfbNBZMUPe80/MTaIrPgVnsisyeBdNHw0AhsPTs/Ut4eh4wg5B7fxGAOm0+vfS7wTB/jLdD52p9vUcXJWUzwdP6jbMXw+OIH/AA==", file_map: { "14": { source: "// docs:start:ecdsa_secp256k1\n/// Verifies a ECDSA signature over the secp256k1 curve.\n/// - inputs:\n/// - x coordinate of public key as 32 bytes\n/// - y coordinate of public key as 32 bytes\n/// - the signature, as a 64 bytes array\n/// The signature internally will be represented as `(r, s)`,\n/// where `r` and `s` are fixed-sized big endian scalar values.\n/// As the `secp256k1` has a 256-bit modulus, we have a 64 byte signature\n/// while `r` and `s` will both be 32 bytes.\n/// We expect `s` to be normalized. This means given the curve's order,\n/// `s` should be less than or equal to `order / 2`.\n/// This is done to prevent malleability.\n/// For more context regarding malleability you can reference BIP 0062.\n/// - the hash of the message, as a vector of bytes\n/// - output: false for failure and true for success\npub fn verify_signature(\n public_key_x: [u8; 32],\n public_key_y: [u8; 32],\n signature: [u8; 64],\n message_hash: [u8; 32],\n) -> bool\n// docs:end:ecdsa_secp256k1\n{\n _verify_signature(public_key_x, public_key_y, signature, message_hash, true)\n}\n\n#[foreign(ecdsa_secp256k1)]\npub fn _verify_signature(\n public_key_x: [u8; 32],\n public_key_y: [u8; 32],\n signature: [u8; 64],\n message_hash: [u8; 32],\n predicate: bool,\n) -> bool {}\n", path: "std/ecdsa_secp256k1.nr" }, "16": { source: "use crate::cmp::Eq;\nuse crate::hash::Hash;\nuse crate::ops::arith::{Add, Neg, Sub};\n\n/// A point on the embedded elliptic curve\n/// By definition, the base field of the embedded curve is the scalar field of the proof system curve, i.e the Noir Field.\n/// x and y denotes the Weierstrass coordinates of the point, if is_infinite is false.\npub struct EmbeddedCurvePoint {\n pub x: Field,\n pub y: Field,\n pub is_infinite: bool,\n}\n\nimpl EmbeddedCurvePoint {\n /// Elliptic curve point doubling operation\n /// returns the doubled point of a point P, i.e P+P\n pub fn double(self) -> EmbeddedCurvePoint {\n embedded_curve_add(self, self)\n }\n\n /// Returns the null element of the curve; 'the point at infinity'\n pub fn point_at_infinity() -> EmbeddedCurvePoint {\n EmbeddedCurvePoint { x: 0, y: 0, is_infinite: true }\n }\n\n /// Returns the curve's generator point.\n pub fn generator() -> EmbeddedCurvePoint {\n // Generator point for the grumpkin curve (y^2 = x^3 - 17)\n EmbeddedCurvePoint {\n x: 1,\n y: 17631683881184975370165255887551781615748388533673675138860, // sqrt(-16)\n is_infinite: false,\n }\n }\n}\n\nimpl Add for EmbeddedCurvePoint {\n /// Adds two points P+Q, using the curve addition formula, and also handles point at infinity\n fn add(self, other: EmbeddedCurvePoint) -> EmbeddedCurvePoint {\n embedded_curve_add(self, other)\n }\n}\n\nimpl Sub for EmbeddedCurvePoint {\n /// Points subtraction operation, using addition and negation\n fn sub(self, other: EmbeddedCurvePoint) -> EmbeddedCurvePoint {\n self + other.neg()\n }\n}\n\nimpl Neg for EmbeddedCurvePoint {\n /// Negates a point P, i.e returns -P, by negating the y coordinate.\n /// If the point is at infinity, then the result is also at infinity.\n fn neg(self) -> EmbeddedCurvePoint {\n EmbeddedCurvePoint { x: self.x, y: -self.y, is_infinite: self.is_infinite }\n }\n}\n\nimpl Eq for EmbeddedCurvePoint {\n /// Checks whether two points are equal\n fn eq(self: Self, b: EmbeddedCurvePoint) -> bool {\n (self.is_infinite & b.is_infinite)\n | ((self.is_infinite == b.is_infinite) & (self.x == b.x) & (self.y == b.y))\n }\n}\n\nimpl Hash for EmbeddedCurvePoint {\n fn hash<H>(self, state: &mut H)\n where\n H: crate::hash::Hasher,\n {\n if self.is_infinite {\n self.is_infinite.hash(state);\n } else {\n self.x.hash(state);\n self.y.hash(state);\n }\n }\n}\n\n/// Scalar for the embedded curve represented as low and high limbs\n/// By definition, the scalar field of the embedded curve is base field of the proving system curve.\n/// It may not fit into a Field element, so it is represented with two Field elements; its low and high limbs.\npub struct EmbeddedCurveScalar {\n pub lo: Field,\n pub hi: Field,\n}\n\nimpl EmbeddedCurveScalar {\n pub fn new(lo: Field, hi: Field) -> Self {\n EmbeddedCurveScalar { lo, hi }\n }\n\n #[field(bn254)]\n pub fn from_field(scalar: Field) -> EmbeddedCurveScalar {\n let (a, b) = crate::field::bn254::decompose(scalar);\n EmbeddedCurveScalar { lo: a, hi: b }\n }\n\n //Bytes to scalar: take the first (after the specified offset) 16 bytes of the input as the lo value, and the next 16 bytes as the hi value\n #[field(bn254)]\n pub(crate) fn from_bytes(bytes: [u8; 64], offset: u32) -> EmbeddedCurveScalar {\n let mut v = 1;\n let mut lo = 0 as Field;\n let mut hi = 0 as Field;\n for i in 0..16 {\n lo = lo + (bytes[offset + 31 - i] as Field) * v;\n hi = hi + (bytes[offset + 15 - i] as Field) * v;\n v = v * 256;\n }\n let sig_s = crate::embedded_curve_ops::EmbeddedCurveScalar { lo, hi };\n sig_s\n }\n}\n\nimpl Eq for EmbeddedCurveScalar {\n fn eq(self, other: Self) -> bool {\n (other.hi == self.hi) & (other.lo == self.lo)\n }\n}\n\nimpl Hash for EmbeddedCurveScalar {\n fn hash<H>(self, state: &mut H)\n where\n H: crate::hash::Hasher,\n {\n self.hi.hash(state);\n self.lo.hash(state);\n }\n}\n\n// Computes a multi scalar multiplication over the embedded curve.\n// For bn254, We have Grumpkin and Baby JubJub.\n// For bls12-381, we have JubJub and Bandersnatch.\n//\n// The embedded curve being used is decided by the\n// underlying proof system.\n// docs:start:multi_scalar_mul\npub fn multi_scalar_mul<let N: u32>(\n points: [EmbeddedCurvePoint; N],\n scalars: [EmbeddedCurveScalar; N],\n) -> EmbeddedCurvePoint\n// docs:end:multi_scalar_mul\n{\n multi_scalar_mul_array_return(points, scalars, true)[0]\n}\n\n#[foreign(multi_scalar_mul)]\npub(crate) fn multi_scalar_mul_array_return<let N: u32>(\n points: [EmbeddedCurvePoint; N],\n scalars: [EmbeddedCurveScalar; N],\n predicate: bool,\n) -> [EmbeddedCurvePoint; 1] {}\n\n// docs:start:fixed_base_scalar_mul\npub fn fixed_base_scalar_mul(scalar: EmbeddedCurveScalar) -> EmbeddedCurvePoint\n// docs:end:fixed_base_scalar_mul\n{\n multi_scalar_mul([EmbeddedCurvePoint::generator()], [scalar])\n}\n\n/// This function only assumes that the points are on the curve\n/// It handles corner cases around the infinity point causing some overhead compared to embedded_curve_add_not_nul and embedded_curve_add_unsafe\n// docs:start:embedded_curve_add\npub fn embedded_curve_add(\n point1: EmbeddedCurvePoint,\n point2: EmbeddedCurvePoint,\n) -> EmbeddedCurvePoint {\n // docs:end:embedded_curve_add\n if crate::runtime::is_unconstrained() {\n // `embedded_curve_add_unsafe` requires the inputs not to be the infinity point, so we check it here.\n // This is because `embedded_curve_add_unsafe` uses the `embedded_curve_add` opcode.\n // For efficiency, the backend does not check the inputs for the infinity point, but it assumes that they are not the infinity point\n // so that it can apply the ec addition formula directly.\n if point1.is_infinite {\n point2\n } else if point2.is_infinite {\n point1\n } else {\n embedded_curve_add_unsafe(point1, point2)\n }\n } else {\n // In a constrained context, we also need to check the inputs are not the infinity point because we also use `embedded_curve_add_unsafe`\n // However we also need to identify the case where the two inputs are the same, because then\n // the addition formula does not work and we need to use the doubling formula instead.\n // In unconstrained context, we can check directly if the input values are the same when solving the opcode, so it is not an issue.\n\n // x_coordinates_match is true if both abscissae are the same\n let x_coordinates_match = point1.x == point2.x;\n // y_coordinates_match is true if both ordinates are the same\n let y_coordinates_match = point1.y == point2.y;\n // double_predicate is true if both abscissae and ordinates are the same\n let double_predicate = (x_coordinates_match & y_coordinates_match);\n // If the abscissae are the same, but not the ordinates, then one point is the opposite of the other\n let infinity_predicate = (x_coordinates_match & !y_coordinates_match);\n\n // `embedded_curve_add_unsafe` would not perform doubling, even if the inputs point1 and point2 are the same, because it cannot know this without adding some logic (and some constraints)\n // However we did this logic when we computed `double_predicate`, so we set the result to 2*point1 if point1 and point2 are the same\n let mut result = if double_predicate {\n // `embedded_curve_add_unsafe` is doing a doubling if the input is the same variable, because in this case it is guaranteed (at 'compile time') that the input is the same.\n embedded_curve_add_unsafe(point1, point1)\n } else {\n let point1_1 = EmbeddedCurvePoint {\n x: point1.x + (x_coordinates_match as Field),\n y: point1.y,\n is_infinite: false,\n };\n let point2_1 = EmbeddedCurvePoint { x: point2.x, y: point2.y, is_infinite: false };\n // point1_1 is guaranteed to have a different abscissa than point2:\n // - if x_coordinates_match is 0, that means point1.x != point2.x, and point1_1.x = point1.x + 0\n // - if x_coordinates_match is 1, that means point1.x = point2.x, but point1_1.x = point1.x + 1 in this case\n // Because the abscissa is different, the addition formula is guaranteed to succeed, so we can safely use `embedded_curve_add_unsafe`\n // Note that this computation may be garbage: if x_coordinates_match is 1, or if one of the input is the point at infinity.\n // therefore we only want to do this if we need the result, otherwise it needs to be eliminated as a dead instruction, lest we want the circuit to fail.\n embedded_curve_add_unsafe(point1_1, point2_1)\n };\n\n // Same logic as above for unconstrained context, we set the proper result when one of the inputs is the infinity point\n if point1.is_infinite {\n result = point2;\n }\n if point2.is_infinite {\n result = point1;\n }\n\n // Finally, we set the is_infinity flag of the result:\n // Opposite points should sum into the infinity point, however, if one of them is point at infinity, their coordinates are not meaningful\n // so we should not use the fact that the inputs are opposite in this case:\n let mut result_is_infinity =\n infinity_predicate & (!point1.is_infinite & !point2.is_infinite);\n // However, if both of them are at infinity, then the result is also at infinity\n result.is_infinite = result_is_infinity | (point1.is_infinite & point2.is_infinite);\n result\n }\n}\n\n#[foreign(embedded_curve_add)]\nfn embedded_curve_add_array_return(\n _point1: EmbeddedCurvePoint,\n _point2: EmbeddedCurvePoint,\n _predicate: bool,\n) -> [EmbeddedCurvePoint; 1] {}\n\n/// This function assumes that:\n/// The points are on the curve, and\n/// The points don't share an x-coordinate, and\n/// Neither point is the infinity point.\n/// If it is used with correct input, the function ensures the correct non-zero result is returned.\n/// Except for points on the curve, the other assumptions are checked by the function. It will cause assertion failure if they are not respected.\npub fn embedded_curve_add_not_nul(\n point1: EmbeddedCurvePoint,\n point2: EmbeddedCurvePoint,\n) -> EmbeddedCurvePoint {\n assert(point1.x != point2.x);\n assert(!point1.is_infinite);\n assert(!point2.is_infinite);\n // Ensure is_infinite is comptime\n let point1_1 = EmbeddedCurvePoint { x: point1.x, y: point1.y, is_infinite: false };\n let point2_1 = EmbeddedCurvePoint { x: point2.x, y: point2.y, is_infinite: false };\n embedded_curve_add_unsafe(point1_1, point2_1)\n}\n\n/// Unsafe ec addition\n/// If the inputs are the same, it will perform a doubling, but only if point1 and point2 are the same variable.\n/// If they have the same value but are different variables, the result will be incorrect because in this case\n/// it assumes (but does not check) that the points' x-coordinates are not equal.\n/// It also assumes neither point is the infinity point.\npub fn embedded_curve_add_unsafe(\n point1: EmbeddedCurvePoint,\n point2: EmbeddedCurvePoint,\n) -> EmbeddedCurvePoint {\n embedded_curve_add_array_return(point1, point2, true)[0]\n}\n", path: "std/embedded_curve_ops.nr" }, "17": { source: `use crate::field::field_less_than;
3927
- use crate::runtime::is_unconstrained;
3928
-
3929
- // The low and high decomposition of the field modulus
3930
- global PLO: Field = 53438638232309528389504892708671455233;
3931
- global PHI: Field = 64323764613183177041862057485226039389;
3932
-
3933
- pub(crate) global TWO_POW_128: Field = 0x100000000000000000000000000000000;
3934
-
3935
- // Decomposes a single field into two 16 byte fields.
3936
- fn compute_decomposition(x: Field) -> (Field, Field) {
3937
- // Here's we're taking advantage of truncating 128 bit limbs from the input field
3938
- // and then subtracting them from the input such the field division is equivalent to integer division.
3939
- let low = (x as u128) as Field;
3940
- let high = (x - low) / TWO_POW_128;
3941
-
3942
- (low, high)
3943
- }
3944
-
3945
- pub(crate) unconstrained fn decompose_hint(x: Field) -> (Field, Field) {
3946
- compute_decomposition(x)
3947
- }
3948
-
3949
- unconstrained fn lte_hint(x: Field, y: Field) -> bool {
3950
- if x == y {
3951
- true
3952
- } else {
3953
- field_less_than(x, y)
3954
- }
3955
- }
3956
-
3957
- // Assert that (alo > blo && ahi >= bhi) || (alo <= blo && ahi > bhi)
3958
- fn assert_gt_limbs(a: (Field, Field), b: (Field, Field)) {
3959
- let (alo, ahi) = a;
3960
- let (blo, bhi) = b;
3961
- // Safety: borrow is enforced to be boolean due to its type.
3962
- // if borrow is 0, it asserts that (alo > blo && ahi >= bhi)
3963
- // if borrow is 1, it asserts that (alo <= blo && ahi > bhi)
3964
- unsafe {
3965
- let borrow = lte_hint(alo, blo);
3966
-
3967
- let rlo = alo - blo - 1 + (borrow as Field) * TWO_POW_128;
3968
- let rhi = ahi - bhi - (borrow as Field);
3969
-
3970
- rlo.assert_max_bit_size::<128>();
3971
- rhi.assert_max_bit_size::<128>();
3972
- }
3973
- }
3974
-
3975
- /// Decompose a single field into two 16 byte fields.
3976
- pub fn decompose(x: Field) -> (Field, Field) {
3977
- if is_unconstrained() {
3978
- compute_decomposition(x)
3979
- } else {
3980
- // Safety: decomposition is properly checked below
3981
- unsafe {
3982
- // Take hints of the decomposition
3983
- let (xlo, xhi) = decompose_hint(x);
3984
-
3985
- // Range check the limbs
3986
- xlo.assert_max_bit_size::<128>();
3987
- xhi.assert_max_bit_size::<128>();
3988
-
3989
- // Check that the decomposition is correct
3990
- assert_eq(x, xlo + TWO_POW_128 * xhi);
3991
-
3992
- // Assert that the decomposition of P is greater than the decomposition of x
3993
- assert_gt_limbs((PLO, PHI), (xlo, xhi));
3994
- (xlo, xhi)
3995
- }
3996
- }
3997
- }
3998
-
3999
- pub fn assert_gt(a: Field, b: Field) {
4000
- if is_unconstrained() {
4001
- assert(
4002
- // Safety: already unconstrained
4003
- unsafe { field_less_than(b, a) },
4004
- );
4005
- } else {
4006
- // Decompose a and b
4007
- let a_limbs = decompose(a);
4008
- let b_limbs = decompose(b);
4009
-
4010
- // Assert that a_limbs is greater than b_limbs
4011
- assert_gt_limbs(a_limbs, b_limbs)
4012
- }
4013
- }
4014
-
4015
- pub fn assert_lt(a: Field, b: Field) {
4016
- assert_gt(b, a);
4017
- }
4018
-
4019
- pub fn gt(a: Field, b: Field) -> bool {
4020
- if is_unconstrained() {
4021
- // Safety: unsafe in unconstrained
4022
- unsafe {
4023
- field_less_than(b, a)
4024
- }
4025
- } else if a == b {
4026
- false
4027
- } else {
4028
- // Safety: Take a hint of the comparison and verify it
4029
- unsafe {
4030
- if field_less_than(a, b) {
4031
- assert_gt(b, a);
4032
- false
4033
- } else {
4034
- assert_gt(a, b);
4035
- true
4036
- }
4037
- }
4038
- }
4039
- }
4040
-
4041
- pub fn lt(a: Field, b: Field) -> bool {
4042
- gt(b, a)
4043
- }
4044
-
4045
- mod tests {
4046
- // TODO: Allow imports from "super"
4047
- use crate::field::bn254::{assert_gt, decompose, gt, lt, lte_hint, PHI, PLO, TWO_POW_128};
4048
-
4049
- #[test]
4050
- fn check_decompose() {
4051
- assert_eq(decompose(TWO_POW_128), (0, 1));
4052
- assert_eq(decompose(TWO_POW_128 + 0x1234567890), (0x1234567890, 1));
4053
- assert_eq(decompose(0x1234567890), (0x1234567890, 0));
4054
- }
4055
-
4056
- #[test]
4057
- unconstrained fn check_lte_hint() {
4058
- assert(lte_hint(0, 1));
4059
- assert(lte_hint(0, 0x100));
4060
- assert(lte_hint(0x100, TWO_POW_128 - 1));
4061
- assert(!lte_hint(0 - 1, 0));
4062
-
4063
- assert(lte_hint(0, 0));
4064
- assert(lte_hint(0x100, 0x100));
4065
- assert(lte_hint(0 - 1, 0 - 1));
4066
- }
4067
-
4068
- #[test]
4069
- fn check_gt() {
4070
- assert(gt(1, 0));
4071
- assert(gt(0x100, 0));
4072
- assert(gt((0 - 1), (0 - 2)));
4073
- assert(gt(TWO_POW_128, 0));
4074
- assert(!gt(0, 0));
4075
- assert(!gt(0, 0x100));
4076
- assert(gt(0 - 1, 0 - 2));
4077
- assert(!gt(0 - 2, 0 - 1));
4078
- assert_gt(0 - 1, 0);
4079
- }
4080
-
4081
- #[test]
4082
- fn check_plo_phi() {
4083
- assert_eq(PLO + PHI * TWO_POW_128, 0);
4084
- let p_bytes = crate::field::modulus_le_bytes();
4085
- let mut p_low: Field = 0;
4086
- let mut p_high: Field = 0;
4087
-
4088
- let mut offset = 1;
4089
- for i in 0..16 {
4090
- p_low += (p_bytes[i] as Field) * offset;
4091
- p_high += (p_bytes[i + 16] as Field) * offset;
4092
- offset *= 256;
4093
- }
4094
- assert_eq(p_low, PLO);
4095
- assert_eq(p_high, PHI);
4096
- }
4097
-
4098
- #[test]
4099
- fn check_decompose_edge_cases() {
4100
- assert_eq(decompose(0), (0, 0));
4101
- assert_eq(decompose(TWO_POW_128 - 1), (TWO_POW_128 - 1, 0));
4102
- assert_eq(decompose(TWO_POW_128 + 1), (1, 1));
4103
- assert_eq(decompose(TWO_POW_128 * 2), (0, 2));
4104
- assert_eq(decompose(TWO_POW_128 * 2 + 0x1234567890), (0x1234567890, 2));
4105
- }
4106
-
4107
- #[test]
4108
- fn check_decompose_large_values() {
4109
- let large_field = 0xffffffffffffffff;
4110
- let (lo, hi) = decompose(large_field);
4111
- assert_eq(large_field, lo + TWO_POW_128 * hi);
4112
-
4113
- let large_value = large_field - TWO_POW_128;
4114
- let (lo2, hi2) = decompose(large_value);
4115
- assert_eq(large_value, lo2 + TWO_POW_128 * hi2);
4116
- }
4117
-
4118
- #[test]
4119
- fn check_lt_comprehensive() {
4120
- assert(lt(0, 1));
4121
- assert(!lt(1, 0));
4122
- assert(!lt(0, 0));
4123
- assert(!lt(42, 42));
4124
-
4125
- assert(lt(TWO_POW_128 - 1, TWO_POW_128));
4126
- assert(!lt(TWO_POW_128, TWO_POW_128 - 1));
4127
- }
4128
- }
4129
- `, path: "std/field/bn254.nr" }, "19": { source: '// Exposed only for usage in `std::meta`\npub(crate) mod poseidon2;\n\nuse crate::default::Default;\nuse crate::embedded_curve_ops::{\n EmbeddedCurvePoint, EmbeddedCurveScalar, multi_scalar_mul, multi_scalar_mul_array_return,\n};\nuse crate::meta::derive_via;\n\n#[foreign(sha256_compression)]\n// docs:start:sha256_compression\npub fn sha256_compression(input: [u32; 16], state: [u32; 8]) -> [u32; 8] {}\n// docs:end:sha256_compression\n\n#[foreign(keccakf1600)]\n// docs:start:keccakf1600\npub fn keccakf1600(input: [u64; 25]) -> [u64; 25] {}\n// docs:end:keccakf1600\n\npub mod keccak {\n #[deprecated("This function has been moved to std::hash::keccakf1600")]\n pub fn keccakf1600(input: [u64; 25]) -> [u64; 25] {\n super::keccakf1600(input)\n }\n}\n\n#[foreign(blake2s)]\n// docs:start:blake2s\npub fn blake2s<let N: u32>(input: [u8; N]) -> [u8; 32]\n// docs:end:blake2s\n{}\n\n// docs:start:blake3\npub fn blake3<let N: u32>(input: [u8; N]) -> [u8; 32]\n// docs:end:blake3\n{\n if crate::runtime::is_unconstrained() {\n // Temporary measure while Barretenberg is main proving system.\n // Please open an issue if you\'re working on another proving system and running into problems due to this.\n crate::static_assert(\n N <= 1024,\n "Barretenberg cannot prove blake3 hashes with inputs larger than 1024 bytes",\n );\n }\n __blake3(input)\n}\n\n#[foreign(blake3)]\nfn __blake3<let N: u32>(input: [u8; N]) -> [u8; 32] {}\n\n// docs:start:pedersen_commitment\npub fn pedersen_commitment<let N: u32>(input: [Field; N]) -> EmbeddedCurvePoint {\n // docs:end:pedersen_commitment\n pedersen_commitment_with_separator(input, 0)\n}\n\n#[inline_always]\npub fn pedersen_commitment_with_separator<let N: u32>(\n input: [Field; N],\n separator: u32,\n) -> EmbeddedCurvePoint {\n let mut points = [EmbeddedCurveScalar { lo: 0, hi: 0 }; N];\n for i in 0..N {\n // we use the unsafe version because the multi_scalar_mul will constrain the scalars.\n points[i] = from_field_unsafe(input[i]);\n }\n let generators = derive_generators("DEFAULT_DOMAIN_SEPARATOR".as_bytes(), separator);\n multi_scalar_mul(generators, points)\n}\n\n// docs:start:pedersen_hash\npub fn pedersen_hash<let N: u32>(input: [Field; N]) -> Field\n// docs:end:pedersen_hash\n{\n pedersen_hash_with_separator(input, 0)\n}\n\n#[no_predicates]\npub fn pedersen_hash_with_separator<let N: u32>(input: [Field; N], separator: u32) -> Field {\n let mut scalars: [EmbeddedCurveScalar; N + 1] = [EmbeddedCurveScalar { lo: 0, hi: 0 }; N + 1];\n let mut generators: [EmbeddedCurvePoint; N + 1] =\n [EmbeddedCurvePoint::point_at_infinity(); N + 1];\n let domain_generators: [EmbeddedCurvePoint; N] =\n derive_generators("DEFAULT_DOMAIN_SEPARATOR".as_bytes(), separator);\n\n for i in 0..N {\n scalars[i] = from_field_unsafe(input[i]);\n generators[i] = domain_generators[i];\n }\n scalars[N] = EmbeddedCurveScalar { lo: N as Field, hi: 0 as Field };\n\n let length_generator: [EmbeddedCurvePoint; 1] =\n derive_generators("pedersen_hash_length".as_bytes(), 0);\n generators[N] = length_generator[0];\n multi_scalar_mul_array_return(generators, scalars, true)[0].x\n}\n\n#[field(bn254)]\n#[inline_always]\npub fn derive_generators<let N: u32, let M: u32>(\n domain_separator_bytes: [u8; M],\n starting_index: u32,\n) -> [EmbeddedCurvePoint; N] {\n crate::assert_constant(domain_separator_bytes);\n // TODO(https://github.com/noir-lang/noir/issues/5672): Add back assert_constant on starting_index\n __derive_generators(domain_separator_bytes, starting_index)\n}\n\n#[builtin(derive_pedersen_generators)]\n#[field(bn254)]\nfn __derive_generators<let N: u32, let M: u32>(\n domain_separator_bytes: [u8; M],\n starting_index: u32,\n) -> [EmbeddedCurvePoint; N] {}\n\n#[field(bn254)]\n// Same as from_field but:\n// does not assert the limbs are 128 bits\n// does not assert the decomposition does not overflow the EmbeddedCurveScalar\nfn from_field_unsafe(scalar: Field) -> EmbeddedCurveScalar {\n // Safety: xlo and xhi decomposition is checked below\n let (xlo, xhi) = unsafe { crate::field::bn254::decompose_hint(scalar) };\n // Check that the decomposition is correct\n assert_eq(scalar, xlo + crate::field::bn254::TWO_POW_128 * xhi);\n EmbeddedCurveScalar { lo: xlo, hi: xhi }\n}\n\npub fn poseidon2_permutation<let N: u32>(input: [Field; N], state_len: u32) -> [Field; N] {\n assert_eq(input.len(), state_len);\n poseidon2_permutation_internal(input)\n}\n\n#[foreign(poseidon2_permutation)]\nfn poseidon2_permutation_internal<let N: u32>(input: [Field; N]) -> [Field; N] {}\n\n// Generic hashing support.\n// Partially ported and impacted by rust.\n\n// Hash trait shall be implemented per type.\n#[derive_via(derive_hash)]\npub trait Hash {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher;\n}\n\n// docs:start:derive_hash\ncomptime fn derive_hash(s: TypeDefinition) -> Quoted {\n let name = quote { $crate::hash::Hash };\n let signature = quote { fn hash<H>(_self: Self, _state: &mut H) where H: $crate::hash::Hasher };\n let for_each_field = |name| quote { _self.$name.hash(_state); };\n crate::meta::make_trait_impl(\n s,\n name,\n signature,\n for_each_field,\n quote {},\n |fields| fields,\n )\n}\n// docs:end:derive_hash\n\n// Hasher trait shall be implemented by algorithms to provide hash-agnostic means.\n// TODO: consider making the types generic here ([u8], [Field], etc.)\npub trait Hasher {\n fn finish(self) -> Field;\n\n fn write(&mut self, input: Field);\n}\n\n// BuildHasher is a factory trait, responsible for production of specific Hasher.\npub trait BuildHasher {\n type H: Hasher;\n\n fn build_hasher(self) -> H;\n}\n\npub struct BuildHasherDefault<H>;\n\nimpl<H> BuildHasher for BuildHasherDefault<H>\nwhere\n H: Hasher + Default,\n{\n type H = H;\n\n fn build_hasher(_self: Self) -> H {\n H::default()\n }\n}\n\nimpl<H> Default for BuildHasherDefault<H>\nwhere\n H: Hasher + Default,\n{\n fn default() -> Self {\n BuildHasherDefault {}\n }\n}\n\nimpl Hash for Field {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self);\n }\n}\n\nimpl Hash for u1 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u8 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u16 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u32 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u64 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for u128 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for i8 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as u8 as Field);\n }\n}\n\nimpl Hash for i16 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as u16 as Field);\n }\n}\n\nimpl Hash for i32 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as u32 as Field);\n }\n}\n\nimpl Hash for i64 {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as u64 as Field);\n }\n}\n\nimpl Hash for bool {\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n H::write(state, self as Field);\n }\n}\n\nimpl Hash for () {\n fn hash<H>(_self: Self, _state: &mut H)\n where\n H: Hasher,\n {}\n}\n\nimpl<T, let N: u32> Hash for [T; N]\nwhere\n T: Hash,\n{\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl<T> Hash for [T]\nwhere\n T: Hash,\n{\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n self.len().hash(state);\n for elem in self {\n elem.hash(state);\n }\n }\n}\n\nimpl<A, B> Hash for (A, B)\nwhere\n A: Hash,\n B: Hash,\n{\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n self.0.hash(state);\n self.1.hash(state);\n }\n}\n\nimpl<A, B, C> Hash for (A, B, C)\nwhere\n A: Hash,\n B: Hash,\n C: Hash,\n{\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n }\n}\n\nimpl<A, B, C, D> Hash for (A, B, C, D)\nwhere\n A: Hash,\n B: Hash,\n C: Hash,\n D: Hash,\n{\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n }\n}\n\nimpl<A, B, C, D, E> Hash for (A, B, C, D, E)\nwhere\n A: Hash,\n B: Hash,\n C: Hash,\n D: Hash,\n E: Hash,\n{\n fn hash<H>(self, state: &mut H)\n where\n H: Hasher,\n {\n self.0.hash(state);\n self.1.hash(state);\n self.2.hash(state);\n self.3.hash(state);\n self.4.hash(state);\n }\n}\n\n// Some test vectors for Pedersen hash and Pedersen Commitment.\n// They have been generated using the same functions so the tests are for now useless\n// but they will be useful when we switch to Noir implementation.\n#[test]\nfn assert_pedersen() {\n assert_eq(\n pedersen_hash_with_separator([1], 1),\n 0x1b3f4b1a83092a13d8d1a59f7acb62aba15e7002f4440f2275edb99ebbc2305f,\n );\n assert_eq(\n pedersen_commitment_with_separator([1], 1),\n EmbeddedCurvePoint {\n x: 0x054aa86a73cb8a34525e5bbed6e43ba1198e860f5f3950268f71df4591bde402,\n y: 0x209dcfbf2cfb57f9f6046f44d71ac6faf87254afc7407c04eb621a6287cac126,\n is_infinite: false,\n },\n );\n\n assert_eq(\n pedersen_hash_with_separator([1, 2], 2),\n 0x26691c129448e9ace0c66d11f0a16d9014a9e8498ee78f4d69f0083168188255,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2], 2),\n EmbeddedCurvePoint {\n x: 0x2e2b3b191e49541fe468ec6877721d445dcaffe41728df0a0eafeb15e87b0753,\n y: 0x2ff4482400ad3a6228be17a2af33e2bcdf41be04795f9782bd96efe7e24f8778,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3], 3),\n 0x0bc694b7a1f8d10d2d8987d07433f26bd616a2d351bc79a3c540d85b6206dbe4,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3], 3),\n EmbeddedCurvePoint {\n x: 0x1fee4e8cf8d2f527caa2684236b07c4b1bad7342c01b0f75e9a877a71827dc85,\n y: 0x2f9fedb9a090697ab69bf04c8bc15f7385b3e4b68c849c1536e5ae15ff138fd1,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4], 4),\n 0xdae10fb32a8408521803905981a2b300d6a35e40e798743e9322b223a5eddc,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4], 4),\n EmbeddedCurvePoint {\n x: 0x07ae3e202811e1fca39c2d81eabe6f79183978e6f12be0d3b8eda095b79bdbc9,\n y: 0x0afc6f892593db6fbba60f2da558517e279e0ae04f95758587760ba193145014,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4, 5], 5),\n 0xfc375b062c4f4f0150f7100dfb8d9b72a6d28582dd9512390b0497cdad9c22,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4, 5], 5),\n EmbeddedCurvePoint {\n x: 0x1754b12bd475a6984a1094b5109eeca9838f4f81ac89c5f0a41dbce53189bb29,\n y: 0x2da030e3cfcdc7ddad80eaf2599df6692cae0717d4e9f7bfbee8d073d5d278f7,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4, 5, 6], 6),\n 0x1696ed13dc2730062a98ac9d8f9de0661bb98829c7582f699d0273b18c86a572,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4, 5, 6], 6),\n EmbeddedCurvePoint {\n x: 0x190f6c0e97ad83e1e28da22a98aae156da083c5a4100e929b77e750d3106a697,\n y: 0x1f4b60f34ef91221a0b49756fa0705da93311a61af73d37a0c458877706616fb,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4, 5, 6, 7], 7),\n 0x128c0ff144fc66b6cb60eeac8a38e23da52992fc427b92397a7dffd71c45ede3,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4, 5, 6, 7], 7),\n EmbeddedCurvePoint {\n x: 0x015441e9d29491b06563fac16fc76abf7a9534c715421d0de85d20dbe2965939,\n y: 0x1d2575b0276f4e9087e6e07c2cb75aa1baafad127af4be5918ef8a2ef2fea8fc,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4, 5, 6, 7, 8], 8),\n 0x2f960e117482044dfc99d12fece2ef6862fba9242be4846c7c9a3e854325a55c,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4, 5, 6, 7, 8], 8),\n EmbeddedCurvePoint {\n x: 0x1657737676968887fceb6dd516382ea13b3a2c557f509811cd86d5d1199bc443,\n y: 0x1f39f0cb569040105fa1e2f156521e8b8e08261e635a2b210bdc94e8d6d65f77,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4, 5, 6, 7, 8, 9], 9),\n 0x0c96db0790602dcb166cc4699e2d306c479a76926b81c2cb2aaa92d249ec7be7,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4, 5, 6, 7, 8, 9], 9),\n EmbeddedCurvePoint {\n x: 0x0a3ceae42d14914a432aa60ec7fded4af7dad7dd4acdbf2908452675ec67e06d,\n y: 0xfc19761eaaf621ad4aec9a8b2e84a4eceffdba78f60f8b9391b0bd9345a2f2,\n is_infinite: false,\n },\n );\n assert_eq(\n pedersen_hash_with_separator([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 10),\n 0x2cd37505871bc460a62ea1e63c7fe51149df5d0801302cf1cbc48beb8dff7e94,\n );\n assert_eq(\n pedersen_commitment_with_separator([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 10),\n EmbeddedCurvePoint {\n x: 0x2fb3f8b3d41ddde007c8c3c62550f9a9380ee546fcc639ffbb3fd30c8d8de30c,\n y: 0x300783be23c446b11a4c0fabf6c91af148937cea15fcf5fb054abf7f752ee245,\n is_infinite: false,\n },\n );\n}\n', path: "std/hash/mod.nr" }, "50": { source: `/// Fulfillment Proof Circuit
4130
- ///
4131
- /// Proves: "The solver correctly executed the intent and delivered
4132
- /// the required output to the recipient, without revealing execution
4133
- /// path, liquidity sources, or intermediate transactions."
4134
- ///
4135
- /// @see docs/specs/FULFILLMENT-PROOF.md
4136
-
4137
- use std::hash::pedersen_hash;
4138
- use std::hash::pedersen_commitment;
4139
- use std::ecdsa_secp256k1::verify_signature;
4140
-
4141
- // --- Main Circuit ---
4142
-
4143
- /// Main fulfillment proof entry point
4144
- ///
4145
- /// Public inputs: intent_hash, output_commitment, recipient_stealth,
4146
- /// min_output_amount, solver_id, fulfillment_time, expiry
4147
- /// Private inputs: output_amount, output_blinding, attestation data, solver_secret
4148
- ///
4149
- /// Constraints:
4150
- /// 1. output_amount >= min_output_amount (range proof via u64)
4151
- /// 2. output_commitment = Pedersen(output_amount, output_blinding)
4152
- /// 3. Oracle attestation is valid and matches claimed values
4153
- /// 4. Solver is authorized (solver_id derived from solver_secret)
4154
- /// 5. fulfillment_time <= expiry
4155
- pub fn main(
4156
- // Public inputs
4157
- intent_hash: pub Field,
4158
- output_commitment_x: pub Field,
4159
- output_commitment_y: pub Field,
4160
- recipient_stealth: pub Field,
4161
- min_output_amount: pub u64,
4162
- solver_id: pub Field,
4163
- fulfillment_time: pub u64,
4164
- expiry: pub u64,
4165
-
4166
- // Private inputs
4167
- output_amount: u64,
4168
- output_blinding: Field,
4169
- solver_secret: Field,
4170
-
4171
- // Oracle attestation (private)
4172
- attestation_recipient: Field,
4173
- attestation_amount: u64,
4174
- attestation_tx_hash: Field,
4175
- attestation_block: u64,
4176
- oracle_signature: [u8; 64],
4177
- oracle_message_hash: [u8; 32],
4178
- oracle_pub_key_x: [u8; 32],
4179
- oracle_pub_key_y: [u8; 32],
4180
- ) {
4181
- // Constraint 1: Output meets minimum requirement
4182
- // Range proof is implicit via u64 type comparison
4183
- assert(output_amount >= min_output_amount, "Output below minimum");
4184
-
4185
- // Constraint 2: Output commitment is valid
4186
- // C = Pedersen(output_amount, output_blinding)
4187
- let commitment = pedersen_commitment([output_amount as Field, output_blinding]);
4188
- assert(commitment.x == output_commitment_x, "Commitment X mismatch");
4189
- assert(commitment.y == output_commitment_y, "Commitment Y mismatch");
4190
-
4191
- // Constraint 3a: Attestation matches claimed values
4192
- assert(attestation_recipient == recipient_stealth, "Recipient mismatch in attestation");
4193
- assert(attestation_amount == output_amount, "Amount mismatch in attestation");
4194
-
4195
- // Constraint 3b: Oracle signature is valid
4196
- let valid_attestation = verify_signature(
4197
- oracle_pub_key_x,
4198
- oracle_pub_key_y,
4199
- oracle_signature,
4200
- oracle_message_hash
4201
- );
4202
- assert(valid_attestation, "Invalid oracle attestation signature");
4203
-
4204
- // Constraint 4: Solver authorization
4205
- // solver_id = pedersen_hash(solver_secret)
4206
- let computed_solver_id = pedersen_hash([solver_secret]);
4207
- assert(computed_solver_id == solver_id, "Unauthorized solver");
4208
-
4209
- // Constraint 5: Time constraint
4210
- assert(fulfillment_time <= expiry, "Fulfillment after expiry");
4211
-
4212
- // Intent hash binding (ensures this proof is for this specific intent)
4213
- // The intent_hash is a public input, binding this proof to the intent
4214
- // No additional constraint needed - it's enforced by the verifier checking public inputs
4215
- let _ = intent_hash;
4216
-
4217
- // Attestation metadata (tx_hash and block) are included for auditability
4218
- // but not strictly constrained in circuit (oracle signature covers them)
4219
- let _ = attestation_tx_hash;
4220
- let _ = attestation_block;
4221
- }
4222
-
4223
- // --- Tests ---
4224
-
4225
- #[test]
4226
- fn test_output_commitment() {
4227
- // Test that commitment is correctly computed
4228
- let output_amount: u64 = 1000000;
4229
- let output_blinding: Field = 0x123456789;
4230
-
4231
- let commitment1 = pedersen_commitment([output_amount as Field, output_blinding]);
4232
- let commitment2 = pedersen_commitment([output_amount as Field, output_blinding]);
4233
-
4234
- // Commitment should be deterministic
4235
- assert(commitment1.x == commitment2.x, "Commitment X should be deterministic");
4236
- assert(commitment1.y == commitment2.y, "Commitment Y should be deterministic");
4237
- }
4238
-
4239
- #[test]
4240
- fn test_solver_authorization() {
4241
- // Test solver_id derivation
4242
- let solver_secret: Field = 0x1234567890ABCDEF;
4243
-
4244
- let solver_id1 = pedersen_hash([solver_secret]);
4245
- let solver_id2 = pedersen_hash([solver_secret]);
4246
-
4247
- // Solver ID should be deterministic
4248
- assert(solver_id1 == solver_id2, "Solver ID should be deterministic");
4249
-
4250
- // Different secret should give different solver_id
4251
- let different_secret: Field = 0xFEDCBA0987654321;
4252
- let different_id = pedersen_hash([different_secret]);
4253
- assert(solver_id1 != different_id, "Different secrets should give different solver IDs");
4254
- }
4255
-
4256
- #[test]
4257
- fn test_range_proof_passes() {
4258
- // Test that output >= min passes
4259
- let output_amount: u64 = 1050000;
4260
- let min_output_amount: u64 = 1000000;
4261
-
4262
- assert(output_amount >= min_output_amount, "Output should be >= minimum");
4263
- }
4264
-
4265
- #[test]
4266
- fn test_time_constraint_valid() {
4267
- // Test valid time constraint
4268
- let fulfillment_time: u64 = 1732650000;
4269
- let expiry: u64 = 1732686400;
4270
-
4271
- assert(fulfillment_time <= expiry, "Fulfillment time should be <= expiry");
4272
- }
4273
-
4274
- #[test]
4275
- fn test_time_constraint_edge_case() {
4276
- // Edge case: exactly at expiry should be valid
4277
- let fulfillment_time: u64 = 1732686400;
4278
- let expiry: u64 = 1732686400;
4279
-
4280
- assert(fulfillment_time <= expiry, "Fulfillment at exactly expiry should be valid");
4281
- }
4282
-
4283
- // NOTE: Full integration tests with ECDSA oracle signatures require TypeScript SDK
4284
- // The NoirProofProvider will generate valid oracle signature test vectors
4285
- `, path: "/Users/rz/local-dev/sip-protocol/packages/circuits/fulfillment_proof/src/main.nr" } }, expression_width: { Bounded: { width: 4 } } };
4286
-
4287
- // src/proofs/noir.ts
4288
- var NoirProofProvider = class {
4289
- framework = "noir";
4290
- _isReady = false;
4291
- config;
4292
- // Circuit instances
4293
- fundingNoir = null;
4294
- fundingBackend = null;
4295
- validityNoir = null;
4296
- validityBackend = null;
4297
- fulfillmentNoir = null;
4298
- fulfillmentBackend = null;
4299
- constructor(config = {}) {
4300
- this.config = {
4301
- backend: "barretenberg",
4302
- verbose: false,
4303
- ...config
4304
- };
4305
- }
4306
- get isReady() {
4307
- return this._isReady;
4308
- }
4309
- /**
4310
- * Derive secp256k1 public key coordinates from a private key
4311
- *
4312
- * Utility method that can be used to generate public key coordinates
4313
- * for use in ValidityProofParams.senderPublicKey or NoirProviderConfig.oraclePublicKey
4314
- *
4315
- * @param privateKey - 32-byte private key
4316
- * @returns X and Y coordinates as 32-byte arrays
4317
- *
4318
- * @example
4319
- * ```typescript
4320
- * const privateKey = new Uint8Array(32).fill(1) // Your secret key
4321
- * const publicKey = NoirProofProvider.derivePublicKey(privateKey)
4322
- *
4323
- * // Use for oracle configuration
4324
- * const provider = new NoirProofProvider({
4325
- * oraclePublicKey: publicKey
4326
- * })
4327
- *
4328
- * // Or use for validity proof params
4329
- * const validityParams = {
4330
- * // ... other params
4331
- * senderPublicKey: {
4332
- * x: new Uint8Array(publicKey.x),
4333
- * y: new Uint8Array(publicKey.y)
4334
- * }
4335
- * }
4336
- * ```
4337
- */
4338
- static derivePublicKey(privateKey) {
4339
- const uncompressedPubKey = import_secp256k13.secp256k1.getPublicKey(privateKey, false);
4340
- const x = Array.from(uncompressedPubKey.slice(1, 33));
4341
- const y = Array.from(uncompressedPubKey.slice(33, 65));
4342
- return { x, y };
4343
- }
4344
- /**
4345
- * Initialize the Noir provider
4346
- *
4347
- * Loads circuit artifacts and initializes the proving backend.
4348
- */
4349
- async initialize() {
4350
- if (this._isReady) {
4351
- return;
4352
- }
4353
- try {
4354
- if (this.config.verbose) {
4355
- console.log("[NoirProofProvider] Initializing...");
4356
- }
4357
- const fundingCircuit = funding_proof_default;
4358
- this.fundingBackend = new import_bb.UltraHonkBackend(fundingCircuit.bytecode);
4359
- this.fundingNoir = new import_noir_js.Noir(fundingCircuit);
4360
- if (this.config.verbose) {
4361
- console.log("[NoirProofProvider] Funding circuit loaded");
4362
- const artifactVersion = funding_proof_default.noir_version;
4363
- console.log(`[NoirProofProvider] Noir version: ${artifactVersion ?? "unknown"}`);
4364
- }
4365
- const validityCircuit = validity_proof_default;
4366
- this.validityBackend = new import_bb.UltraHonkBackend(validityCircuit.bytecode);
4367
- this.validityNoir = new import_noir_js.Noir(validityCircuit);
4368
- if (this.config.verbose) {
4369
- console.log("[NoirProofProvider] Validity circuit loaded");
4370
- }
4371
- const fulfillmentCircuit = fulfillment_proof_default;
4372
- this.fulfillmentBackend = new import_bb.UltraHonkBackend(fulfillmentCircuit.bytecode);
4373
- this.fulfillmentNoir = new import_noir_js.Noir(fulfillmentCircuit);
4374
- if (this.config.verbose) {
4375
- console.log("[NoirProofProvider] Fulfillment circuit loaded");
4376
- }
4377
- this._isReady = true;
4378
- if (this.config.verbose) {
4379
- console.log("[NoirProofProvider] Initialization complete");
4380
- }
4381
- } catch (error) {
4382
- throw new ProofError(
4383
- `Failed to initialize NoirProofProvider: ${error instanceof Error ? error.message : String(error)}`,
4384
- "SIP_4003" /* PROOF_NOT_IMPLEMENTED */,
4385
- { context: { error } }
4386
- );
4387
- }
4388
- }
4389
- /**
4390
- * Generate a Funding Proof using Noir circuits
4391
- *
4392
- * Proves: balance >= minimumRequired without revealing balance
4393
- *
4394
- * @see docs/specs/FUNDING-PROOF.md
4395
- */
4396
- async generateFundingProof(params) {
4397
- this.ensureReady();
4398
- if (!this.fundingNoir || !this.fundingBackend) {
4399
- throw new ProofGenerationError(
4400
- "funding",
4401
- "Funding circuit not initialized"
4402
- );
4403
- }
4404
- try {
4405
- if (this.config.verbose) {
4406
- console.log("[NoirProofProvider] Generating funding proof...");
4407
- }
4408
- const { commitmentHash, blindingField } = await this.computeCommitmentHash(
4409
- params.balance,
4410
- params.blindingFactor,
4411
- params.assetId
4412
- );
4413
- const witnessInputs = {
4414
- // Public inputs
4415
- commitment_hash: commitmentHash,
4416
- minimum_required: params.minimumRequired.toString(),
4417
- asset_id: this.assetIdToField(params.assetId),
4418
- // Private inputs
4419
- balance: params.balance.toString(),
4420
- blinding: blindingField
4421
- };
4422
- if (this.config.verbose) {
4423
- console.log("[NoirProofProvider] Witness inputs:", {
4424
- commitment_hash: commitmentHash,
4425
- minimum_required: params.minimumRequired.toString(),
4426
- asset_id: this.assetIdToField(params.assetId),
4427
- balance: "[PRIVATE]",
4428
- blinding: "[PRIVATE]"
4429
- });
4430
- }
4431
- const { witness } = await this.fundingNoir.execute(witnessInputs);
4432
- if (this.config.verbose) {
4433
- console.log("[NoirProofProvider] Witness generated, creating proof...");
4434
- }
4435
- const proofData = await this.fundingBackend.generateProof(witness);
4436
- if (this.config.verbose) {
4437
- console.log("[NoirProofProvider] Proof generated successfully");
4438
- }
4439
- const publicInputs = [
4440
- `0x${commitmentHash}`,
4441
- `0x${params.minimumRequired.toString(16).padStart(16, "0")}`,
4442
- `0x${this.assetIdToField(params.assetId)}`
4443
- ];
4444
- const proof = {
4445
- type: "funding",
4446
- proof: `0x${Buffer.from(proofData.proof).toString("hex")}`,
4447
- publicInputs
4448
- };
4449
- return {
4450
- proof,
4451
- publicInputs
4452
- };
4453
- } catch (error) {
4454
- const message = error instanceof Error ? error.message : String(error);
4455
- if (message.includes("Insufficient balance")) {
4456
- throw new ProofGenerationError(
4457
- "funding",
4458
- "Insufficient balance to generate proof",
4459
- error instanceof Error ? error : void 0
4460
- );
4461
- }
4462
- if (message.includes("Commitment hash mismatch")) {
4463
- throw new ProofGenerationError(
4464
- "funding",
4465
- "Commitment hash verification failed",
4466
- error instanceof Error ? error : void 0
4467
- );
4468
- }
4469
- throw new ProofGenerationError(
4470
- "funding",
4471
- `Failed to generate funding proof: ${message}`,
4472
- error instanceof Error ? error : void 0
4473
- );
4474
- }
4475
- }
4476
- /**
4477
- * Generate a Validity Proof using Noir circuits
4478
- *
4479
- * Proves: Intent is authorized by sender without revealing identity
4480
- *
4481
- * @see docs/specs/VALIDITY-PROOF.md
4482
- */
4483
- async generateValidityProof(params) {
4484
- this.ensureReady();
4485
- if (!this.validityNoir || !this.validityBackend) {
4486
- throw new ProofGenerationError(
4487
- "validity",
4488
- "Validity circuit not initialized"
4489
- );
4490
- }
4491
- try {
4492
- if (this.config.verbose) {
4493
- console.log("[NoirProofProvider] Generating validity proof...");
4494
- }
4495
- const intentHashField = this.hexToField(params.intentHash);
4496
- const senderAddressField = this.hexToField(params.senderAddress);
4497
- const senderBlindingField = this.bytesToField(params.senderBlinding);
4498
- const senderSecretField = this.bytesToField(params.senderSecret);
4499
- const nonceField = this.bytesToField(params.nonce);
4500
- const { commitmentX, commitmentY } = await this.computeSenderCommitment(
4501
- senderAddressField,
4502
- senderBlindingField
4503
- );
4504
- const nullifier = await this.computeNullifier(
4505
- senderSecretField,
4506
- intentHashField,
4507
- nonceField
4508
- );
4509
- const signature = Array.from(params.authorizationSignature);
4510
- const messageHash = this.fieldToBytes32(intentHashField);
4511
- let pubKeyX;
4512
- let pubKeyY;
4513
- if (params.senderPublicKey) {
4514
- pubKeyX = Array.from(params.senderPublicKey.x);
4515
- pubKeyY = Array.from(params.senderPublicKey.y);
4516
- } else {
4517
- const coords = this.getPublicKeyCoordinates(params.senderSecret);
4518
- pubKeyX = coords.x;
4519
- pubKeyY = coords.y;
4520
- }
4521
- const witnessInputs = {
4522
- // Public inputs
4523
- intent_hash: intentHashField,
4524
- sender_commitment_x: commitmentX,
4525
- sender_commitment_y: commitmentY,
4526
- nullifier,
4527
- timestamp: params.timestamp.toString(),
4528
- expiry: params.expiry.toString(),
4529
- // Private inputs
4530
- sender_address: senderAddressField,
4531
- sender_blinding: senderBlindingField,
4532
- sender_secret: senderSecretField,
4533
- pub_key_x: pubKeyX,
4534
- pub_key_y: pubKeyY,
4535
- signature,
4536
- message_hash: messageHash,
4537
- nonce: nonceField
4538
- };
4539
- if (this.config.verbose) {
4540
- console.log("[NoirProofProvider] Validity witness inputs:", {
4541
- intent_hash: intentHashField,
4542
- sender_commitment_x: commitmentX,
4543
- sender_commitment_y: commitmentY,
4544
- nullifier,
4545
- timestamp: params.timestamp,
4546
- expiry: params.expiry,
4547
- sender_address: "[PRIVATE]",
4548
- sender_blinding: "[PRIVATE]",
4549
- sender_secret: "[PRIVATE]",
4550
- signature: "[PRIVATE]"
4551
- });
4552
- }
4553
- const { witness } = await this.validityNoir.execute(witnessInputs);
4554
- if (this.config.verbose) {
4555
- console.log("[NoirProofProvider] Validity witness generated, creating proof...");
4556
- }
4557
- const proofData = await this.validityBackend.generateProof(witness);
4558
- if (this.config.verbose) {
4559
- console.log("[NoirProofProvider] Validity proof generated successfully");
4560
- }
4561
- const publicInputs = [
4562
- `0x${intentHashField}`,
4563
- `0x${commitmentX}`,
4564
- `0x${commitmentY}`,
4565
- `0x${nullifier}`,
4566
- `0x${params.timestamp.toString(16).padStart(16, "0")}`,
4567
- `0x${params.expiry.toString(16).padStart(16, "0")}`
4568
- ];
4569
- const proof = {
4570
- type: "validity",
4571
- proof: `0x${Buffer.from(proofData.proof).toString("hex")}`,
4572
- publicInputs
4573
- };
4574
- return {
4575
- proof,
4576
- publicInputs
4577
- };
4578
- } catch (error) {
4579
- const message = error instanceof Error ? error.message : String(error);
4580
- if (message.includes("Sender commitment")) {
4581
- throw new ProofGenerationError(
4582
- "validity",
4583
- "Sender commitment verification failed",
4584
- error instanceof Error ? error : void 0
4585
- );
4586
- }
4587
- if (message.includes("Invalid ECDSA")) {
4588
- throw new ProofGenerationError(
4589
- "validity",
4590
- "Authorization signature verification failed",
4591
- error instanceof Error ? error : void 0
4592
- );
4593
- }
4594
- if (message.includes("Nullifier mismatch")) {
4595
- throw new ProofGenerationError(
4596
- "validity",
4597
- "Nullifier derivation failed",
4598
- error instanceof Error ? error : void 0
4599
- );
4600
- }
4601
- if (message.includes("Intent expired")) {
4602
- throw new ProofGenerationError(
4603
- "validity",
4604
- "Intent has expired (timestamp >= expiry)",
4605
- error instanceof Error ? error : void 0
4606
- );
4607
- }
4608
- throw new ProofGenerationError(
4609
- "validity",
4610
- `Failed to generate validity proof: ${message}`,
4611
- error instanceof Error ? error : void 0
4612
- );
4613
- }
4614
- }
4615
- /**
4616
- * Generate a Fulfillment Proof using Noir circuits
4617
- *
4618
- * Proves: Solver correctly executed the intent and delivered the required
4619
- * output to the recipient, without revealing execution path or liquidity sources.
4620
- *
4621
- * @see docs/specs/FULFILLMENT-PROOF.md
4622
- */
4623
- async generateFulfillmentProof(params) {
4624
- this.ensureReady();
4625
- if (!this.fulfillmentNoir || !this.fulfillmentBackend) {
4626
- throw new ProofGenerationError(
4627
- "fulfillment",
4628
- "Fulfillment circuit not initialized"
4629
- );
4630
- }
4631
- try {
4632
- if (this.config.verbose) {
4633
- console.log("[NoirProofProvider] Generating fulfillment proof...");
4634
- }
4635
- const intentHashField = this.hexToField(params.intentHash);
4636
- const recipientStealthField = this.hexToField(params.recipientStealth);
4637
- const { commitmentX, commitmentY } = await this.computeOutputCommitment(
4638
- params.outputAmount,
4639
- params.outputBlinding
4640
- );
4641
- const solverSecretField = this.bytesToField(params.solverSecret);
4642
- const solverId = await this.computeSolverId(solverSecretField);
4643
- const outputBlindingField = this.bytesToField(params.outputBlinding);
4644
- const attestation = params.oracleAttestation;
4645
- const attestationRecipientField = this.hexToField(attestation.recipient);
4646
- const attestationTxHashField = this.hexToField(attestation.txHash);
4647
- const oracleSignature = Array.from(attestation.signature);
4648
- const oracleMessageHash = await this.computeOracleMessageHash(
4649
- attestation.recipient,
4650
- attestation.amount,
4651
- attestation.txHash,
4652
- attestation.blockNumber
4653
- );
4654
- const oraclePubKeyX = this.config.oraclePublicKey?.x ?? new Array(32).fill(0);
4655
- const oraclePubKeyY = this.config.oraclePublicKey?.y ?? new Array(32).fill(0);
4656
- if (!this.config.oraclePublicKey && this.config.verbose) {
4657
- console.warn("[NoirProofProvider] Warning: No oracle public key configured. Using placeholder keys.");
4658
- }
4659
- const witnessInputs = {
4660
- // Public inputs
4661
- intent_hash: intentHashField,
4662
- output_commitment_x: commitmentX,
4663
- output_commitment_y: commitmentY,
4664
- recipient_stealth: recipientStealthField,
4665
- min_output_amount: params.minOutputAmount.toString(),
4666
- solver_id: solverId,
4667
- fulfillment_time: params.fulfillmentTime.toString(),
4668
- expiry: params.expiry.toString(),
4669
- // Private inputs
4670
- output_amount: params.outputAmount.toString(),
4671
- output_blinding: outputBlindingField,
4672
- solver_secret: solverSecretField,
4673
- attestation_recipient: attestationRecipientField,
4674
- attestation_amount: attestation.amount.toString(),
4675
- attestation_tx_hash: attestationTxHashField,
4676
- attestation_block: attestation.blockNumber.toString(),
4677
- oracle_signature: oracleSignature,
4678
- oracle_message_hash: oracleMessageHash,
4679
- oracle_pub_key_x: oraclePubKeyX,
4680
- oracle_pub_key_y: oraclePubKeyY
4681
- };
4682
- if (this.config.verbose) {
4683
- console.log("[NoirProofProvider] Fulfillment witness inputs:", {
4684
- intent_hash: intentHashField,
4685
- output_commitment_x: commitmentX,
4686
- output_commitment_y: commitmentY,
4687
- recipient_stealth: recipientStealthField,
4688
- min_output_amount: params.minOutputAmount.toString(),
4689
- solver_id: solverId,
4690
- fulfillment_time: params.fulfillmentTime,
4691
- expiry: params.expiry,
4692
- output_amount: "[PRIVATE]",
4693
- output_blinding: "[PRIVATE]",
4694
- solver_secret: "[PRIVATE]",
4695
- oracle_attestation: "[PRIVATE]"
4696
- });
4697
- }
4698
- const { witness } = await this.fulfillmentNoir.execute(witnessInputs);
4699
- if (this.config.verbose) {
4700
- console.log("[NoirProofProvider] Fulfillment witness generated, creating proof...");
4701
- }
4702
- const proofData = await this.fulfillmentBackend.generateProof(witness);
4703
- if (this.config.verbose) {
4704
- console.log("[NoirProofProvider] Fulfillment proof generated successfully");
4705
- }
4706
- const publicInputs = [
4707
- `0x${intentHashField}`,
4708
- `0x${commitmentX}`,
4709
- `0x${commitmentY}`,
4710
- `0x${recipientStealthField}`,
4711
- `0x${params.minOutputAmount.toString(16).padStart(16, "0")}`,
4712
- `0x${solverId}`,
4713
- `0x${params.fulfillmentTime.toString(16).padStart(16, "0")}`,
4714
- `0x${params.expiry.toString(16).padStart(16, "0")}`
4715
- ];
4716
- const proof = {
4717
- type: "fulfillment",
4718
- proof: `0x${Buffer.from(proofData.proof).toString("hex")}`,
4719
- publicInputs
4720
- };
4721
- return {
4722
- proof,
4723
- publicInputs
4724
- };
4725
- } catch (error) {
4726
- const message = error instanceof Error ? error.message : String(error);
4727
- if (message.includes("Output below minimum")) {
4728
- throw new ProofGenerationError(
4729
- "fulfillment",
4730
- "Output amount is below minimum required",
4731
- error instanceof Error ? error : void 0
4732
- );
4733
- }
4734
- if (message.includes("Commitment") && message.includes("mismatch")) {
4735
- throw new ProofGenerationError(
4736
- "fulfillment",
4737
- "Output commitment verification failed",
4738
- error instanceof Error ? error : void 0
4739
- );
4740
- }
4741
- if (message.includes("Recipient mismatch")) {
4742
- throw new ProofGenerationError(
4743
- "fulfillment",
4744
- "Attestation recipient does not match",
4745
- error instanceof Error ? error : void 0
4746
- );
4747
- }
4748
- if (message.includes("Invalid oracle")) {
4749
- throw new ProofGenerationError(
4750
- "fulfillment",
4751
- "Oracle attestation signature is invalid",
4752
- error instanceof Error ? error : void 0
4753
- );
4754
- }
4755
- if (message.includes("Unauthorized solver")) {
4756
- throw new ProofGenerationError(
4757
- "fulfillment",
4758
- "Solver not authorized for this intent",
4759
- error instanceof Error ? error : void 0
4760
- );
4761
- }
4762
- if (message.includes("Fulfillment after expiry")) {
4763
- throw new ProofGenerationError(
4764
- "fulfillment",
4765
- "Fulfillment occurred after intent expiry",
4766
- error instanceof Error ? error : void 0
4767
- );
4768
- }
4769
- throw new ProofGenerationError(
4770
- "fulfillment",
4771
- `Failed to generate fulfillment proof: ${message}`,
4772
- error instanceof Error ? error : void 0
4773
- );
4774
- }
4775
- }
4776
- /**
4777
- * Verify a Noir proof
4778
- */
4779
- async verifyProof(proof) {
4780
- this.ensureReady();
4781
- let backend = null;
4782
- switch (proof.type) {
4783
- case "funding":
4784
- backend = this.fundingBackend;
4785
- break;
4786
- case "validity":
4787
- backend = this.validityBackend;
4788
- break;
4789
- case "fulfillment":
4790
- backend = this.fulfillmentBackend;
4791
- break;
4792
- default:
4793
- throw new ProofError(
4794
- `Unknown proof type: ${proof.type}`,
4795
- "SIP_4003" /* PROOF_NOT_IMPLEMENTED */
4796
- );
4797
- }
4798
- if (!backend) {
4799
- throw new ProofError(
4800
- `${proof.type} backend not initialized`,
4801
- "SIP_4004" /* PROOF_PROVIDER_NOT_READY */
4802
- );
4803
- }
4804
- try {
4805
- const proofHex = proof.proof.startsWith("0x") ? proof.proof.slice(2) : proof.proof;
4806
- const proofBytes = new Uint8Array(Buffer.from(proofHex, "hex"));
4807
- const isValid = await backend.verifyProof({
4808
- proof: proofBytes,
4809
- publicInputs: proof.publicInputs.map(
4810
- (input) => input.startsWith("0x") ? input.slice(2) : input
4811
- )
4812
- });
4813
- return isValid;
4814
- } catch (error) {
4815
- if (this.config.verbose) {
4816
- console.error("[NoirProofProvider] Verification error:", error);
4817
- }
4818
- return false;
4819
- }
4820
- }
4821
- /**
4822
- * Destroy the provider and free resources
4823
- */
4824
- async destroy() {
4825
- if (this.fundingBackend) {
4826
- await this.fundingBackend.destroy();
4827
- this.fundingBackend = null;
4828
- }
4829
- if (this.validityBackend) {
4830
- await this.validityBackend.destroy();
4831
- this.validityBackend = null;
4832
- }
4833
- if (this.fulfillmentBackend) {
4834
- await this.fulfillmentBackend.destroy();
4835
- this.fulfillmentBackend = null;
4836
- }
4837
- this.fundingNoir = null;
4838
- this.validityNoir = null;
4839
- this.fulfillmentNoir = null;
4840
- this._isReady = false;
4841
- }
4842
- // ─── Private Methods ───────────────────────────────────────────────────────
4843
- ensureReady() {
4844
- if (!this._isReady) {
4845
- throw new ProofError(
4846
- "NoirProofProvider not initialized. Call initialize() first.",
4847
- "SIP_4004" /* PROOF_PROVIDER_NOT_READY */
4848
- );
4849
- }
4850
- }
4851
- /**
4852
- * Compute the commitment hash that the circuit expects
4853
- *
4854
- * The circuit computes:
4855
- * 1. commitment = pedersen_commitment([balance, blinding])
4856
- * 2. commitment_hash = pedersen_hash([commitment.x, commitment.y, asset_id])
4857
- *
4858
- * We need to compute this outside to pass as a public input.
4859
- *
4860
- * **IMPORTANT**: This SDK uses SHA256 as a deterministic stand-in for Pedersen hash.
4861
- * Both the SDK and circuit MUST use the same hash function. The bundled circuit
4862
- * artifacts are configured to use SHA256 for compatibility. If you use custom
4863
- * circuits with actual Pedersen hashing, you must update this implementation.
4864
- *
4865
- * @see docs/specs/HASH-COMPATIBILITY.md for hash function requirements
4866
- */
4867
- async computeCommitmentHash(balance, blindingFactor, assetId) {
4868
- const blindingField = this.bytesToField(blindingFactor);
4869
- const { sha256: sha25611 } = await import("@noble/hashes/sha256");
4870
- const { bytesToHex: bytesToHex15 } = await import("@noble/hashes/utils");
4871
- const preimage = new Uint8Array([
4872
- ...this.bigintToBytes(balance, 8),
4873
- ...blindingFactor.slice(0, 32),
4874
- ...this.hexToBytes(this.assetIdToField(assetId))
4875
- ]);
4876
- const hash2 = sha25611(preimage);
4877
- const commitmentHash = bytesToHex15(hash2);
4878
- return { commitmentHash, blindingField };
4879
- }
4880
- /**
4881
- * Convert asset ID to field element
4882
- */
4883
- assetIdToField(assetId) {
4884
- if (assetId.startsWith("0x")) {
4885
- return assetId.slice(2).padStart(64, "0");
4886
- }
4887
- const encoder = new TextEncoder();
4888
- const bytes = encoder.encode(assetId);
4889
- let result = 0n;
4890
- for (let i = 0; i < bytes.length && i < 31; i++) {
4891
- result = result * 256n + BigInt(bytes[i]);
4892
- }
4893
- return result.toString(16).padStart(64, "0");
4894
- }
4895
- /**
4896
- * Convert bytes to field element string
4897
- */
4898
- bytesToField(bytes) {
4899
- let result = 0n;
4900
- const len = Math.min(bytes.length, 31);
4901
- for (let i = 0; i < len; i++) {
4902
- result = result * 256n + BigInt(bytes[i]);
4903
- }
4904
- return result.toString();
4905
- }
4906
- /**
4907
- * Convert bigint to bytes
4908
- */
4909
- bigintToBytes(value, length) {
4910
- const bytes = new Uint8Array(length);
4911
- let v = value;
4912
- for (let i = length - 1; i >= 0; i--) {
4913
- bytes[i] = Number(v & 0xffn);
4914
- v = v >> 8n;
4915
- }
4916
- return bytes;
4917
- }
4918
- /**
4919
- * Convert hex string to bytes
4920
- */
4921
- hexToBytes(hex) {
4922
- const h = hex.startsWith("0x") ? hex.slice(2) : hex;
4923
- const bytes = new Uint8Array(h.length / 2);
4924
- for (let i = 0; i < bytes.length; i++) {
4925
- bytes[i] = parseInt(h.slice(i * 2, i * 2 + 2), 16);
4926
- }
4927
- return bytes;
4928
- }
4929
- /**
4930
- * Convert hex string to field element string
4931
- */
4932
- hexToField(hex) {
4933
- const h = hex.startsWith("0x") ? hex.slice(2) : hex;
4934
- return h.padStart(64, "0");
4935
- }
4936
- /**
4937
- * Convert field string to 32-byte array
4938
- */
4939
- fieldToBytes32(field) {
4940
- const hex = field.padStart(64, "0");
4941
- const bytes = [];
4942
- for (let i = 0; i < 32; i++) {
4943
- bytes.push(parseInt(hex.slice(i * 2, i * 2 + 2), 16));
4944
- }
4945
- return bytes;
4946
- }
4947
- /**
4948
- * Compute sender commitment for validity proof
4949
- *
4950
- * Uses SHA256 for SDK-side computation. The bundled circuit artifacts
4951
- * are compiled to use SHA256 for compatibility with this SDK.
4952
- *
4953
- * @see computeCommitmentHash for hash function compatibility notes
4954
- */
4955
- async computeSenderCommitment(senderAddressField, senderBlindingField) {
4956
- const { sha256: sha25611 } = await import("@noble/hashes/sha256");
4957
- const { bytesToHex: bytesToHex15 } = await import("@noble/hashes/utils");
4958
- const addressBytes = this.hexToBytes(senderAddressField);
4959
- const blindingBytes = this.hexToBytes(senderBlindingField.padStart(64, "0"));
4960
- const preimage = new Uint8Array([...addressBytes, ...blindingBytes]);
4961
- const hash2 = sha25611(preimage);
4962
- const commitmentX = bytesToHex15(hash2.slice(0, 16)).padStart(64, "0");
4963
- const commitmentY = bytesToHex15(hash2.slice(16, 32)).padStart(64, "0");
4964
- return { commitmentX, commitmentY };
4965
- }
4966
- /**
4967
- * Compute nullifier for validity proof
4968
- *
4969
- * Uses SHA256 for SDK-side computation. The bundled circuit artifacts
4970
- * are compiled to use SHA256 for compatibility with this SDK.
4971
- *
4972
- * @see computeCommitmentHash for hash function compatibility notes
4973
- */
4974
- async computeNullifier(senderSecretField, intentHashField, nonceField) {
4975
- const { sha256: sha25611 } = await import("@noble/hashes/sha256");
4976
- const { bytesToHex: bytesToHex15 } = await import("@noble/hashes/utils");
4977
- const secretBytes = this.hexToBytes(senderSecretField.padStart(64, "0"));
4978
- const intentBytes = this.hexToBytes(intentHashField);
4979
- const nonceBytes = this.hexToBytes(nonceField.padStart(64, "0"));
4980
- const preimage = new Uint8Array([...secretBytes, ...intentBytes, ...nonceBytes]);
4981
- const hash2 = sha25611(preimage);
4982
- return bytesToHex15(hash2);
4983
- }
4984
- /**
4985
- * Compute output commitment for fulfillment proof
4986
- *
4987
- * Uses SHA256 for SDK-side computation. The bundled circuit artifacts
4988
- * are compiled to use SHA256 for compatibility with this SDK.
4989
- *
4990
- * @see computeCommitmentHash for hash function compatibility notes
4991
- */
4992
- async computeOutputCommitment(outputAmount, outputBlinding) {
4993
- const { sha256: sha25611 } = await import("@noble/hashes/sha256");
4994
- const { bytesToHex: bytesToHex15 } = await import("@noble/hashes/utils");
4995
- const amountBytes = this.bigintToBytes(outputAmount, 8);
4996
- const blindingBytes = outputBlinding.slice(0, 32);
4997
- const preimage = new Uint8Array([...amountBytes, ...blindingBytes]);
4998
- const hash2 = sha25611(preimage);
4999
- const commitmentX = bytesToHex15(hash2.slice(0, 16)).padStart(64, "0");
5000
- const commitmentY = bytesToHex15(hash2.slice(16, 32)).padStart(64, "0");
5001
- return { commitmentX, commitmentY };
5002
- }
5003
- /**
5004
- * Compute solver ID from solver secret
5005
- *
5006
- * Uses SHA256 for SDK-side computation. The bundled circuit artifacts
5007
- * are compiled to use SHA256 for compatibility with this SDK.
5008
- *
5009
- * @see computeCommitmentHash for hash function compatibility notes
5010
- */
5011
- async computeSolverId(solverSecretField) {
5012
- const { sha256: sha25611 } = await import("@noble/hashes/sha256");
5013
- const { bytesToHex: bytesToHex15 } = await import("@noble/hashes/utils");
5014
- const secretBytes = this.hexToBytes(solverSecretField.padStart(64, "0"));
5015
- const hash2 = sha25611(secretBytes);
5016
- return bytesToHex15(hash2);
5017
- }
5018
- /**
5019
- * Compute oracle message hash for fulfillment proof
5020
- *
5021
- * Hash of attestation data that oracle signs
5022
- */
5023
- async computeOracleMessageHash(recipient, amount, txHash, blockNumber) {
5024
- const { sha256: sha25611 } = await import("@noble/hashes/sha256");
5025
- const recipientBytes = this.hexToBytes(this.hexToField(recipient));
5026
- const amountBytes = this.bigintToBytes(amount, 8);
5027
- const txHashBytes = this.hexToBytes(this.hexToField(txHash));
5028
- const blockBytes = this.bigintToBytes(blockNumber, 8);
5029
- const preimage = new Uint8Array([
5030
- ...recipientBytes,
5031
- ...amountBytes,
5032
- ...txHashBytes,
5033
- ...blockBytes
5034
- ]);
5035
- const hash2 = sha25611(preimage);
5036
- return Array.from(hash2);
5037
- }
5038
- /**
5039
- * Derive secp256k1 public key coordinates from a private key
5040
- *
5041
- * @param privateKey - 32-byte private key as Uint8Array
5042
- * @returns X and Y coordinates as 32-byte arrays
5043
- */
5044
- getPublicKeyCoordinates(privateKey) {
5045
- const uncompressedPubKey = import_secp256k13.secp256k1.getPublicKey(privateKey, false);
5046
- const x = Array.from(uncompressedPubKey.slice(1, 33));
5047
- const y = Array.from(uncompressedPubKey.slice(33, 65));
5048
- return { x, y };
5049
- }
5050
- /**
5051
- * Derive public key coordinates from a field string (private key)
5052
- *
5053
- * @param privateKeyField - Private key as hex field string
5054
- * @returns X and Y coordinates as 32-byte arrays
5055
- */
5056
- getPublicKeyFromField(privateKeyField) {
5057
- const privateKeyBytes = this.hexToBytes(privateKeyField.padStart(64, "0"));
5058
- return this.getPublicKeyCoordinates(privateKeyBytes);
5059
- }
5060
- };
5061
-
5062
3303
  // src/proofs/browser-utils.ts
5063
3304
  function hexToBytes5(hex) {
5064
3305
  const h = hex.startsWith("0x") ? hex.slice(2) : hex;
@@ -5097,609 +3338,6 @@ function getBrowserInfo() {
5097
3338
  };
5098
3339
  }
5099
3340
 
5100
- // src/proofs/browser.ts
5101
- var import_noir_js2 = require("@noir-lang/noir_js");
5102
- var import_bb2 = require("@aztec/bb.js");
5103
- var import_secp256k14 = require("@noble/curves/secp256k1");
5104
- var BrowserNoirProvider = class _BrowserNoirProvider {
5105
- framework = "noir";
5106
- _isReady = false;
5107
- config;
5108
- // Circuit instances
5109
- fundingNoir = null;
5110
- fundingBackend = null;
5111
- validityNoir = null;
5112
- validityBackend = null;
5113
- fulfillmentNoir = null;
5114
- fulfillmentBackend = null;
5115
- // Worker instance (optional)
5116
- worker = null;
5117
- workerPending = /* @__PURE__ */ new Map();
5118
- constructor(config = {}) {
5119
- this.config = {
5120
- useWorker: config.useWorker ?? true,
5121
- verbose: config.verbose ?? false,
5122
- oraclePublicKey: config.oraclePublicKey ?? void 0,
5123
- timeout: config.timeout ?? 6e4
5124
- };
5125
- if (!isBrowser()) {
5126
- console.warn(
5127
- "[BrowserNoirProvider] Not running in browser environment. Consider using NoirProofProvider for Node.js."
5128
- );
5129
- }
5130
- }
5131
- get isReady() {
5132
- return this._isReady;
5133
- }
5134
- /**
5135
- * Get browser environment info
5136
- */
5137
- static getBrowserInfo() {
5138
- return getBrowserInfo();
5139
- }
5140
- /**
5141
- * Check if browser supports all required features
5142
- */
5143
- static checkBrowserSupport() {
5144
- const missing = [];
5145
- if (!isBrowser()) {
5146
- missing.push("browser environment");
5147
- }
5148
- if (typeof WebAssembly === "undefined") {
5149
- missing.push("WebAssembly");
5150
- }
5151
- if (!supportsSharedArrayBuffer()) {
5152
- missing.push("SharedArrayBuffer (requires COOP/COEP headers)");
5153
- }
5154
- return {
5155
- supported: missing.length === 0,
5156
- missing
5157
- };
5158
- }
5159
- /**
5160
- * Derive secp256k1 public key coordinates from a private key
5161
- */
5162
- static derivePublicKey(privateKey) {
5163
- const uncompressedPubKey = import_secp256k14.secp256k1.getPublicKey(privateKey, false);
5164
- const x = Array.from(uncompressedPubKey.slice(1, 33));
5165
- const y = Array.from(uncompressedPubKey.slice(33, 65));
5166
- return { x, y };
5167
- }
5168
- /**
5169
- * Initialize the browser provider
5170
- *
5171
- * Loads WASM and circuit artifacts. This should be called before any
5172
- * proof generation. Consider showing a loading indicator during init.
5173
- *
5174
- * @param onProgress - Optional progress callback
5175
- */
5176
- async initialize(onProgress) {
5177
- if (this._isReady) {
5178
- return;
5179
- }
5180
- const { supported, missing } = _BrowserNoirProvider.checkBrowserSupport();
5181
- if (!supported) {
5182
- throw new ProofError(
5183
- `Browser missing required features: ${missing.join(", ")}`,
5184
- "SIP_4004" /* PROOF_PROVIDER_NOT_READY */
5185
- );
5186
- }
5187
- try {
5188
- onProgress?.({
5189
- stage: "initializing",
5190
- percent: 0,
5191
- message: "Loading WASM runtime..."
5192
- });
5193
- if (this.config.verbose) {
5194
- console.log("[BrowserNoirProvider] Initializing...");
5195
- console.log("[BrowserNoirProvider] Browser info:", getBrowserInfo());
5196
- }
5197
- const fundingCircuit = funding_proof_default;
5198
- const validityCircuit = validity_proof_default;
5199
- const fulfillmentCircuit = fulfillment_proof_default;
5200
- onProgress?.({
5201
- stage: "initializing",
5202
- percent: 20,
5203
- message: "Creating proof backends..."
5204
- });
5205
- this.fundingBackend = new import_bb2.UltraHonkBackend(fundingCircuit.bytecode);
5206
- this.validityBackend = new import_bb2.UltraHonkBackend(validityCircuit.bytecode);
5207
- this.fulfillmentBackend = new import_bb2.UltraHonkBackend(fulfillmentCircuit.bytecode);
5208
- onProgress?.({
5209
- stage: "initializing",
5210
- percent: 60,
5211
- message: "Initializing Noir circuits..."
5212
- });
5213
- this.fundingNoir = new import_noir_js2.Noir(fundingCircuit);
5214
- this.validityNoir = new import_noir_js2.Noir(validityCircuit);
5215
- this.fulfillmentNoir = new import_noir_js2.Noir(fulfillmentCircuit);
5216
- onProgress?.({
5217
- stage: "initializing",
5218
- percent: 90,
5219
- message: "Setting up worker..."
5220
- });
5221
- if (this.config.useWorker && supportsWebWorkers()) {
5222
- await this.initializeWorker();
5223
- }
5224
- this._isReady = true;
5225
- onProgress?.({
5226
- stage: "complete",
5227
- percent: 100,
5228
- message: "Ready for proof generation"
5229
- });
5230
- if (this.config.verbose) {
5231
- console.log("[BrowserNoirProvider] Initialization complete");
5232
- }
5233
- } catch (error) {
5234
- throw new ProofError(
5235
- `Failed to initialize BrowserNoirProvider: ${error instanceof Error ? error.message : String(error)}`,
5236
- "SIP_4003" /* PROOF_NOT_IMPLEMENTED */,
5237
- { context: { error } }
5238
- );
5239
- }
5240
- }
5241
- /**
5242
- * Initialize Web Worker for off-main-thread proof generation
5243
- */
5244
- async initializeWorker() {
5245
- if (this.config.verbose) {
5246
- console.log("[BrowserNoirProvider] Worker support: using async main-thread");
5247
- }
5248
- }
5249
- /**
5250
- * Generate a Funding Proof
5251
- *
5252
- * Proves: balance >= minimumRequired without revealing balance
5253
- *
5254
- * @param params - Funding proof parameters
5255
- * @param onProgress - Optional progress callback
5256
- */
5257
- async generateFundingProof(params, onProgress) {
5258
- this.ensureReady();
5259
- if (!this.fundingNoir || !this.fundingBackend) {
5260
- throw new ProofGenerationError("funding", "Funding circuit not initialized");
5261
- }
5262
- try {
5263
- onProgress?.({
5264
- stage: "witness",
5265
- percent: 10,
5266
- message: "Preparing witness inputs..."
5267
- });
5268
- const { commitmentHash, blindingField } = await this.computeCommitmentHash(
5269
- params.balance,
5270
- params.blindingFactor,
5271
- params.assetId
5272
- );
5273
- const witnessInputs = {
5274
- commitment_hash: commitmentHash,
5275
- minimum_required: params.minimumRequired.toString(),
5276
- asset_id: this.assetIdToField(params.assetId),
5277
- balance: params.balance.toString(),
5278
- blinding: blindingField
5279
- };
5280
- onProgress?.({
5281
- stage: "witness",
5282
- percent: 30,
5283
- message: "Generating witness..."
5284
- });
5285
- const { witness } = await this.fundingNoir.execute(witnessInputs);
5286
- onProgress?.({
5287
- stage: "proving",
5288
- percent: 50,
5289
- message: "Generating proof (this may take a moment)..."
5290
- });
5291
- const proofData = await this.fundingBackend.generateProof(witness);
5292
- onProgress?.({
5293
- stage: "complete",
5294
- percent: 100,
5295
- message: "Proof generated successfully"
5296
- });
5297
- const publicInputs = [
5298
- `0x${commitmentHash}`,
5299
- `0x${params.minimumRequired.toString(16).padStart(16, "0")}`,
5300
- `0x${this.assetIdToField(params.assetId)}`
5301
- ];
5302
- const proof = {
5303
- type: "funding",
5304
- proof: `0x${bytesToHex7(proofData.proof)}`,
5305
- publicInputs
5306
- };
5307
- return { proof, publicInputs };
5308
- } catch (error) {
5309
- const message = error instanceof Error ? error.message : String(error);
5310
- throw new ProofGenerationError(
5311
- "funding",
5312
- `Failed to generate funding proof: ${message}`,
5313
- error instanceof Error ? error : void 0
5314
- );
5315
- }
5316
- }
5317
- /**
5318
- * Generate a Validity Proof
5319
- *
5320
- * Proves: Intent is authorized by sender without revealing identity
5321
- */
5322
- async generateValidityProof(params, onProgress) {
5323
- this.ensureReady();
5324
- if (!this.validityNoir || !this.validityBackend) {
5325
- throw new ProofGenerationError("validity", "Validity circuit not initialized");
5326
- }
5327
- try {
5328
- onProgress?.({
5329
- stage: "witness",
5330
- percent: 10,
5331
- message: "Preparing validity witness..."
5332
- });
5333
- const intentHashField = this.hexToField(params.intentHash);
5334
- const senderAddressField = this.hexToField(params.senderAddress);
5335
- const senderBlindingField = this.bytesToField(params.senderBlinding);
5336
- const senderSecretField = this.bytesToField(params.senderSecret);
5337
- const nonceField = this.bytesToField(params.nonce);
5338
- const { commitmentX, commitmentY } = await this.computeSenderCommitment(
5339
- senderAddressField,
5340
- senderBlindingField
5341
- );
5342
- const nullifier = await this.computeNullifier(senderSecretField, intentHashField, nonceField);
5343
- const signature = Array.from(params.authorizationSignature);
5344
- const messageHash = this.fieldToBytes32(intentHashField);
5345
- let pubKeyX;
5346
- let pubKeyY;
5347
- if (params.senderPublicKey) {
5348
- pubKeyX = Array.from(params.senderPublicKey.x);
5349
- pubKeyY = Array.from(params.senderPublicKey.y);
5350
- } else {
5351
- const coords = this.getPublicKeyCoordinates(params.senderSecret);
5352
- pubKeyX = coords.x;
5353
- pubKeyY = coords.y;
5354
- }
5355
- const witnessInputs = {
5356
- intent_hash: intentHashField,
5357
- sender_commitment_x: commitmentX,
5358
- sender_commitment_y: commitmentY,
5359
- nullifier,
5360
- timestamp: params.timestamp.toString(),
5361
- expiry: params.expiry.toString(),
5362
- sender_address: senderAddressField,
5363
- sender_blinding: senderBlindingField,
5364
- sender_secret: senderSecretField,
5365
- pub_key_x: pubKeyX,
5366
- pub_key_y: pubKeyY,
5367
- signature,
5368
- message_hash: messageHash,
5369
- nonce: nonceField
5370
- };
5371
- onProgress?.({
5372
- stage: "witness",
5373
- percent: 30,
5374
- message: "Generating witness..."
5375
- });
5376
- const { witness } = await this.validityNoir.execute(witnessInputs);
5377
- onProgress?.({
5378
- stage: "proving",
5379
- percent: 50,
5380
- message: "Generating validity proof..."
5381
- });
5382
- const proofData = await this.validityBackend.generateProof(witness);
5383
- onProgress?.({
5384
- stage: "complete",
5385
- percent: 100,
5386
- message: "Validity proof generated"
5387
- });
5388
- const publicInputs = [
5389
- `0x${intentHashField}`,
5390
- `0x${commitmentX}`,
5391
- `0x${commitmentY}`,
5392
- `0x${nullifier}`,
5393
- `0x${params.timestamp.toString(16).padStart(16, "0")}`,
5394
- `0x${params.expiry.toString(16).padStart(16, "0")}`
5395
- ];
5396
- const proof = {
5397
- type: "validity",
5398
- proof: `0x${bytesToHex7(proofData.proof)}`,
5399
- publicInputs
5400
- };
5401
- return { proof, publicInputs };
5402
- } catch (error) {
5403
- const message = error instanceof Error ? error.message : String(error);
5404
- throw new ProofGenerationError(
5405
- "validity",
5406
- `Failed to generate validity proof: ${message}`,
5407
- error instanceof Error ? error : void 0
5408
- );
5409
- }
5410
- }
5411
- /**
5412
- * Generate a Fulfillment Proof
5413
- *
5414
- * Proves: Solver correctly executed the intent
5415
- */
5416
- async generateFulfillmentProof(params, onProgress) {
5417
- this.ensureReady();
5418
- if (!this.fulfillmentNoir || !this.fulfillmentBackend) {
5419
- throw new ProofGenerationError("fulfillment", "Fulfillment circuit not initialized");
5420
- }
5421
- try {
5422
- onProgress?.({
5423
- stage: "witness",
5424
- percent: 10,
5425
- message: "Preparing fulfillment witness..."
5426
- });
5427
- const intentHashField = this.hexToField(params.intentHash);
5428
- const recipientStealthField = this.hexToField(params.recipientStealth);
5429
- const { commitmentX, commitmentY } = await this.computeOutputCommitment(
5430
- params.outputAmount,
5431
- params.outputBlinding
5432
- );
5433
- const solverSecretField = this.bytesToField(params.solverSecret);
5434
- const solverId = await this.computeSolverId(solverSecretField);
5435
- const outputBlindingField = this.bytesToField(params.outputBlinding);
5436
- const attestation = params.oracleAttestation;
5437
- const attestationRecipientField = this.hexToField(attestation.recipient);
5438
- const attestationTxHashField = this.hexToField(attestation.txHash);
5439
- const oracleSignature = Array.from(attestation.signature);
5440
- const oracleMessageHash = await this.computeOracleMessageHash(
5441
- attestation.recipient,
5442
- attestation.amount,
5443
- attestation.txHash,
5444
- attestation.blockNumber
5445
- );
5446
- const oraclePubKeyX = this.config.oraclePublicKey?.x ?? new Array(32).fill(0);
5447
- const oraclePubKeyY = this.config.oraclePublicKey?.y ?? new Array(32).fill(0);
5448
- const witnessInputs = {
5449
- intent_hash: intentHashField,
5450
- output_commitment_x: commitmentX,
5451
- output_commitment_y: commitmentY,
5452
- recipient_stealth: recipientStealthField,
5453
- min_output_amount: params.minOutputAmount.toString(),
5454
- solver_id: solverId,
5455
- fulfillment_time: params.fulfillmentTime.toString(),
5456
- expiry: params.expiry.toString(),
5457
- output_amount: params.outputAmount.toString(),
5458
- output_blinding: outputBlindingField,
5459
- solver_secret: solverSecretField,
5460
- attestation_recipient: attestationRecipientField,
5461
- attestation_amount: attestation.amount.toString(),
5462
- attestation_tx_hash: attestationTxHashField,
5463
- attestation_block: attestation.blockNumber.toString(),
5464
- oracle_signature: oracleSignature,
5465
- oracle_message_hash: oracleMessageHash,
5466
- oracle_pub_key_x: oraclePubKeyX,
5467
- oracle_pub_key_y: oraclePubKeyY
5468
- };
5469
- onProgress?.({
5470
- stage: "witness",
5471
- percent: 30,
5472
- message: "Generating witness..."
5473
- });
5474
- const { witness } = await this.fulfillmentNoir.execute(witnessInputs);
5475
- onProgress?.({
5476
- stage: "proving",
5477
- percent: 50,
5478
- message: "Generating fulfillment proof..."
5479
- });
5480
- const proofData = await this.fulfillmentBackend.generateProof(witness);
5481
- onProgress?.({
5482
- stage: "complete",
5483
- percent: 100,
5484
- message: "Fulfillment proof generated"
5485
- });
5486
- const publicInputs = [
5487
- `0x${intentHashField}`,
5488
- `0x${commitmentX}`,
5489
- `0x${commitmentY}`,
5490
- `0x${recipientStealthField}`,
5491
- `0x${params.minOutputAmount.toString(16).padStart(16, "0")}`,
5492
- `0x${solverId}`,
5493
- `0x${params.fulfillmentTime.toString(16).padStart(16, "0")}`,
5494
- `0x${params.expiry.toString(16).padStart(16, "0")}`
5495
- ];
5496
- const proof = {
5497
- type: "fulfillment",
5498
- proof: `0x${bytesToHex7(proofData.proof)}`,
5499
- publicInputs
5500
- };
5501
- return { proof, publicInputs };
5502
- } catch (error) {
5503
- const message = error instanceof Error ? error.message : String(error);
5504
- throw new ProofGenerationError(
5505
- "fulfillment",
5506
- `Failed to generate fulfillment proof: ${message}`,
5507
- error instanceof Error ? error : void 0
5508
- );
5509
- }
5510
- }
5511
- /**
5512
- * Verify a proof
5513
- */
5514
- async verifyProof(proof) {
5515
- this.ensureReady();
5516
- let backend = null;
5517
- switch (proof.type) {
5518
- case "funding":
5519
- backend = this.fundingBackend;
5520
- break;
5521
- case "validity":
5522
- backend = this.validityBackend;
5523
- break;
5524
- case "fulfillment":
5525
- backend = this.fulfillmentBackend;
5526
- break;
5527
- default:
5528
- throw new ProofError(`Unknown proof type: ${proof.type}`, "SIP_4003" /* PROOF_NOT_IMPLEMENTED */);
5529
- }
5530
- if (!backend) {
5531
- throw new ProofError(
5532
- `${proof.type} backend not initialized`,
5533
- "SIP_4004" /* PROOF_PROVIDER_NOT_READY */
5534
- );
5535
- }
5536
- try {
5537
- const proofHex = proof.proof.startsWith("0x") ? proof.proof.slice(2) : proof.proof;
5538
- const proofBytes = hexToBytes5(proofHex);
5539
- const isValid = await backend.verifyProof({
5540
- proof: proofBytes,
5541
- publicInputs: proof.publicInputs.map(
5542
- (input) => input.startsWith("0x") ? input.slice(2) : input
5543
- )
5544
- });
5545
- return isValid;
5546
- } catch (error) {
5547
- if (this.config.verbose) {
5548
- console.error("[BrowserNoirProvider] Verification error:", error);
5549
- }
5550
- return false;
5551
- }
5552
- }
5553
- /**
5554
- * Destroy the provider and free resources
5555
- */
5556
- async destroy() {
5557
- if (this.fundingBackend) {
5558
- await this.fundingBackend.destroy();
5559
- this.fundingBackend = null;
5560
- }
5561
- if (this.validityBackend) {
5562
- await this.validityBackend.destroy();
5563
- this.validityBackend = null;
5564
- }
5565
- if (this.fulfillmentBackend) {
5566
- await this.fulfillmentBackend.destroy();
5567
- this.fulfillmentBackend = null;
5568
- }
5569
- if (this.worker) {
5570
- this.worker.terminate();
5571
- this.worker = null;
5572
- }
5573
- this.fundingNoir = null;
5574
- this.validityNoir = null;
5575
- this.fulfillmentNoir = null;
5576
- this._isReady = false;
5577
- }
5578
- // ─── Private Utility Methods ────────────────────────────────────────────────
5579
- ensureReady() {
5580
- if (!this._isReady) {
5581
- throw new ProofError(
5582
- "BrowserNoirProvider not initialized. Call initialize() first.",
5583
- "SIP_4004" /* PROOF_PROVIDER_NOT_READY */
5584
- );
5585
- }
5586
- }
5587
- async computeCommitmentHash(balance, blindingFactor, assetId) {
5588
- const blindingField = this.bytesToField(blindingFactor);
5589
- const { sha256: sha25611 } = await import("@noble/hashes/sha256");
5590
- const { bytesToHex: nobleToHex } = await import("@noble/hashes/utils");
5591
- const preimage = new Uint8Array([
5592
- ...this.bigintToBytes(balance, 8),
5593
- ...blindingFactor.slice(0, 32),
5594
- ...hexToBytes5(this.assetIdToField(assetId))
5595
- ]);
5596
- const hash2 = sha25611(preimage);
5597
- const commitmentHash = nobleToHex(hash2);
5598
- return { commitmentHash, blindingField };
5599
- }
5600
- assetIdToField(assetId) {
5601
- if (assetId.startsWith("0x")) {
5602
- return assetId.slice(2).padStart(64, "0");
5603
- }
5604
- const encoder = new TextEncoder();
5605
- const bytes = encoder.encode(assetId);
5606
- let result = 0n;
5607
- for (let i = 0; i < bytes.length && i < 31; i++) {
5608
- result = result * 256n + BigInt(bytes[i]);
5609
- }
5610
- return result.toString(16).padStart(64, "0");
5611
- }
5612
- bytesToField(bytes) {
5613
- let result = 0n;
5614
- const len = Math.min(bytes.length, 31);
5615
- for (let i = 0; i < len; i++) {
5616
- result = result * 256n + BigInt(bytes[i]);
5617
- }
5618
- return result.toString();
5619
- }
5620
- bigintToBytes(value, length) {
5621
- const bytes = new Uint8Array(length);
5622
- let v = value;
5623
- for (let i = length - 1; i >= 0; i--) {
5624
- bytes[i] = Number(v & 0xffn);
5625
- v = v >> 8n;
5626
- }
5627
- return bytes;
5628
- }
5629
- hexToField(hex) {
5630
- const h = hex.startsWith("0x") ? hex.slice(2) : hex;
5631
- return h.padStart(64, "0");
5632
- }
5633
- fieldToBytes32(field) {
5634
- const hex = field.padStart(64, "0");
5635
- const bytes = [];
5636
- for (let i = 0; i < 32; i++) {
5637
- bytes.push(parseInt(hex.slice(i * 2, i * 2 + 2), 16));
5638
- }
5639
- return bytes;
5640
- }
5641
- async computeSenderCommitment(senderAddressField, senderBlindingField) {
5642
- const { sha256: sha25611 } = await import("@noble/hashes/sha256");
5643
- const { bytesToHex: nobleToHex } = await import("@noble/hashes/utils");
5644
- const addressBytes = hexToBytes5(senderAddressField);
5645
- const blindingBytes = hexToBytes5(senderBlindingField.padStart(64, "0"));
5646
- const preimage = new Uint8Array([...addressBytes, ...blindingBytes]);
5647
- const hash2 = sha25611(preimage);
5648
- const commitmentX = nobleToHex(hash2.slice(0, 16)).padStart(64, "0");
5649
- const commitmentY = nobleToHex(hash2.slice(16, 32)).padStart(64, "0");
5650
- return { commitmentX, commitmentY };
5651
- }
5652
- async computeNullifier(senderSecretField, intentHashField, nonceField) {
5653
- const { sha256: sha25611 } = await import("@noble/hashes/sha256");
5654
- const { bytesToHex: nobleToHex } = await import("@noble/hashes/utils");
5655
- const secretBytes = hexToBytes5(senderSecretField.padStart(64, "0"));
5656
- const intentBytes = hexToBytes5(intentHashField);
5657
- const nonceBytes = hexToBytes5(nonceField.padStart(64, "0"));
5658
- const preimage = new Uint8Array([...secretBytes, ...intentBytes, ...nonceBytes]);
5659
- const hash2 = sha25611(preimage);
5660
- return nobleToHex(hash2);
5661
- }
5662
- async computeOutputCommitment(outputAmount, outputBlinding) {
5663
- const { sha256: sha25611 } = await import("@noble/hashes/sha256");
5664
- const { bytesToHex: nobleToHex } = await import("@noble/hashes/utils");
5665
- const amountBytes = this.bigintToBytes(outputAmount, 8);
5666
- const blindingBytes = outputBlinding.slice(0, 32);
5667
- const preimage = new Uint8Array([...amountBytes, ...blindingBytes]);
5668
- const hash2 = sha25611(preimage);
5669
- const commitmentX = nobleToHex(hash2.slice(0, 16)).padStart(64, "0");
5670
- const commitmentY = nobleToHex(hash2.slice(16, 32)).padStart(64, "0");
5671
- return { commitmentX, commitmentY };
5672
- }
5673
- async computeSolverId(solverSecretField) {
5674
- const { sha256: sha25611 } = await import("@noble/hashes/sha256");
5675
- const { bytesToHex: nobleToHex } = await import("@noble/hashes/utils");
5676
- const secretBytes = hexToBytes5(solverSecretField.padStart(64, "0"));
5677
- const hash2 = sha25611(secretBytes);
5678
- return nobleToHex(hash2);
5679
- }
5680
- async computeOracleMessageHash(recipient, amount, txHash, blockNumber) {
5681
- const { sha256: sha25611 } = await import("@noble/hashes/sha256");
5682
- const recipientBytes = hexToBytes5(this.hexToField(recipient));
5683
- const amountBytes = this.bigintToBytes(amount, 8);
5684
- const txHashBytes = hexToBytes5(this.hexToField(txHash));
5685
- const blockBytes = this.bigintToBytes(blockNumber, 8);
5686
- const preimage = new Uint8Array([
5687
- ...recipientBytes,
5688
- ...amountBytes,
5689
- ...txHashBytes,
5690
- ...blockBytes
5691
- ]);
5692
- const hash2 = sha25611(preimage);
5693
- return Array.from(hash2);
5694
- }
5695
- getPublicKeyCoordinates(privateKey) {
5696
- const uncompressedPubKey = import_secp256k14.secp256k1.getPublicKey(privateKey, false);
5697
- const x = Array.from(uncompressedPubKey.slice(1, 33));
5698
- const y = Array.from(uncompressedPubKey.slice(33, 65));
5699
- return { x, y };
5700
- }
5701
- };
5702
-
5703
3341
  // src/oracle/types.ts
5704
3342
  var ORACLE_DOMAIN = "SIP-ORACLE-ATTESTATION-V1";
5705
3343
  var ATTESTATION_VERSION = 1;
@@ -7653,7 +5291,7 @@ function getPaymentSummary(payment) {
7653
5291
 
7654
5292
  // src/treasury/treasury.ts
7655
5293
  var import_types12 = require("@sip-protocol/types");
7656
- var import_secp256k15 = require("@noble/curves/secp256k1");
5294
+ var import_secp256k13 = require("@noble/curves/secp256k1");
7657
5295
  var import_sha25610 = require("@noble/hashes/sha256");
7658
5296
  var import_utils12 = require("@noble/hashes/utils");
7659
5297
  var DEFAULT_PROPOSAL_TTL = 7 * 24 * 60 * 60;
@@ -8169,7 +5807,7 @@ function signMessage(messageHash, privateKey) {
8169
5807
  const keyHex = privateKey.startsWith("0x") ? privateKey.slice(2) : privateKey;
8170
5808
  const keyBytes = (0, import_utils12.hexToBytes)(keyHex);
8171
5809
  try {
8172
- const signature = import_secp256k15.secp256k1.sign(messageHash, keyBytes);
5810
+ const signature = import_secp256k13.secp256k1.sign(messageHash, keyBytes);
8173
5811
  return `0x${signature.toCompactHex()}`;
8174
5812
  } finally {
8175
5813
  secureWipe(keyBytes);
@@ -8181,7 +5819,7 @@ function verifySignature(messageHash, signature, publicKey) {
8181
5819
  try {
8182
5820
  const sigBytes = (0, import_utils12.hexToBytes)(sigHex);
8183
5821
  const pubKeyBytes = (0, import_utils12.hexToBytes)(pubKeyHex);
8184
- return import_secp256k15.secp256k1.verify(sigBytes, messageHash, pubKeyBytes);
5822
+ return import_secp256k13.secp256k1.verify(sigBytes, messageHash, pubKeyBytes);
8185
5823
  } catch {
8186
5824
  return false;
8187
5825
  }
@@ -12710,7 +10348,6 @@ var import_types32 = require("@sip-protocol/types");
12710
10348
  0 && (module.exports = {
12711
10349
  ATTESTATION_VERSION,
12712
10350
  BaseWalletAdapter,
12713
- BrowserNoirProvider,
12714
10351
  CHAIN_NUMERIC_IDS,
12715
10352
  ComplianceManager,
12716
10353
  CryptoError,
@@ -12737,7 +10374,6 @@ var import_types32 = require("@sip-protocol/types");
12737
10374
  NATIVE_TOKENS,
12738
10375
  NEARIntentsAdapter,
12739
10376
  NetworkError,
12740
- NoirProofProvider,
12741
10377
  ORACLE_DOMAIN,
12742
10378
  OneClickClient,
12743
10379
  OneClickDepositMode,